diff options
| author | Dean Rasheed | 2024-10-03 12:48:32 +0000 |
|---|---|---|
| committer | Dean Rasheed | 2024-10-03 12:48:32 +0000 |
| commit | 259a0a99fe3d45dcf624788c1724d9989f3382dc (patch) | |
| tree | 7991c70de01489ec8148a0d896ad5f0ad5a0b94c /src/test | |
| parent | dddb5640c6d32f3d48994dac0f4e80d0f4567262 (diff) | |
Fix wrong varnullingrels error for MERGE WHEN NOT MATCHED BY SOURCE.
If a MERGE command contains WHEN NOT MATCHED BY SOURCE actions, the
source relation appears on the outer side of the join. Thus, any Vars
referring to the source in the merge join condition, actions, and
RETURNING list should be marked as nullable by the join, since they
are used in the ModifyTable node above the join. Note that this only
applies to the copy of join condition used in the executor to
distinguish MATCHED from NOT MATCHED BY SOURCE cases. Vars in the
original join condition, inside the join node itself, should not be
marked.
Failure to correctly mark these Vars led to a "wrong varnullingrels"
error in the final stage of query planning, in some circumstances. We
happened to get away without this in all previous tests, since they
all involved a ModifyTable node directly on top of the join node, so
that the top plan targetlist coincided with the output of the join,
and the varnullingrels check was more lax. However, if another plan
node, such as a one-time filter Result node, gets inserted between the
ModifyTable node and the join node, then a stricter check is applied,
which fails.
Per bug #18634 from Alexander Lakhin. Thanks to Tom Lane and Richard
Guo for review and analysis.
Back-patch to v17, where WHEN NOT MATCHED BY SOURCE support was added
to MERGE.
Discussion: https://postgr.es/m/18634-db5299c937877f2b%40postgresql.org
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/merge.out | 29 | ||||
| -rw-r--r-- | src/test/regress/sql/merge.sql | 21 |
2 files changed, 50 insertions, 0 deletions
diff --git a/src/test/regress/expected/merge.out b/src/test/regress/expected/merge.out index 0e59bae1a7f..c236f155c08 100644 --- a/src/test/regress/expected/merge.out +++ b/src/test/regress/expected/merge.out @@ -2711,6 +2711,35 @@ SELECT * FROM tgt; (0 rows) DROP TABLE src, tgt; + +-- +-- test for bug #18634 (wrong varnullingrels error) +-- +CREATE TABLE bug18634t (a int, b int, c text); +INSERT INTO bug18634t VALUES(1, 10, 'tgt1'), (2, 20, 'tgt2'); +CREATE VIEW bug18634v AS + SELECT * FROM bug18634t WHERE EXISTS (SELECT 1 FROM bug18634t); +CREATE TABLE bug18634s (a int, b int, c text); +INSERT INTO bug18634s VALUES (1, 2, 'src1'); +MERGE INTO bug18634v t USING bug18634s s ON s.a = t.a + WHEN MATCHED THEN UPDATE SET b = s.b + WHEN NOT MATCHED BY SOURCE THEN DELETE + RETURNING merge_action(), s.c, t.*; + merge_action | c | a | b | c +--------------+------+---+----+------ + UPDATE | src1 | 1 | 2 | tgt1 + DELETE | | 2 | 20 | tgt2 +(2 rows) + +SELECT * FROM bug18634t; + a | b | c +---+---+------ + 1 | 2 | tgt1 +(1 row) + +DROP TABLE bug18634t CASCADE; +NOTICE: drop cascades to view bug18634v +DROP TABLE bug18634s; -- prepare RESET SESSION AUTHORIZATION; -- try a system catalog diff --git a/src/test/regress/sql/merge.sql b/src/test/regress/sql/merge.sql index 2a7753c65b0..ce9981d70b1 100644 --- a/src/test/regress/sql/merge.sql +++ b/src/test/regress/sql/merge.sql @@ -1727,6 +1727,27 @@ MERGE INTO tgt USING src ON tgt.a IS NOT DISTINCT FROM src.a SELECT * FROM tgt; DROP TABLE src, tgt; + +-- +-- test for bug #18634 (wrong varnullingrels error) +-- +CREATE TABLE bug18634t (a int, b int, c text); +INSERT INTO bug18634t VALUES(1, 10, 'tgt1'), (2, 20, 'tgt2'); +CREATE VIEW bug18634v AS + SELECT * FROM bug18634t WHERE EXISTS (SELECT 1 FROM bug18634t); + +CREATE TABLE bug18634s (a int, b int, c text); +INSERT INTO bug18634s VALUES (1, 2, 'src1'); + +MERGE INTO bug18634v t USING bug18634s s ON s.a = t.a + WHEN MATCHED THEN UPDATE SET b = s.b + WHEN NOT MATCHED BY SOURCE THEN DELETE + RETURNING merge_action(), s.c, t.*; + +SELECT * FROM bug18634t; + +DROP TABLE bug18634t CASCADE; +DROP TABLE bug18634s; -- prepare |
