On Fri, Sep 20, 2024 at 07:00:00AM +0300, Alexander Lakhin wrote:
> I've found another two paths to reach that condition:
> CREATE INDEX CONCURRENTLY ON def (vec_quantizer(id, :'b'));
> ERROR:  cannot fetch toast data without an active snapshot
> 
> REINDEX INDEX CONCURRENTLY def_vec_quantizer_idx;
> (or REINDEX TABLE CONCURRENTLY def;)
> TRAP: failed Assert("HaveRegisteredOrActiveSnapshot()"), File: 
> "toast_internals.c", Line: 668, PID: 2934502

Here's a (probably naive) attempt at fixing these, too.  I'll give each
path a closer look once it feels like we've identified all the bugs.

> Perhaps it would make sense to check all CatalogTupleUpdate(pg_index, ...)
> calls (I've found 10 such instances, but haven't checked them yet).

Indeed.

-- 
nathan
>From b7432c42c0cea9c1aadba7c72f9ce2ba6e6407d2 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <nat...@postgresql.org>
Date: Fri, 20 Sep 2024 11:48:52 -0500
Subject: [PATCH v2 1/1] fix failed assertions due to pg_index's TOAST table

---
 src/backend/catalog/index.c      | 5 +++++
 src/backend/commands/indexcmds.c | 9 +++++++++
 2 files changed, 14 insertions(+)

diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index b2b3ecb524..2e378ef4ef 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -2255,6 +2255,7 @@ index_drop(Oid indexId, bool concurrent, bool 
concurrent_lock_mode)
                PopActiveSnapshot();
                CommitTransactionCommand();
                StartTransactionCommand();
+               PushActiveSnapshot(GetTransactionSnapshot());
 
                /*
                 * Now we must wait until no running transaction could be using 
the
@@ -2283,8 +2284,10 @@ index_drop(Oid indexId, bool concurrent, bool 
concurrent_lock_mode)
                 * Again, commit the transaction to make the pg_index update 
visible
                 * to other sessions.
                 */
+               PopActiveSnapshot();
                CommitTransactionCommand();
                StartTransactionCommand();
+               PushActiveSnapshot(GetTransactionSnapshot());
 
                /*
                 * Wait till every transaction that saw the old index state has
@@ -2387,6 +2390,8 @@ index_drop(Oid indexId, bool concurrent, bool 
concurrent_lock_mode)
        {
                UnlockRelationIdForSession(&heaprelid, 
ShareUpdateExclusiveLock);
                UnlockRelationIdForSession(&indexrelid, 
ShareUpdateExclusiveLock);
+
+               PopActiveSnapshot();
        }
 }
 
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f99c2d2dee..36318c81ea 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -1798,11 +1798,15 @@ DefineIndex(Oid tableId,
                                                                 
PROGRESS_CREATEIDX_PHASE_WAIT_3);
        WaitForOlderSnapshots(limitXmin, true);
 
+       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
@@ -4236,6 +4240,8 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid 
relationOid, const Rein
         */
        set_indexsafe_procflags();
 
+       PushActiveSnapshot(GetTransactionSnapshot());
+
        forboth(lc, indexIds, lc2, newIndexIds)
        {
                ReindexIndexInfo *oldidx = lfirst(lc);
@@ -4280,8 +4286,10 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid 
relationOid, const Rein
        }
 
        /* Commit this transaction and make index swaps visible */
+       PopActiveSnapshot();
        CommitTransactionCommand();
        StartTransactionCommand();
+       PushActiveSnapshot(GetTransactionSnapshot());
 
        /*
         * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's 
no
@@ -4316,6 +4324,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid 
relationOid, const Rein
        }
 
        /* Commit this transaction to make the updates visible. */
+       PopActiveSnapshot();
        CommitTransactionCommand();
        StartTransactionCommand();
 
-- 
2.39.5 (Apple Git-154)

Reply via email to