From 2cecfede527f3f886f3defb27938b3610257e1c6 Mon Sep 17 00:00:00 2001 From: Mikhail Nikalayeu Date: Wed, 14 Jan 2026 15:59:47 +0300 Subject: [PATCH v2 2/4] Reset snapshots periodically in non-unique non-parallel concurrent index builds Long-living snapshots used by CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY can hold back the global xmin horizon. Commit d9d076222f5b attempted to allow VACUUM to ignore such snapshots to mitigate this problem. However, this was reverted in commit e28bb8851969 because it could cause indexes to miss heap tuples that were HOT-updated and HOT-pruned during the index creation, leading to index corruption. This patch introduces an alternative by periodically resetting the snapshot used during the first phase. By resetting the snapshot every N pages during the heap scan, it allows the xmin horizon to advance. Currently, this technique is applied to: - only during the first scan of the heap: The second scan during index validation still uses a single snapshot to ensure index correctness - non-parallel index builds: Parallel index builds are not yet supported and will be addressed in following commits - non-unique indexes: Unique index builds still require a consistent snapshot to enforce uniqueness constraints, will be addressed in following commits A new scan option SO_RESET_SNAPSHOT is introduced. When set, it causes the snapshot to be reset "between" every SO_RESET_SNAPSHOT_EACH_N_PAGE pages during the scan. The heap scan code is adjusted to support this option, and the index build code is modified to use it for applicable concurrent index builds that are not on system catalogs and not using parallel workers. --- contrib/amcheck/verify_nbtree.c | 3 +- contrib/pgstattuple/pgstattuple.c | 2 +- src/backend/access/brin/brin.c | 19 ++- src/backend/access/gin/gininsert.c | 22 ++++ src/backend/access/gist/gistbuild.c | 4 + src/backend/access/hash/hash.c | 3 + src/backend/access/heap/heapam.c | 47 ++++++- src/backend/access/heap/heapam_handler.c | 57 +++++++-- src/backend/access/index/genam.c | 2 +- src/backend/access/nbtree/nbtsort.c | 30 ++++- src/backend/access/spgist/spginsert.c | 3 + src/backend/access/transam/xact.c | 11 ++ src/backend/catalog/index.c | 31 ++++- src/backend/commands/indexcmds.c | 17 +-- src/backend/optimizer/plan/planner.c | 10 ++ src/backend/tcop/utility.c | 3 + src/backend/utils/errcodes.txt | 1 + src/bin/pg_amcheck/t/006_cic.pl | 2 +- src/include/access/heapam.h | 2 + src/include/access/tableam.h | 28 ++++- src/include/access/xact.h | 1 + src/test/modules/injection_points/Makefile | 2 +- .../expected/cic_reset_snapshots.out | 115 ++++++++++++++++++ src/test/modules/injection_points/meson.build | 1 + .../sql/cic_reset_snapshots.sql | 95 +++++++++++++++ 25 files changed, 474 insertions(+), 37 deletions(-) create mode 100644 src/test/modules/injection_points/expected/cic_reset_snapshots.out create mode 100644 src/test/modules/injection_points/sql/cic_reset_snapshots.sql diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c index e04b7ca694e..032df2d1999 100644 --- a/contrib/amcheck/verify_nbtree.c +++ b/contrib/amcheck/verify_nbtree.c @@ -557,7 +557,8 @@ bt_check_every_level(Relation rel, Relation heaprel, bool heapkeyspace, 0, /* number of keys */ NULL, /* scan key */ true, /* buffer access strategy OK */ - true); /* syncscan OK? */ + true, /* syncscan OK? */ + false); /* * Scan will behave as the first scan of a CREATE INDEX CONCURRENTLY diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c index 6a7f8cb4a7c..a0e7ed2e137 100644 --- a/contrib/pgstattuple/pgstattuple.c +++ b/contrib/pgstattuple/pgstattuple.c @@ -335,7 +335,7 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo) errmsg("only heap AM is supported"))); /* Disable syncscan because we assume we scan from block zero upwards */ - scan = table_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false); + scan = table_beginscan_strat(rel, SnapshotAny, 0, NULL, true, false, false); hscan = (HeapScanDesc) scan; InitDirtySnapshot(SnapshotDirty); diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c index 146ee97a47d..498eb2b991b 100644 --- a/src/backend/access/brin/brin.c +++ b/src/backend/access/brin/brin.c @@ -1219,11 +1219,12 @@ brinbuild(Relation heap, Relation index, IndexInfo *indexInfo) state->bs_sortstate = tuplesort_begin_index_brin(maintenance_work_mem, coordinate, TUPLESORT_NONE); - + InvalidateCatalogSnapshot(); /* scan the relation and merge per-worker results */ reltuples = _brin_parallel_merge(state); _brin_end_parallel(state->bs_leader, state); + Assert(!indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); } else /* no parallel index build */ { @@ -1236,6 +1237,7 @@ brinbuild(Relation heap, Relation index, IndexInfo *indexInfo) reltuples = table_index_build_scan(heap, index, indexInfo, false, true, brinbuildCallback, state, NULL); + InvalidateCatalogSnapshot(); /* * process the final batch * @@ -1255,6 +1257,7 @@ brinbuild(Relation heap, Relation index, IndexInfo *indexInfo) brin_fill_empty_ranges(state, state->bs_currRangeStart, state->bs_maxRangeStart); + Assert(!indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); } /* release resources */ @@ -2391,6 +2394,7 @@ _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, WalUsage *walusage; BufferUsage *bufferusage; bool leaderparticipates = true; + bool need_pop_active_snapshot = true; int querylen; #ifdef DISABLE_LEADER_PARTICIPATION @@ -2416,9 +2420,16 @@ _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, * live according to that. */ if (!isconcurrent) + { + Assert(ActiveSnapshotSet()); snapshot = SnapshotAny; + need_pop_active_snapshot = false; + } else + { snapshot = RegisterSnapshot(GetTransactionSnapshot()); + PushActiveSnapshot(GetTransactionSnapshot()); + } /* * Estimate size for our own PARALLEL_KEY_BRIN_SHARED workspace. @@ -2461,6 +2472,8 @@ _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, /* If no DSM segment was available, back out (do serial build) */ if (pcxt->seg == NULL) { + if (need_pop_active_snapshot) + PopActiveSnapshot(); if (IsMVCCSnapshot(snapshot)) UnregisterSnapshot(snapshot); DestroyParallelContext(pcxt); @@ -2540,6 +2553,8 @@ _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, /* If no workers were successfully launched, back out (do serial build) */ if (pcxt->nworkers_launched == 0) { + if (need_pop_active_snapshot) + PopActiveSnapshot(); _brin_end_parallel(brinleader, NULL); return; } @@ -2556,6 +2571,8 @@ _brin_begin_parallel(BrinBuildState *buildstate, Relation heap, Relation index, * sure that the failure-to-start case will not hang forever. */ WaitForParallelWorkersToAttach(pcxt); + if (need_pop_active_snapshot) + PopActiveSnapshot(); } /* diff --git a/src/backend/access/gin/gininsert.c b/src/backend/access/gin/gininsert.c index c7e38dbe193..b0087cb1a62 100644 --- a/src/backend/access/gin/gininsert.c +++ b/src/backend/access/gin/gininsert.c @@ -29,6 +29,7 @@ #include "storage/bufmgr.h" #include "storage/proc.h" #include "storage/predicate.h" +#include "storage/proc.h" #include "tcop/tcopprot.h" #include "utils/datum.h" #include "utils/memutils.h" @@ -680,6 +681,9 @@ ginbuild(Relation heap, Relation index, IndexInfo *indexInfo) buildstate.accum.ginstate = &buildstate.ginstate; ginInitBA(&buildstate.accum); + InvalidateCatalogSnapshot(); + Assert(!indexInfo->ii_Concurrent || indexInfo->ii_ParallelWorkers || !TransactionIdIsValid(MyProc->xmin)); + /* Report table scan phase started */ pgstat_progress_update_param(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_GIN_PHASE_INDEXBUILD_TABLESCAN); @@ -742,11 +746,13 @@ ginbuild(Relation heap, Relation index, IndexInfo *indexInfo) tuplesort_begin_index_gin(heap, index, maintenance_work_mem, coordinate, TUPLESORT_NONE); + InvalidateCatalogSnapshot(); /* scan the relation in parallel and merge per-worker results */ reltuples = _gin_parallel_merge(state); _gin_end_parallel(state->bs_leader, state); + Assert(!indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); } else /* no parallel index build */ { @@ -756,6 +762,7 @@ ginbuild(Relation heap, Relation index, IndexInfo *indexInfo) */ reltuples = table_index_build_scan(heap, index, indexInfo, false, true, ginBuildCallback, &buildstate, NULL); + InvalidateCatalogSnapshot(); /* dump remaining entries to the index */ oldCtx = MemoryContextSwitchTo(buildstate.tmpCtx); @@ -769,6 +776,7 @@ ginbuild(Relation heap, Relation index, IndexInfo *indexInfo) list, nlist, &buildstate.buildStats); } MemoryContextSwitchTo(oldCtx); + Assert(!indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); } MemoryContextDelete(buildstate.funcCtx); @@ -941,6 +949,7 @@ _gin_begin_parallel(GinBuildState *buildstate, Relation heap, Relation index, WalUsage *walusage; BufferUsage *bufferusage; bool leaderparticipates = true; + bool need_pop_active_snapshot = true; int querylen; #ifdef DISABLE_LEADER_PARTICIPATION @@ -965,9 +974,16 @@ _gin_begin_parallel(GinBuildState *buildstate, Relation heap, Relation index, * live according to that. */ if (!isconcurrent) + { + Assert(ActiveSnapshotSet()); snapshot = SnapshotAny; + need_pop_active_snapshot = false; + } else + { snapshot = RegisterSnapshot(GetTransactionSnapshot()); + PushActiveSnapshot(GetTransactionSnapshot()); + } /* * Estimate size for our own PARALLEL_KEY_GIN_SHARED workspace. @@ -1010,6 +1026,8 @@ _gin_begin_parallel(GinBuildState *buildstate, Relation heap, Relation index, /* If no DSM segment was available, back out (do serial build) */ if (pcxt->seg == NULL) { + if (need_pop_active_snapshot) + PopActiveSnapshot(); if (IsMVCCSnapshot(snapshot)) UnregisterSnapshot(snapshot); DestroyParallelContext(pcxt); @@ -1084,6 +1102,8 @@ _gin_begin_parallel(GinBuildState *buildstate, Relation heap, Relation index, /* If no workers were successfully launched, back out (do serial build) */ if (pcxt->nworkers_launched == 0) { + if (need_pop_active_snapshot) + PopActiveSnapshot(); _gin_end_parallel(ginleader, NULL); return; } @@ -1100,6 +1120,8 @@ _gin_begin_parallel(GinBuildState *buildstate, Relation heap, Relation index, * sure that the failure-to-start case will not hang forever. */ WaitForParallelWorkersToAttach(pcxt); + if (need_pop_active_snapshot) + PopActiveSnapshot(); } /* diff --git a/src/backend/access/gist/gistbuild.c b/src/backend/access/gist/gistbuild.c index 7f57c787f4c..6207e9f5d81 100644 --- a/src/backend/access/gist/gistbuild.c +++ b/src/backend/access/gist/gistbuild.c @@ -43,6 +43,7 @@ #include "optimizer/optimizer.h" #include "storage/bufmgr.h" #include "storage/bulk_write.h" +#include "storage/proc.h" #include "utils/memutils.h" #include "utils/rel.h" @@ -259,6 +260,7 @@ gistbuild(Relation heap, Relation index, IndexInfo *indexInfo) buildstate.indtuples = 0; buildstate.indtuplesSize = 0; + Assert(!indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); if (buildstate.buildMode == GIST_SORTED_BUILD) { /* @@ -350,6 +352,8 @@ gistbuild(Relation heap, Relation index, IndexInfo *indexInfo) result->heap_tuples = reltuples; result->index_tuples = (double) buildstate.indtuples; + InvalidateCatalogSnapshot(); + Assert(!indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); return result; } diff --git a/src/backend/access/hash/hash.c b/src/backend/access/hash/hash.c index e88ddb32a05..1ee1da1ec9b 100644 --- a/src/backend/access/hash/hash.c +++ b/src/backend/access/hash/hash.c @@ -30,6 +30,7 @@ #include "nodes/execnodes.h" #include "optimizer/plancat.h" #include "pgstat.h" +#include "storage/proc.h" #include "utils/fmgrprotos.h" #include "utils/index_selfuncs.h" #include "utils/rel.h" @@ -198,6 +199,8 @@ hashbuild(Relation heap, Relation index, IndexInfo *indexInfo) result->heap_tuples = reltuples; result->index_tuples = buildstate.indtuples; + InvalidateCatalogSnapshot(); + Assert(!indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); return result; } diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index a231563f0df..62657d07f04 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -54,6 +54,7 @@ #include "utils/inval.h" #include "utils/spccache.h" #include "utils/syscache.h" +#include "utils/injection_point.h" static HeapTuple heap_prepare_insert(Relation relation, HeapTuple tup, @@ -697,6 +698,36 @@ heap_prepare_pagescan(TableScanDesc sscan) LockBuffer(buffer, BUFFER_LOCK_UNLOCK); } +/* + * Reset the active snapshot during a scan. + * This ensures the xmin horizon can advance while maintaining safe tuple visibility. + * Note: No other snapshot should be active during this operation. + */ +static inline void +heap_reset_scan_snapshot(TableScanDesc sscan) +{ + /* Make sure no other snapshot was set as active. */ + Assert(GetActiveSnapshot() == sscan->rs_snapshot); + /* And make sure active snapshot is not registered. */ + Assert(GetActiveSnapshot()->regd_count == 0); + PopActiveSnapshot(); + + sscan->rs_snapshot = InvalidSnapshot; /* just to be tidy */ + Assert(!HaveRegisteredOrActiveSnapshot()); + InvalidateCatalogSnapshot(); + + /* The goal of snapshot reset is to allow horizon to advance. */ + Assert(!TransactionIdIsValid(MyProc->xmin)); +#if USE_INJECTION_POINTS + /* In some cases it is still not possible due xid assign. */ + if (!TransactionIdIsValid(MyProc->xid)) + INJECTION_POINT("heap_reset_scan_snapshot_effective", NULL); +#endif + + PushActiveSnapshot(GetLatestSnapshot()); + sscan->rs_snapshot = GetActiveSnapshot(); +} + /* * heap_fetch_next_buffer - read and pin the next block from MAIN_FORKNUM. * @@ -738,7 +769,12 @@ heap_fetch_next_buffer(HeapScanDesc scan, ScanDirection dir) scan->rs_cbuf = read_stream_next_buffer(scan->rs_read_stream, NULL); if (BufferIsValid(scan->rs_cbuf)) + { scan->rs_cblock = BufferGetBlockNumber(scan->rs_cbuf); + if (scan->rs_base.rs_flags & SO_RESET_SNAPSHOT && + scan->rs_cblock % SO_RESET_SNAPSHOT_EACH_N_PAGE == 0) + heap_reset_scan_snapshot((TableScanDesc) scan); + } } /* @@ -1399,7 +1435,16 @@ heap_endscan(TableScanDesc sscan) if (scan->rs_parallelworkerdata != NULL) pfree(scan->rs_parallelworkerdata); - +#ifdef USE_ASSERT_CHECKING + if (scan->rs_base.rs_flags & SO_RESET_SNAPSHOT) + { + Assert(!(scan->rs_base.rs_flags & SO_TEMP_SNAPSHOT)); + /* Make sure no other snapshot was set as active. */ + Assert(GetActiveSnapshot() == sscan->rs_snapshot); + /* And make sure snapshot is not registered. */ + Assert(GetActiveSnapshot()->regd_count == 0); + } +#endif if (scan->rs_base.rs_flags & SO_TEMP_SNAPSHOT) UnregisterSnapshot(scan->rs_base.rs_snapshot); diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 3ff36f59bf8..16c460aa23f 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -1210,6 +1210,8 @@ heapam_index_build_range_scan(Relation heapRelation, ExprContext *econtext; Snapshot snapshot; bool need_unregister_snapshot = false; + bool need_pop_active_snapshot = false; + bool reset_snapshots = false; TransactionId OldestXmin; BlockNumber previous_blkno = InvalidBlockNumber; BlockNumber root_blkno = InvalidBlockNumber; @@ -1244,9 +1246,6 @@ heapam_index_build_range_scan(Relation heapRelation, /* Arrange for econtext's scan tuple to be the tuple under test */ econtext->ecxt_scantuple = slot; - /* Set up execution state for predicate, if any. */ - predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate); - /* * Prepare for scan of the base relation. In a normal index build, we use * SnapshotAny because we must retrieve all tuples and do our own time @@ -1256,6 +1255,15 @@ heapam_index_build_range_scan(Relation heapRelation, */ OldestXmin = InvalidTransactionId; + /* + * For unique index we need consistent snapshot for the whole scan. + * In case of parallel scan some additional infrastructure required + * to perform scan with SO_RESET_SNAPSHOT which is not yet ready. + */ + reset_snapshots = indexInfo->ii_Concurrent && + !indexInfo->ii_Unique && + !is_system_catalog; /* just for the case */ + /* okay to ignore lazy VACUUMs here */ if (!IsBootstrapProcessingMode() && !indexInfo->ii_Concurrent) OldestXmin = GetOldestNonRemovableTransactionId(heapRelation); @@ -1264,24 +1272,41 @@ heapam_index_build_range_scan(Relation heapRelation, { /* * Serial index build. - * - * Must begin our own heap scan in this case. We may also need to - * register a snapshot whose lifetime is under our direct control. */ if (!TransactionIdIsValid(OldestXmin)) { - snapshot = RegisterSnapshot(GetTransactionSnapshot()); - need_unregister_snapshot = true; + snapshot = GetTransactionSnapshot(); + /* + * Must begin our own heap scan in this case. We may also need to + * register a snapshot whose lifetime is under our direct control. + * In case of resetting of snapshot during the scan registration is + * not allowed because snapshot is going to be changed every so + * often. + */ + if (!reset_snapshots) + { + snapshot = RegisterSnapshot(snapshot); + need_unregister_snapshot = true; + } + Assert(!ActiveSnapshotSet()); + PushActiveSnapshot(snapshot); + /* store link to snapshot because it may be copied */ + snapshot = GetActiveSnapshot(); + need_pop_active_snapshot = true; } else + { + Assert(!indexInfo->ii_Concurrent); snapshot = SnapshotAny; + } scan = table_beginscan_strat(heapRelation, /* relation */ snapshot, /* snapshot */ 0, /* number of keys */ NULL, /* scan key */ true, /* buffer access strategy OK */ - allow_sync); /* syncscan OK? */ + allow_sync, /* syncscan OK? */ + reset_snapshots /* reset snapshots? */); } else { @@ -1295,6 +1320,8 @@ heapam_index_build_range_scan(Relation heapRelation, Assert(!IsBootstrapProcessingMode()); Assert(allow_sync); snapshot = scan->rs_snapshot; + PushActiveSnapshot(snapshot); + need_pop_active_snapshot = true; } hscan = (HeapScanDesc) scan; @@ -1309,6 +1336,13 @@ heapam_index_build_range_scan(Relation heapRelation, Assert(snapshot == SnapshotAny ? TransactionIdIsValid(OldestXmin) : !TransactionIdIsValid(OldestXmin)); Assert(snapshot == SnapshotAny || !anyvisible); + Assert(snapshot == SnapshotAny || ActiveSnapshotSet()); + + /* Set up execution state for predicate, if any. */ + predicate = ExecPrepareQual(indexInfo->ii_Predicate, estate); + /* Clear reference to snapshot since it may be changed by the scan itself. */ + if (reset_snapshots) + snapshot = InvalidSnapshot; /* Publish number of blocks to scan */ if (progress) @@ -1744,6 +1778,8 @@ heapam_index_build_range_scan(Relation heapRelation, table_endscan(scan); + if (need_pop_active_snapshot) + PopActiveSnapshot(); /* we can now forget our snapshot, if set and registered by us */ if (need_unregister_snapshot) UnregisterSnapshot(snapshot); @@ -1816,7 +1852,8 @@ heapam_index_validate_scan(Relation heapRelation, 0, /* number of keys */ NULL, /* scan key */ true, /* buffer access strategy OK */ - false); /* syncscan not OK */ + false, /* syncscan not OK */ + false); hscan = (HeapScanDesc) scan; pgstat_progress_update_param(PROGRESS_SCAN_BLOCKS_TOTAL, diff --git a/src/backend/access/index/genam.c b/src/backend/access/index/genam.c index 5e89b86a62c..e4284181738 100644 --- a/src/backend/access/index/genam.c +++ b/src/backend/access/index/genam.c @@ -472,7 +472,7 @@ systable_beginscan(Relation heapRelation, */ sysscan->scan = table_beginscan_strat(heapRelation, snapshot, nkeys, key, - true, false); + true, false, false); sysscan->iscan = NULL; } diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c index 69ef1527e06..6fc72bd2c94 100644 --- a/src/backend/access/nbtree/nbtsort.c +++ b/src/backend/access/nbtree/nbtsort.c @@ -261,7 +261,7 @@ static double _bt_spools_heapscan(Relation heap, Relation index, static void _bt_spooldestroy(BTSpool *btspool); static void _bt_spool(BTSpool *btspool, const ItemPointerData *self, const Datum *values, const bool *isnull); -static void _bt_leafbuild(BTSpool *btspool, BTSpool *btspool2); +static void _bt_leafbuild(BTSpool *btspool, BTSpool *btspool2, bool reset_snapshots); static void _bt_build_callback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state); static BulkWriteBuffer _bt_blnewpage(BTWriteState *wstate, uint32 level); @@ -324,18 +324,22 @@ btbuild(Relation heap, Relation index, IndexInfo *indexInfo) RelationGetRelationName(index)); reltuples = _bt_spools_heapscan(heap, index, &buildstate, indexInfo); + Assert(indexInfo->ii_ParallelWorkers || indexInfo->ii_Unique || + !indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); /* * Finish the build by (1) completing the sort of the spool file, (2) * inserting the sorted tuples into btree pages and (3) building the upper * levels. Finally, it may also be necessary to end use of parallelism. */ - _bt_leafbuild(buildstate.spool, buildstate.spool2); + _bt_leafbuild(buildstate.spool, buildstate.spool2, !indexInfo->ii_ParallelWorkers && indexInfo->ii_Concurrent); _bt_spooldestroy(buildstate.spool); if (buildstate.spool2) _bt_spooldestroy(buildstate.spool2); if (buildstate.btleader) _bt_end_parallel(buildstate.btleader); + Assert(indexInfo->ii_ParallelWorkers || indexInfo->ii_Unique || + !indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); result = palloc_object(IndexBuildResult); @@ -483,6 +487,9 @@ _bt_spools_heapscan(Relation heap, Relation index, BTBuildState *buildstate, else reltuples = _bt_parallel_heapscan(buildstate, &indexInfo->ii_BrokenHotChain); + InvalidateCatalogSnapshot(); + Assert(indexInfo->ii_ParallelWorkers || indexInfo->ii_Unique || + !indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); /* * Set the progress target for the next phase. Reset the block number @@ -538,7 +545,7 @@ _bt_spool(BTSpool *btspool, const ItemPointerData *self, const Datum *values, co * create an entire btree. */ static void -_bt_leafbuild(BTSpool *btspool, BTSpool *btspool2) +_bt_leafbuild(BTSpool *btspool, BTSpool *btspool2, bool reset_snapshots) { BTWriteState wstate; @@ -560,18 +567,21 @@ _bt_leafbuild(BTSpool *btspool, BTSpool *btspool2) PROGRESS_BTREE_PHASE_PERFORMSORT_2); tuplesort_performsort(btspool2->sortstate); } + Assert(!reset_snapshots || !TransactionIdIsValid(MyProc->xmin)); wstate.heap = btspool->heap; wstate.index = btspool->index; wstate.inskey = _bt_mkscankey(wstate.index, NULL); /* _bt_mkscankey() won't set allequalimage without metapage */ wstate.inskey->allequalimage = _bt_allequalimage(wstate.index, true); + InvalidateCatalogSnapshot(); /* reserve the metapage */ wstate.btws_pages_alloced = BTREE_METAPAGE + 1; pgstat_progress_update_param(PROGRESS_CREATEIDX_SUBPHASE, PROGRESS_BTREE_PHASE_LEAF_LOAD); + Assert(!reset_snapshots || !TransactionIdIsValid(MyProc->xmin)); _bt_load(&wstate, btspool, btspool2); } @@ -1410,6 +1420,7 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) WalUsage *walusage; BufferUsage *bufferusage; bool leaderparticipates = true; + bool need_pop_active_snapshot = true; int querylen; #ifdef DISABLE_LEADER_PARTICIPATION @@ -1435,9 +1446,16 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) * live according to that. */ if (!isconcurrent) + { + Assert(ActiveSnapshotSet()); snapshot = SnapshotAny; + need_pop_active_snapshot = false; + } else + { snapshot = RegisterSnapshot(GetTransactionSnapshot()); + PushActiveSnapshot(snapshot); + } /* * Estimate size for our own PARALLEL_KEY_BTREE_SHARED workspace, and @@ -1491,6 +1509,8 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) /* If no DSM segment was available, back out (do serial build) */ if (pcxt->seg == NULL) { + if (need_pop_active_snapshot) + PopActiveSnapshot(); if (IsMVCCSnapshot(snapshot)) UnregisterSnapshot(snapshot); DestroyParallelContext(pcxt); @@ -1585,6 +1605,8 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) /* If no workers were successfully launched, back out (do serial build) */ if (pcxt->nworkers_launched == 0) { + if (need_pop_active_snapshot) + PopActiveSnapshot(); _bt_end_parallel(btleader); return; } @@ -1601,6 +1623,8 @@ _bt_begin_parallel(BTBuildState *buildstate, bool isconcurrent, int request) * sure that the failure-to-start case will not hang forever. */ WaitForParallelWorkersToAttach(pcxt); + if (need_pop_active_snapshot) + PopActiveSnapshot(); } /* diff --git a/src/backend/access/spgist/spginsert.c b/src/backend/access/spgist/spginsert.c index 780ef646a54..49832a36470 100644 --- a/src/backend/access/spgist/spginsert.c +++ b/src/backend/access/spgist/spginsert.c @@ -24,6 +24,7 @@ #include "nodes/execnodes.h" #include "storage/bufmgr.h" #include "storage/bulk_write.h" +#include "storage/proc.h" #include "utils/memutils.h" #include "utils/rel.h" @@ -143,6 +144,8 @@ spgbuild(Relation heap, Relation index, IndexInfo *indexInfo) result = palloc0_object(IndexBuildResult); result->heap_tuples = reltuples; result->index_tuples = buildstate.indtuples; + InvalidateCatalogSnapshot(); + Assert(!indexInfo->ii_Concurrent || !TransactionIdIsValid(MyProc->xmin)); return result; } diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index aafc53e0164..7772d1379bc 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -3708,6 +3708,17 @@ PreventInTransactionBlock(bool isTopLevel, const char *stmtType) MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT; } +void +PreventIsolationUsesXactSnapshot(const char *stmtType) +{ + if (IsolationUsesXactSnapshot()) + ereport(ERROR, + (errcode(ERRCODE_INAPPROPRIATE_ISOLATION_LEVEL_FOR_COMMAND), + /* translator: %s represents an SQL statement name */ + errmsg("%s does not support transaction isolation higher than READ COMMITTED", + stmtType))); +} + /* * WarnNoTransactionBlock * RequireTransactionBlock diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 43de42ce39e..2ace60c1f07 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -80,6 +80,7 @@ #include "utils/snapmgr.h" #include "utils/syscache.h" #include "utils/tuplesort.h" +#include "storage/proc.h" /* Potentially set by pg_upgrade_support functions */ Oid binary_upgrade_next_index_pg_class_oid = InvalidOid; @@ -1490,8 +1491,8 @@ index_concurrently_build(Oid heapRelationId, Relation indexRelation; IndexInfo *indexInfo; - /* This had better make sure that a snapshot is active */ - Assert(ActiveSnapshotSet()); + Assert(!TransactionIdIsValid(MyProc->xmin)); + Assert(!TransactionIdIsValid(MyProc->xid)); /* Open and lock the parent heap relation */ heapRel = table_open(heapRelationId, ShareUpdateExclusiveLock); @@ -1509,19 +1510,29 @@ index_concurrently_build(Oid heapRelationId, indexRelation = index_open(indexRelationId, RowExclusiveLock); + /* BuildIndexInfo may require as snapshot for expressions and predicates */ + PushActiveSnapshot(GetTransactionSnapshot()); /* * We have to re-build the IndexInfo struct, since it was lost in the * commit of the transaction where this concurrent index was created at * the catalog level. */ indexInfo = BuildIndexInfo(indexRelation); + /* Done with snapshot */ + PopActiveSnapshot(); Assert(!indexInfo->ii_ReadyForInserts); indexInfo->ii_Concurrent = true; indexInfo->ii_BrokenHotChain = false; + InvalidateCatalogSnapshot(); + Assert(!TransactionIdIsValid(MyProc->xmin)); + /* Now build the index */ index_build(heapRel, indexRelation, indexInfo, false, true); + InvalidateCatalogSnapshot(); + Assert((indexInfo->ii_ParallelWorkers || indexInfo->ii_Unique) || !TransactionIdIsValid(MyProc->xmin)); + /* Roll back any GUC changes executed by index functions */ AtEOXact_GUC(false, save_nestlevel); @@ -1532,12 +1543,19 @@ index_concurrently_build(Oid heapRelationId, table_close(heapRel, NoLock); index_close(indexRelation, NoLock); + /* + * Updating pg_index might involve TOAST table access, so ensure we + * have a valid snapshot. + */ + PushActiveSnapshot(GetTransactionSnapshot()); /* * Update the pg_index row to mark the index as ready for inserts. Once we * commit this transaction, any new transactions that open the table must * insert new entries into the index for insertions and non-HOT updates. */ index_set_state_flags(indexRelationId, INDEX_CREATE_SET_READY); + /* we can do away with our snapshot */ + PopActiveSnapshot(); } /* @@ -3234,7 +3252,8 @@ IndexCheckExclusion(Relation heapRelation, 0, /* number of keys */ NULL, /* scan key */ true, /* buffer access strategy OK */ - true); /* syncscan OK */ + true, /* syncscan OK */ + false); while (table_scan_getnextslot(scan, ForwardScanDirection, slot)) { @@ -3297,12 +3316,16 @@ IndexCheckExclusion(Relation heapRelation, * as of the start of the scan (see table_index_build_scan), whereas a normal * build takes care to include recently-dead tuples. This is OK because * we won't mark the index valid until all transactions that might be able - * to see those tuples are gone. The reason for doing that is to avoid + * to see those tuples are gone. One of reasons for doing that is to avoid * bogus unique-index failures due to concurrent UPDATEs (we might see * different versions of the same row as being valid when we pass over them, * if we used HeapTupleSatisfiesVacuum). This leaves us with an index that * does not contain any tuples added to the table while we built the index. * + * Furthermore, in case of non-unique index we set SO_RESET_SNAPSHOT for the + * scan, which causes new snapshot to be set as active every so often. The reason + * for that is to propagate the xmin horizon forward. + * * Next, we mark the index "indisready" (but still not "indisvalid") and * commit the second transaction and start a third. Again we wait for all * transactions that could have been modifying the table to terminate. Now diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 635679cc1f2..67e207220d1 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -1701,23 +1701,17 @@ DefineIndex(ParseState *pstate, * chains can be created where the new tuple and the old tuple in the * chain have different index keys. * - * We now take a new snapshot, and build the index using all tuples that - * are visible in this snapshot. We can be sure that any HOT updates to + * We build the index using all tuples that are visible using single or + * multiple refreshing snapshots. We can be sure that any HOT updates to * these tuples will be compatible with the index, since any updates made * by transactions that didn't know about the index are now committed or * rolled back. Thus, each visible tuple is either the end of its * HOT-chain or the extension of the chain is HOT-safe for this index. */ - /* Set ActiveSnapshot since functions in the indexes may need it */ - PushActiveSnapshot(GetTransactionSnapshot()); - /* Perform concurrent build of index */ index_concurrently_build(tableId, indexRelationId); - /* we can do away with our snapshot */ - PopActiveSnapshot(); - /* * Commit this transaction to make the indisready update visible. */ @@ -2874,8 +2868,11 @@ ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel) } if (concurrently) + { PreventInTransactionBlock(isTopLevel, "REINDEX CONCURRENTLY"); + PreventIsolationUsesXactSnapshot("REINDEX CONCURRENTLY"); + } params.options = (verbose ? REINDEXOPT_VERBOSE : 0) | @@ -4131,9 +4128,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein if (newidx->safe) set_indexsafe_procflags(); - /* Set ActiveSnapshot since functions in the indexes may need it */ - PushActiveSnapshot(GetTransactionSnapshot()); - /* * Update progress for the index to build, with the correct parent * table involved. @@ -4148,7 +4142,6 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein /* Perform concurrent build of new index */ index_concurrently_build(newidx->tableId, newidx->indexId); - PopActiveSnapshot(); CommitTransactionCommand(); } diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 42604a0f75c..d6bb3546bea 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -63,6 +63,7 @@ #include "utils/lsyscache.h" #include "utils/rel.h" #include "utils/selfuncs.h" +#include "utils/snapmgr.h" /* GUC parameters */ double cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION; @@ -7027,6 +7028,7 @@ plan_create_index_workers(Oid tableOid, Oid indexOid) Relation heap; Relation index; RelOptInfo *rel; + bool need_pop_active_snapshot = false; int parallel_workers; BlockNumber heap_blocks; double reltuples; @@ -7082,6 +7084,12 @@ plan_create_index_workers(Oid tableOid, Oid indexOid) heap = table_open(tableOid, NoLock); index = index_open(indexOid, NoLock); + /* Set ActiveSnapshot since functions in the indexes may need it */ + if (!ActiveSnapshotSet()) + { + PushActiveSnapshot(GetTransactionSnapshot()); + need_pop_active_snapshot = true; + } /* * Determine if it's safe to proceed. * @@ -7139,6 +7147,8 @@ plan_create_index_workers(Oid tableOid, Oid indexOid) parallel_workers--; done: + if (need_pop_active_snapshot) + PopActiveSnapshot(); index_close(index, NoLock); table_close(heap, NoLock); diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index bf707f2d57f..a0895f2c46b 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1460,8 +1460,11 @@ ProcessUtilitySlow(ParseState *pstate, bool is_alter_table; if (stmt->concurrent) + { PreventInTransactionBlock(isTopLevel, "CREATE INDEX CONCURRENTLY"); + PreventIsolationUsesXactSnapshot("CREATE INDEX CONCURRENTLY"); + } /* * Look up the relation OID just once, right here at the diff --git a/src/backend/utils/errcodes.txt b/src/backend/utils/errcodes.txt index 5b25402ebbe..a80f08af241 100644 --- a/src/backend/utils/errcodes.txt +++ b/src/backend/utils/errcodes.txt @@ -259,6 +259,7 @@ Section: Class 25 - Invalid Transaction State 25P02 E ERRCODE_IN_FAILED_SQL_TRANSACTION in_failed_sql_transaction 25P03 E ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT idle_in_transaction_session_timeout 25P04 E ERRCODE_TRANSACTION_TIMEOUT transaction_timeout +25P05 E ERRCODE_INAPPROPRIATE_ISOLATION_LEVEL_FOR_COMMAND inappropriate_isolation_level_for_command Section: Class 26 - Invalid SQL Statement Name diff --git a/src/bin/pg_amcheck/t/006_cic.pl b/src/bin/pg_amcheck/t/006_cic.pl index 70b185212e7..4ae7b796c6e 100644 --- a/src/bin/pg_amcheck/t/006_cic.pl +++ b/src/bin/pg_amcheck/t/006_cic.pl @@ -152,7 +152,7 @@ $node->pgbench( 0, [qr{actually processed}], [qr{^$}], - 'concurrent operations with REINDEX/CREATE INDEX CONCURRENTLY for GIN/GIST/BRIN/HASH/SPGIST', + 'concurrent operations with REINDEX/CREATE INDEX CONCURRENTLY for GIN', { 'concurrent_ops_gin_idx' => q( SELECT pg_try_advisory_lock(42)::integer AS gotlock \gset diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h index 24a27cc043a..9499d5f05a9 100644 --- a/src/include/access/heapam.h +++ b/src/include/access/heapam.h @@ -43,6 +43,8 @@ #define HEAP_PAGE_PRUNE_MARK_UNUSED_NOW (1 << 0) #define HEAP_PAGE_PRUNE_FREEZE (1 << 1) +#define SO_RESET_SNAPSHOT_EACH_N_PAGE 4096 + typedef struct BulkInsertStateData *BulkInsertState; typedef struct GlobalVisState GlobalVisState; typedef struct TupleTableSlot TupleTableSlot; diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h index 06084752245..d0709782dde 100644 --- a/src/include/access/tableam.h +++ b/src/include/access/tableam.h @@ -25,6 +25,7 @@ #include "storage/read_stream.h" #include "utils/rel.h" #include "utils/snapshot.h" +#include "utils/injection_point.h" #define DEFAULT_TABLE_ACCESS_METHOD "heap" @@ -63,6 +64,17 @@ typedef enum ScanOptions /* unregister snapshot at scan end? */ SO_TEMP_SNAPSHOT = 1 << 9, + /* + * Reset scan and catalog snapshot every so often? If so, each + * SO_RESET_SNAPSHOT_EACH_N_PAGE pages active snapshot is popped, + * catalog snapshot invalidated, latest snapshot pushed as active. + * + * At the end of the scan snapshot is not popped. + * Goal of such mode is keep xmin propagating horizon forward. + * + * see heap_reset_scan_snapshot for details. + */ + SO_RESET_SNAPSHOT = 1 << 10, } ScanOptions; /* @@ -919,7 +931,8 @@ extern TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, static inline TableScanDesc table_beginscan_strat(Relation rel, Snapshot snapshot, int nkeys, ScanKeyData *key, - bool allow_strat, bool allow_sync) + bool allow_strat, bool allow_sync, + bool reset_snapshot) { uint32 flags = SO_TYPE_SEQSCAN | SO_ALLOW_PAGEMODE; @@ -927,6 +940,15 @@ table_beginscan_strat(Relation rel, Snapshot snapshot, flags |= SO_ALLOW_STRAT; if (allow_sync) flags |= SO_ALLOW_SYNC; + if (reset_snapshot) + { + INJECTION_POINT("table_beginscan_strat_reset_snapshots", NULL); + /* Active snapshot is required on start. */ + Assert(GetActiveSnapshot() == snapshot); + /* Active snapshot should not be registered to keep xmin propagating. */ + Assert(GetActiveSnapshot()->regd_count == 0); + flags |= (SO_RESET_SNAPSHOT); + } return table_beginscan_common(rel, snapshot, nkeys, key, NULL, flags); } @@ -1760,6 +1782,10 @@ table_scan_analyze_next_tuple(TableScanDesc scan, * very hard to detect whether they're really incompatible with the chain tip. * This only really makes sense for heap AM, it might need to be generalized * for other AMs later. + * + * In case of non-unique index and non-parallel concurrent build, + * SO_RESET_SNAPSHOT is applied for the scan. That leads to changing snapshots + * on the fly to allow xmin horizon propagate. */ static inline double table_index_build_scan(Relation table_rel, diff --git a/src/include/access/xact.h b/src/include/access/xact.h index f0b4d795071..2e3f617a949 100644 --- a/src/include/access/xact.h +++ b/src/include/access/xact.h @@ -488,6 +488,7 @@ extern bool IsTransactionOrTransactionBlock(void); extern char TransactionBlockStatusCode(void); extern void AbortOutOfAnyTransaction(void); extern void PreventInTransactionBlock(bool isTopLevel, const char *stmtType); +extern void PreventIsolationUsesXactSnapshot(const char *stmtType); extern void RequireTransactionBlock(bool isTopLevel, const char *stmtType); extern void WarnNoTransactionBlock(bool isTopLevel, const char *stmtType); extern bool IsInTransactionBlock(bool isTopLevel); diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile index a41d781f8c9..eeaeaaf2cc6 100644 --- a/src/test/modules/injection_points/Makefile +++ b/src/test/modules/injection_points/Makefile @@ -9,7 +9,7 @@ EXTENSION = injection_points DATA = injection_points--1.0.sql PGFILEDESC = "injection_points - facility for injection points" -REGRESS = injection_points hashagg reindex_conc vacuum +REGRESS = injection_points hashagg reindex_conc vacuum cic_reset_snapshots REGRESS_OPTS = --dlpath=$(top_builddir)/src/test/regress ISOLATION = basic \ diff --git a/src/test/modules/injection_points/expected/cic_reset_snapshots.out b/src/test/modules/injection_points/expected/cic_reset_snapshots.out new file mode 100644 index 00000000000..b4ad90eb339 --- /dev/null +++ b/src/test/modules/injection_points/expected/cic_reset_snapshots.out @@ -0,0 +1,115 @@ +CREATE EXTENSION injection_points; +SELECT injection_points_set_local(); + injection_points_set_local +---------------------------- + +(1 row) + +SELECT injection_points_attach('heap_reset_scan_snapshot_effective', 'notice'); + injection_points_attach +------------------------- + +(1 row) + +SELECT injection_points_attach('table_beginscan_strat_reset_snapshots', 'notice'); + injection_points_attach +------------------------- + +(1 row) + +CREATE SCHEMA cic_reset_snap; +CREATE TABLE cic_reset_snap.tbl(i int primary key, j int); +INSERT INTO cic_reset_snap.tbl SELECT i, i * I FROM generate_series(1, 200) s(i); +CREATE FUNCTION cic_reset_snap.predicate_stable(integer) RETURNS bool IMMUTABLE + LANGUAGE plpgsql AS $$ +BEGIN + EXECUTE 'SELECT txid_current()'; + RETURN MOD($1, 2) = 0; +END; $$; +CREATE FUNCTION cic_reset_snap.predicate_stable_no_param() RETURNS bool IMMUTABLE + LANGUAGE plpgsql AS $$ +BEGIN + EXECUTE 'SELECT txid_current()'; + RETURN false; +END; $$; +---------------- +ALTER TABLE cic_reset_snap.tbl SET (parallel_workers=0); +CREATE UNIQUE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(MOD(i, 2), j) WHERE MOD(i, 2) = 0; +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i, j) WHERE cic_reset_snap.predicate_stable(i); +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i, j) WHERE cic_reset_snap.predicate_stable_no_param(); +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl USING BRIN(i); +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +-- The same in parallel mode +ALTER TABLE cic_reset_snap.tbl SET (parallel_workers=2); +CREATE UNIQUE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(MOD(i, 2), j) WHERE MOD(i, 2) = 0; +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i, j) WHERE cic_reset_snap.predicate_stable(i); +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +NOTICE: notice triggered for injection point table_beginscan_strat_reset_snapshots +NOTICE: notice triggered for injection point heap_reset_scan_snapshot_effective +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i, j) WHERE cic_reset_snap.predicate_stable_no_param(); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i DESC NULLS LAST); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl USING BRIN(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; +BEGIN TRANSACTION; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block +ROLLBACK ; +SET default_transaction_isolation = serializable; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +ERROR: CREATE INDEX CONCURRENTLY does not support transaction isolation higher than READ COMMITTED +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +ERROR: REINDEX CONCURRENTLY does not support transaction isolation higher than READ COMMITTED +RESET default_transaction_isolation; +DROP SCHEMA cic_reset_snap CASCADE; +NOTICE: drop cascades to 3 other objects +DETAIL: drop cascades to table cic_reset_snap.tbl +drop cascades to function cic_reset_snap.predicate_stable(integer) +drop cascades to function cic_reset_snap.predicate_stable_no_param() +DROP EXTENSION injection_points; diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build index fcc85414515..5d298cd2710 100644 --- a/src/test/modules/injection_points/meson.build +++ b/src/test/modules/injection_points/meson.build @@ -36,6 +36,7 @@ tests += { 'hashagg', 'reindex_conc', 'vacuum', + 'cic_reset_snapshots', ], 'regress_args': ['--dlpath', meson.project_build_root() / 'src/test/regress'], # The injection points are cluster-wide, so disable installcheck diff --git a/src/test/modules/injection_points/sql/cic_reset_snapshots.sql b/src/test/modules/injection_points/sql/cic_reset_snapshots.sql new file mode 100644 index 00000000000..7f7dffa5be4 --- /dev/null +++ b/src/test/modules/injection_points/sql/cic_reset_snapshots.sql @@ -0,0 +1,95 @@ +CREATE EXTENSION injection_points; + +SELECT injection_points_set_local(); +SELECT injection_points_attach('heap_reset_scan_snapshot_effective', 'notice'); +SELECT injection_points_attach('table_beginscan_strat_reset_snapshots', 'notice'); + + +CREATE SCHEMA cic_reset_snap; +CREATE TABLE cic_reset_snap.tbl(i int primary key, j int); +INSERT INTO cic_reset_snap.tbl SELECT i, i * I FROM generate_series(1, 200) s(i); + +CREATE FUNCTION cic_reset_snap.predicate_stable(integer) RETURNS bool IMMUTABLE + LANGUAGE plpgsql AS $$ +BEGIN + EXECUTE 'SELECT txid_current()'; + RETURN MOD($1, 2) = 0; +END; $$; + +CREATE FUNCTION cic_reset_snap.predicate_stable_no_param() RETURNS bool IMMUTABLE + LANGUAGE plpgsql AS $$ +BEGIN + EXECUTE 'SELECT txid_current()'; + RETURN false; +END; $$; + +---------------- +ALTER TABLE cic_reset_snap.tbl SET (parallel_workers=0); + +CREATE UNIQUE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(MOD(i, 2), j) WHERE MOD(i, 2) = 0; +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i, j) WHERE cic_reset_snap.predicate_stable(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i, j) WHERE cic_reset_snap.predicate_stable_no_param(); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl USING BRIN(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +-- The same in parallel mode +ALTER TABLE cic_reset_snap.tbl SET (parallel_workers=2); + +CREATE UNIQUE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(MOD(i, 2), j) WHERE MOD(i, 2) = 0; +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i, j) WHERE cic_reset_snap.predicate_stable(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i, j) WHERE cic_reset_snap.predicate_stable_no_param(); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i DESC NULLS LAST); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl USING BRIN(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +DROP INDEX CONCURRENTLY cic_reset_snap.idx; + +BEGIN TRANSACTION; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +ROLLBACK ; + +SET default_transaction_isolation = serializable; +CREATE INDEX CONCURRENTLY idx ON cic_reset_snap.tbl(i); +REINDEX INDEX CONCURRENTLY cic_reset_snap.idx; +RESET default_transaction_isolation; + +DROP SCHEMA cic_reset_snap CASCADE; + +DROP EXTENSION injection_points; -- 2.43.0