Skip to content

Commit 2ca19aa

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 617a239 commit 2ca19aa

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
@@ -1480,6 +1480,8 @@ vac_update_datfrozenxid(void)
14801480
/*
14811481
* We must seqscan pg_class to find the minimum Xid, because there is no
14821482
* index that can help us here.
1483+
*
1484+
* See vac_truncate_clog() for the race condition to prevent.
14831485
*/
14841486
relation = table_open(RelationRelationId, AccessShareLock);
14851487

@@ -1488,7 +1490,9 @@ vac_update_datfrozenxid(void)
14881490

14891491
while ((classTup = systable_getnext(scan)) != NULL)
14901492
{
1491-
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
1493+
volatile FormData_pg_class *classForm = (Form_pg_class) GETSTRUCT(classTup);
1494+
TransactionId relfrozenxid = classForm->relfrozenxid;
1495+
TransactionId relminmxid = classForm->relminmxid;
14921496

14931497
/*
14941498
* Only consider relations able to hold unfrozen XIDs (anything else
@@ -1498,8 +1502,8 @@ vac_update_datfrozenxid(void)
14981502
classForm->relkind != RELKIND_MATVIEW &&
14991503
classForm->relkind != RELKIND_TOASTVALUE)
15001504
{
1501-
Assert(!TransactionIdIsValid(classForm->relfrozenxid));
1502-
Assert(!MultiXactIdIsValid(classForm->relminmxid));
1505+
Assert(!TransactionIdIsValid(relfrozenxid));
1506+
Assert(!MultiXactIdIsValid(relminmxid));
15031507
continue;
15041508
}
15051509

@@ -1518,34 +1522,34 @@ vac_update_datfrozenxid(void)
15181522
* before those relations have been scanned and cleaned up.
15191523
*/
15201524

1521-
if (TransactionIdIsValid(classForm->relfrozenxid))
1525+
if (TransactionIdIsValid(relfrozenxid))
15221526
{
1523-
Assert(TransactionIdIsNormal(classForm->relfrozenxid));
1527+
Assert(TransactionIdIsNormal(relfrozenxid));
15241528

15251529
/* check for values in the future */
1526-
if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid))
1530+
if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
15271531
{
15281532
bogus = true;
15291533
break;
15301534
}
15311535

15321536
/* determine new horizon */
1533-
if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
1534-
newFrozenXid = classForm->relfrozenxid;
1537+
if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1538+
newFrozenXid = relfrozenxid;
15351539
}
15361540

1537-
if (MultiXactIdIsValid(classForm->relminmxid))
1541+
if (MultiXactIdIsValid(relminmxid))
15381542
{
15391543
/* check for values in the future */
1540-
if (MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
1544+
if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
15411545
{
15421546
bogus = true;
15431547
break;
15441548
}
15451549

15461550
/* determine new horizon */
1547-
if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
1548-
newMinMulti = classForm->relminmxid;
1551+
if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1552+
newMinMulti = relminmxid;
15491553
}
15501554
}
15511555

0 commit comments

Comments
 (0)