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

From: Ivan Alekseev <nettrash(at)nettrash(dot)me>
To: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
Cc: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, 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-12-04 20:53:01
Message-ID: 17709FAF-D59C-4234-966B-B6E46E2C974C@nettrash.me
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I reviewed and tested v19 on top of master (cb94cb4124c), in a build with
assertions enabled.

On unpatched master the original report still reproduces: after the
caught error the slot stays active, and the next slot function in the
session fails the assertion and the backend is terminated by signal 6.

With v19 I tried the following, each time with the error caught by a
PL/pgSQL exception block unless noted, and checked afterwards that the
slot shows active = f and can be used again in the same session:

1. pg_replication_slot_advance() to 0/1 on a logical slot (the original
report).
2. pg_logical_slot_get_changes() with an unknown output plugin option.
3. pg_copy_logical_replication_slot() with a nonexistent plugin. The copy
isn't left behind.
4. pg_replication_slot_advance() to 0/1 on a physical slot.
5. Nested blocks, where the inner block's handler doesn't match the error
and the outer block catches it.
6. Plain SQL: SAVEPOINT, the failing call, ROLLBACK TO, then using the
slot again in the same transaction.
7. An error in the middle of decoding: statement_timeout firing inside
pg_logical_slot_peek_changes() on a 1.5 million row transaction, caught
with WHEN query_canceled. That goes through the decoding
subtransactions and the new Assert(!HistoricSnapshotActive()), which
held.
8. A temporary slot: kept after the caught error and dropped at session
end, as the doc changes say.

All of them behaved as expected, with no assertion failures, and
contrib/test_decoding passes.

One suggestion: the new tests in slot.sql use only
pg_replication_slot_advance() on a logical slot. The commit message says
physical slots are covered too, and a physical-slot case (like 4 above)
would be cheap to add. Case 7 is the only one that exercises the
decoding subtransactions together with the new assertion, but a test
based on statement_timeout would be too timing-dependent for the
regression suite, so I'm not suggesting that one as-is.

Apart from that, the approach looks right to me, and the patch does what
it says. Unless someone has further comments, I'll mark it Ready for
Committer in the commitfest app.

Regards,
Ivan Alekseev

> On 25 Sep 2026, at 19:43, Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com> wrote:
>
> Hi,
>
> On Tue, Sep 22, 2026 at 4:35 PM Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> wrote:
>>
>> As Amit and Chao Li reported upthread, if it happens, the slot is left
>> behind while holding catalog_xmin. Without the v19 patch,
>> only the backend who created the slot can drop it (non-assertion
>> builds), or the slot drop hits assertion failure (on assertion
>> builds), whether the logical slot is temp or persistent. With the
>> patch, the persistent logical slot is dropped on the slot release
>> whereas users would still need to drop the incomplete slot manually.
>> Such slots are left behind while having confirmed_flush_lsn being
>> NULL.
>>
>> If users use such a slot for logical decoding, it can decode partial
>> transactions. Since confirmed_flush is invalid,
>> CreateDecodingContext() starts with an invalid start_decoding_at, and
>> as it's not in slot creation, the snapshot builder can restore a
>> serialized snapshot and jump to the consistent state in the middle of
>> a transaction that started before the slot's restart_lsn.
>
> Thanks all for the thoughts. I moved the temporary slot discussion to
> a new thread [1] so that this thread can move forward with the issue
> reported here, a slot that stays acquired after a slot function's
> error is caught by a PL/pgSQL exception block. The fix in v19 releases
> the slot when that subtransaction aborts.
>
> It covers persistent logical and physical slots for both creation and
> non-creation failures, and temporary logical and physical slots for
> non-creation failures. A persistent logical slot whose creation failed
> gets dropped by that release, since it is still ephemeral. Temporary
> slots whose creation failed are handled in the new thread.
>
> Appreciate any further comments on the v19 patches posted upthread.
>
> [1] https://postgr.es/m/CALj2ACXDsQhq0MFuyRfZ0BJT6F93CNQmG5iv%2BwDzKp%2BYyOGD%2BA%40mail.gmail.com
>
> --
> Bharath Rupireddy
> Amazon Web Services: https://aws.amazon.com
>
>
>
>

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2026-12-04 20:53:03 Re: Add sortsupport to money_ops, pg_lsn_ops and xid8_ops
Previous Message Tom Lane 2026-12-04 20:44:19 Re: Compute the distance between a point and a box exactly