Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a790a11

Browse files
Zhijie HouCommitfest Bot
Zhijie Hou
authored and
Commitfest Bot
committed
Fix replica identity check for MERGE command
This commit enforces proper checks for MERGE command to determine if it can be executed with current replica identity. It ensures that each conditional MERGE action (insert, update, delete) undergoes the same check as individual DML commands.
1 parent b006bcd commit a790a11

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/backend/executor/execMain.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,11 @@ CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation,
10591059
{
10601060
case RELKIND_RELATION:
10611061
case RELKIND_PARTITIONED_TABLE:
1062-
CheckCmdReplicaIdentity(resultRel, operation);
1062+
if (operation == CMD_MERGE)
1063+
foreach_node(MergeAction, action, mergeActions)
1064+
CheckCmdReplicaIdentity(resultRel, action->commandType);
1065+
else
1066+
CheckCmdReplicaIdentity(resultRel, operation);
10631067
break;
10641068
case RELKIND_SEQUENCE:
10651069
ereport(ERROR,

src/test/regress/expected/publication.out

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,32 @@ DROP PUBLICATION pub1;
19241924
DROP PUBLICATION pub2;
19251925
DROP TABLE gencols;
19261926
RESET client_min_messages;
1927+
-- Test that the MERGE command correctly checks REPLICA IDENTITY when the
1928+
-- target table is published.
1929+
CREATE TABLE testpub_merge_no_ri (a int, b int);
1930+
CREATE TABLE testpub_merge_pk (a int primary key, b int);
1931+
SET client_min_messages = 'ERROR';
1932+
CREATE PUBLICATION pub1 FOR ALL TABLES;
1933+
RESET client_min_messages;
1934+
-- fail - missing REPLICA IDENTITY
1935+
MERGE INTO testpub_merge_no_ri USING testpub_merge_pk s ON s.a >= 1
1936+
WHEN MATCHED THEN UPDATE SET b = s.b;
1937+
ERROR: cannot update table "testpub_merge_no_ri" because it does not have a replica identity and publishes updates
1938+
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.
1939+
-- fail - missing REPLICA IDENTITY
1940+
MERGE INTO testpub_merge_no_ri USING testpub_merge_pk s ON s.a >= 1
1941+
WHEN MATCHED THEN DELETE;
1942+
ERROR: cannot delete from table "testpub_merge_no_ri" because it does not have a replica identity and publishes deletes
1943+
HINT: To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE.
1944+
-- ok - inserts are not restricted
1945+
MERGE INTO testpub_merge_no_ri USING testpub_merge_pk s ON s.a >= 1
1946+
WHEN NOT MATCHED THEN INSERT (a, b) VALUES (0, 0);
1947+
-- ok - REPLICA IDENTITY is DEFAULT and table has a PK
1948+
MERGE INTO testpub_merge_pk USING testpub_merge_no_ri s ON s.a >= 1
1949+
WHEN MATCHED THEN UPDATE SET b = s.b;
1950+
DROP PUBLICATION pub1;
1951+
DROP TABLE testpub_merge_no_ri;
1952+
DROP TABLE testpub_merge_pk;
19271953
RESET SESSION AUTHORIZATION;
19281954
DROP ROLE regress_publication_user, regress_publication_user2;
19291955
DROP ROLE regress_publication_user_dummy;

src/test/regress/sql/publication.sql

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,36 @@ DROP PUBLICATION pub2;
12231223
DROP TABLE gencols;
12241224

12251225
RESET client_min_messages;
1226+
1227+
-- Test that the MERGE command correctly checks REPLICA IDENTITY when the
1228+
-- target table is published.
1229+
CREATE TABLE testpub_merge_no_ri (a int, b int);
1230+
CREATE TABLE testpub_merge_pk (a int primary key, b int);
1231+
1232+
SET client_min_messages = 'ERROR';
1233+
CREATE PUBLICATION pub1 FOR ALL TABLES;
1234+
RESET client_min_messages;
1235+
1236+
-- fail - missing REPLICA IDENTITY
1237+
MERGE INTO testpub_merge_no_ri USING testpub_merge_pk s ON s.a >= 1
1238+
WHEN MATCHED THEN UPDATE SET b = s.b;
1239+
1240+
-- fail - missing REPLICA IDENTITY
1241+
MERGE INTO testpub_merge_no_ri USING testpub_merge_pk s ON s.a >= 1
1242+
WHEN MATCHED THEN DELETE;
1243+
1244+
-- ok - inserts are not restricted
1245+
MERGE INTO testpub_merge_no_ri USING testpub_merge_pk s ON s.a >= 1
1246+
WHEN NOT MATCHED THEN INSERT (a, b) VALUES (0, 0);
1247+
1248+
-- ok - REPLICA IDENTITY is DEFAULT and table has a PK
1249+
MERGE INTO testpub_merge_pk USING testpub_merge_no_ri s ON s.a >= 1
1250+
WHEN MATCHED THEN UPDATE SET b = s.b;
1251+
1252+
DROP PUBLICATION pub1;
1253+
DROP TABLE testpub_merge_no_ri;
1254+
DROP TABLE testpub_merge_pk;
1255+
12261256
RESET SESSION AUTHORIZATION;
12271257
DROP ROLE regress_publication_user, regress_publication_user2;
12281258
DROP ROLE regress_publication_user_dummy;

0 commit comments

Comments
 (0)