diff options
| author | Dean Rasheed | 2024-10-03 11:50:38 +0000 |
|---|---|---|
| committer | Dean Rasheed | 2024-10-03 11:50:38 +0000 |
| commit | d7d297f8449641bfd71750d04c302572a350052c (patch) | |
| tree | 964be585baaf53d0d9faaccf53b739727392ce33 /src/test | |
| parent | c7a201053e3f7d322782e57879b43d1fd8b7bf32 (diff) | |
Fix incorrect non-strict join recheck in MERGE WHEN NOT MATCHED BY SOURCE.
If a MERGE command contains WHEN NOT MATCHED BY SOURCE actions, the
merge join condition is used by the executor to distinguish MATCHED
from NOT MATCHED BY SOURCE cases. However, this qual is executed using
the output from the join subplan node, which nulls the output from the
source relation in the not matched case, and so the result may be
incorrect if the join condition is "non-strict" -- for example,
something like "src.col IS NOT DISTINCT FROM tgt.col".
Fix this by enhancing the join recheck condition with an additional
"src IS NOT NULL" check, so that it does the right thing when
evaluated using the output from the join subplan.
Noted by Tom Lane while investigating bug #18634 from Alexander
Lakhin.
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 | 22 | ||||
| -rw-r--r-- | src/test/regress/sql/merge.sql | 18 |
2 files changed, 40 insertions, 0 deletions
diff --git a/src/test/regress/expected/merge.out b/src/test/regress/expected/merge.out index 3d33259e8ff..0e59bae1a7f 100644 --- a/src/test/regress/expected/merge.out +++ b/src/test/regress/expected/merge.out @@ -2689,6 +2689,28 @@ DETAIL: drop cascades to table measurement_y2006m02 drop cascades to table measurement_y2006m03 drop cascades to table measurement_y2007m01 DROP FUNCTION measurement_insert_trigger(); +-- +-- test non-strict join clause +-- +CREATE TABLE src (a int, b text); +INSERT INTO src VALUES (1, 'src row'); +CREATE TABLE tgt (a int, b text); +INSERT INTO tgt VALUES (NULL, 'tgt row'); +MERGE INTO tgt USING src ON tgt.a IS NOT DISTINCT FROM src.a + WHEN MATCHED THEN UPDATE SET a = src.a, b = src.b + WHEN NOT MATCHED BY SOURCE THEN DELETE + RETURNING merge_action(), src.*, tgt.*; + merge_action | a | b | a | b +--------------+---+---+---+--------- + DELETE | | | | tgt row +(1 row) + +SELECT * FROM tgt; + a | b +---+--- +(0 rows) + +DROP TABLE src, tgt; -- 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 92163ec9fe0..2a7753c65b0 100644 --- a/src/test/regress/sql/merge.sql +++ b/src/test/regress/sql/merge.sql @@ -1710,6 +1710,24 @@ SELECT * FROM new_measurement ORDER BY city_id, logdate; DROP TABLE measurement, new_measurement CASCADE; DROP FUNCTION measurement_insert_trigger(); +-- +-- test non-strict join clause +-- +CREATE TABLE src (a int, b text); +INSERT INTO src VALUES (1, 'src row'); + +CREATE TABLE tgt (a int, b text); +INSERT INTO tgt VALUES (NULL, 'tgt row'); + +MERGE INTO tgt USING src ON tgt.a IS NOT DISTINCT FROM src.a + WHEN MATCHED THEN UPDATE SET a = src.a, b = src.b + WHEN NOT MATCHED BY SOURCE THEN DELETE + RETURNING merge_action(), src.*, tgt.*; + +SELECT * FROM tgt; + +DROP TABLE src, tgt; + -- prepare RESET SESSION AUTHORIZATION; |
