From 4fd273458298289cfb1609192fad519768039c2f 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 v3] tablecmds: cleanup unreachable AT_AddIndex and
 AT_AddIndexConstraint in ATPrepCmd

ADD INDEX and ADD CONSTRAINT USING INDEX are initially parsed as
AT_AddConstraint and later transformed into AT_AddIndex or
AT_AddIndexConstraint by ATParseTransformCmd(), which appends
the transformed command directly to the subcommand list. As a result,
AT_AddIndex and 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: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/CE5CFCE7-F101-4A42-9461-221DC5BF099E@gmail.com
---
 src/backend/commands/tablecmds.c           | 24 +++++++++++++---------
 src/test/regress/expected/create_index.out |  4 ++--
 src/test/regress/sql/create_index.sql      |  4 ++--
 3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index f976c0e5c7e..330b0909a67 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5081,12 +5081,6 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 			/* Recursion occurs during execution phase */
 			pass = AT_PASS_DROP;
 			break;
-		case AT_AddIndex:		/* ADD INDEX */
-			ATSimplePermissions(cmd->subtype, rel, ATT_TABLE | ATT_PARTITIONED_TABLE);
-			/* This command never recurses */
-			/* No command-specific prep needed */
-			pass = AT_PASS_ADD_INDEX;
-			break;
 		case AT_AddConstraint:	/* ADD CONSTRAINT */
 			ATSimplePermissions(cmd->subtype, rel,
 								ATT_TABLE | ATT_PARTITIONED_TABLE | ATT_FOREIGN_TABLE);
@@ -5099,11 +5093,21 @@ ATPrepCmd(List **wqueue, Relation rel, AlterTableCmd *cmd,
 			}
 			pass = AT_PASS_ADD_CONSTR;
 			break;
+		case AT_AddIndex:		/* ADD INDEX */
 		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;
+
+			/*
+			 * These subtypes should never reach ATPrepCmd().
+			 *
+			 * ADD INDEX and ADD CONSTRAINT USING INDEX are initially parsed
+			 * as AT_AddConstraint. During execution, ATParseTransformCmd()
+			 * transforms them into AT_AddIndex or AT_AddIndexConstraint and
+			 * appends them 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)

