From 3246090c36e2ef84907aa2fb895f6bfa9d90499d 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 v2] 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().

Delete AT_AddIndexConstraint from ATPrepCmd() and add a comment under
AT_AddConstraint to explain for AT_AddIndexConstraint.

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

Author: Chao Li <lic@highgo.com>
Reviewed-by:
Discussion: CE5CFCE7-F101-4A42-9461-221DC5BF099E@gmail.com
---
 src/backend/commands/tablecmds.c           | 17 ++++++++++-------
 src/test/regress/expected/create_index.out |  4 ++--
 src/test/regress/sql/create_index.sql      |  4 ++--
 3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index f976c0e5c7e..42afa3def16 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5087,7 +5087,7 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 			/* No command-specific prep needed */
 			pass = AT_PASS_ADD_INDEX;
 			break;
-		case AT_AddConstraint:	/* ADD CONSTRAINT */
+		case AT_AddConstraint:	/* ADD CONSTRAINT / ADD CONSTRAINT USING INDEX */
 			ATSimplePermissions(cmd->subtype, rel,
 								ATT_TABLE | ATT_PARTITIONED_TABLE | ATT_FOREIGN_TABLE);
 			ATPrepAddPrimaryKey(wqueue, rel, cmd, recurse, lockmode, context);
@@ -5097,14 +5097,17 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 				(void) find_all_inheritors(RelationGetRelid(rel), lockmode, NULL);
 				cmd->recurse = true;
 			}
+
+			/*
+			 * 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.
+			 *
+			 * See ATParseTransformCmd() and transformAlterTableStmt().
+			 */
 			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;
-			break;
 		case AT_DropConstraint: /* DROP CONSTRAINT */
 			ATSimplePermissions(cmd->subtype, rel,
 								ATT_TABLE | ATT_PARTITIONED_TABLE | ATT_FOREIGN_TABLE);
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)

