postgres.git / summary / log / commit / refs
commit 116bd1a912090ef9cd56b87b79eb1bf8ab48a1dd
Author: Michael Paquier <michael@paquier.xyz>
Date: Fri Nov 20 04:45:58 2026 +0000
Pin relations in phase 1 of REINDEX CONCURRENTLY
Phase 1 of ReindexRelationConcurrently() evaluates index expressions and
predicates when computing the safety of each index and when creating the
definition of each new index. These may call functions run as the table
owner.
The old and new indexes processed in the previous iterations of the loop
were closed at this stage. Hence, a function was able to drop one of
them or reindex one of the new indexes, leading to an assertion failure
in the build phase, or to an internal error with a dropped index
committed.
The relations opened in phase 1 are now kept opened until the session
locks are taken, so as DDLs on them are blocked by CheckTableNotInUse().
The build and validation phases already pin the relations, and the
protections in build phase limit the damage to an ERROR.
Author: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Reviewed-by: Shihao Zhong <zhong950419@gmail.com>
Discussion: https://postgr.es/m/aver6A1EQEd2FaKq@paquier.xyz
Backpatch-through: 15
src/backend/commands/indexcmds.c | 28 ++++++++++++++++++----
src/test/regress/expected/create_index.out | 37 +++++++++++++++++++++++++++++-
src/test/regress/sql/create_index.sql | 26 ++++++++++++++++++++-
3 files changed, 84 insertions(+), 7 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index febc00f0a23..9d44bc4e7dd 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -3507,6 +3507,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
List *indexIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
+ List *phase1Rels = NIL;
List *lockTags = NIL;
ListCell *lc,
*lc2;
@@ -3632,6 +3633,10 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
indexIds = lappend(indexIds, idx);
MemoryContextSwitchTo(oldcontext);
+
+ /* Keep it opened until session locks are taken */
+ phase1Rels = lappend(phase1Rels, indexRelation);
+ continue;
}
index_close(indexRelation, NoLock);
@@ -3681,6 +3686,10 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
/* other fields set later */
MemoryContextSwitchTo(oldcontext);
+
+ /* Keep it opened until session locks are taken */
+ phase1Rels = lappend(phase1Rels, indexRelation);
+ continue;
}
index_close(indexRelation, NoLock);
@@ -3915,10 +3924,10 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
newIndexIds = lappend(newIndexIds, newidx);
/*
- * Save lockrelid to protect each relation from drop then close
- * relations. The lockrelid on parent relation is not taken here to
- * avoid multiple locks taken on the same relation, instead we rely on
- * parentRelationIds built earlier.
+ * Save lockrelid to protect each relation from drop. The lockrelid
+ * on parent relation is not taken here to avoid multiple locks taken
+ * on the same relation, instead we rely on parentRelationIds built
+ * earlier.
*/
lockrelid = palloc_object(LockRelId);
*lockrelid = indexRel->rd_lockInfo.lockRelId;
@@ -3929,8 +3938,13 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
MemoryContextSwitchTo(oldcontext);
+ /*
+ * Index expressions and predicates of the indexes processed in the
+ * next iterations are evaluated in this loop, so keep the new index
+ * opened until the session locks are taken.
+ */
index_close(indexRel, NoLock);
- index_close(newIndexRel, NoLock);
+ phase1Rels = lappend(phase1Rels, newIndexRel);
/* Roll back any GUC changes executed by index functions */
AtEOXact_GUC(false, save_nestlevel);
@@ -3993,6 +4007,10 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
LockRelationIdForSession(lockrelid, ShareUpdateExclusiveLock);
}
+ /* Session locks are taken, release the relations opened in phase 1 */
+ ReindexUnpinRelations(phase1Rels);
+ phase1Rels = NIL;
+
PopActiveSnapshot();
CommitTransactionCommand();
StartTransactionCommand();
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 7b7497ce796..e1a3e9c0ca3 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2376,7 +2376,8 @@ Indexes:
DROP TABLE concur_appclass_tab;
-- Check that a function called from an index expression is not able to
--- manipulate a relation involved in the operation.
+-- manipulate a relation involved in the operation. This involves phase 2 of
+-- REINDEX CONCURRENTLY.
CREATE TABLE concur_reindex_ddl_tab (i int);
INSERT INTO concur_reindex_ddl_tab VALUES (1);
-- Use two functions, one volatile called in a second, immutable, one.
@@ -2399,6 +2400,40 @@ REINDEX INDEX CONCURRENTLY concur_reindex_ddl_ind2; -- ok
REINDEX INDEX CONCURRENTLY concur_reindex_ddl_ind1; -- ok
REINDEX INDEX CONCURRENTLY concur_reindex_ddl_ind2; -- dropped
ERROR: relation "concur_reindex_ddl_ind2" does not exist
+DROP TABLE concur_reindex_ddl_tab;
+DROP FUNCTION concur_reindex_ddl_im(int);
+DROP FUNCTION concur_reindex_ddl_f();
+-- Same, with the function called when the index definitions are created.
+-- This involves phase 1 of REINDEX CONCURRENTLY.
+CREATE TABLE concur_reindex_ddl_tab (i int);
+INSERT INTO concur_reindex_ddl_tab VALUES (1);
+CREATE FUNCTION concur_reindex_ddl_f() RETURNS void LANGUAGE plpgsql AS
+ 'begin end';
+CREATE FUNCTION concur_reindex_ddl_im(int) RETURNS int
+ LANGUAGE plpgsql IMMUTABLE AS
+ 'begin perform public.concur_reindex_ddl_f(); return $1; end';
+CREATE INDEX concur_reindex_ddl_ind1 ON concur_reindex_ddl_tab (i);
+CREATE INDEX concur_reindex_ddl_ind2 ON
+ concur_reindex_ddl_tab ((i + concur_reindex_ddl_im(0)));
+CREATE OR REPLACE FUNCTION concur_reindex_ddl_f() RETURNS void
+ LANGUAGE plpgsql AS
+ 'begin DROP INDEX public.concur_reindex_ddl_ind1; end';
+REINDEX TABLE CONCURRENTLY concur_reindex_ddl_tab; -- error
+ERROR: cannot DROP INDEX "concur_reindex_ddl_ind1" because it is being used by active queries in this session
+CONTEXT: SQL statement "DROP INDEX public.concur_reindex_ddl_ind1"
+PL/pgSQL function concur_reindex_ddl_f() line 1 at SQL statement
+SQL statement "SELECT public.concur_reindex_ddl_f()"
+PL/pgSQL function concur_reindex_ddl_im(integer) line 1 at PERFORM
+SELECT c.relname, i.indisvalid FROM pg_index i
+ JOIN pg_class c ON c.oid = i.indexrelid
+ WHERE i.indrelid = 'concur_reindex_ddl_tab'::regclass
+ ORDER BY c.relname COLLATE "C";
+ relname | indisvalid
+-------------------------+------------
+ concur_reindex_ddl_ind1 | t
+ concur_reindex_ddl_ind2 | t
+(2 rows)
+
DROP TABLE concur_reindex_ddl_tab;
DROP FUNCTION concur_reindex_ddl_im(int);
DROP FUNCTION concur_reindex_ddl_f();
diff --git a/src/test/regress/sql/create_index.sql b/src/test/regress/sql/create_index.sql
index b9fc5673b2d..24f756c00fa 100644
--- a/src/test/regress/sql/create_index.sql
+++ b/src/test/regress/sql/create_index.sql
@@ -976,7 +976,8 @@ REINDEX TABLE CONCURRENTLY concur_appclass_tab;
DROP TABLE concur_appclass_tab;
-- Check that a function called from an index expression is not able to
--- manipulate a relation involved in the operation.
+-- manipulate a relation involved in the operation. This involves phase 2 of
+-- REINDEX CONCURRENTLY.
CREATE TABLE concur_reindex_ddl_tab (i int);
INSERT INTO concur_reindex_ddl_tab VALUES (1);
-- Use two functions, one volatile called in a second, immutable, one.
@@ -995,6 +996,29 @@ REINDEX INDEX CONCURRENTLY concur_reindex_ddl_ind2; -- dropped
DROP TABLE concur_reindex_ddl_tab;
DROP FUNCTION concur_reindex_ddl_im(int);
DROP FUNCTION concur_reindex_ddl_f();
+-- Same, with the function called when the index definitions are created.
+-- This involves phase 1 of REINDEX CONCURRENTLY.
+CREATE TABLE concur_reindex_ddl_tab (i int);
+INSERT INTO concur_reindex_ddl_tab VALUES (1);
+CREATE FUNCTION concur_reindex_ddl_f() RETURNS void LANGUAGE plpgsql AS
+ 'begin end';
+CREATE FUNCTION concur_reindex_ddl_im(int) RETURNS int
+ LANGUAGE plpgsql IMMUTABLE AS
+ 'begin perform public.concur_reindex_ddl_f(); return $1; end';
+CREATE INDEX concur_reindex_ddl_ind1 ON concur_reindex_ddl_tab (i);
+CREATE INDEX concur_reindex_ddl_ind2 ON
+ concur_reindex_ddl_tab ((i + concur_reindex_ddl_im(0)));
+CREATE OR REPLACE FUNCTION concur_reindex_ddl_f() RETURNS void
+ LANGUAGE plpgsql AS
+ 'begin DROP INDEX public.concur_reindex_ddl_ind1; end';
+REINDEX TABLE CONCURRENTLY concur_reindex_ddl_tab; -- error
+SELECT c.relname, i.indisvalid FROM pg_index i
+ JOIN pg_class c ON c.oid = i.indexrelid
+ WHERE i.indrelid = 'concur_reindex_ddl_tab'::regclass
+ ORDER BY c.relname COLLATE "C";
+DROP TABLE concur_reindex_ddl_tab;
+DROP FUNCTION concur_reindex_ddl_im(int);
+DROP FUNCTION concur_reindex_ddl_f();
-- Partitions
-- Create some partitioned tables
[parent: abfd5b9d95a0]