Permalink
Browse files

Improve concurrency of foreign key locking

This patch introduces two additional lock modes for tuples: "SELECT FOR
KEY SHARE" and "SELECT FOR NO KEY UPDATE".  These don't block each
other, in contrast with already existing "SELECT FOR SHARE" and "SELECT
FOR UPDATE".  UPDATE commands that do not modify the values stored in
the columns that are part of the key of the tuple now grab a SELECT FOR
NO KEY UPDATE lock on the tuple, allowing them to proceed concurrently
with tuple locks of the FOR KEY SHARE variety.

Foreign key triggers now use FOR KEY SHARE instead of FOR SHARE; this
means the concurrency improvement applies to them, which is the whole
point of this patch.

The added tuple lock semantics require some rejiggering of the multixact
module, so that the locking level that each transaction is holding can
be stored alongside its Xid.  Also, multixacts now need to persist
across server restarts and crashes, because they can now represent not
only tuple locks, but also tuple updates.  This means we need more
careful tracking of lifetime of pg_multixact SLRU files; since they now
persist longer, we require more infrastructure to figure out when they
can be removed.  pg_upgrade also needs to be careful to copy
pg_multixact files over from the old server to the new, or at least part
of multixact.c state, depending on the versions of the old and new
servers.

Tuple time qualification rules (HeapTupleSatisfies routines) need to be
careful not to consider tuples with the "is multi" infomask bit set as
being only locked; they might need to look up MultiXact values (i.e.
possibly do pg_multixact I/O) to find out the Xid that updated a tuple,
whereas they previously were assured to only use information readily
available from the tuple header.  This is considered acceptable, because
the extra I/O would involve cases that would previously cause some
commands to block waiting for concurrent transactions to finish.

Another important change is the fact that locking tuples that have
previously been updated causes the future versions to be marked as
locked, too; this is essential for correctness of foreign key checks.
This causes additional WAL-logging, also (there was previously a single
WAL record for a locked tuple; now there are as many as updated copies
of the tuple there exist.)

With all this in place, contention related to tuples being checked by
foreign key rules should be much reduced.

As a bonus, the old behavior that a subtransaction grabbing a stronger
tuple lock than the parent (sub)transaction held on a given tuple and
later aborting caused the weaker lock to be lost, has been fixed.

Many new spec files were added for isolation tester framework, to ensure
overall behavior is sane.  There's probably room for several more tests.

There were several reviewers of this patch; in particular, Noah Misch
and Andres Freund spent considerable time in it.  Original idea for the
patch came from Simon Riggs, after a problem report by Joel Jacobson.
Most code is from me, with contributions from Marti Raudsepp, Alexander
Shulgin, Noah Misch and Andres Freund.

This patch was discussed in several pgsql-hackers threads; the most
important start at the following message-ids:
	AANLkTimo9XVcEzfiBR-ut3KVNDkjm2Vxh+t8kAmWjPuv@mail.gmail.com
	1290721684-sup-3951@alvh.no-ip.org
	1294953201-sup-2099@alvh.no-ip.org
	1320343602-sup-2290@alvh.no-ip.org
	1339690386-sup-8927@alvh.no-ip.org
	4FE5FF020200002500048A3D@gw.wicourts.gov
	4FEAB90A0200002500048B7D@gw.wicourts.gov
  • Loading branch information...
alvherre committed Jan 23, 2013
1 parent f925c79 commit 0ac5ad5134f2769ccbaefec73844f8504c4d6182
Showing with 6,023 additions and 1,487 deletions.
  1. +1 −1 contrib/file_fdw/output/file_fdw.source
  2. +1 −1 contrib/pageinspect/heapfuncs.c
  3. +46 −0 contrib/pg_upgrade/controldata.c
  4. +46 −0 contrib/pg_upgrade/pg_upgrade.c
  5. +7 −0 contrib/pg_upgrade/pg_upgrade.h
  6. +1 −1 contrib/pgrowlocks/Makefile
  7. +17 −0 contrib/pgrowlocks/pgrowlocks--1.0--1.1.sql
  8. +2 −2 contrib/pgrowlocks/{pgrowlocks--1.0.sql → pgrowlocks--1.1.sql}
  9. +126 −52 contrib/pgrowlocks/pgrowlocks.c
  10. +1 −1 contrib/pgrowlocks/pgrowlocks.control
  11. +9 −6 doc/src/sgml/pgrowlocks.sgml
  12. +96 −50 doc/src/sgml/ref/select.sgml
  13. +1 −1 src/backend/access/common/heaptuple.c
  14. +139 −0 src/backend/access/heap/README.tuplock
  15. +1,806 −439 src/backend/access/heap/heapam.c
  16. +5 −5 src/backend/access/heap/pruneheap.c
  17. +12 −5 src/backend/access/heap/rewriteheap.c
  18. +40 −16 src/backend/access/rmgrdesc/heapdesc.c
  19. +33 −4 src/backend/access/rmgrdesc/mxactdesc.c
  20. +4 −1 src/backend/access/rmgrdesc/xlogdesc.c
  21. +3 −3 src/backend/access/transam/README
  22. +814 −377 src/backend/access/transam/multixact.c
  23. +2 −0 src/backend/access/transam/varsup.c
  24. +13 −1 src/backend/access/transam/xlog.c
  25. +13 −1 src/backend/catalog/heap.c
  26. +5 −4 src/backend/catalog/index.c
  27. +6 −3 src/backend/commands/analyze.c
  28. +25 −12 src/backend/commands/cluster.c
  29. +11 −4 src/backend/commands/dbcommands.c
  30. +7 −3 src/backend/commands/sequence.c
  31. +9 −3 src/backend/commands/tablecmds.c
  32. +25 −7 src/backend/commands/trigger.c
  33. +79 −17 src/backend/commands/vacuum.c
  34. +18 −6 src/backend/commands/vacuumlazy.c
  35. +14 −7 src/backend/executor/execMain.c
  36. +20 −5 src/backend/executor/nodeLockRows.c
  37. +5 −1 src/backend/executor/nodeModifyTable.c
  38. +2 −2 src/backend/nodes/copyfuncs.c
  39. +2 −2 src/backend/nodes/equalfuncs.c
  40. +2 −2 src/backend/nodes/outfuncs.c
  41. +1 −1 src/backend/nodes/readfuncs.c
  42. +3 −3 src/backend/optimizer/plan/initsplan.c
  43. +24 −13 src/backend/optimizer/plan/planner.c
  44. +29 −29 src/backend/parser/analyze.c
  45. +15 −14 src/backend/parser/gram.y
  46. +41 −4 src/backend/postmaster/autovacuum.c
  47. +16 −16 src/backend/rewrite/rewriteHandler.c
  48. +14 −1 src/backend/storage/lmgr/lock.c
  49. +2 −2 src/backend/storage/lmgr/predicate.c
  50. +41 −9 src/backend/tcop/utility.c
  51. +13 −10 src/backend/utils/adt/ri_triggers.c
  52. +21 −7 src/backend/utils/adt/ruleutils.c
  53. +26 −5 src/backend/utils/cache/relcache.c
  54. +2 −3 src/backend/utils/time/combocid.c
  55. +379 −87 src/backend/utils/time/tqual.c
  56. +4 −0 src/bin/pg_controldata/pg_controldata.c
  57. +35 −2 src/bin/pg_resetxlog/pg_resetxlog.c
  58. +18 −5 src/include/access/heapam.h
  59. +28 −5 src/include/access/heapam_xlog.h
  60. +4 −2 src/include/access/htup.h
  61. +54 −8 src/include/access/htup_details.h
  62. +56 −10 src/include/access/multixact.h
  63. +1 −1 src/include/access/rewriteheap.h
  64. +1 −1 src/include/catalog/catversion.h
  65. +15 −9 src/include/catalog/pg_class.h
  66. +3 −1 src/include/catalog/pg_control.h
  67. +6 −4 src/include/catalog/pg_database.h
  68. +2 −0 src/include/catalog/pg_proc.h
  69. +2 −1 src/include/commands/cluster.h
  70. +4 −2 src/include/commands/vacuum.h
  71. +1 −1 src/include/executor/executor.h
  72. +4 −4 src/include/nodes/execnodes.h
  73. +23 −13 src/include/nodes/parsenodes.h
  74. +7 −5 src/include/nodes/plannodes.h
  75. +1 −1 src/include/parser/analyze.h
  76. +7 −0 src/include/postgres.h
  77. +1 −0 src/include/storage/lock.h
  78. +3 −0 src/include/utils/builtins.h
  79. +1 −0 src/include/utils/rel.h
  80. +2 −2 src/include/utils/relcache.h
  81. +1 −0 src/include/utils/tqual.h
  82. +276 −0 src/test/isolation/expected/aborted-keyrevoke.out
  83. +278 −0 src/test/isolation/expected/aborted-keyrevoke_2.out
  84. +76 −0 src/test/isolation/expected/delete-abort-savept-2.out
  85. +243 −0 src/test/isolation/expected/delete-abort-savept.out
  86. +1 −2 src/test/isolation/expected/fk-contention.out
  87. +110 −16 src/test/isolation/expected/fk-deadlock.out
  88. +80 −33 src/test/isolation/expected/fk-deadlock2.out
  89. +35 −40 src/test/isolation/expected/fk-deadlock2_1.out
  90. +105 −0 src/test/isolation/expected/fk-deadlock2_2.out
  91. +20 −24 src/test/isolation/expected/fk-deadlock_1.out
  92. +67 −0 src/test/isolation/expected/fk-deadlock_2.out
  93. +41 −0 src/test/isolation/expected/fk-delete-insert.out
  94. +65 −0 src/test/isolation/expected/lock-update-delete.out
  95. +18 −0 src/test/isolation/expected/lock-update-traversal.out
  96. +24 −0 src/test/isolation/expected/multixact-no-deadlock.out
  97. +5 −0 src/test/isolation/isolation_schedule
  98. +1 −0 src/test/isolation/isolationtester.c
  99. +31 −0 src/test/isolation/specs/aborted-keyrevoke.spec
  100. +34 −0 src/test/isolation/specs/delete-abort-savept-2.spec
  101. +29 −0 src/test/isolation/specs/delete-abort-savept.spec
  102. +0 −23 src/test/isolation/specs/fk-deadlock.spec
  103. +0 −23 src/test/isolation/specs/fk-deadlock2.spec
  104. +38 −0 src/test/isolation/specs/lock-update-delete.spec
  105. +32 −0 src/test/isolation/specs/lock-update-traversal.spec
  106. +35 −0 src/test/isolation/specs/multixact-no-deadlock.spec
@@ -191,7 +191,7 @@ ERROR: cannot change foreign table "agg_csv"
DELETE FROM agg_csv WHERE a = 100;
ERROR: cannot change foreign table "agg_csv"
SELECT * FROM agg_csv FOR UPDATE OF agg_csv;
ERROR: SELECT FOR UPDATE/SHARE cannot be used with foreign table "agg_csv"
ERROR: SELECT FOR UPDATE/SHARE/KEY UPDATE/KEY SHARE cannot be used with foreign table "agg_csv"
LINE 1: SELECT * FROM agg_csv FOR UPDATE OF agg_csv;
^
-- but this should be ignored
@@ -163,7 +163,7 @@ heap_page_items(PG_FUNCTION_ARGS)
tuphdr = (HeapTupleHeader) PageGetItem(page, id);
values[4] = UInt32GetDatum(HeapTupleHeaderGetXmin(tuphdr));
values[5] = UInt32GetDatum(HeapTupleHeaderGetXmax(tuphdr));
values[5] = UInt32GetDatum(HeapTupleHeaderGetRawXmax(tuphdr));
values[6] = UInt32GetDatum(HeapTupleHeaderGetRawCommandId(tuphdr)); /* shared with xvac */
values[7] = PointerGetDatum(&tuphdr->t_ctid);
values[8] = UInt32GetDatum(tuphdr->t_infomask2);
@@ -40,6 +40,9 @@ get_control_data(ClusterInfo *cluster, bool live_check)
bool got_xid = false;
bool got_oid = false;
bool got_nextxlogfile = false;
bool got_multi = false;
bool got_mxoff = false;
bool got_oldestmulti = false;
bool got_log_id = false;
bool got_log_seg = false;
bool got_tli = false;
@@ -246,6 +249,39 @@ get_control_data(ClusterInfo *cluster, bool live_check)
cluster->controldata.chkpnt_nxtoid = str2uint(p);
got_oid = true;
}
else if ((p = strstr(bufin, "Latest checkpoint's NextMultiXactId:")) != NULL)
{
p = strchr(p, ':');
if (p == NULL || strlen(p) <= 1)
pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__);
p++; /* removing ':' char */
cluster->controldata.chkpnt_nxtmulti = str2uint(p);
got_multi = true;
}
else if ((p = strstr(bufin, "Latest checkpoint's oldestMultiXid:")) != NULL)
{
p = strchr(p, ':');
if (p == NULL || strlen(p) <= 1)
pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__);
p++; /* removing ':' char */
cluster->controldata.chkpnt_oldstMulti = str2uint(p);
got_oldestmulti = true;
}
else if ((p = strstr(bufin, "Latest checkpoint's NextMultiOffset:")) != NULL)
{
p = strchr(p, ':');
if (p == NULL || strlen(p) <= 1)
pg_log(PG_FATAL, "%d: controldata retrieval problem\n", __LINE__);
p++; /* removing ':' char */
cluster->controldata.chkpnt_nxtmxoff = str2uint(p);
got_mxoff = true;
}
else if ((p = strstr(bufin, "Maximum data alignment:")) != NULL)
{
p = strchr(p, ':');
@@ -433,6 +469,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
/* verify that we got all the mandatory pg_control data */
if (!got_xid || !got_oid ||
!got_multi || !got_mxoff || !got_oldestmulti ||
(!live_check && !got_nextxlogfile) ||
!got_tli ||
!got_align || !got_blocksz || !got_largesz || !got_walsz ||
@@ -448,6 +485,15 @@ get_control_data(ClusterInfo *cluster, bool live_check)
if (!got_oid)
pg_log(PG_REPORT, " latest checkpoint next OID\n");
if (!got_multi)
pg_log(PG_REPORT, " latest checkpoint next MultiXactId\n");
if (!got_mxoff)
pg_log(PG_REPORT, " latest checkpoint next MultiXactOffset\n");
if (!got_oldestmulti)
pg_log(PG_REPORT, " latest checkpoint oldest MultiXactId\n");
if (!live_check && !got_nextxlogfile)
pg_log(PG_REPORT, " first WAL segment after reset\n");
@@ -382,6 +382,52 @@ copy_clog_xlog_xid(void)
new_cluster.pgdata);
check_ok();
/*
* If both new and old are after the pg_multixact change commit, copy those
* files too. If the old server is before that change and the new server
* is after, then we don't copy anything but we need to reset pg_control so
* that the new server doesn't attempt to read multis older than the cutoff
* value.
*/
if (old_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER &&
new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
{
copy_subdir_files("pg_multixact/offsets");
copy_subdir_files("pg_multixact/members");
prep_status("Setting next multixact ID and offset for new cluster");
/*
* we preserve all files and contents, so we must preserve both "next"
* counters here and the oldest multi present on system.
*/
exec_prog(UTILITY_LOG_FILE, NULL, true,
"\"%s/pg_resetxlog\" -O %u -m %u,%u \"%s\"",
new_cluster.bindir,
old_cluster.controldata.chkpnt_nxtmxoff,
old_cluster.controldata.chkpnt_nxtmulti,
old_cluster.controldata.chkpnt_oldstMulti,
new_cluster.pgdata);
check_ok();
}
else if (new_cluster.controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
{
prep_status("Setting oldest multixact ID on new cluster");
/*
* We don't preserve files in this case, but it's important that the
* oldest multi is set to the latest value used by the old system, so
* that multixact.c returns the empty set for multis that might be
* present on disk. We set next multi to the value following that; it
* might end up wrapped around (i.e. 0) if the old cluster had
* next=MaxMultiXactId, but multixact.c can cope with that just fine.
*/
exec_prog(UTILITY_LOG_FILE, NULL, true,
"\"%s/pg_resetxlog\" -m %u,%u \"%s\"",
new_cluster.bindir,
old_cluster.controldata.chkpnt_nxtmulti + 1,
old_cluster.controldata.chkpnt_nxtmulti,
new_cluster.pgdata);
check_ok();
}
/* now reset the wal archives in the new cluster */
prep_status("Resetting WAL archives");
exec_prog(UTILITY_LOG_FILE, NULL, true,
@@ -108,6 +108,10 @@ extern char *output_files[];
*/
#define VISIBILITY_MAP_CRASHSAFE_CAT_VER 201107031
/*
* pg_multixact format changed in this catversion:
*/
#define MULTIXACT_FORMATCHANGE_CAT_VER 201301231
/*
* Each relation is represented by a relinfo structure.
@@ -182,6 +186,9 @@ typedef struct
uint32 chkpnt_tli;
uint32 chkpnt_nxtxid;
uint32 chkpnt_nxtoid;
uint32 chkpnt_nxtmulti;
uint32 chkpnt_nxtmxoff;
uint32 chkpnt_oldstMulti;
uint32 align;
uint32 blocksz;
uint32 largesz;
@@ -4,7 +4,7 @@ MODULE_big = pgrowlocks
OBJS = pgrowlocks.o
EXTENSION = pgrowlocks
DATA = pgrowlocks--1.0.sql pgrowlocks--unpackaged--1.0.sql
DATA = pgrowlocks--1.1.sql pgrowlocks--1.0--1.1.sql pgrowlocks--unpackaged--1.0.sql
ifdef USE_PGXS
PG_CONFIG = pg_config
@@ -0,0 +1,17 @@
/* contrib/pgrowlocks/pgrowlocks--1.0--1.1.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pgrowlocks" to load this file. \quit
ALTER EXTENSION pgrowlocks DROP FUNCTION pgrowlocks(text);
DROP FUNCTION pgrowlocks(text);
CREATE FUNCTION pgrowlocks(IN relname text,
OUT locked_row TID, -- row TID
OUT locker XID, -- locking XID
OUT multi bool, -- multi XID?
OUT xids xid[], -- multi XIDs
OUT modes text[], -- multi XID statuses
OUT pids INTEGER[]) -- locker's process id
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'pgrowlocks'
LANGUAGE C STRICT;
@@ -1,14 +1,14 @@
/* contrib/pgrowlocks/pgrowlocks--1.0.sql */
/* contrib/pgrowlocks/pgrowlocks--1.1.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pgrowlocks" to load this file. \quit
CREATE FUNCTION pgrowlocks(IN relname text,
OUT locked_row TID, -- row TID
OUT lock_type TEXT, -- lock type
OUT locker XID, -- locking XID
OUT multi bool, -- multi XID?
OUT xids xid[], -- multi XIDs
OUT modes text[], -- multi XID statuses
OUT pids INTEGER[]) -- locker's process id
RETURNS SETOF record
AS 'MODULE_PATHNAME', 'pgrowlocks'
Oops, something went wrong.

0 comments on commit 0ac5ad5

Please sign in to comment.