MERGE fails to reject source references in NOT MATCHED BY SOURCE actions
MERGE fails to reject source references in NOT MATCHED BY SOURCE actions
Hi,
I noticed that MERGE allows source references in NOT MATCHED BY SOURCE
actions when the source is an unaliased join. For example:
postgres=# CREATE TABLE t (id int, balance int);
CREATE TABLE s (id int, delta int);
CREATE TABLE
CREATE TABLE
postgres=# MERGE INTO t
USING s CROSS JOIN (SELECT 1 AS k) j ON t.id = s.id
WHEN MATCHED THEN UPDATE SET balance = 1
WHEN NOT MATCHED BY SOURCE THEN UPDATE SET balance = s.delta;
MERGE 0
This should reject s.delta, as it does when USING contains only s.
Source references in WHEN conditions are also incorrectly accepted.
postgres=# MERGE INTO t
USING s ON t.id = s.id
WHEN MATCHED THEN UPDATE SET balance = 1
WHEN NOT MATCHED BY SOURCE THEN UPDATE SET balance = s.delta;
ERROR: invalid reference to FROM-clause entry for table "s"
LINE 4: ...HEN NOT MATCHED BY SOURCE THEN UPDATE SET balance = s.delta;
^
DETAIL: There is an entry for table "s", but it cannot be referenced
from this part of the query.
setNamespaceForMergeWhen() hides only the top-level source RTE. For an
unaliased join, its input relations remain visible by name.
The attached patch temporarily selects the appropriate namespace for
each action and restores it afterward, following transformJoinOnClause().
It preserves the source visibility flags established by
transformFromClause(). Regression tests are included.
Thoughts?
--
Thanks,
Tender Wang
Re: MERGE fails to reject source references in NOT MATCHED BY SOURCE actions
От:
Joao Rito <joaovictor.rito@gmail.com>
Дата:
Hi Tender,
Thanks for the patch. I reviewed
0001-Fix-source-visibility-in-MERGE-NOT-MATCHED-BY-SOURCE.patch.
Setup: master at debfb3c8a8, and the same commit with the patch applied
(the only version on the CF page, 2026-11-05). The patch applied
cleanly. Both were built with --without-icu, and the build showed no
warnings.
What I tested
1. Your report case (unaliased join, "WHEN NOT MATCHED BY SOURCE THEN
UPDATE SET balance = s.delta"): master returns "MERGE 2". The patched
build returns "invalid reference to FROM-clause entry for table "s""
with the usual DETAIL. The same happens for the WHEN condition ("AND
s.delta > 0", "AND s2.k > 0"), for both inputs of a CROSS JOIN, for a
subquery that references the source ("(SELECT s.delta)"), for a
nested join (s JOIN (s2 JOIN s3 ON true) ON true), and for a
partitioned target. On master all of these were accepted.
2. Valid cases: target-only references in NOT MATCHED BY SOURCE
(qualified, unqualified, with a target alias, in a scalar subquery,
in a DELETE action), MATCHED actions that use both source and target,
NOT MATCHED INSERT that uses join inputs, a NOT MATCHED BY SOURCE
action placed before the other actions, and RETURNING that references
the source. For these statements the output is the same on master and
on the patched build. NOT MATCHED INSERT still cannot see the target,
on both.
3. Regression tests: "make check" failed for me in the temp-install
step (initdb-template). I did not find the cause. I was building on a
Windows-mounted filesystem under WSL, so I suspect my setup and not
the patch. So I ran "make installcheck" against a separately
initialized cluster with the patched build instead: 243/243 tests
passed, including merge with your new cases.
Review comments
a) Behavior difference you may want to mention. For an unqualified
source column in a NOT MATCHED BY SOURCE action, master prints 'HINT:
To reference that column, you must use a table-qualified name.' The
patched build no longer prints it (for example "UPDATE SET balance =
delta" on a join). I think dropping it is right, because a qualified
name does not work there either, so the hint was misleading. The
error text and DETAIL are the same.
b) The patch deletes the three Assert()s on commandType that were in
setNamespaceForMergeWhen() (for example, NOT MATCHED BY SOURCE must
be UPDATE, DELETE or DO NOTHING). Maybe keep them in the new switch,
or mention in the commit message that they are dropped on purpose.
c) parse_merge.c now ends with an extra blank line after the closing
brace of transformMergeStmt(), left over from removing
setNamespaceVisibilityForRTE(). pgindent or git diff --check would
probably point at it.
d) In the new tests, the first two failing statements have "EXPLAIN
(COSTS OFF)" in front. It is not needed, since the statements fail
during parse analysis. Plain MERGE statements would be simpler.
Overall the patch looks good to me and fixes the problem as
described. The simplification (no flag mutation, a temporary
namespace per action, restored afterwards) is easier to read than the
old code.
What I did not test: other platforms, the docs (I did not look for text
that needs updating), and the TAP tests.
Regards,
Joao Rito
Re: MERGE fails to reject source references in NOT MATCHED BY SOURCE actions
Hi, Joao Rito 于2026年11月19日周四 03:47写道: > > c) parse_merge.c now ends with an extra blank line after the closing > brace of transformMergeStmt(), left over from removing > setNamespaceVisibilityForRTE(). pgindent or git diff --check would > probably point at it. > Done. > Overall the patch looks good to me and fixes the problem as > described. The simplification (no flag mutation, a temporary > namespace per action, restored afterwards) is easier to read than the > old code. Thanks for the review. I've cc'd Dean since this issue dates back to the introduction of WHEN NOT MATCHED BY SOURCE. Dean, could you take a look at the namespace handling in v2 when you have a chance? -- Tender Wang
Re: MERGE fails to reject source references in NOT MATCHED BY SOURCE actions
От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Sat, 21 Nov 2026 at 02:42, Tender Wang wrote: > > I've cc'd Dean since this issue dates back to the introduction of WHEN > NOT MATCHED BY SOURCE. > Dean, could you take a look at the namespace handling in v2 when you > have a chance? I agree that it's wrong for NOT MATCHED BY SOURCE actions to allow access to source columns under an unaliased join. That was certainly an oversight. It was intended to be disallowed, for consistency with SQL-standard NOT MATCHED [BY TARGET] actions. OTOH, it also seems pretty harmless. Fixing it risks breaking existing queries that currently work (the Vars just return NULL, and the result is arguably correct). So the fix would need to be flagged in the release notes as a breaking change. Perhaps we should just fix it in master? Regards, Dean
Re: MERGE fails to reject source references in NOT MATCHED BY SOURCE actions
BTW, while looking at this, I noticed that the docs for the "condition" expression that can appear after MERGE ... WHEN ... AND currently say: A condition on a WHEN MATCHED clause can refer to columns in both the source and the target relations. A condition on a WHEN NOT MATCHED BY SOURCE clause can only refer to columns from the target relation, since by definition there is no matching source row. A condition on a WHEN NOT MATCHED [BY TARGET] clause can only refer to columns from the source relation, since by definition there is no matching target row. Only the system attributes from the target table are accessible. That last sentence has existed since MERGE was added and it seems wrong to me. In fact, I find that sentence hard to interpret -- there are multiple possible interpretations, none of which seem to be true. It could mean that, when accessing the target relation, the condition can only refer to system attributes, not user-defined attributes, which certainly isn't true. Or it could mean that system attributes from the target table are accessible, but system attributes from the source are not. That may have been true at some point during the development of the MERGE feature, but it's not true now -- if the source is a base table, its system attributes are accessible. Or it could be trying to document an exception to the previous sentence -- a condition on a WHEN NOT MATCHED clause can't refer to attributes of the target row, since there is no target row, but an exception is made for system attributes. But that isn't true either -- there is no such exception. So I propose simply deleting that sentence. Regards, Dean
Re: MERGE fails to reject source references in NOT MATCHED BY SOURCE actions
От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
On 2026-Nov-23, Dean Rasheed wrote: > OTOH, it also seems pretty harmless. Fixing it risks breaking existing > queries that currently work (the Vars just return NULL, and the result > is arguably correct). So the fix would need to be flagged in the > release notes as a breaking change. > > Perhaps we should just fix it in master? Agreed on a master-only change. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ "I'm impressed how quickly you are fixing this obscure issue. I came from MS SQL and it would be hard for me to put into words how much of a better job you all are doing on [PostgreSQL]." Steve Midgley, http://archives.postgresql.org/pgsql-sql/2008-08/msg00000.php
Re: MERGE fails to reject source references in NOT MATCHED BY SOURCE actions
От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
On 2026-Nov-23, Dean Rasheed wrote:
> BTW, while looking at this, I noticed that the docs for the
> "condition" expression that can appear after MERGE ... WHEN ... AND
> currently say:
>
> A condition on a WHEN MATCHED clause can refer to columns in both the
> source and the target relations. A condition on a WHEN NOT MATCHED BY
> SOURCE clause can only refer to columns from the target relation, since
> by definition there is no matching source row. A condition on a WHEN
> NOT MATCHED [BY TARGET] clause can only refer to columns from the source
> relation, since by definition there is no matching target row. Only the
> system attributes from the target table are accessible.
>
> That last sentence has existed since MERGE was added and it seems wrong to me.
>
> In fact, I find that sentence hard to interpret -- there are multiple
> possible interpretations, none of which seem to be true.
Yeah ...
> Or it could mean that system attributes from the target table are
> accessible, but system attributes from the source are not. That may
> have been true at some point during the development of the MERGE
> feature, but it's not true now -- if the source is a base table, its
> system attributes are accessible.
I think this is the correct interpretation.
> So I propose simply deleting that sentence.
+1, please do.
--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/
"I can see support will not be a problem. 10 out of 10." (Simon Wittber)
(http://archives.postgresql.org/pgsql-general/2004-12/msg00159.php)