Re: Proposal: Conflict log history table for Logical Replication

From: Nisha Moond <nisha(dot)moond412(at)gmail(dot)com>
To: shveta malik <shveta(dot)malik(at)gmail(dot)com>
Cc: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, "Hayato Kuroda (Fujitsu)" <kuroda(dot)hayato(at)fujitsu(dot)com>, Zhijie Hou <houzhijie22(at)gmail(dot)com>, vignesh C <vignesh21(at)gmail(dot)com>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, shihao zhong <zhong950419(at)gmail(dot)com>, Dilip Kumar <dilipbalaut(at)gmail(dot)com>, saurabh singh <saurabh(dot)singh214(at)gmail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Peter Smith <smithpb2250(at)gmail(dot)com>, Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, shveta malik <shvetamalik(at)gmail(dot)com>
Subject: Re: Proposal: Conflict log history table for Logical Replication
Date: 2026-11-25 11:52:03
Message-ID: CABdArM5AoWHDxn-caSpGb7gro0FCZjJoz6Q67UgB2e6Uh6RDow@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Nov 25, 2026 at 12:23 PM shveta malik <shveta(dot)malik(at)gmail(dot)com> wrote:
>
> On Wed, Nov 25, 2026 at 11:48 AM shveta malik <shveta(dot)malik(at)gmail(dot)com> wrote:
> >
> > Thanks for the patch Nisha. I have just started the reveiw, and
> > noticed thatthe patch moves the drop_sub_conflict_log_table() call in
> > DropSubscription() from 'after' the prepared-transaction check to
> > 'before' it. Won't this reintroduce the hang that check was
> > specifically added to prevent? As I understand it, if a prepared
> > transaction holds a lock on the conflict log table, the drop now takes
> > AccessExclusiveLock on the table and blocks before we ever reach the
> > check. Isn't it?
> >
>
> Let me clarify the situation. It depends on whether the apply worker
> is running, because the drop now also happens before the worker is
> stopped:
>
> With the worker still running (enabled), the DROP hangs on the
> conflict log table lock, but it later clears on its own once the
> prepared transaction is resolved.
>
> With the subscription disabled before the DROP-SUB (or the worker
> otherwise gone), the DROP hangs and does not clear by itself. There's
> no worker to apply a resolution from the publisher, and trying to
> re-enable the sub in the meantime just blocks on the same subscription
> lock DROP holds, so no new worker can start.
>
> Earlier the check raised the error before we actually dropped the
> table, which seems better to me than blocking on the lock.
>
> Shall we move the drop_sub_conflict_log_table() call to just after the
> prepared-transaction check? IIUC it would still be before the
> non-transactional cleanup (the replication slot drop on the
> publisher), so it should also satisfy the reason given in the commit
> message for moving it up.
>

Agree, the v2 patch moves drop_sub_conflict_log_table() to just after
the prepared-transaction check.

One clarification on the non-transactional part in commit message. The
slot drop is at the very end of DropSubscription(), so the CLT drop
was already before it even at its original place. The step that
matters is the tablesync origin cleanup, which ran before the CLT
drop. replorigin_drop_by_name() runs inside the transaction and its
pg_replication_origin delete is transactional, but it also clears the
origin's progress in shared memory, which is not undone on rollback.

While further assessing the risk of keeping the drop at its original
place, I found two problems:

1. Cache lookup failure. DropSubscription() deletes the
pg_subscription row early, and the CommandCounterIncrement() in
replorigin_drop_by_name() makes that delete visible. If the CLT drop
then fails with RESTRICT, reportDependentObjects() looks up the
subscription name for the error message and fails:

postgres=# DROP SUBSCRIPTION sub;
ERROR: cache lookup failed for subscription 16392

2. Lost tablesync origin progress. After the failed DROP, a table
still in initial sync keeps its origin but with no progress. When its
tablesync worker restarts in FINISHEDCOPY state, it streams from the
slot's confirmed_flush, which can be behind what it had already
applied, so it applies those changes again. With a primary key this
becomes a repeating insert_exists conflict error, and the table never
finishes syncing.

Neither showed up before, because the CLT was always dropped with
CASCADE and this step could not fail. With RESTRICT it fails whenever
other objects depend on the CLT. Placing the drop right after the
prepared-transaction check avoids both problems.

Attached file has steps to reproduce both case when the
drop_sub_conflict_log_table() is at original possition at head.

--
Thanks,
Nisha

Attachment Content-Type Size
drop_after_origin_cleanup_cases.txt text/plain 3.6 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nisha Moond 2026-11-25 11:53:29 Re: Proposal: Conflict log history table for Logical Replication
Previous Message Jim Jones 2026-11-25 11:50:16 OOM kill in xmlelement() with large arrays