Re: Transactional behavior of pg_create_logical_replication_slot

From: Mats Kindahl <mats(at)timescale(dot)com>
To: Andres Freund <andres(at)anarazel(dot)de>
Cc: PostgreSQL Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Transactional behavior of pg_create_logical_replication_slot
Date: 2025-06-18 05:23:09
Message-ID: CA+14427d3-kg9Oo2N9r5LnCkis+_+QeyReyrWq6PUR2rOTLxxw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Jun 17, 2025 at 1:22 PM Andres Freund <andres(at)anarazel(dot)de> wrote:

> Hi,
>
> On 2025-06-17 10:30:34 +0200, Mats Kindahl wrote:
> > Hi PostgreSQL Hackers,
> >
> > If you try to create a logical replication slot inside a transaction
> where
> > you have issued writes, it will abort with an error message:
> >
> > mats=# start transaction;
> > START TRANSACTION
> > mats=*# select txid_current();
> > txid_current
> > --------------
> > 406252
> > (1 row)
> >
> > mats=*# insert into foo values (txid_current());
> > INSERT 0 1
> > mats=*# select txid_current();
> > txid_current
> > --------------
> > 406252
> > (1 row)
> >
> > mats=*# select * from pg_create_logical_replication_slot('my_slot',
> > 'test_decoding', false, true);
> > ERROR: cannot create logical replication slot in transaction that has
> > performed writes
> > mats=!# rollback;
> > ROLLBACK
> >
> >
> > I'm slightly unsure why, but I assume that it is because if the
> transaction
> > is rolled back, it will have the XID of a transaction that does not
> "exist"
> > (quotes because the XID is still assigned and incremented, even if the
> > transaction is rolled back).
>
> Logical slot creation basically waits for concurrent transactions to end,
> that
> wouldn't work if itself had a transaction open (it'd be a self deadlock).
>

Thanks Andres, that explains it.

> > If that is the case, then having the call first in the transaction will
> > still work fine, since no writes are done yet, but if that transaction is
> > rolled back, the slot will point to a "non-existing" XID similar to the
> > first case.
>
> That's not the / a problem.

> Greetings,
>
> Andres Freund
>

--
Best wishes,
Mats Kindahl, Timescale

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2025-06-18 05:26:21 Re: Replication slot is not able to sync up
Previous Message Michael Paquier 2025-06-18 05:13:27 Re: [PATCH] Split varlena.c into varlena.c and bytea.c