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

From: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
To: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
Cc: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, shveta malik <shveta(dot)malik(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-03 20:58:01
Message-ID: CAD21AoBuKvEwGEPyUYiRE2Qz4LsbnydEnhqK9mtg21FL6v9GpA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Mon, Aug 31, 2026 at 1:47 PM Bharath Rupireddy
<bharath(dot)rupireddyforpostgres(at)gmail(dot)com> wrote:
>
> Hi,
>
> On Sun, Aug 30, 2026 at 9:40 PM Amit Kapila <amit(dot)kapila16(at)gmail(dot)com> wrote:
> >
> > > Hi Amit, By restricting in the code, does that mean adding an Assert,
> > > or a WARNING, or a WARNING plus slot release (not an error), in the
> > > replication slot subxact callback on the commit path, instead of
> > > handing the slot off to the parent across the subtransaction boundary?
> >
> > Yes, I would prefer WARNING similar to existing cases for resource
> > leaks in commit paths. One example of a similar existing case is:
> > ------
> > /* Complain if any allocated files remain open at commit. */
> > if (isCommit && numAllocatedDescs > 0)
> > elog(WARNING, "%d temporary files and directories not closed at
> > end-of-transaction",
> > numAllocatedDescs);
> > -------
> >
> > Based on above, I am imagining a check/WARNING on lines of:
>
> Thanks, Amit. That works for me. Please find the attached v15 patch.
> If it looks good, I can prepare patches for the back branches.
>

Thank you for updating the patch! Here are some review comments:

+ if (isCommit)
+ {
+ Assert(MyReplicationSlot != NULL);
+ ereport(WARNING,
+ (errcode(ERRCODE_WARNING),
+ errmsg("subtransaction left replication slot \"%s\" acquired",
+ NameStr(MyReplicationSlot->data.name)),
+ errhint("Check for missing \"ReplicationSlotRelease\"
calls.")));

The hint message "Check for missing ReplicationSlotRelease call" seems
to be for us (PostgreSQL hackers) but not users. I think such messages
should be left as a comment instead of in errhint.

Also, we don't prohibit external extensions or functions to commit a
subtransaction while holding a replication slot. If there are such
extensions, users would get WARNING messages. Which seems to be
something I'd like to avoid in minor releases.

---
+-- Error raised inside a PL/pgSQL block with an EXCEPTION clause is caught in a
+-- subtransaction; the slot must still be released.
+SELECT 'init' FROM
pg_create_logical_replication_slot('regress_subxact_slot',
'test_decoding');
+DO $$
+BEGIN
+ PERFORM pg_replication_slot_advance('regress_subxact_slot', '0/1');
+EXCEPTION WHEN OTHERS THEN
+ RAISE NOTICE 'caught SQLSTATE %', SQLSTATE;
+END $$;
+SELECT count(*) >= 0 AS peek_ok
+ FROM pg_logical_slot_peek_changes('regress_subxact_slot', NULL, NULL);

The last sentence is the comment doesn't match the test well and this
test doesn't fail on non-assertion builds. I think we can check the
active column in pg_replication_slots instead or before
slot_peek_changes() call.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2026-09-03 21:11:40 Re: auto-vectorize varbit bitwise operators
Previous Message Peter Eisentraut 2026-09-03 20:53:38 Re: PGQ catalog representation and pg_dump support