\set VERBOSITY terse
-- predictability
SET synchronous_commit = on;
CREATE TABLE w2j_position (a integer, b integer, primary key(a));
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'wal2json');
 ?column? 
----------
 init
(1 row)

INSERT INTO w2j_position (a, b) VALUES(1,2);
UPDATE w2j_position SET b = 3 WHERE a = 1;
ALTER TABLE w2j_position ADD COLUMN c integer;
ALTER TABLE w2j_position DROP COLUMN b;
INSERT INTO w2j_position (a, c) VALUES(5,6);
UPDATE w2j_position SET c = 7 WHERE a = 5;
-- without include-column-position parameter
SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '1');
                                                                                                             data                                                                                                             
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"change":[{"kind":"insert","schema":"public","table":"w2j_position","columnnames":["a","b"],"columntypes":["integer","integer"],"columnvalues":[1,2]}]}
 {"change":[{"kind":"update","schema":"public","table":"w2j_position","columnnames":["a","b"],"columntypes":["integer","integer"],"columnvalues":[1,3],"oldkeys":{"keynames":["a"],"keytypes":["integer"],"keyvalues":[1]}}]}
 {"change":[]}
 {"change":[]}
 {"change":[{"kind":"insert","schema":"public","table":"w2j_position","columnnames":["a","c"],"columntypes":["integer","integer"],"columnvalues":[5,6]}]}
 {"change":[{"kind":"update","schema":"public","table":"w2j_position","columnnames":["a","c"],"columntypes":["integer","integer"],"columnvalues":[5,7],"oldkeys":{"keynames":["a"],"keytypes":["integer"],"keyvalues":[5]}}]}
(6 rows)

SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '2');
                                                                                                   data                                                                                                   
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"action":"B"}
 {"action":"I","schema":"public","table":"w2j_position","columns":[{"name":"a","type":"integer","value":1},{"name":"b","type":"integer","value":2}]}
 {"action":"C"}
 {"action":"B"}
 {"action":"U","schema":"public","table":"w2j_position","columns":[{"name":"a","type":"integer","value":1},{"name":"b","type":"integer","value":3}],"identity":[{"name":"a","type":"integer","value":1}]}
 {"action":"C"}
 {"action":"B"}
 {"action":"C"}
 {"action":"B"}
 {"action":"C"}
 {"action":"B"}
 {"action":"I","schema":"public","table":"w2j_position","columns":[{"name":"a","type":"integer","value":5},{"name":"c","type":"integer","value":6}]}
 {"action":"C"}
 {"action":"B"}
 {"action":"U","schema":"public","table":"w2j_position","columns":[{"name":"a","type":"integer","value":5},{"name":"c","type":"integer","value":7}],"identity":[{"name":"a","type":"integer","value":5}]}
 {"action":"C"}
(16 rows)

-- with include-column-position parameter
SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '1', 'include-column-positions', '1');
                                                                                                                         data                                                                                                                         
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"change":[{"kind":"insert","schema":"public","table":"w2j_position","columnnames":["a","b"],"columntypes":["integer","integer"],"columnpositions":[1,2],"columnvalues":[1,2]}]}
 {"change":[{"kind":"update","schema":"public","table":"w2j_position","columnnames":["a","b"],"columntypes":["integer","integer"],"columnpositions":[1,2],"columnvalues":[1,3],"oldkeys":{"keynames":["a"],"keytypes":["integer"],"keyvalues":[1]}}]}
 {"change":[]}
 {"change":[]}
 {"change":[{"kind":"insert","schema":"public","table":"w2j_position","columnnames":["a","c"],"columntypes":["integer","integer"],"columnpositions":[1,3],"columnvalues":[5,6]}]}
 {"change":[{"kind":"update","schema":"public","table":"w2j_position","columnnames":["a","c"],"columntypes":["integer","integer"],"columnpositions":[1,3],"columnvalues":[5,7],"oldkeys":{"keynames":["a"],"keytypes":["integer"],"keyvalues":[5]}}]}
(6 rows)

SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '2', 'include-column-positions', '1');
                                                                                                                data                                                                                                                
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"action":"B"}
 {"action":"I","schema":"public","table":"w2j_position","columns":[{"name":"a","type":"integer","value":1,"position":1},{"name":"b","type":"integer","value":2,"position":2}]}
 {"action":"C"}
 {"action":"B"}
 {"action":"U","schema":"public","table":"w2j_position","columns":[{"name":"a","type":"integer","value":1,"position":1},{"name":"b","type":"integer","value":3,"position":2}],"identity":[{"name":"a","type":"integer","value":1}]}
 {"action":"C"}
 {"action":"B"}
 {"action":"C"}
 {"action":"B"}
 {"action":"C"}
 {"action":"B"}
 {"action":"I","schema":"public","table":"w2j_position","columns":[{"name":"a","type":"integer","value":5,"position":1},{"name":"c","type":"integer","value":6,"position":3}]}
 {"action":"C"}
 {"action":"B"}
 {"action":"U","schema":"public","table":"w2j_position","columns":[{"name":"a","type":"integer","value":5,"position":1},{"name":"c","type":"integer","value":7,"position":3}],"identity":[{"name":"a","type":"integer","value":5}]}
 {"action":"C"}
(16 rows)

SELECT 'stop' FROM pg_drop_replication_slot('regression_slot');
 ?column? 
----------
 stop
(1 row)

