mirror of
https://github.com/postgres/postgres.git
synced 2025-08-25 20:23:07 +03:00
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
This commit is contained in:
@@ -69,6 +69,7 @@
|
||||
|
||||
#include "access/heapam.h"
|
||||
#include "access/htup_details.h"
|
||||
#include "access/multixact.h"
|
||||
#include "access/reloptions.h"
|
||||
#include "access/transam.h"
|
||||
#include "access/xact.h"
|
||||
@@ -136,8 +137,9 @@ static volatile sig_atomic_t got_SIGHUP = false;
|
||||
static volatile sig_atomic_t got_SIGUSR2 = false;
|
||||
static volatile sig_atomic_t got_SIGTERM = false;
|
||||
|
||||
/* Comparison point for determining whether freeze_max_age is exceeded */
|
||||
/* Comparison points for determining whether freeze_max_age is exceeded */
|
||||
static TransactionId recentXid;
|
||||
static MultiXactId recentMulti;
|
||||
|
||||
/* Default freeze ages to use for autovacuum (varies by database) */
|
||||
static int default_freeze_min_age;
|
||||
@@ -161,6 +163,7 @@ typedef struct avw_dbase
|
||||
Oid adw_datid;
|
||||
char *adw_name;
|
||||
TransactionId adw_frozenxid;
|
||||
MultiXactId adw_frozenmulti;
|
||||
PgStat_StatDBEntry *adw_entry;
|
||||
} avw_dbase;
|
||||
|
||||
@@ -1076,7 +1079,9 @@ do_start_worker(void)
|
||||
List *dblist;
|
||||
ListCell *cell;
|
||||
TransactionId xidForceLimit;
|
||||
MultiXactId multiForceLimit;
|
||||
bool for_xid_wrap;
|
||||
bool for_multi_wrap;
|
||||
avw_dbase *avdb;
|
||||
TimestampTz current_time;
|
||||
bool skipit = false;
|
||||
@@ -1122,12 +1127,20 @@ do_start_worker(void)
|
||||
if (xidForceLimit < FirstNormalTransactionId)
|
||||
xidForceLimit -= FirstNormalTransactionId;
|
||||
|
||||
/* Also determine the oldest datminmxid we will consider. */
|
||||
recentMulti = ReadNextMultiXactId();
|
||||
multiForceLimit = recentMulti - autovacuum_freeze_max_age;
|
||||
if (multiForceLimit < FirstMultiXactId)
|
||||
multiForceLimit -= FirstMultiXactId;
|
||||
|
||||
/*
|
||||
* Choose a database to connect to. We pick the database that was least
|
||||
* recently auto-vacuumed, or one that needs vacuuming to prevent Xid
|
||||
* wraparound-related data loss. If any db at risk of wraparound is
|
||||
* wraparound-related data loss. If any db at risk of Xid wraparound is
|
||||
* found, we pick the one with oldest datfrozenxid, independently of
|
||||
* autovacuum times.
|
||||
* autovacuum times; similarly we pick the one with the oldest datminmxid
|
||||
* if any is in MultiXactId wraparound. Note that those in Xid wraparound
|
||||
* danger are given more priority than those in multi wraparound danger.
|
||||
*
|
||||
* Note that a database with no stats entry is not considered, except for
|
||||
* Xid wraparound purposes. The theory is that if no one has ever
|
||||
@@ -1143,6 +1156,7 @@ do_start_worker(void)
|
||||
*/
|
||||
avdb = NULL;
|
||||
for_xid_wrap = false;
|
||||
for_multi_wrap = false;
|
||||
current_time = GetCurrentTimestamp();
|
||||
foreach(cell, dblist)
|
||||
{
|
||||
@@ -1153,13 +1167,25 @@ do_start_worker(void)
|
||||
if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
|
||||
{
|
||||
if (avdb == NULL ||
|
||||
TransactionIdPrecedes(tmp->adw_frozenxid, avdb->adw_frozenxid))
|
||||
TransactionIdPrecedes(tmp->adw_frozenxid,
|
||||
avdb->adw_frozenxid))
|
||||
avdb = tmp;
|
||||
for_xid_wrap = true;
|
||||
continue;
|
||||
}
|
||||
else if (for_xid_wrap)
|
||||
continue; /* ignore not-at-risk DBs */
|
||||
else if (MultiXactIdPrecedes(tmp->adw_frozenmulti, multiForceLimit))
|
||||
{
|
||||
if (avdb == NULL ||
|
||||
MultiXactIdPrecedes(tmp->adw_frozenmulti,
|
||||
avdb->adw_frozenmulti))
|
||||
avdb = tmp;
|
||||
for_multi_wrap = true;
|
||||
continue;
|
||||
}
|
||||
else if (for_multi_wrap)
|
||||
continue; /* ignore not-at-risk DBs */
|
||||
|
||||
/* Find pgstat entry if any */
|
||||
tmp->adw_entry = pgstat_fetch_stat_dbentry(tmp->adw_datid);
|
||||
@@ -1642,6 +1668,7 @@ AutoVacWorkerMain(int argc, char *argv[])
|
||||
|
||||
/* And do an appropriate amount of work */
|
||||
recentXid = ReadNewTransactionId();
|
||||
recentMulti = ReadNextMultiXactId();
|
||||
do_autovacuum();
|
||||
}
|
||||
|
||||
@@ -1847,6 +1874,7 @@ get_database_list(void)
|
||||
avdb->adw_datid = HeapTupleGetOid(tup);
|
||||
avdb->adw_name = pstrdup(NameStr(pgdatabase->datname));
|
||||
avdb->adw_frozenxid = pgdatabase->datfrozenxid;
|
||||
avdb->adw_frozenmulti = pgdatabase->datminmxid;
|
||||
/* this gets set later: */
|
||||
avdb->adw_entry = NULL;
|
||||
|
||||
@@ -2601,6 +2629,7 @@ relation_needs_vacanalyze(Oid relid,
|
||||
/* freeze parameters */
|
||||
int freeze_max_age;
|
||||
TransactionId xidForceLimit;
|
||||
MultiXactId multiForceLimit;
|
||||
|
||||
AssertArg(classForm != NULL);
|
||||
AssertArg(OidIsValid(relid));
|
||||
@@ -2641,6 +2670,14 @@ relation_needs_vacanalyze(Oid relid,
|
||||
force_vacuum = (TransactionIdIsNormal(classForm->relfrozenxid) &&
|
||||
TransactionIdPrecedes(classForm->relfrozenxid,
|
||||
xidForceLimit));
|
||||
if (!force_vacuum)
|
||||
{
|
||||
multiForceLimit = recentMulti - autovacuum_freeze_max_age;
|
||||
if (multiForceLimit < FirstMultiXactId)
|
||||
multiForceLimit -= FirstMultiXactId;
|
||||
force_vacuum = MultiXactIdPrecedes(classForm->relminmxid,
|
||||
multiForceLimit);
|
||||
}
|
||||
*wraparound = force_vacuum;
|
||||
|
||||
/* User disabled it in pg_class.reloptions? (But ignore if at risk) */
|
||||
|
Reference in New Issue
Block a user