Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f65ab86

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 17a834a commit f65ab86

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/backend/commands/vacuum.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,8 @@ vac_update_datfrozenxid(void)
16111611
/*
16121612
* We must seqscan pg_class to find the minimum Xid, because there is no
16131613
* index that can help us here.
1614+
*
1615+
* See vac_truncate_clog() for the race condition to prevent.
16141616
*/
16151617
relation = table_open(RelationRelationId, AccessShareLock);
16161618

@@ -1619,7 +1621,9 @@ vac_update_datfrozenxid(void)
16191621

16201622
while ((classTup = systable_getnext(scan)) != NULL)
16211623
{
1622-
Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
1624+
volatile FormData_pg_class *classForm = (Form_pg_class) GETSTRUCT(classTup);
1625+
TransactionId relfrozenxid = classForm->relfrozenxid;
1626+
TransactionId relminmxid = classForm->relminmxid;
16231627

16241628
/*
16251629
* Only consider relations able to hold unfrozen XIDs (anything else
@@ -1629,8 +1633,8 @@ vac_update_datfrozenxid(void)
16291633
classForm->relkind != RELKIND_MATVIEW &&
16301634
classForm->relkind != RELKIND_TOASTVALUE)
16311635
{
1632-
Assert(!TransactionIdIsValid(classForm->relfrozenxid));
1633-
Assert(!MultiXactIdIsValid(classForm->relminmxid));
1636+
Assert(!TransactionIdIsValid(relfrozenxid));
1637+
Assert(!MultiXactIdIsValid(relminmxid));
16341638
continue;
16351639
}
16361640

@@ -1649,34 +1653,34 @@ vac_update_datfrozenxid(void)
16491653
* before those relations have been scanned and cleaned up.
16501654
*/
16511655

1652-
if (TransactionIdIsValid(classForm->relfrozenxid))
1656+
if (TransactionIdIsValid(relfrozenxid))
16531657
{
1654-
Assert(TransactionIdIsNormal(classForm->relfrozenxid));
1658+
Assert(TransactionIdIsNormal(relfrozenxid));
16551659

16561660
/* check for values in the future */
1657-
if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid))
1661+
if (TransactionIdPrecedes(lastSaneFrozenXid, relfrozenxid))
16581662
{
16591663
bogus = true;
16601664
break;
16611665
}
16621666

16631667
/* determine new horizon */
1664-
if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
1665-
newFrozenXid = classForm->relfrozenxid;
1668+
if (TransactionIdPrecedes(relfrozenxid, newFrozenXid))
1669+
newFrozenXid = relfrozenxid;
16661670
}
16671671

1668-
if (MultiXactIdIsValid(classForm->relminmxid))
1672+
if (MultiXactIdIsValid(relminmxid))
16691673
{
16701674
/* check for values in the future */
1671-
if (MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
1675+
if (MultiXactIdPrecedes(lastSaneMinMulti, relminmxid))
16721676
{
16731677
bogus = true;
16741678
break;
16751679
}
16761680

16771681
/* determine new horizon */
1678-
if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
1679-
newMinMulti = classForm->relminmxid;
1682+
if (MultiXactIdPrecedes(relminmxid, newMinMulti))
1683+
newMinMulti = relminmxid;
16801684
}
16811685
}
16821686

0 commit comments

Comments
 (0)