repro_local_origin_stuck.txt

text/plain

Filename: repro_local_origin_stuck.txt
Type: text/plain
Part: 0
Message: Disabled logical replication origin session causes primary key errors
NOTE:  wal_level = logical

Publication Session:

CREATE TABLE t1 (a INT);
CREATE PUBLICATION my_publication FOR TABLE t1;

Subscription Session:

CREATE TABLE t1 (a INT);

CREATE SUBSCRIPTION my_subscription
CONNECTION '<conninfo>'
PUBLICATION my_publication;

CREATE OR REPLACE FUNCTION handle_exception_trigger()
RETURNS TRIGGER AS $$
BEGIN
    BEGIN
        -- Raise an exception
        RAISE EXCEPTION 'This is a test exception';
    EXCEPTION
        WHEN OTHERS THEN
            -- Silently handle the exception
            -- You can optionally log it if needed
            -- RAISE NOTICE 'Caught exception: %', SQLERRM;
            RETURN NEW;
    END;

    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER silent_exception_trigger
AFTER INSERT OR UPDATE ON t1
FOR EACH ROW
EXECUTE FUNCTION handle_exception_trigger();

-- Just force it to always trigger for simplicity
ALTER TABLE t1 ENABLE ALWAYS TRIGGER silent_exception_trigger;

-- Mark down the current value
SELECT * from pg_replication_origin_status;

Publication Session:

INSERT INTO t1 VALUES (1);
INSERT INTO t1 VALUES (2);
INSERT INTO t1 VALUES (3);

Subscription Session:

-- All 3 changes should be present
SELECT * FROM t1;

-- The new value will be equivalent to the original value
-- even though three new changes have been applied from the remote
SELECT * from pg_replication_origin_status;