-- on the source, put this to a file named create_table.sql
CREATE TABLE test (key int, value text, data jsonb);
INSERT INTO test SELECT i, i::text, row_to_json(row(i)) FROM generate_series(1,1000001)i;
CREATE PUBLICATION pub_1 FOR TABLE test WHERE (key > 0  AND value::int > 0 AND (data->'f1')::int > 0);


-- on the target, put this to a file named wait_data_sync.sql
--  helper function
CREATE OR REPLACE PROCEDURE public.wait_until_data_syncs() LANGUAGE PLPGSQL AS $procedure$
    declare
       table_count integer:= 0;
       start timestamptz := clock_timestamp() ;

    begin
    	RAISE NOTICE 'Execution starts %', start;

    loop
	    SELECT count(*) INTO table_count FROM test;
		exit when table_count > 0 ;

	    PERFORM pg_sleep(1);
	    	RAISE NOTICE 'seconds passed, the data not synced yet: %', clock_timestamp() - start;
	end loop;

	RAISE NOTICE 'seconds passed until the data is synced: %', clock_timestamp() - start;
end; $procedure$;

-- create the subscription and wait until data is visible
CREATE SUBSCRIPTION sub_1 CONNECTION 'host=127.0.0.1 port=5432 dbname=postgres' PUBLICATION pub_1;
SELECT public.wait_until_data_syncs();


-- execute psql -f create_table.sql -p PRIMARY_PORT && psql -f wait_data_sync.sql -p SECONDARY_PORT