postgres.git / summary / log / commit / refs

commit    7fffb114377507fca5e8c2b4db1820a4ec11615b
Author:   Dean Rasheed <dean.a.rasheed@gmail.com>
Date:     Thu Nov 12 21:20:06 2026 +0000
First released in: 19.0

    Fix MERGE into inheritance parent with extra child columns.
    
    When a MERGE with INSERT actions targets the parent of an inheritance
    tree, and the parent itself is excluded by constraint exclusion, and
    the first child table has extra columns, the command would fail during
    initialization, while attempting to convert the child row type to the
    parent.
    
    Fix in ExecInitMerge() by calling build_attrmap_by_name() with
    missing_ok = true to build the child-to-parent attribute map --- that
    code (added by commit b006bcd5310) deals exclusively with plain
    inheritance, not partitioning, so the child table may have extra
    columns, which can be safely ignored, since they cannot be referenced
    in the main query.
    
    Backpatch to v15, where MERGE was introduced.
    
    Reported-by: Tender Wang <tndrwang@gmail.com>
    Author: Tender Wang <tndrwang@gmail.com>
    Reviewed-by: Keyerror Smart <smartkeyerror@gmail.com>
    Discussion: https://postgr.es/m/CAHewXNm-SQqJ6UeT2sTb4q-CUVyA3NN7tKgcSmPRjYFoZQvdGA%40mail.gmail.com
    Backpatch-through: 15


src/backend/executor/nodeModifyTable.c | 12 +++++++++--- src/test/regress/expected/merge.out | 34 ++++++++++++++++++++++++++++++++++ src/test/regress/sql/merge.sql | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index cb73cc61821..966094b6a32 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -4118,7 +4118,11 @@ ExecInitMerge(ModifyTableState *mtstate, EState *estate) * Note that the planner does not build a withCheckOptionList or * returningList for the root relation, but as in ExecInitPartitionInfo, * we can use the first resultRelInfo entry as a reference to calculate - * the attno's for the root table. + * the attno's for the root table. Since the resultRelInfo entry is an + * inheritance child, not a partition, it may have columns that are not + * present in the root relation. We can safely ignore such columns when + * building the attribute map, since they cannot be referenced by a query + * that targets the root relation. */ if (rootRelInfo != mtstate->resultRelInfo && rootRelInfo->ri_RelationDesc->rd_rel->relkind != RELKIND_PARTITIONED_TABLE && @@ -4152,7 +4156,8 @@ ExecInitMerge(ModifyTableState *mtstate, EState *estate) part_attmap = build_attrmap_by_name(RelationGetDescr(rootRelation), RelationGetDescr(firstResultRel), - false); + true); /* ignore extra child + * columns */ wcoList = (List *) map_variable_attnos((Node *) wcoList, @@ -4196,7 +4201,8 @@ ExecInitMerge(ModifyTableState *mtstate, EState *estate) part_attmap = build_attrmap_by_name(RelationGetDescr(rootRelation), RelationGetDescr(firstResultRel), - false); + true); /* ignore extra child + * columns */ returningList = (List *) map_variable_attnos((Node *) returningList, diff --git a/src/test/regress/expected/merge.out b/src/test/regress/expected/merge.out index 5e2873fa0bd..905c3fe8cb1 100644 --- a/src/test/regress/expected/merge.out +++ b/src/test/regress/expected/merge.out @@ -2788,6 +2788,40 @@ RETURNING merge_action(), m.*; INSERT | 0 | 01-18-2007 | 25 | 200 (1 row) +-- Test WCO/RETURNING list initialization from child with extra columns +ALTER TABLE measurement_y2007m01 ADD COLUMN extra integer; +BEGIN; +SET LOCAL constraint_exclusion = on; +MERGE INTO measurement m + USING (VALUES (1, '2007-01-19'::date)) nm(city_id, logdate) ON + (m.city_id = nm.city_id AND m.logdate = nm.logdate) +WHEN NOT MATCHED THEN INSERT + (city_id, logdate, peaktemp, unitsales) + VALUES (0, nm.logdate, 25, 100); -- ok +MERGE INTO measurement m + USING (VALUES (1, '2007-01-19'::date)) nm(city_id, logdate) ON + (m.city_id = nm.city_id AND m.logdate = nm.logdate) +WHEN NOT MATCHED THEN INSERT + (city_id, logdate, peaktemp, unitsales) + VALUES (0, nm.logdate, NULL, 100); -- should fail +ERROR: new row violates row-level security policy for table "measurement" +ROLLBACK; +BEGIN; +SET LOCAL constraint_exclusion = on; +ALTER TABLE measurement DISABLE ROW LEVEL SECURITY; +MERGE INTO measurement m + USING (VALUES (1, '2007-01-19'::date)) nm(city_id, logdate) ON + (m.city_id = nm.city_id AND m.logdate = nm.logdate) +WHEN NOT MATCHED THEN INSERT + (city_id, logdate, peaktemp, unitsales) + VALUES (0, nm.logdate, 25, 100) +RETURNING m.city_id, m.peaktemp, m; + city_id | peaktemp | m +---------+----------+----------------------- + 0 | 25 | (0,01-19-2007,25,100) +(1 row) + +ROLLBACK; DROP TABLE measurement, new_measurement CASCADE; NOTICE: drop cascades to 3 other objects DETAIL: drop cascades to table measurement_y2006m02 diff --git a/src/test/regress/sql/merge.sql b/src/test/regress/sql/merge.sql index 44cb01439a2..37aef4de68a 100644 --- a/src/test/regress/sql/merge.sql +++ b/src/test/regress/sql/merge.sql @@ -1782,6 +1782,38 @@ WHEN NOT MATCHED THEN INSERT VALUES (city_id - 1, logdate, 25, 200) RETURNING merge_action(), m.*; +-- Test WCO/RETURNING list initialization from child with extra columns +ALTER TABLE measurement_y2007m01 ADD COLUMN extra integer; + +BEGIN; +SET LOCAL constraint_exclusion = on; +MERGE INTO measurement m + USING (VALUES (1, '2007-01-19'::date)) nm(city_id, logdate) ON + (m.city_id = nm.city_id AND m.logdate = nm.logdate) +WHEN NOT MATCHED THEN INSERT + (city_id, logdate, peaktemp, unitsales) + VALUES (0, nm.logdate, 25, 100); -- ok + +MERGE INTO measurement m + USING (VALUES (1, '2007-01-19'::date)) nm(city_id, logdate) ON + (m.city_id = nm.city_id AND m.logdate = nm.logdate) +WHEN NOT MATCHED THEN INSERT + (city_id, logdate, peaktemp, unitsales) + VALUES (0, nm.logdate, NULL, 100); -- should fail +ROLLBACK; + +BEGIN; +SET LOCAL constraint_exclusion = on; +ALTER TABLE measurement DISABLE ROW LEVEL SECURITY; +MERGE INTO measurement m + USING (VALUES (1, '2007-01-19'::date)) nm(city_id, logdate) ON + (m.city_id = nm.city_id AND m.logdate = nm.logdate) +WHEN NOT MATCHED THEN INSERT + (city_id, logdate, peaktemp, unitsales) + VALUES (0, nm.logdate, 25, 100) +RETURNING m.city_id, m.peaktemp, m; +ROLLBACK; + DROP TABLE measurement, new_measurement CASCADE; DROP FUNCTION measurement_insert_trigger(); [parent: eacabd7d031c]