| From: | Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com> |
|---|---|
| To: | Nisha Moond <nisha(dot)moond412(at)gmail(dot)com> |
| Cc: | vignesh C <vignesh21(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-28 07:06:13 |
| Message-ID: | CANhcyEXHKEnpVsZ2KZLyiUDPvjyR370AQ6ipd9OyBJfjnmPGog@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Mon, 23 Nov 2026 at 18:40, Nisha Moond <nisha(dot)moond412(at)gmail(dot)com> wrote:
>
> 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.
>
Thanks Nisha and Vignesh for testing the patch.
I am also able to reproduce the issue and agree with Nisha's analysis.
'ENABLE TRIGGER ALL;' changes the triggers to TRIGGER_FIRES_ON_ORIGIN.
```
case AT_EnableTrigAll: /* ENABLE TRIGGER ALL */
ATExecEnableDisableTrigger(rel, NULL,
TRIGGER_FIRES_ON_ORIGIN, false,
cmd->recurse,
lockmode);
```
> 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.
>
I made the change and agree that the issue shared by Vignesh is
resolved by this.
I had a concern that with this approach the user won't have any way to
disable this trigger only for logical replication. Will it be ok?
Attached a patch with the above approach.
The change in 0002 for DEFERRABLE INITIALLY IMMEDIATELY would still be required.
Attached the updated patches.
Thanks,
Shlok Kyal
| Attachment | Content-Type | Size |
|---|---|---|
| v3-0002-Reject-initially-immediate-deferrable-subscriptio.patch | application/octet-stream | 13.2 KB |
| v3-0001-Fire-deferrable-constraint-recheck-triggers-in-re.patch | application/octet-stream | 7.2 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Shlok Kyal | 2026-11-28 07:07:11 | Re: Logical replication commits duplicates in DEFERRABLE unique indexes |
| Previous Message | Michael Paquier | 2026-11-28 00:46:31 | Re: BUG #19953: pg_stat_io exposes impossible rows for io worker backends |