Skip to content

Commit 9df61e3

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

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
@@ -910,6 +910,12 @@ DefineIndex(Oid relationId,
910910
int maplen;
911911

912912
childrel = heap_open(childRelid, lockmode);
913+
/* Foreign table doesn't need indexes */
914+
if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
915+
{
916+
heap_close(childrel, NoLock);
917+
continue;
918+
}
913919
childidxs = RelationGetIndexList(childrel);
914920
attmap =
915921
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
@@ -910,12 +910,13 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
910910

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

14331+
/* Foreign table doesn't need indexes */
14332+
if (attachrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
14333+
return;
14334+
1433014335
cxt = AllocSetContextCreate(CurrentMemoryContext,
1433114336
"AttachPartitionEnsureIndexes",
1433214337
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

@@ -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

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy