Do not select new object OIDs that match recently-dead entries.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 11 Apr 2018 21:41:10 +0000 (17:41 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 11 Apr 2018 21:41:26 +0000 (17:41 -0400)
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

src/backend/access/heap/tuptoaster.c
src/backend/catalog/catalog.c

index f989f9577d3768f5ce2facf8e52942902433ef3b..0fa3c4132b3256daecc48112fb930d51a3d296a5 100644 (file)
@@ -1721,7 +1721,9 @@ toast_delete_datum(Relation rel, Datum value, bool is_speculative)
 /* ----------
  * toastrel_valueid_exists -
  *
- * Test whether a toast value with the given ID exists in the toast relation
+ * Test whether a toast value with the given ID exists in the toast relation.
+ * For safety, we consider a value to exist if there are either live or dead
+ * toast rows with that ID; see notes for GetNewOid().
  * ----------
  */
 static bool
@@ -1753,7 +1755,7 @@ toastrel_valueid_exists(Relation toastrel, Oid valueid)
     */
    toastscan = systable_beginscan(toastrel,
                                   RelationGetRelid(toastidxs[validIndex]),
-                                  true, SnapshotToast, 1, &toastkey);
+                                  true, SnapshotAny, 1, &toastkey);
 
    if (systable_getnext(toastscan) != NULL)
        result = true;
index f2d6745eb9e2884d9b18d64d7dcfde11e13bf78f..8c5bbf801d54a16aa2c5f6922359ab6b156694b9 100644 (file)
@@ -277,8 +277,12 @@ IsSharedRelation(Oid relationId)
  * managed to cycle through 2^32 OIDs and generate the same OID before we
  * finish inserting our row.  This seems unlikely to be a problem.  Note
  * that if we had to *commit* the row to end the race condition, the risk
- * would be rather higher; therefore we use SnapshotDirty in the test,
- * so that we will see uncommitted rows.
+ * would be rather higher; therefore we use SnapshotAny in the test, so that
+ * we will see uncommitted rows.  (We used to use SnapshotDirty, but that has
+ * the disadvantage that it ignores recently-deleted rows, creating a risk
+ * of transient conflicts for as long as our own MVCC snapshots think a
+ * recently-deleted row is live.  The risk is far higher when selecting TOAST
+ * OIDs, because SnapshotToast considers dead rows as active indefinitely.)
  */
 Oid
 GetNewOid(Relation relation)
@@ -331,7 +335,6 @@ Oid
 GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
 {
    Oid         newOid;
-   SnapshotData SnapshotDirty;
    SysScanDesc scan;
    ScanKeyData key;
    bool        collides;
@@ -344,8 +347,6 @@ GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
     */
    Assert(!IsBinaryUpgrade || RelationGetRelid(relation) != TypeRelationId);
 
-   InitDirtySnapshot(SnapshotDirty);
-
    /* Generate new OIDs until we find one not in the table */
    do
    {
@@ -358,9 +359,9 @@ GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
                    BTEqualStrategyNumber, F_OIDEQ,
                    ObjectIdGetDatum(newOid));
 
-       /* see notes above about using SnapshotDirty */
+       /* see notes above about using SnapshotAny */
        scan = systable_beginscan(relation, indexId, true,
-                                 &SnapshotDirty, 1, &key);
+                                 SnapshotAny, 1, &key);
 
        collides = HeapTupleIsValid(systable_getnext(scan));