Revive "snapshot too old" with wal_level=minimal and SET TABLESPACE.
authorNoah Misch <[email protected]>
Sat, 30 Jan 2021 08:12:18 +0000 (00:12 -0800)
committerNoah Misch <[email protected]>
Sat, 30 Jan 2021 08:12:18 +0000 (00:12 -0800)
Given a permanent relation rewritten in the current transaction, the
old_snapshot_threshold mechanism assumed the relation had never been
subject to early pruning.  Hence, a query could fail to report "snapshot
too old" when the rewrite followed an early truncation.  ALTER TABLE SET
TABLESPACE is probably the only rewrite mechanism capable of exposing
this bug.  REINDEX sets indcheckxmin, avoiding the problem.  CLUSTER has
zeroed page LSNs since before old_snapshot_threshold existed, so
old_snapshot_threshold has never cooperated with it.  ALTER TABLE
... SET DATA TYPE makes the table look empty to every past snapshot,
which is strictly worse.  Back-patch to v13, where commit
c6b92041d38512a4176ed76ad06f713d2e6c01a8 broke this.

Kyotaro Horiguchi and Noah Misch

Discussion: https://postgr.es/m/20210113.160705.2225256954956139776[email protected]

src/backend/utils/time/snapmgr.c
src/include/utils/snapmgr.h

index ae16c3ed7d60baef15c9f9fcc0f5e4e228690723..95704265b6785712fc370c6343a1dcb2e6b33554 100644 (file)
@@ -1764,7 +1764,11 @@ TransactionIdLimitedForOldSnapshots(TransactionId recentXmin,
    Assert(OldSnapshotThresholdActive());
    Assert(limit_ts != NULL && limit_xid != NULL);
 
-   if (!RelationAllowsEarlyPruning(relation))
+   /*
+    * TestForOldSnapshot() assumes early pruning advances the page LSN, so we
+    * can't prune early when skipping WAL.
+    */
+   if (!RelationAllowsEarlyPruning(relation) || !RelationNeedsWAL(relation))
        return false;
 
    ts = GetSnapshotCurrentTimestamp();
index 579be352c5f4631137511962b5b7b5afe5652d82..c21ee3c289c967b5e2cdf4e9d99d26b4be6d07cf 100644 (file)
@@ -37,7 +37,7 @@
  */
 #define RelationAllowsEarlyPruning(rel) \
 ( \
-    RelationNeedsWAL(rel) \
+    (rel)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT  \
   && !IsCatalogRelation(rel) \
   && !RelationIsAccessibleInLogicalDecoding(rel) \
 )