diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 31a1438..945f55c 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -2060,7 +2060,8 @@ index_build(Relation heapRelation, * about any concurrent readers of the tuple; no other transaction can see * it yet. */ - if (indexInfo->ii_BrokenHotChain && !isreindex && + if ((indexInfo->ii_BrokenHotChain || EarlyVacuumEnabled(indexRelation)) && + !isreindex && !indexInfo->ii_Concurrent) { Oid indexId = RelationGetRelid(indexRelation); @@ -3409,9 +3410,10 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence, !indexForm->indisready || !indexForm->indislive); if (index_bad || - (indexForm->indcheckxmin && !indexInfo->ii_BrokenHotChain)) + (indexForm->indcheckxmin && + !(indexInfo->ii_BrokenHotChain || EarlyVacuumEnabled(iRel)))) { - if (!indexInfo->ii_BrokenHotChain) + if (!(indexInfo->ii_BrokenHotChain || EarlyVacuumEnabled(iRel))) indexForm->indcheckxmin = false; else if (index_bad) indexForm->indcheckxmin = true; diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 8a830d4..4e50b19 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -4290,8 +4290,7 @@ IssuePendingWritebacks(WritebackContext *context) void TestForOldSnapshot_impl(Snapshot snapshot, Relation relation) { - if (!IsCatalogRelation(relation) - && !RelationIsAccessibleInLogicalDecoding(relation) + if (RelationAllowsEarlyVacuum(relation) && (snapshot)->whenTaken < GetOldSnapshotThresholdTimestamp()) ereport(ERROR, (errcode(ERRCODE_SNAPSHOT_TOO_OLD), diff --git a/src/backend/utils/time/snapmgr.c b/src/backend/utils/time/snapmgr.c index f8a2a83..2eabd1c 100644 --- a/src/backend/utils/time/snapmgr.c +++ b/src/backend/utils/time/snapmgr.c @@ -1597,10 +1597,7 @@ TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, { if (TransactionIdIsNormal(recentXmin) && old_snapshot_threshold >= 0 - && RelationNeedsWAL(relation) - && !IsCatalogRelation(relation) - && !RelationIsAccessibleInLogicalDecoding(relation) - && !RelationHasUnloggedIndex(relation)) + && RelationAllowsEarlyVacuum(relation)) { int64 ts = GetSnapshotCurrentTimestamp(); TransactionId xlimit = recentXmin; diff --git a/src/include/utils/snapmgr.h b/src/include/utils/snapmgr.h index 4270696..effdb0c 100644 --- a/src/include/utils/snapmgr.h +++ b/src/include/utils/snapmgr.h @@ -31,6 +31,19 @@ #define OLD_SNAPSHOT_PADDING_ENTRIES 10 #define OLD_SNAPSHOT_TIME_MAP_ENTRIES (old_snapshot_threshold + OLD_SNAPSHOT_PADDING_ENTRIES) +/* + * Common definition of relation properties that allow early pruning/vacuuming + * when old_snapshot_threshold >= 0. + */ +#define RelationAllowsEarlyVacuum(rel) \ +( \ + RelationNeedsWAL(rel) \ + && !IsCatalogRelation(rel) \ + && !RelationIsAccessibleInLogicalDecoding(rel) \ + && !RelationHasUnloggedIndex(rel) \ +) + +#define EarlyVacuumEnabled(rel) (old_snapshot_threshold >= 0 && RelationAllowsEarlyVacuum(rel)) /* GUC variables */ extern PGDLLIMPORT int old_snapshot_threshold;