Skip to content

Commit f222349

Browse files
committed
Close race condition between datfrozen and relfrozen updates.
vac_update_datfrozenxid() did multiple loads of relfrozenxid and relminmxid from buffer memory, and it assumed each would get the same value. Not so if a concurrent vac_update_relstats() did an inplace update. Commit 2d2e40e fixed the same kind of bug in vac_truncate_clog(). Today's bug could cause the rel-level field and XIDs in the rel's rows to precede the db-level field. A cluster having such values should VACUUM affected tables. Back-patch to v12 (all supported versions). Discussion: https://postgr.es/m/20240423003956.e7.nmisch@google.com
1 parent cb0ccef commit f222349

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/backend/commands/vacuum.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,8 @@ vac_update_datfrozenxid(void)
13301330
/*
13311331
* We must seqscan pg_class to find the minimum Xid, because there is no
13321332
* index that can help us here.
1333+
*
1334+
* See vac_truncate_clog() for the race condition to prevent.
13331335
*/
13341336
relation = table_open(RelationRelationId, AccessShareLock);
13351337

@@ -1338,7 +1340,9 @@ vac_update_datfrozenxid(void)
13381340

13391341
while ((classTup = systable_getnext(scan)) != NULL)
13401342
{
1341-
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
1343+
volatile FormData_pg_class *classForm = (Form_pg_class) GETSTRUCT(classTup);
1344+
TransactionId relfrozenxid = classForm->relfrozenxid;
1345+
TransactionId relminmxid = classForm->relminmxid;
13421346

13431347
/*
13441348
* Only consider relations able to hold unfrozen XIDs (anything else
@@ -1348,8 +1352,8 @@ vac_update_datfrozenxid(void)
13481352
classForm->relkind != RELKIND_MATVIEW &&
13491353
classForm->relkind != RELKIND_TOASTVALUE)
13501354
{
1351-
Assert(!TransactionIdIsValid(classForm->relfrozenxid));
1352-
Assert(!MultiXactIdIsValid(classForm->relminmxid));
1355+
Assert(!TransactionIdIsValid(relfrozenxid));
1356+
Assert(!MultiXactIdIsValid(relminmxid));
13531357
continue;
13541358
}
13551359

@@ -1368,34 +1372,34 @@ vac_update_datfrozenxid(void)
13681372
* before those relations have been scanned and cleaned up.
13691373
*/
13701374

1371-
if (TransactionIdIsValid(classForm->relfrozenxid))
1375+
if (TransactionIdIsValid(relfrozenxid))
13721376
{
1373-
Assert(TransactionIdIsNormal(classForm->relfrozenxid));
1377+
Assert(TransactionIdIsNormal(relfrozenxid));
13741378

13751379
/* check for values in the future */
1376-
if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid))
1380+
if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
13771381
{
13781382
bogus = true;
13791383
break;
13801384
}
13811385

13821386
/* determine new horizon */
1383-
if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
1384-
newFrozenXid = classForm->relfrozenxid;
1387+
if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1388+
newFrozenXid = relfrozenxid;
13851389
}
13861390

1387-
if (MultiXactIdIsValid(classForm->relminmxid))
1391+
if (MultiXactIdIsValid(relminmxid))
13881392
{
13891393
/* check for values in the future */
1390-
if (MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
1394+
if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
13911395
{
13921396
bogus = true;
13931397
break;
13941398
}
13951399

13961400
/* determine new horizon */
1397-
if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
1398-
newMinMulti = classForm->relminmxid;
1401+
if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1402+
newMinMulti = relminmxid;
13991403
}
14001404
}
14011405

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy