From f252e77e8bc7ea26963a183b36fa16c026b468f7 Mon Sep 17 00:00:00 2001
From: Zhenghua Lyu <kainwen@gmail.com>
Date: Thu, 17 Nov 2022 20:49:19 +0800
Subject: [PATCH] Don't treate IndexStmt like AlterTable when DefineIndex is
 called from ProcessUtilitySlow.

Previously, when calling DefineIndex from ProcessUtilitySlow,
is_alter_table will be set true if this statement is came from
expandTableLikeClause.

I check the code of DefineIndex, there are only two places use
is_alter_table:
   1. the function index_check_primary_key
   2. print a debug log on what the statement is

For 1, since we are doing create table xxx (like yyy including
indexes), we are sure that the check relationHasPrimaryKey in the
function index_check_primary_key will be satisfied because we are just
create the new table.

For 2, I do not think it will mislead the user if we print it as
CreateStmt.

Based on the above, we can always a false value for is_alter_table
when DefineIndex is called from ProcessUtilitySlow.
---
 src/backend/tcop/utility.c | 13 +------------
 1 file changed, 1 insertion(+), 12 deletions(-)

diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c
index 247d0816ad..7d4094fdc5 100644
--- a/src/backend/tcop/utility.c
+++ b/src/backend/tcop/utility.c
@@ -1523,17 +1523,6 @@ ProcessUtilitySlow(ParseState *pstate,
 						list_free(inheritors);
 					}
 
-					/*
-					 * If the IndexStmt is already transformed, it must have
-					 * come from generateClonedIndexStmt, which in current
-					 * usage means it came from expandTableLikeClause rather
-					 * than from original parse analysis.  And that means we
-					 * must treat it like ALTER TABLE ADD INDEX, not CREATE.
-					 * (This is a bit grotty, but currently it doesn't seem
-					 * worth adding a separate bool field for the purpose.)
-					 */
-					is_alter_table = stmt->transformed;
-
 					/* Run parse analysis ... */
 					stmt = transformIndexStmt(relid, stmt, queryString);
 
@@ -1545,7 +1534,7 @@ ProcessUtilitySlow(ParseState *pstate,
 									InvalidOid, /* no predefined OID */
 									InvalidOid, /* no parent index */
 									InvalidOid, /* no parent constraint */
-									is_alter_table,
+									false,
 									true,	/* check_rights */
 									true,	/* check_not_in_use */
 									false,	/* skip_build */
-- 
2.25.1

