From fafad2d4e561af802746b614ba3adebc45389711 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Wed, 28 Jan 2026 13:47:56 +0800
Subject: [PATCH v1] tablecmds: cleanup unreachable AT_AddIndexConstraint in
 ATPrepCmd

ADD CONSTRAINT USING INDEX is initially parsed as AT_AddConstraint and later
transformed into AT_AddIndexConstraint by ATParseTransformCmd(), which appends
the transformed command directly to the subcommand list. As a result,
AT_AddIndexConstraint should never reach ATPrepCmd().

Clean up ATPrepCmd() by replacing the existing handling with an Assert(false)
to document and enforce this invariant.

The accompanying test changes are cosmetic only (SQL formatting), with no
behavioral impact.

Author: Chao Li <lic@highgo.com>
Reviewed-by:
Discussion:
---
 src/backend/commands/tablecmds.c           | 16 ++++++++++++----
 src/test/regress/expected/create_index.out |  4 ++--
 src/test/regress/sql/create_index.sql      |  4 ++--
 3 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index f976c0e5c7e..e839fcf42d7 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5100,10 +5100,18 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 			pass = AT_PASS_ADD_CONSTR;
 			break;
 		case AT_AddIndexConstraint: /* ADD CONSTRAINT USING INDEX */
-			ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_PARTITIONED_TABLE);
-			/* This command never recurses */
-			/* No command-specific prep needed */
-			pass = AT_PASS_ADD_INDEXCONSTR;
+
+			/*
+			 * This subtype should never reach ATPrepCmd().
+			 *
+			 * ADD CONSTRAINT USING INDEX is initially parsed as
+			 * AT_AddConstraint. During execution, ATParseTransformCmd()
+			 * transforms it into AT_AddIndexConstraint and appends it
+			 * directly to the subcommand list, bypassing ATPrepCmd().
+			 *
+			 * See ATParseTransformCmd() and transformAlterTableStmt().
+			 */
+			Assert(false);
 			break;
 		case AT_DropConstraint: /* DROP CONSTRAINT */
 			ATSimplePermissions(cmd->subtype, rel,
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index c743fc769cb..045983115ca 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -1609,8 +1609,8 @@ DETAIL:  Cannot create a primary key or unique constraint using such an index.
 DROP TABLE cwi_test;
 -- ADD CONSTRAINT USING INDEX is forbidden on partitioned tables
 CREATE TABLE cwi_test(a int) PARTITION BY hash (a);
-create unique index on cwi_test (a);
-alter table cwi_test add primary key using index cwi_test_a_idx ;
+CREATE UNIQUE INDEX on cwi_test (a);
+ALTER TABLE cwi_test ADD PRIMARY KEY USING INDEX cwi_test_a_idx;
 ERROR:  ALTER TABLE / ADD CONSTRAINT USING INDEX is not supported on partitioned tables
 DROP TABLE cwi_test;
 -- PRIMARY KEY constraint cannot be backed by a NULLS NOT DISTINCT index
diff --git a/src/test/regress/sql/create_index.sql b/src/test/regress/sql/create_index.sql
index eabc9623b20..61923c415cd 100644
--- a/src/test/regress/sql/create_index.sql
+++ b/src/test/regress/sql/create_index.sql
@@ -619,8 +619,8 @@ DROP TABLE cwi_test;
 
 -- ADD CONSTRAINT USING INDEX is forbidden on partitioned tables
 CREATE TABLE cwi_test(a int) PARTITION BY hash (a);
-create unique index on cwi_test (a);
-alter table cwi_test add primary key using index cwi_test_a_idx ;
+CREATE UNIQUE INDEX on cwi_test (a);
+ALTER TABLE cwi_test ADD PRIMARY KEY USING INDEX cwi_test_a_idx;
 DROP TABLE cwi_test;
 
 -- PRIMARY KEY constraint cannot be backed by a NULLS NOT DISTINCT index
-- 
2.50.1 (Apple Git-155)

