2 SET synchronous_commit = on;
4 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
5 -- fail because of an already existing slot
6 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
7 -- fail because of an invalid name
8 SELECT 'init' FROM pg_create_logical_replication_slot('Invalid Name', 'test_decoding');
10 -- fail twice because of an invalid parameter values
11 SELECT 'init' FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', 'frakbar');
12 SELECT 'init' FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'nonexistant-option', 'frakbar');
13 SELECT 'init' FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', 'frakbar');
16 SELECT pg_drop_replication_slot('regression_slot');
18 SELECT pg_drop_replication_slot('regression_slot');
20 -- check that we're detecting a streaming rep slot used for logical decoding
21 SELECT 'init' FROM pg_create_physical_replication_slot('repl');
22 SELECT data FROM pg_logical_slot_get_changes('repl', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
23 SELECT pg_drop_replication_slot('repl');
26 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
28 /* check whether status function reports us, only reproduceable columns */
29 SELECT slot_name, plugin, slot_type, active,
30 NOT catalog_xmin IS NULL AS catalog_xmin_set,
31 xmin IS NULl AS data_xmin_not_set,
32 pg_xlog_location_diff(restart_lsn, '0/01000000') > 0 AS some_wal
33 FROM pg_replication_slots;
36 * Check that changes are handled correctly when interleaved with ddl
38 CREATE TABLE replication_example(id SERIAL PRIMARY KEY, somedata int, text varchar(120));
40 INSERT INTO replication_example(somedata, text) VALUES (1, 1);
41 INSERT INTO replication_example(somedata, text) VALUES (1, 2);
44 ALTER TABLE replication_example ADD COLUMN bar int;
46 INSERT INTO replication_example(somedata, text, bar) VALUES (2, 1, 4);
49 INSERT INTO replication_example(somedata, text, bar) VALUES (2, 2, 4);
50 INSERT INTO replication_example(somedata, text, bar) VALUES (2, 3, 4);
51 INSERT INTO replication_example(somedata, text, bar) VALUES (2, 4, NULL);
54 ALTER TABLE replication_example DROP COLUMN bar;
55 INSERT INTO replication_example(somedata, text) VALUES (3, 1);
58 INSERT INTO replication_example(somedata, text) VALUES (3, 2);
59 INSERT INTO replication_example(somedata, text) VALUES (3, 3);
62 ALTER TABLE replication_example RENAME COLUMN text TO somenum;
64 INSERT INTO replication_example(somedata, somenum) VALUES (4, 1);
66 -- collect all changes
67 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
69 ALTER TABLE replication_example ALTER COLUMN somenum TYPE int4 USING (somenum::int4);
70 -- throw away changes, they contain oids
71 SELECT count(data) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
73 INSERT INTO replication_example(somedata, somenum) VALUES (5, 1);
76 INSERT INTO replication_example(somedata, somenum) VALUES (6, 1);
77 ALTER TABLE replication_example ADD COLUMN zaphod1 int;
78 INSERT INTO replication_example(somedata, somenum, zaphod1) VALUES (6, 2, 1);
79 ALTER TABLE replication_example ADD COLUMN zaphod2 int;
80 INSERT INTO replication_example(somedata, somenum, zaphod2) VALUES (6, 3, 1);
81 INSERT INTO replication_example(somedata, somenum, zaphod1) VALUES (6, 4, 2);
85 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
87 -- ON CONFLICT DO UPDATE support
89 INSERT INTO replication_example(id, somedata, somenum) SELECT i, i, i FROM generate_series(-15, 15) i
90 ON CONFLICT (id) DO UPDATE SET somenum = excluded.somenum + 1;
93 /* display results, but hide most of the output */
94 SELECT count(*), min(data), max(data)
95 FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1')
96 GROUP BY substring(data, 1, 40)
99 -- hide changes bc of oid visible in full table rewrites
100 CREATE TABLE tr_unique(id2 serial unique NOT NULL, data int);
101 INSERT INTO tr_unique(data) VALUES(10);
102 ALTER TABLE tr_unique RENAME TO tr_pkey;
103 ALTER TABLE tr_pkey ADD COLUMN id serial primary key;
104 SELECT count(data) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
106 INSERT INTO tr_pkey(data) VALUES(1);
107 --show deletion with primary key
110 /* display results */
111 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
114 * check that disk spooling works
117 CREATE TABLE tr_etoomuch (id serial primary key, data int);
118 INSERT INTO tr_etoomuch(data) SELECT g.i FROM generate_series(1, 10234) g(i);
119 DELETE FROM tr_etoomuch WHERE id < 5000;
120 UPDATE tr_etoomuch SET data = - data WHERE id > 5000;
123 /* display results, but hide most of the output */
124 SELECT count(*), min(data), max(data)
125 FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1')
126 GROUP BY substring(data, 1, 24)
129 -- check that a large, spooled, upsert works
130 INSERT INTO tr_etoomuch (id, data)
131 SELECT g.i, -g.i FROM generate_series(8000, 12000) g(i)
132 ON CONFLICT(id) DO UPDATE SET data = EXCLUDED.data;
134 SELECT substring(data, 1, 29), count(*)
135 FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1')
137 ORDER BY min(location - '0/0');
140 * check whether we decode subtransactions correctly in relation with each
143 CREATE TABLE tr_sub (id serial primary key, path text);
145 -- toplevel, subtxn, toplevel, subtxn, subtxn
147 INSERT INTO tr_sub(path) VALUES ('1-top-#1');
150 INSERT INTO tr_sub(path) VALUES ('1-top-1-#1');
151 INSERT INTO tr_sub(path) VALUES ('1-top-1-#2');
156 INSERT INTO tr_sub(path) VALUES ('1-top-2-1-#1');
157 INSERT INTO tr_sub(path) VALUES ('1-top-2-1-#2');
159 INSERT INTO tr_sub(path) VALUES ('1-top-2-#1');
163 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
165 -- check that we handle xlog assignments correctly
168 SAVEPOINT subtop;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
169 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
170 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
171 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
172 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
173 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
174 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
175 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
176 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
177 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
178 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
179 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
180 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
181 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
182 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
183 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
184 -- assign xid by inserting
185 INSERT INTO tr_sub(path) VALUES ('2-top-1...--#1');
186 INSERT INTO tr_sub(path) VALUES ('2-top-1...--#2');
187 INSERT INTO tr_sub(path) VALUES ('2-top-1...--#3');
188 RELEASE SAVEPOINT subtop;
189 INSERT INTO tr_sub(path) VALUES ('2-top-#1');
192 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
194 -- make sure rollbacked subtransactions aren't decoded
196 INSERT INTO tr_sub(path) VALUES ('3-top-2-#1');
198 INSERT INTO tr_sub(path) VALUES ('3-top-2-1-#1');
200 INSERT INTO tr_sub(path) VALUES ('3-top-2-2-#1');
201 ROLLBACK TO SAVEPOINT b;
202 INSERT INTO tr_sub(path) VALUES ('3-top-2-#2');
205 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
207 -- test whether a known, but not yet logged toplevel xact, followed by a
208 -- subxact commit is handled correctly
210 SELECT txid_current() != 0; -- so no fixed xid apears in the outfile
212 INSERT INTO tr_sub(path) VALUES ('4-top-1-#1');
216 -- test whether a change in a subtransaction, in an unknown toplevel
217 -- xact is handled correctly.
220 INSERT INTO tr_sub(path) VALUES ('5-top-1-#1');
224 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
228 * Check whether treating a table as a catalog table works somewhat
230 CREATE TABLE replication_metadata (
231 id serial primary key,
232 relation name NOT NULL,
235 WITH (user_catalog_table = true)
237 \d+ replication_metadata
239 INSERT INTO replication_metadata(relation, options)
240 VALUES ('foo', ARRAY['a', 'b']);
242 ALTER TABLE replication_metadata RESET (user_catalog_table);
243 \d+ replication_metadata
245 INSERT INTO replication_metadata(relation, options)
246 VALUES ('bar', ARRAY['a', 'b']);
248 ALTER TABLE replication_metadata SET (user_catalog_table = true);
249 \d+ replication_metadata
251 INSERT INTO replication_metadata(relation, options)
252 VALUES ('blub', NULL);
254 -- make sure rewrites don't work
255 ALTER TABLE replication_metadata ADD COLUMN rewritemeornot int;
256 ALTER TABLE replication_metadata ALTER COLUMN rewritemeornot TYPE text;
258 ALTER TABLE replication_metadata SET (user_catalog_table = false);
259 \d+ replication_metadata
261 INSERT INTO replication_metadata(relation, options)
262 VALUES ('zaphod', NULL);
264 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
267 * check whether we handle updates/deletes correct with & without a pkey
270 /* we should handle the case without a key at all more gracefully */
271 CREATE TABLE table_without_key(id serial, data int);
272 INSERT INTO table_without_key(data) VALUES(1),(2);
273 DELETE FROM table_without_key WHERE data = 1;
274 -- won't log old keys
275 UPDATE table_without_key SET data = 3 WHERE data = 2;
276 UPDATE table_without_key SET id = -id;
277 UPDATE table_without_key SET id = -id;
278 -- should log the full old row now
279 ALTER TABLE table_without_key REPLICA IDENTITY FULL;
280 UPDATE table_without_key SET data = 3 WHERE data = 2;
281 UPDATE table_without_key SET id = -id;
282 UPDATE table_without_key SET id = -id;
283 DELETE FROM table_without_key WHERE data = 3;
285 CREATE TABLE table_with_pkey(id serial primary key, data int);
286 INSERT INTO table_with_pkey(data) VALUES(1), (2);
287 DELETE FROM table_with_pkey WHERE data = 1;
288 -- should log the old pkey
289 UPDATE table_with_pkey SET data = 3 WHERE data = 2;
290 UPDATE table_with_pkey SET id = -id;
291 UPDATE table_with_pkey SET id = -id;
292 -- check that we log nothing despite having a pkey
293 ALTER TABLE table_without_key REPLICA IDENTITY NOTHING;
294 UPDATE table_with_pkey SET id = -id;
295 -- check that we log everything despite having a pkey
296 ALTER TABLE table_without_key REPLICA IDENTITY FULL;
297 UPDATE table_with_pkey SET id = -id;
298 DELETE FROM table_with_pkey WHERE data = 3;
300 CREATE TABLE table_with_unique_not_null(id serial unique, data int);
301 ALTER TABLE table_with_unique_not_null ALTER COLUMN id SET NOT NULL; --already set
302 -- won't log anything, replica identity not setup
303 INSERT INTO table_with_unique_not_null(data) VALUES(1), (2);
304 DELETE FROM table_with_unique_not_null WHERE data = 1;
305 UPDATE table_with_unique_not_null SET data = 3 WHERE data = 2;
306 UPDATE table_with_unique_not_null SET id = -id;
307 UPDATE table_with_unique_not_null SET id = -id;
308 DELETE FROM table_with_unique_not_null WHERE data = 3;
309 -- should log old key
310 ALTER TABLE table_with_unique_not_null REPLICA IDENTITY USING INDEX table_with_unique_not_null_id_key;
311 INSERT INTO table_with_unique_not_null(data) VALUES(1), (2);
312 DELETE FROM table_with_unique_not_null WHERE data = 1;
313 UPDATE table_with_unique_not_null SET data = 3 WHERE data = 2;
314 UPDATE table_with_unique_not_null SET id = -id;
315 UPDATE table_with_unique_not_null SET id = -id;
316 DELETE FROM table_with_unique_not_null WHERE data = 3;
318 -- check toast support
320 CREATE SEQUENCE toasttable_rand_seq START 79 INCREMENT 1499; -- portable "random"
321 CREATE TABLE toasttable(
322 id serial primary key,
324 rand1 float8 DEFAULT nextval('toasttable_rand_seq'),
326 rand2 float8 DEFAULT nextval('toasttable_rand_seq')
329 -- uncompressed external toast data
330 INSERT INTO toasttable(toasted_col1) SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i);
332 -- compressed external toast data
333 INSERT INTO toasttable(toasted_col2) SELECT repeat(string_agg(to_char(g.i, 'FM0000'), ''), 50) FROM generate_series(1, 500) g(i);
335 -- update of existing column
337 SET toasted_col1 = (SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i))
340 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
342 INSERT INTO toasttable(toasted_col1) SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i);
344 -- update of second column, first column unchanged
346 SET toasted_col2 = (SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i))
349 -- make sure we decode correctly even if the toast table is gone
350 DROP TABLE toasttable;
352 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
354 -- done, free logical replication slot
355 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
357 SELECT pg_drop_replication_slot('regression_slot');
359 /* check that the slot is gone */
360 SELECT * FROM pg_replication_slots;