Re: Fix replica identity checks for MERGE command on published table.
Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
From: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
To: "Zhijie Hou (Fujitsu)" <houzj.fnst@fujitsu.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2025-04-14T04:39:57Z
Lists: pgsql-hackers
On Fri, Apr 11, 2025 at 10:54 AM Zhijie Hou (Fujitsu) <houzj.fnst@fujitsu.com> wrote: > > Hi, > > While testing publication DDLs, I noticed an unexpected behavior where the > MERGE command can be executed on tables lacking replica identity keys, > regardless of whether they are part of a publication that publishes updates and > deletes. > > Replication and application of the updates and deletes generated by MERGE > command require replica identity keys in the WAL record, which are essential > for the apply worker on the subscriber to find local tuples for updating or > deletion. Furthermore, publications require specific columns to be part of the > replica identity key if the table specifies a publication row filter or column > list. > > We already have restrictions on executing UPDATE and DELETE commands for tables > without replica identity keys under similar conditions. So, I think the same > restriction should apply to the MERGE command as well. > +-- fail - missing REPLICA IDENTITY +MERGE INTO testpub_merge_no_ri USING testpub_merge_pk s ON s.a >= 1 + WHEN MATCHED THEN UPDATE SET b = s.b; +ERROR: cannot update table "testpub_merge_no_ri" because it does not have a replica identity and publishes updates +HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE. +-- fail - missing REPLICA IDENTITY +MERGE INTO testpub_merge_no_ri USING testpub_merge_pk s ON s.a >= 1 + WHEN MATCHED THEN DELETE; +ERROR: cannot delete from table "testpub_merge_no_ri" because it does not have a replica identity and publishes deletes +HINT: To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE. I was wondering whether we should mention MERGE somewhere in the error message like "cannot merge into table ...". But the error message is reported depending upon the actual operation being performed and whether it's being published by the publication, so mentioning specific operations is better than mentioning just MERGE. So I think the current error message is ok; and it will help point out the operations that caused it. But that opens up another question: some merge operations (on the same table) will go through and some won't if the publication publishes only some of the operations. I am wondering, albeit quite late after the feature has sailed, whether MERGE should be considered a separate operation as far as publication is concerned. This topic may have been discussed either when MERGE was implemented or when operation filters were implemented. Sorry for the noise in that case. This isn't a detailed review. -- Best Wishes, Ashutosh Bapat
Commits
-
Fix replica identity check for MERGE.
- 5481cc332b0f 15.15 landed
- 421d7a157985 16.11 landed
- 76f45be9315b 17.7 landed
- 311340f17863 18.0 landed
- fc6600fc1cd1 19 (unreleased) landed
-
Fix replica identity check for INSERT ON CONFLICT DO UPDATE.
- 0610b843c971 13.23 landed
- 5fd569ef2d62 14.20 landed
- 451b22efd9ac 15.15 landed
- 0c4d5a45dbd7 16.11 landed
- 0b934d3994ff 17.7 landed
- 344662848ac1 18.0 landed
- 5386bfb9c1f5 19 (unreleased) landed