Re: [PATCH] Release replication slot on error in SQL-callable slot functions

From: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
To: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Cc: shveta malik <shveta(dot)malik(at)gmail(dot)com>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, Ashutosh Sharma <ashu(dot)coek88(at)gmail(dot)com>, "Zhijie Hou (Fujitsu)" <houzj(dot)fnst(at)fujitsu(dot)com>, SATYANARAYANA NARLAPURAM <satyanarlapuram(at)gmail(dot)com>, Fujii Masao <masao(dot)fujii(at)gmail(dot)com>, vignesh C <vignesh21(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Release replication slot on error in SQL-callable slot functions
Date: 2026-09-17 01:37:13
Message-ID: CALj2ACUBJ53hY8EOUJnWA7m1GzBP_CoPo9N6e01x5+q9GPQoXQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Wed, Sep 16, 2026 at 2:02 PM Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> wrote:
>
> > Sawada-san, does that mean that on the back branches, even for the
> > case where the concerned subtransaction is committing while the slot
> > is still held (a scenario we don't know can happen), we would release
> > the slot and clean up MyReplicationSlotSubId? Is my understanding
> > correct?
>
> I don't think we should release the slot at subxact commit. I think
> it's better to leave it to the caller as it might release the slot
> afterward. Another problem is that nothing tests this case.
>
> Please refer to the attached patch that can be applied on v16 patch
> and implements my idea. It adds additional regression tests too.

Thanks for sharing the diff. I apologize for going back and forth on
the subtransaction commit hand-off. After looking at it, here is what
I have.

The slot acquired in a subtransaction gets cleaned up only if that
subtransaction is aborted, which is the reported bug and easily
reachable from SQL today.

MyReplicationSlotSubId is not a stale value within the transaction but
the owner's subtransaction id. However, across transactions it becomes
stale and can get reused, so it is reset at the subtransaction commit
path, preventing MyReplicationSlotSubId from going stale for users
carrying the slot across transactions (if any). In core code, the
owning subtransaction always releases the slot before it ends, and the
release clears the id. No hand-off, no warning on subtransaction
commit.

Please find the attached patch for HEAD. I dropped the two unnecessary
header file inclusions added in the diff but otherwise took it as-is.
If it looks good, I can prepare the patches for all supported
branches.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

Attachment Content-Type Size
v17-0001-Fix-replication-slot-leak-on-error-caught-in-a-s.patch application/x-patch 18.6 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Sami Imseih 2026-09-17 01:42:48 Re: Reject WAIT FOR earlier in transaction-snapshot mode
Previous Message Henson Choi 2026-09-17 01:21:27 Re: Row pattern recognition