Skip to content

Commit b808fdd

Browse files
committed
Revert "Don't allow partitioned index on foreign-table partitions"
See 4eaa537 and archives for discussion.
1 parent 40dde82 commit b808fdd

File tree

5 files changed

+25
-41
lines changed

5 files changed

+25
-41
lines changed

src/backend/commands/indexcmds.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,12 @@ DefineIndex(Oid relationId,
922922
int maplen;
923923

924924
childrel = heap_open(childRelid, lockmode);
925+
/* Foreign table doesn't need indexes */
926+
if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
927+
{
928+
heap_close(childrel, NoLock);
929+
continue;
930+
}
925931
childidxs = RelationGetIndexList(childrel);
926932
attmap =
927933
convert_tuples_by_name_map(RelationGetDescr(childrel),

src/backend/commands/tablecmds.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,12 +911,13 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
911911

912912
/*
913913
* If we're creating a partition, create now all the indexes, triggers,
914-
* FKs defined in the parent.
914+
* FKs defined in the parent -- except when we are creating foreign table,
915+
* for which those doesn't make sense.
915916
*
916917
* We can't do it earlier, because DefineIndex wants to know the partition
917918
* key which we just stored.
918919
*/
919-
if (stmt->partbound)
920+
if (stmt->partbound && relkind != RELKIND_FOREIGN_TABLE)
920921
{
921922
Oid parentId = linitial_oid(inheritOids);
922923
Relation parent;
@@ -14987,6 +14988,10 @@ AttachPartitionEnsureIndexes(Relation rel, Relation attachrel)
1498714988
MemoryContext cxt;
1498814989
MemoryContext oldcxt;
1498914990

14991+
/* Foreign table doesn't need indexes */
14992+
if (attachrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
14993+
return;
14994+
1499014995
cxt = AllocSetContextCreate(CurrentMemoryContext,
1499114996
"AttachPartitionEnsureIndexes",
1499214997
ALLOCSET_DEFAULT_SIZES);

src/backend/tcop/utility.c

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
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

@@ -1298,6 +1297,7 @@ ProcessUtilitySlow(ParseState *pstate,
12981297
IndexStmt *stmt = (IndexStmt *) parsetree;
12991298
Oid relid;
13001299
LOCKMODE lockmode;
1300+
List *inheritors = NIL;
13011301

13021302
if (stmt->concurrent)
13031303
PreventInTransactionBlock(isTopLevel,
@@ -1324,33 +1324,17 @@ ProcessUtilitySlow(ParseState *pstate,
13241324
* CREATE INDEX on partitioned tables (but not regular
13251325
* inherited tables) recurses to partitions, so we must
13261326
* acquire locks early to avoid deadlocks.
1327-
*
1328-
* We also take the opportunity to verify that all
1329-
* partitions are something we can put an index on, to
1330-
* avoid building some indexes only to fail later.
13311327
*/
1332-
if (stmt->relation->inh &&
1333-
get_rel_relkind(relid) == RELKIND_PARTITIONED_TABLE)
1328+
if (stmt->relation->inh)
13341329
{
1335-
ListCell *lc;
1336-
List *inheritors = NIL;
1337-
1338-
inheritors = find_all_inheritors(relid, lockmode, NULL);
1339-
foreach(lc, inheritors)
1340-
{
1341-
char relkind = get_rel_relkind(lfirst_oid(lc));
1342-
1343-
if (relkind != RELKIND_RELATION &&
1344-
relkind != RELKIND_MATVIEW &&
1345-
relkind != RELKIND_PARTITIONED_TABLE)
1346-
ereport(ERROR,
1347-
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
1348-
errmsg("cannot create index on partitioned table \"%s\"",
1349-
stmt->relation->relname),
1350-
errdetail("Table \"%s\" contains partitions that are foreign tables.",
1351-
stmt->relation->relname)));
1352-
}
1353-
list_free(inheritors);
1330+
Relation rel;
1331+
1332+
/* already locked by RangeVarGetRelidExtended */
1333+
rel = heap_open(relid, NoLock);
1334+
if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1335+
inheritors = find_all_inheritors(relid, lockmode,
1336+
NULL);
1337+
heap_close(rel, NoLock);
13541338
}
13551339

13561340
/* Run parse analysis ... */
@@ -1379,6 +1363,8 @@ ProcessUtilitySlow(ParseState *pstate,
13791363
parsetree);
13801364
commandCollected = true;
13811365
EventTriggerAlterTableEnd();
1366+
1367+
list_free(inheritors);
13821368
}
13831369
break;
13841370

src/test/regress/expected/foreign_data.out

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -749,13 +749,6 @@ SELECT * FROM ft1; -- ERROR
749749
ERROR: foreign-data wrapper "dummy" has no handler
750750
EXPLAIN SELECT * FROM ft1; -- ERROR
751751
ERROR: foreign-data wrapper "dummy" has no handler
752-
CREATE TABLE lt1 (a INT) PARTITION BY RANGE (a);
753-
CREATE FOREIGN TABLE ft_part1
754-
PARTITION OF lt1 FOR VALUES FROM (0) TO (1000) SERVER s0;
755-
CREATE INDEX ON lt1 (a); -- ERROR
756-
ERROR: cannot create index on partitioned table "lt1"
757-
DETAIL: Table "lt1" contains partitions that are foreign tables.
758-
DROP TABLE lt1;
759752
-- ALTER FOREIGN TABLE
760753
COMMENT ON FOREIGN TABLE ft1 IS 'foreign table';
761754
COMMENT ON FOREIGN TABLE ft1 IS NULL;

src/test/regress/sql/foreign_data.sql

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,6 @@ CREATE INDEX id_ft1_c2 ON ft1 (c2); -- ERROR
316316
SELECT * FROM ft1; -- ERROR
317317
EXPLAIN SELECT * FROM ft1; -- ERROR
318318

319-
CREATE TABLE lt1 (a INT) PARTITION BY RANGE (a);
320-
CREATE FOREIGN TABLE ft_part1
321-
PARTITION OF lt1 FOR VALUES FROM (0) TO (1000) SERVER s0;
322-
CREATE INDEX ON lt1 (a); -- ERROR
323-
DROP TABLE lt1;
324-
325319
-- ALTER FOREIGN TABLE
326320
COMMENT ON FOREIGN TABLE ft1 IS 'foreign table';
327321
COMMENT ON FOREIGN TABLE ft1 IS NULL;

0 commit comments

Comments
 (0)