I try to create PostgreSQL replication slot by the next command:
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_publication WHERE pubname = 'trade_events_pub')
THEN
CREATE PUBLICATION trade_events_pub FOR TABLE
public.trade_events;
END IF;
IF NOT EXISTS (SELECT 1 from pg_catalog.pg_replication_slots WHERE slot_name = 'trade_events_slot')
THEN
PERFORM pg_create_logical_replication_slot('trade_events_slot', 'pgoutput');
END IF;
END;
$$ LANGUAGE plpgsql;
I get the error:
[25001] ERROR: cannot create logical replication slot in transaction that has performed writes Where: SQL statement “SELECT pg_create_logical_replication_slot(‘trade_events_slot’, ‘pgoutput’)” PL/pgSQL function inline_code_block line 15 at PERFORM
If I try to run command CREATE PUBLICATION and PERFORM pg_create_logical_replication_slot in separate transactions it works fine.
I use PostgreSQL 15.3