aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/indexcmds.c
diff options
context:
space:
mode:
authorNathan Bossart <nathan@postgresql.org>2024-09-26 15:51:23 -0500
committerNathan Bossart <nathan@postgresql.org>2024-09-26 15:51:23 -0500
commitb52adbad46740524cbfbffaeb202b01a2c16202a (patch)
tree329f4423c29e64f9fa9db9a1c97daac543d54258 /src/backend/commands/indexcmds.c
parent9726653185de62f2f8bf42a25e961bc56895a41b (diff)
downloadpostgresql-b52adbad46740524cbfbffaeb202b01a2c16202a.tar.gz
postgresql-b52adbad46740524cbfbffaeb202b01a2c16202a.zip
Ensure we have a snapshot when updating pg_index entries.
Creating, reindexing, and dropping an index concurrently could entail accessing pg_index's TOAST table, which was recently added in commit b52c4fc3c0. These code paths start and commit their own transactions, but they do not always set an active snapshot. This rightfully leads to assertion failures and ERRORs when trying to access pg_index's TOAST table, such as the following: ERROR: cannot fetch toast data without an active snapshot To fix, push an active snapshot just before each section of code that might require accessing pg_index's TOAST table, and pop it shortly afterwards. Reported-by: Alexander Lakhin Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/a97d7401-e7c9-f771-6a00-037379f0a8bb%40gmail.com
Diffstat (limited to 'src/backend/commands/indexcmds.c')
-rw-r--r--src/backend/commands/indexcmds.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index ec5f253b28f..130cebd6588 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -1799,10 +1799,18 @@ DefineIndex(Oid tableId,
WaitForOlderSnapshots(limitXmin, true);
/*
+ * Updating pg_index might involve TOAST table access, so ensure we have a
+ * valid snapshot.
+ */
+ PushActiveSnapshot(GetTransactionSnapshot());
+
+ /*
* Index can now be marked valid -- update its pg_index entry
*/
index_set_state_flags(indexRelationId, INDEX_CREATE_SET_VALID);
+ PopActiveSnapshot();
+
/*
* The pg_index update will cause backends (including this one) to update
* relcache entries for the index itself, but we should also send a
@@ -4257,11 +4265,19 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
false);
/*
+ * Updating pg_index might involve TOAST table access, so ensure we
+ * have a valid snapshot.
+ */
+ PushActiveSnapshot(GetTransactionSnapshot());
+
+ /*
* Swap old index with the new one. This also marks the new one as
* valid and the old one as not valid.
*/
index_concurrently_swap(newidx->indexId, oldidx->indexId, oldName);
+ PopActiveSnapshot();
+
/*
* Invalidate the relcache for the table, so that after this commit
* all sessions will refresh any cached plans that might reference the
@@ -4312,7 +4328,15 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
*/
CHECK_FOR_INTERRUPTS();
+ /*
+ * Updating pg_index might involve TOAST table access, so ensure we
+ * have a valid snapshot.
+ */
+ PushActiveSnapshot(GetTransactionSnapshot());
+
index_concurrently_set_dead(oldidx->tableId, oldidx->indexId);
+
+ PopActiveSnapshot();
}
/* Commit this transaction to make the updates visible. */