Re: Logical replication commits duplicates in DEFERRABLE unique indexes

From: Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com>
To: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org, Michael Malis <malis(at)pgrust(dot)com>
Subject: Re: Logical replication commits duplicates in DEFERRABLE unique indexes
Date: 2026-11-28 07:07:11
Message-ID: CANhcyEUDzuhvYWrC3a7+J78YY8GqFQz8KKpfzDU49Qn+nLzVzw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Zsolt,
Thanks for reviewing the patch.

On Tue, 24 Nov 2026 at 04:19, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com> wrote:
>
> Hello
>
> This can stall the apply worker:
>
> -- subscriber: CREATE ROLE app; CREATE TABLE ledger (id int PRIMARY
> KEY DEFERRABLE INITIALLY DEFERRED, v text);
> -- ALTER TABLE ledger OWNER TO app; subscription owned
> by superuser, run_as_owner = false
> -- publisher (REPLICA IDENTITY FULL), rows 1..3:
> UPDATE ledger SET id = id + 1;
> -- patched subscriber stays {1,2,3}, retried forever:
> ERROR: cannot fire deferred trigger within security-restricted operation
> -- master: {2,3,4}
I am able to reproduce the issue with v2-0001 and also with the
changes shared by Nisha in [1].

The same behaviour can happen on HEAD, when we manually set the
Primary Key Deferrable trigger as 'ENABLE ALWAYS'.
It happens because in when apply worker applies the UPDATE in
apply_handle_update, it calls:
```
if (!run_as_owner)
SwitchToUntrustedUser(rel->localrel->rd_rel->relowner, &ucxt);
```
Here, the security context is updated as 'sec_context |=
SECURITY_RESTRICTED_OPERATION;'.

At later stage when,'AfterTriggerEndQuery() ->
afterTriggerMarkEvents()' is invoked and we hit the following error:
```
/*
* We could allow deferred triggers if, before the end of the
* security-restricted operation, we were to verify that a SET CONSTRAINTS
* ... IMMEDIATE has fired all such triggers. For now, don't bother.
*/
if (deferred_found && InSecurityRestrictedOperation())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("cannot fire deferred trigger within
security-restricted operation")));
```
In afterTriggerMarkEvents, it tries to move the recheck from query’s
event list to the transaction’s deferred-event list marking
'deferred_found = true' and since it is being executed as
SECURITY_RESTRICTED_OPERATION, the above error is thrown.
I think bypassing this check for our case can introduce security complications.

Some ways to work around it are:
1. CREATE SUBSCRIPTION with run_as_owner as true.
2. TABLEs with DEFERRABLE constraint have the same owner as the SUBSCRIPTION.
But both of the above sound risky as documentation [1] suggests
setting 'run_as_owner = false' as a more secure option.

I think stage documenting this behaviour would make more sense. Thought?
Do let me know if I am missing something or we have a better approach.

>
> Also, attach partition bypasses the check:
>
> CREATE TABLE pt2 (id int, v int, PRIMARY KEY (id) DEFERRABLE INITIALLY
> IMMEDIATE);
> ALTER TABLE pt ATTACH PARTITION pt2 FOR VALUES FROM (100) TO (200);
> -- pt is subscribed
>
> And
>
> + if (localrelkind == RELKIND_PARTITIONED_TABLE)
> + relids = find_all_inheritors(relid, AccessShareLock, NULL);
> ...
> + table_close(rel, AccessShareLock);
>
> find_all_inheritors() doesn't lock the root itself, this either
> results in a warning or unlocks a table the caller locked.

Fixed it in the updated 0002 patch.

Attached the updated patch in [2].

[1]: https://www.postgresql.org/docs/devel/sql-createsubscription.html#SQL-CREATESUBSCRIPTION-PARAMS-WITH-RUN-AS-OWNER
[2]: https://www.postgresql.org/message-id/CANhcyEXHKEnpVsZ2KZLyiUDPvjyR370AQ6ipd9OyBJfjnmPGog%40mail.gmail.com

Thanks,
Shlok Kyal

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2026-11-28 07:53:44 Re: BUG #19890: pg_receivewal advances past a truncated LZ4 WAL segment
Previous Message Shlok Kyal 2026-11-28 07:06:13 Re: Logical replication commits duplicates in DEFERRABLE unique indexes