Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xS1gP-00000001WJA-3066 for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Nov 2026 00:31:54 +0000 Received: from localhost ([127.0.0.1]:49902 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xS1gO-0000000CTfG-175I for pgsql-hackers@arkaria.postgresql.org; Sat, 14 Nov 2026 00:31:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]:55150) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xS1gN-0000000CTf8-1gQh for pgsql-hackers@lists.postgresql.org; Sat, 14 Nov 2026 00:31:51 +0000 Received: from fout-b4-smtp.messagingengine.com ([202.12.124.147]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xS1gG-00000000wU3-1xQt for pgsql-hackers@lists.postgresql.org; Sat, 14 Nov 2026 00:31:50 +0000 Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfout.stl.internal (Postfix) with ESMTP id 612071D00136 for ; Fri, 13 Nov 2026 19:31:41 -0500 (EST) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-03.internal (MEProxy); Fri, 13 Nov 2026 19:31:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:subject:subject:to:to; s=fm3; t=1794616301; x=1794702701; bh=Opb3oQdMfZeu7wvyYO1JVkHuDWtXcMHJ cjG73OREZkc=; b=AhK8fjWXs3fdFSGjf1EXVjdNb32OjLo3DCpzT+1fZd3CSWTo bKKpZ/CgB27UhhzUaBd+NJiYNhvsKlS5UtDOdx3eAVVbsydc0Te6hnzkskuDGsLE ibShfMJkFu8ckWw3o7YhulpGYakZ8GG4U6CrjkPb1Wlu2hvGZ9I/6DJxhYUkuB2C 5gNWdxRfi+DV4jLmf8D4kHxt+K01CJ3euPWhhkXcJSXbyinbXFH4G9FKV2lvqwE6 NWQFqUpbeaV5DOiczjHdSLD7EjwU8+f8DrrYJpVm1d0gUeEJOfzis+6fFE1dNsaa t3EdKZwDTjHSrDRAoEemc10OZpsTQJ9c1qC3Xg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1794616301; x= 1794702701; bh=Opb3oQdMfZeu7wvyYO1JVkHuDWtXcMHJcjG73OREZkc=; b=D J51uSEtTJ8EiRwnhUxKPZqhIEBQYXHXxELswK6cPun2GvsMyGsiaqJ+KfLXSR31/ Jo3G/4hH6qTliyqoyubR4kNXsqU6XMY9Wt9oG93m/TPGeae9dOQg0GZHrRD0KEkY oXvda861bkwMeR4liDZeA71rcTbEWt3aMnZk5mo+bmOaxZoiECPKtyQgIvtWiGAi RLmRWU8H30cobrV2uowHe0BBW2cn9pXBUsMuc5t0/FMwC6i2puWkUJMSUCVbTizt dqaJeD6C2c/lc9bc6kJsQFa+WxsxTR0t0RZlMHOjDq7nCaCD8n/tq7IQ9m4XjvXQ ZJ4Xl2GNLisPjNkkOQLCA== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=sign d=paquier.xyz a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1794616301; d=paquier.xyz; mf=PG1pY2hhZWxAcGFxdWllci54eXo+; rt=PHBnc3FsLWhhY2tlcnNAbGlzdHMucG9zdGdyZXNxbC5vcmc+; s=fm3:rsa-sha256:UMX+1RkftSNFSLDdnd/DHnmRlm7AHMGS8xv/xS7Vrjxspgz dVDP+JZub00z0rE9DFEsOdryM76BX7gVLoioGmnh75jUWOKh43L5wyM+iaQ+6e0l nI+vWYPdKpEeRMKeaJksW+0cJEZiOgbw56LB05w+Z9Ui6iw+0SRZ4b+AzKimdpJn ARAJyYs5mt3G6vAsJaZkW2zTdFERJzI+zUUelLQdcO5uc3N0LIWSK3OrfO4Iejen mHi3J/J3+luVJxQCHxd1NS/JIbdiEotyEy6KUPiQS8XxTRZNDr0MgeuWsF6X3HCl zIZOqABzLUOh3aOnKpmyHG3EDV/YEEKAWH6jQjA==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=mi-m=1; hc=9; hn=content-disposition,content-type,date,feedback-id,from, message-id,mime-version,subject,to; Message-Instance: m=1; h=sha256:3okBFv/x948rduGE/oCDc6Ad5yocOi/0FOBzBda0/ZE=:wvvhLQdwPQh0aYBk77sDTXu8iapb/B5YW62YRiaak88=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTE9juGCoykx1ZyVGO418063u4JcNiu8DTFwM8O5p4s0CDNvluOukRiYSGoQ9EbwQq 2GSWnrPA9eXIngKUhN4CGMX8cvnc0rNwPfYSSxT1W51wSekkSlTY53OOS+6kDfPG8gDsZk F944w5UIlAgrZv9FEXpz3BEFzMcsvnCSgMbFkCI5xi4xGUKK+y3XkuXqe9jCoaDKb+FDK6 HRBDNVVixAzyHqFuLwwiOg42d2COrE3VHzYOiToiHGLat0PjiMsVJ9A203CgsEOvq6FGHX zTQ3gGfF0a+Ys87CDqzb1K7tVmThs/Jq471rDN0VuwBdLWhPB75I/TjNaxvWG3YyIIEnIE hiboOcw69sUKuAe/T3HFAtqx4LiqDXU7i/8rl/L75BFQc1W5iZk74hTFst3MlRW+/nq8l+ yAfvHWLVpitnHTvhTm48crRZz9JRwL/V3O/jOGOZduboXOKstotKxWmnlWuuqj2OR7gS/t OIru1UurSe246gTCFwan1bZ/viJ01G3xwyFT1Ne8p1Kmm7L+8vyEId/m2RLWZNWgk4fxzu DV04JlpOcn8udnAuu9eA/oEiCLNevWRNIWojPKW+gM5vi5DAYTQ6UM5nDxBzfwgNbe+ZPz KV6k0V7Tw3/gKUiCCEEFLQekKqXw8osPZe1sKnjN7Jasvs9wdecEe5kVlxWw X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Fri, 13 Nov 2026 19:31:39 -0500 (EST) Date: Sat, 14 Nov 2026 09:31:36 +0900 From: Michael Paquier To: Postgres hackers Subject: Index drops during phase 1 of REINDEX CONCURRENTLY Message-ID: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="9i94MngGHT/iPbku" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --9i94MngGHT/iPbku Content-Type: multipart/mixed; boundary="zDonvkDtFZ4Z+8CI" Content-Disposition: inline --zDonvkDtFZ4Z+8CI Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi all, Some of the feedback I got after f24a08ec13eb involved the fact that indexes could also be dropped during the phase 1 of REINDEX CONCURRENTLY when the expressions or predicates are evaluated. The damage is limited to cache lookup failures or assertion hits during phase 2, thanks to the protections that f24a08ec13eb has added. Anyway, it would be nice to fix this issue. Attached is a patch addressing the problem with a test, for a backpatch down to v15. Thoughts or objections? -- Michael --zDonvkDtFZ4Z+8CI Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=0001-Pin-relations-during-phase-1-of-REINDEX-CONCURRENTLY.patch Content-Transfer-Encoding: quoted-printable =46rom 67a2c319cc3f83b670241f5440fdbab4ccb8ccfa Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier Date: Sat, 14 Nov 2026 09:20:12 +0900 Subject: [PATCH] Pin relations during phase 1 of REINDEX CONCURRENTLY Phase 1 of ReindexRelationConcurrently() (creation of the new indexes) 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 due to a dropped index. The relations opened in phase 1 are now kept pinned until the session locks are taken, so as DDLs on them are blocked by CheckTableNotInUse(). The build and validation phases were already pinning the relations. Author: Michael Paquier Discussion: https://postgr.es/m/TODO Backpatch-through: 15 --- src/backend/commands/indexcmds.c | 16 ++++++++-- src/test/regress/expected/create_index.out | 37 +++++++++++++++++++++- src/test/regress/sql/create_index.sql | 26 ++++++++++++++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexc= mds.c index 68fc87b76a0e..8414b8037222 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -3790,6 +3790,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, = Oid relationOid, const Rein List *indexIds =3D NIL; List *newIndexIds =3D NIL; List *relationLocks =3D NIL; + List *phase1Rels =3D NIL; List *lockTags =3D NIL; ListCell *lc, *lc2; @@ -4215,8 +4216,13 @@ ReindexRelationConcurrently(const ReindexStmt *stmt,= Oid relationOid, const Rein =20 MemoryContextSwitchTo(oldcontext); =20 - index_close(indexRel, NoLock); - index_close(newIndexRel, NoLock); + /* + * Index expressions and predicates of the indexes processed in the + * next iterations are evaluated in this loop, so keep the relations + * opened until all the new indexes are created. + */ + phase1Rels =3D lappend(phase1Rels, indexRel); + phase1Rels =3D lappend(phase1Rels, newIndexRel); =20 /* Roll back any GUC changes executed by index functions */ AtEOXact_GUC(false, save_nestlevel); @@ -4224,7 +4230,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, = Oid relationOid, const Rein /* Restore userid and security context */ SetUserIdAndSecContext(save_userid, save_sec_context); =20 - table_close(heapRel, NoLock); + phase1Rels =3D lappend(phase1Rels, heapRel); =20 /* * If a statement is available, telling that this comes from a REINDEX @@ -4279,6 +4285,10 @@ ReindexRelationConcurrently(const ReindexStmt *stmt,= Oid relationOid, const Rein LockRelationIdForSession(lockrelid, ShareUpdateExclusiveLock); } =20 + /* Session locks are taken, release the relations opened in phase 1 */ + ReindexUnpinRelations(phase1Rels); + phase1Rels =3D NIL; + PopActiveSnapshot(); CommitTransactionCommand(); StartTransactionCommand(); diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/= expected/create_index.out index e914c809b820..857603cd2b4f 100644 --- a/src/test/regress/expected/create_index.out +++ b/src/test/regress/expected/create_index.out @@ -2878,7 +2878,8 @@ Indexes: =20 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. @@ -2901,6 +2902,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 us= ed 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 =3D i.indexrelid + WHERE i.indrelid =3D 'concur_reindex_ddl_tab'::regclass + ORDER BY c.relname COLLATE "C"; + relname | indisvalid=20 +-------------------------+------------ + 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/c= reate_index.sql index e4b8cee1e1c2..27e038fa8087 100644 --- a/src/test/regress/sql/create_index.sql +++ b/src/test/regress/sql/create_index.sql @@ -1144,7 +1144,8 @@ REINDEX TABLE CONCURRENTLY concur_appclass_tab; DROP TABLE concur_appclass_tab; =20 -- 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. @@ -1163,6 +1164,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 =3D i.indexrelid + WHERE i.indrelid =3D '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(); =20 -- Partitions -- Create some partitioned tables --=20 2.56.0 --zDonvkDtFZ4Z+8CI-- --9i94MngGHT/iPbku Content-Type: application/pgp-signature; name=signature.asc -----BEGIN PGP SIGNATURE----- iQIzBAEBCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmr3q+gACgkQnvQgOdby QH0yHxAAgltkw21iBTsIcgOCs6Cf9Jt7v5rR25Ii+ogoLuDdbeSB34OF8H0uDQAk GcdZztNe4TFHPXoalJw9o4QvqOMpJjofEm4J+45Dt1FQvVdByDiPHTyu0IS6SqLF bLs0/pjR/mt+LBZRIIJdkRF52zN5va2Ewiox5kfaa8rek7rotEgmYGv7TjjzDL0r md+QVk/d/jekGSfPZOj3A9ihzASRMecTDQsBJU7XH2nVo61G0E6OlBhuSsGNsfKv W5PEQAYV10b8VIeZjerhZVeXgLA4zkckvBYV/FJCyODYpV/0z1lFsPZPulAA+jk7 lAvZqA8f4OVPQFU0z+ZhFvvp1RiQW3pM5ITEHJqUi0m1/CVngfD7fZuA1IXQR/lN 3J9sLd+/Jz4+v9k8JjPGpnzwFAutPTZ/7cAt5XTc5+pdlzIT9sbNddZVvbQCZvnE zqp1hFJCAxbhuLvfA52b3hnAjl5hdqxxRCzTtgOuViT/PX97XQt8fKWcqQyNifm7 ZHi2qr3IDbm7qo7h4LCYQ/TRn8Y+nAkJzaggNKif4kmvi/qyrR3oxL+aaqyoGzdQ 4VUX4SyNAHZVIT1oABxOQySWFXWUf1oxSfVQN+JR3lZZqOP8GjQ2mTtUXA9JKnFq 6Mcelh4TIEaNhDSgObc2lDrOmCUjr6h3UK6E7FbVU4JRcCcZ9bc= =TIPl -----END PGP SIGNATURE----- --9i94MngGHT/iPbku--