6767#include "tcop/utility.h"
6868#include "utils/acl.h"
6969#include "utils/guc.h"
70- #include "utils/lsyscache.h"
7170#include "utils/syscache.h"
7271#include "utils/rel.h"
7372
@@ -1288,6 +1287,7 @@ ProcessUtilitySlow(ParseState *pstate,
12881287 IndexStmt * stmt = (IndexStmt * ) parsetree ;
12891288 Oid relid ;
12901289 LOCKMODE lockmode ;
1290+ List * inheritors = NIL ;
12911291
12921292 if (stmt -> concurrent )
12931293 PreventInTransactionBlock (isTopLevel ,
@@ -1314,33 +1314,17 @@ ProcessUtilitySlow(ParseState *pstate,
13141314 * CREATE INDEX on partitioned tables (but not regular
13151315 * inherited tables) recurses to partitions, so we must
13161316 * acquire locks early to avoid deadlocks.
1317- *
1318- * We also take the opportunity to verify that all
1319- * partitions are something we can put an index on, to
1320- * avoid building some indexes only to fail later.
13211317 */
1322- if (stmt -> relation -> inh &&
1323- get_rel_relkind (relid ) == RELKIND_PARTITIONED_TABLE )
1318+ if (stmt -> relation -> inh )
13241319 {
1325- ListCell * lc ;
1326- List * inheritors = NIL ;
1327-
1328- inheritors = find_all_inheritors (relid , lockmode , NULL );
1329- foreach (lc , inheritors )
1330- {
1331- char relkind = get_rel_relkind (lfirst_oid (lc ));
1332-
1333- if (relkind != RELKIND_RELATION &&
1334- relkind != RELKIND_MATVIEW &&
1335- relkind != RELKIND_PARTITIONED_TABLE )
1336- ereport (ERROR ,
1337- (errcode (ERRCODE_INVALID_OBJECT_DEFINITION ),
1338- errmsg ("cannot create index on partitioned table \"%s\"" ,
1339- stmt -> relation -> relname ),
1340- errdetail ("Table \"%s\" contains partitions that are foreign tables." ,
1341- stmt -> relation -> relname )));
1342- }
1343- list_free (inheritors );
1320+ Relation rel ;
1321+
1322+ /* already locked by RangeVarGetRelidExtended */
1323+ rel = heap_open (relid , NoLock );
1324+ if (rel -> rd_rel -> relkind == RELKIND_PARTITIONED_TABLE )
1325+ inheritors = find_all_inheritors (relid , lockmode ,
1326+ NULL );
1327+ heap_close (rel , NoLock );
13441328 }
13451329
13461330 /* Run parse analysis ... */
@@ -1369,6 +1353,8 @@ ProcessUtilitySlow(ParseState *pstate,
13691353 parsetree );
13701354 commandCollected = true;
13711355 EventTriggerAlterTableEnd ();
1356+
1357+ list_free (inheritors );
13721358 }
13731359 break ;
13741360
0 commit comments