8000 Fetch XIDs atomically during vac_truncate_clog(). · eldilibra/postgres@edd8873 · GitHub
[go: up one dir, main page]

Skip to content

Commit edd8873

Browse files
committed
Fetch XIDs atomically during vac_truncate_clog().
Because vac_update_datfrozenxid() updates datfrozenxid and datminmxid in-place, it's unsafe to assume that successive reads of those values will give consistent results. Fetch each one just once to ensure sane behavior in the minimum calculation. Noted while reviewing Alexander Korotkov's patch in the same area. Discussion: <8564.1464116473@sss.pgh.pa.us>
1 parent defe936 commit edd8873

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/backend/commands/vacuum.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,12 @@ vac_truncate_clog(TransactionId frozenXID,
10181018
/*
10191019
* Scan pg_database to compute the minimum datfrozenxid/datminmxid
10201020
*
1021+
* Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1022+
* the values could change while we look at them. Fetch each one just
1023+
* once to ensure sane behavior of the comparison logic. (Here, as in
1024+
* many other places, we assume that fetching or updating an XID in shared
1025+
* storage is atomic.)
1026+
*
10211027
* Note: we need not worry about a race condition with new entries being
10221028
* inserted by CREATE DATABASE. Any such entry will have a copy of some
10231029
* existing DB's datfrozenxid, and that source DB cannot be ours because
@@ -1033,10 +1039,12 @@ vac_truncate_clog(TransactionId frozenXID,
10331039

10341040
while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
10351041
{
1036-
Form_pg_database dbform = (Form_pg_database) GETSTRUCT(tuple);
1042+
volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1043+
TransactionId datfrozenxid = dbform->datfrozenxid;
1044+
TransactionId datminmxid = dbform->datminmxid;
10371045

1038-
Assert(TransactionIdIsNormal(dbform->datfrozenxid));
1039-
Assert(MultiXactIdIsValid(dbform->datminmxid));
1046+
Assert(TransactionIdIsNormal(datfrozenxid));
1047+
Assert(MultiXactIdIsValid(datminmxid));
10401048

10411049
/*
10421050
* If things are working properly, no database should have a
@@ -1047,21 +1055,21 @@ vac_truncate_clog(TransactionId frozenXID,
10471055
* databases have been scanned and cleaned up. (We will issue the
10481056
* "already wrapped" warning if appropriate, though.)
10491057
*/
1050-
if (TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid) ||
1051-
MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid))
1058+
if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1059+
MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
10521060
bogus = true;
10531061

1054-
if (TransactionIdPrecedes(nextXID, dbform->datfrozenxid))
1062+
if (TransactionIdPrecedes(nextXID, datfrozenxid))
10551063
frozenAlreadyWrapped = true;
1056-
else if (TransactionIdPrecedes(dbform->datfrozenxid, frozenXID))
1064+
else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
10571065
{
1058-
frozenXID = dbform->datfrozenxid;
1066+
frozenXID = datfrozenxid;
10591067
oldestxid_datoid = HeapTupleGetOid(tuple);
10601068
}
10611069

1062-
if (MultiXactIdPrecedes(dbform->datminmxid, minMulti))
1070+
if (MultiXactIdPrecedes(datminmxid, minMulti))
10631071
{
1064-
minMulti = dbform->datminmxid;
1072+
minMulti = datminmxid;
10651073
minmulti_datoid = HeapTupleGetOid(tuple);
10661074
}
10671075
}

0 commit comments

Comments
 (0)
0