Hi,

I've attached lr01.sql, a minimal psql reproducer for this issue. It
uses separate publisher and subscriber databases in the same instance
and waits for replication between updates.

On an affected build, the final query returns (1,111) and (222,2),
instead of (1,111) and (2,222).

Please run it on a test instance with logical replication enabled and
authentication configured for the subscription connection.

Best regards,
mingjiegao
\set ON_ERROR_STOP on
\pset pager off
\connect postgres
CREATE DATABASE lr01_pub;
CREATE DATABASE lr01_sub;

\connect lr01_pub
\set pub_conn 'host=' :HOST ' port=' :PORT ' user=' :USER ' dbname=lr01_pub 
application_name=map_sub'
CREATE TABLE tab6 (a int PRIMARY KEY, b int);
CREATE TABLE tab6_1 (b int, a int PRIMARY KEY);
CREATE PUBLICATION map_pub FOR TABLE tab6, tab6_1;

CREATE FUNCTION wait_map_sub(target pg_lsn) RETURNS void
LANGUAGE plpgsql AS $$
DECLARE
    deadline timestamptz := clock_timestamp() + interval '60 seconds';
BEGIN
    LOOP
        PERFORM pg_stat_clear_snapshot();
        IF EXISTS (
            SELECT 1 FROM pg_stat_replication
            WHERE application_name = 'map_sub' AND replay_lsn >= target
        ) THEN
            RETURN;
        END IF;
        IF clock_timestamp() >= deadline THEN
            RAISE EXCEPTION 'map_sub did not reach LSN % within 60 seconds', 
target;
        END IF;
        PERFORM pg_sleep(0.1);
    END LOOP;
END
$$;

-- Precreate the slot to avoid CREATE SUBSCRIPTION waiting on itself.
SELECT slot_name FROM pg_create_logical_replication_slot('lr01_slot', 
'pgoutput');

\connect lr01_sub
CREATE TABLE tab6 (a int PRIMARY KEY, b int) PARTITION BY RANGE (a);
CREATE TABLE tab6_1 (b int, a int PRIMARY KEY) PARTITION BY RANGE (a);
ALTER TABLE tab6 ATTACH PARTITION tab6_1 DEFAULT;
CREATE TABLE tab6_leaf PARTITION OF tab6_1 DEFAULT;
CREATE SUBSCRIPTION map_sub CONNECTION :'pub_conn' PUBLICATION map_pub
    WITH (copy_data = false, create_slot = false, slot_name = 'lr01_slot');

\connect lr01_pub
INSERT INTO tab6 (a, b) VALUES (1, 11);
INSERT INTO tab6_1 (a, b) VALUES (2, 22);
SELECT wait_map_sub(pg_current_wal_insert_lsn());

UPDATE tab6 SET b = 111 WHERE a = 1;
SELECT wait_map_sub(pg_current_wal_insert_lsn());

UPDATE tab6_1 SET b = 222 WHERE a = 2;
SELECT wait_map_sub(pg_current_wal_insert_lsn());

\connect lr01_sub
\echo 'Correct: (1,111), (2,222). Bug: (1,111), (222,2).'
SELECT a, b FROM tab6 ORDER BY a;

Reply via email to