Skip to content

Commit

Permalink
Add parameter include-default
Browse files Browse the repository at this point in the history
This parameter adds "columndefaults" that contains all defaults values
(same as pg_get_expr). Both formats (1 and 2) are supported although
element names are different ("columndefaults" for format 1 and "default"
for format 2). Each default value is represented as string because
it can be an expression. Tests are included. Default is false.

I also added prototypes to 2 static functions.
  • Loading branch information
eulerto committed Jul 12, 2020
1 parent caaf9ac commit 9df8c05
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MODULES = wal2json
REGRESS = cmdline insert1 update1 update2 update3 update4 delete1 delete2 \
delete3 delete4 savepoint specialvalue toast bytea message typmod \
filtertable selecttable include_timestamp include_lsn include_xids \
include_domain_data_type truncate actions position
include_domain_data_type truncate actions position default

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Parameters
* `include-domain-data-type`: replace domain name with the underlying data type. Default is _false_.
* `include-column-positions`: add column position (_pg_attribute.attnum_). Default is _false_.
* `include-not-null`: add _not null_ information as _columnoptionals_. Default is _false_.
* `include-default`: add default expression. Default is _false_.
* `pretty-print`: add spaces and indentation to JSON structures. Default is _false_.
* `write-in-chunks`: write after every change instead of every changeset. Default is _false_.
* `include-lsn`: add _nextlsn_ to each changeset. Default is _false_.
Expand Down
66 changes: 66 additions & 0 deletions expected/default.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
\set VERBOSITY terse
-- predictability
SET synchronous_commit = on;
CREATE TABLE w2j_default (a serial, b integer DEFAULT 6, c text DEFAULT 'wal2json', d timestamp DEFAULT '2020-07-12 11:55:30', e integer DEFAULT NULL, f integer, PRIMARY KEY(a));
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'wal2json');
?column?
----------
init
(1 row)

INSERT INTO w2j_default (b, c ,d, e, f) VALUES(2, 'test', '2020-03-01 08:09:00', 80, 10);
INSERT INTO w2j_default DEFAULT VALUES;
UPDATE w2j_default SET b = 3 WHERE a = 1;
-- without include-default parameter
SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '1');
data
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"change":[{"kind":"insert","schema":"public","table":"w2j_default","columnnames":["a","b","c","d","e","f"],"columntypes":["integer","integer","text","timestamp without time zone","integer","integer"],"columnvalues":[1,2,"test","Sun Mar 01 08:09:00 2020",80,10]}]}
{"change":[{"kind":"insert","schema":"public","table":"w2j_default","columnnames":["a","b","c","d","e","f"],"columntypes":["integer","integer","text","timestamp without time zone","integer","integer"],"columnvalues":[2,6,"wal2json","Sun Jul 12 11:55:30 2020",null,null]}]}
{"change":[{"kind":"update","schema":"public","table":"w2j_default","columnnames":["a","b","c","d","e","f"],"columntypes":["integer","integer","text","timestamp without time zone","integer","integer"],"columnvalues":[1,3,"test","Sun Mar 01 08:09:00 2020",80,10],"oldkeys":{"keynames":["a"],"keytypes":["integer"],"keyvalues":[1]}}]}
(3 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_default","columns":[{"name":"a","type":"integer","value":1},{"name":"b","type":"integer","value":2},{"name":"c","type":"text","value":"test"},{"name":"d","type":"timestamp without time zone","value":"Sun Mar 01 08:09:00 2020"},{"name":"e","type":"integer","value":80},{"name":"f","type":"integer","value":10}]}
{"action":"C"}
{"action":"B"}
{"action":"I","schema":"public","table":"w2j_default","columns":[{"name":"a","type":"integer","value":2},{"name":"b","type":"integer","value":6},{"name":"c","type":"text","value":"wal2json"},{"name":"d","type":"timestamp without time zone","value":"Sun Jul 12 11:55:30 2020"},{"name":"e","type":"integer","value":null},{"name":"f","type":"integer","value":null}]}
{"action":"C"}
{"action":"B"}
{"action":"U","schema":"public","table":"w2j_default","columns":[{"name":"a","type":"integer","value":1},{"name":"b","type":"integer","value":3},{"name":"c","type":"text","value":"test"},{"name":"d","type":"timestamp without time zone","value":"Sun Mar 01 08:09:00 2020"},{"name":"e","type":"integer","value":80},{"name":"f","type":"integer","value":10}],"identity":[{"name":"a","type":"integer","value":1}]}
{"action":"C"}
(9 rows)

-- with include-default parameter
SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '1', 'include-default', '1');
data
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"change":[{"kind":"insert","schema":"public","table":"w2j_default","columnnames":["a","b","c","d","e","f"],"columntypes":["integer","integer","text","timestamp without time zone","integer","integer"],"columndefaults":["nextval('w2j_default_a_seq'::regclass)","6","'wal2json'::text","'Sun Jul 12 11:55:30 2020'::timestamp without time zone",null,null],"columnvalues":[1,2,"test","Sun Mar 01 08:09:00 2020",80,10]}]}
{"change":[{"kind":"insert","schema":"public","table":"w2j_default","columnnames":["a","b","c","d","e","f"],"columntypes":["integer","integer","text","timestamp without time zone","integer","integer"],"columndefaults":["nextval('w2j_default_a_seq'::regclass)","6","'wal2json'::text","'Sun Jul 12 11:55:30 2020'::timestamp without time zone",null,null],"columnvalues":[2,6,"wal2json","Sun Jul 12 11:55:30 2020",null,null]}]}
{"change":[{"kind":"update","schema":"public","table":"w2j_default","columnnames":["a","b","c","d","e","f"],"columntypes":["integer","integer","text","timestamp without time zone","integer","integer"],"columndefaults":["nextval('w2j_default_a_seq'::regclass)","6","'wal2json'::text","'Sun Jul 12 11:55:30 2020'::timestamp without time zone",null,null],"columnvalues":[1,3,"test","Sun Mar 01 08:09:00 2020",80,10],"oldkeys":{"keynames":["a"],"keytypes":["integer"],"keyvalues":[1]}}]}
(3 rows)

SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '2', 'include-default', '1');
data
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"action":"B"}
{"action":"I","schema":"public","table":"w2j_default","columns":[{"name":"a","type":"integer","value":1,"default":"nextval('w2j_default_a_seq'::regclass)"},{"name":"b","type":"integer","value":2,"default":"6"},{"name":"c","type":"text","value":"test","default":"'wal2json'::text"},{"name":"d","type":"timestamp without time zone","value":"Sun Mar 01 08:09:00 2020","default":"'Sun Jul 12 11:55:30 2020'::timestamp without time zone"},{"name":"e","type":"integer","value":80,"default":null},{"name":"f","type":"integer","value":10,"default":null}]}
{"action":"C"}
{"action":"B"}
{"action":"I","schema":"public","table":"w2j_default","columns":[{"name":"a","type":"integer","value":2,"default":"nextval('w2j_default_a_seq'::regclass)"},{"name":"b","type":"integer","value":6,"default":"6"},{"name":"c","type":"text","value":"wal2json","default":"'wal2json'::text"},{"name":"d","type":"timestamp without time zone","value":"Sun Jul 12 11:55:30 2020","default":"'Sun Jul 12 11:55:30 2020'::timestamp without time zone"},{"name":"e","type":"integer","value":null,"default":null},{"name":"f","type":"integer","value":null,"default":null}]}
{"action":"C"}
{"action":"B"}
{"action":"U","schema":"public","table":"w2j_default","columns":[{"name":"a","type":"integer","value":1,"default":"nextval('w2j_default_a_seq'::regclass)"},{"name":"b","type":"integer","value":3,"default":"6"},{"name":"c","type":"text","value":"test","default":"'wal2json'::text"},{"name":"d","type":"timestamp without time zone","value":"Sun Mar 01 08:09:00 2020","default":"'Sun Jul 12 11:55:30 2020'::timestamp without time zone"},{"name":"e","type":"integer","value":80,"default":null},{"name":"f","type":"integer","value":10,"default":null}],"identity":[{"name":"a","type":"integer","value":1}]}
{"action":"C"}
(9 rows)

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

DROP TABLE w2j_default;
24 changes: 24 additions & 0 deletions sql/default.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
\set VERBOSITY terse

-- predictability
SET synchronous_commit = on;

CREATE TABLE w2j_default (a serial, b integer DEFAULT 6, c text DEFAULT 'wal2json', d timestamp DEFAULT '2020-07-12 11:55:30', e integer DEFAULT NULL, f integer, PRIMARY KEY(a));

SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'wal2json');

INSERT INTO w2j_default (b, c ,d, e, f) VALUES(2, 'test', '2020-03-01 08:09:00', 80, 10);
INSERT INTO w2j_default DEFAULT VALUES;
UPDATE w2j_default SET b = 3 WHERE a = 1;

-- without include-default parameter
SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '1');
SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '2');

-- with include-default parameter
SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '1', 'include-default', '1');
SELECT data FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'format-version', '2', 'include-default', '1');

SELECT 'stop' FROM pg_drop_replication_slot('regression_slot');

DROP TABLE w2j_default;
Loading

0 comments on commit 9df8c05

Please sign in to comment.