Fix replica identity check for a partitioned table.
authorAmit Kapila
Tue, 16 Aug 2022 09:00:27 +0000 (14:30 +0530)
committerAmit Kapila
Tue, 16 Aug 2022 09:00:27 +0000 (14:30 +0530)
The current publisher code checks if UPDATE or DELETE can be executed with
the replica identity of the table even if it's a partitioned table. We can
skip checking the replica identity for partitioned tables because the
operations are actually performed on the leaf partitions (not the
partitioned table).

Reported-by: Brad Nicholson
Author: Hou Zhijie
Reviewed-by: Peter Smith, Amit Kapila
Backpatch-through: 13
Discussion: https://postgr.es/m/CAMMnM%3D8i5DohH%3DYKzV0_wYuYSYvuOJoL9F5nzXTc%2ByzsG1f6rg%40mail.gmail.com

src/backend/executor/execReplication.c
src/test/regress/expected/publication.out
src/test/regress/sql/publication.sql

index 8f474faed066a46b88c1275a4a07db34327c9712..16cab1db725b9c62619648d286ccd7d68e6dd1c1 100644 (file)
@@ -565,6 +565,13 @@ CheckCmdReplicaIdentity(Relation rel, CmdType cmd)
 {
    PublicationActions *pubactions;
 
+   /*
+    * Skip checking the replica identity for partitioned tables, because the
+    * operations are actually performed on the leaf partitions.
+    */
+   if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+       return;
+
    /* We only need to do checks for UPDATE and DELETE. */
    if (cmd != CMD_UPDATE && cmd != CMD_DELETE)
        return;
index 7c7e0226658ea7c3321b08a0d3505084df954814..b7ce080424f8f71e1a7eab67a03313a56cc2d93c 100644 (file)
@@ -140,6 +140,8 @@ ALTER PUBLICATION testpub_forparted ADD TABLE testpub_parted;
 Tables:
     "public.testpub_parted"
 
+-- works despite missing REPLICA IDENTITY, because no actual update happened
+UPDATE testpub_parted SET a = 1 WHERE false;
 -- should now fail, because parent's publication replicates updates
 UPDATE testpub_parted1 SET a = 1;
 ERROR:  cannot update table "testpub_parted1" because it does not have a replica identity and publishes updates
index 4b7738395ec577c4fab9dec2c13e5cbd13ac2721..7d5c937384554b1bd2ea70fbe70c44c4fd1b9196 100644 (file)
@@ -85,6 +85,8 @@ UPDATE testpub_parted1 SET a = 1;
 -- only parent is listed as being in publication, not the partition
 ALTER PUBLICATION testpub_forparted ADD TABLE testpub_parted;
 \dRp+ testpub_forparted
+-- works despite missing REPLICA IDENTITY, because no actual update happened
+UPDATE testpub_parted SET a = 1 WHERE false;
 -- should now fail, because parent's publication replicates updates
 UPDATE testpub_parted1 SET a = 1;
 ALTER TABLE testpub_parted DETACH PARTITION testpub_parted1;