| From: | Nisha Moond <nisha(dot)moond412(at)gmail(dot)com> |
|---|---|
| To: | vignesh C <vignesh21(at)gmail(dot)com> |
| Cc: | Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com>, Michael Malis <malis(at)pgrust(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | Re: Logical replication commits duplicates in DEFERRABLE unique indexes |
| Date: | 2026-11-23 13:10:45 |
| Message-ID: | CABdArM70PWTr5fdNYJ_q7a2WzZHcCLrg-RG9Gsw7+qCj04DPVQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Mon, Nov 23, 2026 at 6:12 PM vignesh C <vignesh21(at)gmail(dot)com> wrote:
>
> On Mon, 23 Nov 2026 at 15:31, Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com> wrote:
> >
> > I have attached the patches for the above approach
> > 0001 patch : For DEFERRABLE constraint, create the Recheck triggers
> > with TRIGGER_FIRES_ALWAYS
> > 0002 patch : Reject DEFERRABLE INITIALLY IMMEDIATE constraints on
> > subscription target.
> >
> > Thoughts?
>
> Thanks for the patch. I tested v2 and found a case where the
> duplicates still get committed.
> Steps for the same:
> -- publisher
> CREATE TABLE t (id int PRIMARY KEY DEFERRABLE INITIALLY DEFERRED);
> CREATE PUBLICATION pub FOR TABLE t;
>
> -- subscriber
> CREATE TABLE t (id int PRIMARY KEY DEFERRABLE INITIALLY DEFERRED);
> INSERT INTO t VALUES (1);
> CREATE SUBSCRIPTION sub CONNECTION '...' PUBLICATION pub WITH
> (copy_data = false);
> ALTER TABLE t ENABLE TRIGGER ALL;
>
> -- publisher
> INSERT INTO t VALUES (1);
>
> The subscriber applies the row without an error, and the table has two
> rows with id = 1 in a unique index.
>
+1, I can reproduce this too.
The cause is that ALTER TABLE ... ENABLE TRIGGER ALL always sets
tgenabled to 'O', and when a superuser runs it, it also changes
internal triggers. So the recheck trigger that 0001 created as 'A'
goes back to 'O', and the apply worker skips it again.
Shlok, why not go with Michael's first idea and do the check in
TriggerEnabled()? Something like -
if (SessionReplicationRole == SESSION_REPLICATION_ROLE_REPLICA)
{
if (trigger->tgenabled == TRIGGER_DISABLED)
return false;
/* The uniqueness recheck must run in replica mode too. */
if (trigger->tgenabled == TRIGGER_FIRES_ON_ORIGIN &&
!(trigger->tgisinternal && trigger->tgfoid == F_UNIQUE_KEY_RECHECK))
return false;
}
I tested it, and it fixes Vignesh's case. It would also
- cover existing catalogs, so it could be back-patched (may need
further thought though)
- make the index_constraint_create() change unnecessary
Let me know if I'm missing anything.
--
Thanks,
Nisha
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Henson Choi | 2026-11-23 13:22:45 | Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill |
| Previous Message | vignesh C | 2026-11-23 12:42:08 | Re: Logical replication commits duplicates in DEFERRABLE unique indexes |