MERGE fails with RETURNING when an excluded inheritance parent has an extra-column child

Started by Tender Wang19 days ago3 messageshackers
Jump to latest
#1Tender Wang
tndrwang@gmail.com

Hi,

I found that MERGE can fail when the target is an inheritance parent
and a child has additional columns:

postgres=# CREATE TABLE mp2(a int, b text);
CREATE TABLE
postgres=# CREATE TABLE mc2(extra int) INHERITS (mp2);
CREATE TABLE
postgres=# ALTER TABLE mp2 ADD CONSTRAINT mpc2 CHECK (a < 0) NO INHERIT;
ALTER TABLE
postgres=# set constraint_exclusion = on;
SET
postgres=# MERGE INTO mp2 t USING (VALUES (7, -5)) s(k, ins) ON t.a =
s.k AND t.a = 7
WHEN NOT MATCHED THEN INSERT VALUES (s.ins, 'x') RETURNING t.a, t.b;
ERROR: could not convert row type
DETAIL: Attribute "extra" of type mc2 does not exist in type mp2.

The parent's scan is excluded because its CHECK constraint contradicts
the join condition. However, the INSERT still targets the parent, and
the inserted value satisfies its constraint.

ExecInitMerge() uses the first surviving result relation's RETURNING
list to initialize the root relation's projection. The attribute map
is built with missing_ok = false, which rejects child-only columns
even though the expressions originated from the parent and cannot
reference them. The WITH CHECK OPTION code has the same issue.

The attached patch passes missing_ok = true in both places and adds
regression tests for RETURNING and row-level security.

This failure occurs in the code added by b006bcd5310 (Fix MERGE into a
plain inheritance parent table), so I've CC'd Dean.

--
Thanks,
Tender Wang

Attachments:

t254568_1
0001-Fix-MERGE-into-inheritance-parents-with-extra-column.patchtext/plain; charset=US-ASCII; name=0001-Fix-MERGE-into-inheritance-parents-with-extra-column.patchDownload+75-4
#2Keyerror Smart
smartkeyerror@gmail.com
In reply to: Tender Wang (#1)
Re: MERGE fails with RETURNING when an excluded inheritance parent has an extra-column child

Hi Tender Wang,

I can reproduce this on master, and the fix looks right to me.

With `missing_ok = true`, a child-only column just gets a 0 in the
map, and if an expression ever referenced it, `map_variable_attnos()`
would raise "unexpected varattno" instead of mis-mapping it.
ExecGetRootToChildMap() also passes missing_ok = true for
non-partition children, for the same reason:

/*
* When this child table is not a partition (!relispartition), it may
* have columns that are not present in the root table, which we ask
* to ignore by passing true for missing_ok.
*/

I also tried whole-row RETURNING (t, t.*, old/new), RLS as a
non-superuser, and a child with a dropped column. All work fine
with the patch.

Zhenglong Li,
Regards

#3Dean Rasheed
dean.a.rasheed@gmail.com
In reply to: Keyerror Smart (#2)
Re: MERGE fails with RETURNING when an excluded inheritance parent has an extra-column child

On Mon, 9 Nov 2026 at 13:19, Keyerror Smart <smartkeyerror@gmail.com> wrote:

I can reproduce this on master, and the fix looks right to me.

Agreed. Thanks for the report and patch.

Pushed and back-patched to v15.

Regards,
Dean