1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-22 02:52:08 +03:00

Fix partitioned index creation with foreign partitions

When a partitioned tables contains foreign tables as partitions, it is
not possible to implement unique or primary key indexes -- but when
regular indexes are created, there is no reason to do anything other
than ignoring such partitions.  We were raising errors upon encountering
the foreign partitions, which is unfriendly and doesn't protect against
any actual problems.

Relax this restriction so that index creation is allowed on partitioned
tables containing foreign partitions, becoming a no-op on them.  (We may
later want to redefine this so that the FDW is told to create the
indexes on the foreign side.)  This applies to CREATE INDEX, as well as
ALTER TABLE / ATTACH PARTITION and CREATE TABLE / PARTITION OF.

Backpatch to 11, where indexes on partitioned tables were introduced.

Discussion: https://postgr.es/m/15724-d5a58fa9472eef4f@postgresql.org
Author: Álvaro Herrera
Reviewed-by: Amit Langote
This commit is contained in:
Alvaro Herrera
2019-06-26 18:38:51 -04:00
parent 65e6d42140
commit 55ed3defc9
7 changed files with 172 additions and 9 deletions

View File

@ -1085,6 +1085,26 @@ DefineIndex(Oid relationId,
int maplen;
childrel = table_open(childRelid, lockmode);
/*
* Don't try to create indexes on foreign tables, though.
* Skip those if a regular index, or fail if trying to create
* a constraint index.
*/
if (childrel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
{
if (stmt->unique || stmt->primary)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("cannot create unique index on partitioned table \"%s\"",
RelationGetRelationName(rel)),
errdetail("Table \"%s\" contains partitions that are foreign tables.",
RelationGetRelationName(rel))));
table_close(childrel, lockmode);
continue;
}
childidxs = RelationGetIndexList(childrel);
attmap =
convert_tuples_by_name_map(RelationGetDescr(childrel),