Skip to content

Commit 5b3ed6b

Browse files
committed
Do not select new object OIDs that match recently-dead entries.
When selecting a new OID, we take care to avoid picking one that's already in use in the target table, so as not to create duplicates after the OID counter has wrapped around. However, up to now we used SnapshotDirty when scanning for pre-existing entries. That ignores committed-dead rows, so that we could select an OID matching a deleted-but-not-yet-vacuumed row. While that mostly worked, it has two problems: * If recently deleted, the dead row might still be visible to MVCC snapshots, creating a risk for duplicate OIDs when examining the catalogs within our own transaction. Such duplication couldn't be visible outside the object-creating transaction, though, and we've heard few if any field reports corresponding to such a symptom. * When selecting a TOAST OID, deleted toast rows definitely *are* visible to SnapshotToast, and will remain so until vacuumed away. This leads to a conflict that will manifest in errors like "unexpected chunk number 0 (expected 1) for toast value nnnnn". We've been seeing reports of such errors from the field for years, but the cause was unclear before. The fix is simple: just use SnapshotAny to search for conflicting rows. This results in a slightly longer window before object OIDs can be recycled, but that seems unlikely to create any large problems. Pavan Deolasee Discussion: https://postgr.es/m/CABOikdOgWT2hHkYG3Wwo2cyZJq2zfs1FH0FgX-=h4OLosXHf9w@mail.gmail.com
1 parent 310d137 commit 5b3ed6b

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/backend/access/heap/tuptoaster.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,7 +1650,9 @@ toast_delete_datum(Relation rel, Datum value)
16501650
/* ----------
16511651
* toastrel_valueid_exists -
16521652
*
1653-
* Test whether a toast value with the given ID exists in the toast relation
1653+
* Test whether a toast value with the given ID exists in the toast relation.
1654+
* For safety, we consider a value to exist if there are either live or dead
1655+
* toast rows with that ID; see notes for GetNewOid().
16541656
* ----------
16551657
*/
16561658
static bool
@@ -1682,7 +1684,7 @@ toastrel_valueid_exists(Relation toastrel, Oid valueid)
16821684
*/
16831685
toastscan = systable_beginscan(toastrel,
16841686
RelationGetRelid(toastidxs[validIndex]),
1685-
true, SnapshotToast, 1, &toastkey);
1687+
true, SnapshotAny, 1, &toastkey);
16861688

16871689
if (systable_getnext(toastscan) != NULL)
16881690
result = true;

src/backend/catalog/catalog.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,12 @@ IsSharedRelation(Oid relationId)
270270
* managed to cycle through 2^32 OIDs and generate the same OID before we
271271
* finish inserting our row. This seems unlikely to be a problem. Note
272272
* that if we had to *commit* the row to end the race condition, the risk
273-
* would be rather higher; therefore we use SnapshotDirty in the test,
274-
* so that we will see uncommitted rows.
273+
* would be rather higher; therefore we use SnapshotAny in the test, so that
274+
* we will see uncommitted rows. (We used to use SnapshotDirty, but that has
275+
* the disadvantage that it ignores recently-deleted rows, creating a risk
276+
* of transient conflicts for as long as our own MVCC snapshots think a
277+
* recently-deleted row is live. The risk is far higher when selecting TOAST
278+
* OIDs, because SnapshotToast considers dead rows as active indefinitely.)
275279
*/
276280
Oid
277281
GetNewOid(Relation relation)
@@ -324,13 +328,10 @@ Oid
324328
GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
325329
{
326330
Oid newOid;
327-
SnapshotData SnapshotDirty;
328331
SysScanDesc scan;
329332
ScanKeyData key;
330333
bool collides;
331334

332-
InitDirtySnapshot(SnapshotDirty);
333-
334335
/* Generate new OIDs until we find one not in the table */
335336
do
336337
{
@@ -343,9 +344,9 @@ GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
343344
BTEqualStrategyNumber, F_OIDEQ,
344345
ObjectIdGetDatum(newOid));
345346

346-
/* see notes above about using SnapshotDirty */
347+
/* see notes above about using SnapshotAny */
347348
scan = systable_beginscan(relation, indexId, true,
348-
&SnapshotDirty, 1, &key);
349+
SnapshotAny, 1, &key);
349350

350351
collides = HeapTupleIsValid(systable_getnext(scan));
351352

0 commit comments

Comments
 (0)