Re: Fix replica identity checks for MERGE command on published table.

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
Cc: "Zhijie Hou (Fujitsu)" <houzj(dot)fnst(at)fujitsu(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix replica identity checks for MERGE command on published table.
Date: 2025-07-08 08:51:26
Message-ID: CAEZATCXG66Xw_vb_Y603-0N9F9=jhm3a0RDTyw0an-FKi8bnTw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, 7 Jul 2025 at 18:17, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> wrote:
>
> This makes me wonder though, does INSERT ... ON CONFLICT DO UPDATE
> have the same problem as MERGE?
>

Answering my own question, INSERT ... ON CONFLICT DO UPDATE does have
the same problem as MERGE. To reproduce the error, all you need to do
is create the unique index it needs *after* creating the publication,
for example:

CREATE TABLE foo (a int, b text);
INSERT INTO foo VALUES (1, 'xxx');
CREATE PUBLICATION pub1 FOR TABLE foo;
CREATE UNIQUE INDEX foo_a_idx ON foo(a);

Then a plain UPDATE is blocked:

UPDATE foo SET b = 'yyy' WHERE a = 1;

ERROR: cannot update table "foo" because it does not have a replica
identity and publishes updates
HINT: To enable updating the table, set REPLICA IDENTITY using ALTER TABLE.

but the equivalent with INSERT ... ON CONFLICT DO UPDATE is allowed on
the publisher:

INSERT INTO foo VALUES (1)
ON CONFLICT (a) DO UPDATE SET b = 'yyy';

but fails on the subscriber:

ERROR: logical replication target relation "public.foo" has neither
REPLICA IDENTITY index nor PRIMARY KEY and published relation does not
have REPLICA IDENTITY FULL
CONTEXT: processing remote data for replication origin "pg_16535"
during message type "UPDATE" for replication target relation
"public.foo" in transaction 872, finished at 0/01E65718

So INSERT ... ON CONFLICT DO UPDATE needs to check that the result
relation is valid for UPDATEs.

Regards,
Dean

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Mikhail Kharitonov 2025-07-08 08:53:43 Re: [PATCH] Fix replica identity mismatch for partitioned tables with publish_via_partition_root
Previous Message Aleksander Alekseev 2025-07-08 08:45:44 Re: array_random