From 16d11d68437a6a37af1fea08d4a29ef463f0d62c Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Sat, 15 Oct 2022 19:24:26 +0200 Subject: [PATCH] Disallow MERGE cleanly for foreign partitions While directly targetting a foreign table with MERGE was already expressly forbidden, we failed to catch the case of a partitioned table that has a foreign table as a partition; and the result if you try is an incomprehensible error. Fix that by adding a specific check. Backpatch to 15. Reported-by: Tatsuhiro Nakamori Discussion: https://postgr.es/m/bt22nakamorit@oss.nttdata.com --- .../postgres_fdw/expected/postgres_fdw.out | 5 +++++ contrib/postgres_fdw/sql/postgres_fdw.sql | 4 ++++ src/backend/optimizer/plan/createplan.c | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index 1ca248f337..499bf50386 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -8251,6 +8251,11 @@ select tableoid::regclass, * FROM remp2; (3 rows) delete from itrtest; +-- MERGE ought to fail cleanly +merge into itrtest using (select 1, 'foo') as source on (true) + when matched then do nothing; +ERROR: cannot execute MERGE on relation "remp1" +DETAIL: This operation is not supported for foreign tables. create unique index loct1_idx on loct1 (a); -- DO NOTHING without an inference specification is supported insert into itrtest values (1, 'foo') on conflict do nothing returning *; diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 0356f081e7..1561297fc3 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -2194,6 +2194,10 @@ select tableoid::regclass, * FROM remp2; delete from itrtest; +-- MERGE ought to fail cleanly +merge into itrtest using (select 1, 'foo') as source on (true) + when matched then do nothing; + create unique index loct1_idx on loct1 (a); -- DO NOTHING without an inference specification is supported diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 76606faa3e..1bc59c9457 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -7084,12 +7084,32 @@ make_modifytable(PlannerInfo *root, Plan *subplan, RelOptInfo *resultRel = root->simple_rel_array[rti]; fdwroutine = resultRel->fdwroutine; + + /* + * MERGE is not currently supported for foreign tables and we + * already checked when the table mentioned in the query is + * foreign; but we can still get here if a partitioned table has a + * foreign table as partition. Disallow that now, to avoid an + * uglier error message later. + */ + if (operation == CMD_MERGE && fdwroutine != NULL) + { + RangeTblEntry *rte = root->simple_rte_array[rti]; + + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot execute MERGE on relation \"%s\"", + get_rel_name(rte->relid)), + errdetail_relkind_not_supported(rte->relkind)); + } + } else { RangeTblEntry *rte = planner_rt_fetch(rti, root); Assert(rte->rtekind == RTE_RELATION); + Assert(operation != CMD_MERGE); if (rte->relkind == RELKIND_FOREIGN_TABLE) fdwroutine = GetFdwRoutineByRelId(rte->relid); else