Debian Codegraph @

contrib/test_decoding/sql/ddl.sql

raw
1 -- predictability
2 SET synchronous_commit = on;
3
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');
9
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, 'nonexistent-option', 'frakbar');
13 SELECT 'init' FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', 'frakbar');
14
15 -- succeed once
16 SELECT pg_drop_replication_slot('regression_slot');
17 -- fail
18 SELECT pg_drop_replication_slot('regression_slot');
19
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');
24
25
26 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
27
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_wal_lsn_diff(restart_lsn, '0/01000000') > 0 AS some_wal
33 FROM pg_replication_slots;
34
35 /*
36 * Check that changes are handled correctly when interleaved with ddl
37 */
38 CREATE TABLE replication_example(id SERIAL PRIMARY KEY, somedata int, text varchar(120));
39 BEGIN;
40 INSERT INTO replication_example(somedata, text) VALUES (1, 1);
41 INSERT INTO replication_example(somedata, text) VALUES (1, 2);
42 COMMIT;
43
44 ALTER TABLE replication_example ADD COLUMN bar int;
45
46 INSERT INTO replication_example(somedata, text, bar) VALUES (2, 1, 4);
47
48 BEGIN;
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);
52 COMMIT;
53
54 ALTER TABLE replication_example DROP COLUMN bar;
55 INSERT INTO replication_example(somedata, text) VALUES (3, 1);
56
57 BEGIN;
58 INSERT INTO replication_example(somedata, text) VALUES (3, 2);
59 INSERT INTO replication_example(somedata, text) VALUES (3, 3);
60 COMMIT;
61
62 ALTER TABLE replication_example RENAME COLUMN text TO somenum;
63
64 INSERT INTO replication_example(somedata, somenum) VALUES (4, 1);
65
66 -- collect all changes
67 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
68
69 ALTER TABLE replication_example ALTER COLUMN somenum TYPE int4 USING (somenum::int4);
70 -- check that this doesn't produce any changes from the heap rewrite
71 SELECT count(data) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
72
73 INSERT INTO replication_example(somedata, somenum) VALUES (5, 1);
74
75 BEGIN;
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);
82 COMMIT;
83
84 -- show changes
85 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
86
87 -- ON CONFLICT DO UPDATE support
88 BEGIN;
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;
91 COMMIT;
92
93 /* display results */
94 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
95
96 -- MERGE support
97 BEGIN;
98 MERGE INTO replication_example t
99 USING (SELECT i as id, i as data, i as num FROM generate_series(-20, 5) i) s
100 ON t.id = s.id
101 WHEN MATCHED AND t.id < 0 THEN
102 UPDATE SET somenum = somenum + 1
103 WHEN MATCHED AND t.id >= 0 THEN
104 DELETE
105 WHEN NOT MATCHED THEN
106 INSERT VALUES (s.*);
107 COMMIT;
108
109 /* display results */
110 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
111
112 CREATE TABLE tr_unique(id2 serial unique NOT NULL, data int);
113 INSERT INTO tr_unique(data) VALUES(10);
114 ALTER TABLE tr_unique RENAME TO tr_pkey;
115 ALTER TABLE tr_pkey ADD COLUMN id serial primary key;
116 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'include-rewrites', '1');
117
118 INSERT INTO tr_pkey(data) VALUES(1);
119 --show deletion with primary key
120 DELETE FROM tr_pkey;
121
122 /* display results */
123 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
124
125 /*
126 * check that disk spooling works (also for logical messages)
127 */
128 BEGIN;
129 CREATE TABLE tr_etoomuch (id serial primary key, data int);
130 INSERT INTO tr_etoomuch(data) SELECT g.i FROM generate_series(1, 10234) g(i);
131 SELECT 'tx logical msg' FROM pg_logical_emit_message(true, 'test', 'tx logical msg');
132 DELETE FROM tr_etoomuch WHERE id < 5000;
133 UPDATE tr_etoomuch SET data = - data WHERE id > 5000;
134 CREATE TABLE tr_oddlength (id text primary key, data text);
135 INSERT INTO tr_oddlength VALUES('ab', 'foo');
136 COMMIT;
137
138 /* display results, but hide most of the output */
139 SELECT count(*), min(data), max(data)
140 FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1')
141 GROUP BY substring(data, 1, 24)
142 ORDER BY 1,2;
143
144 -- check updates of primary keys work correctly
145 BEGIN;
146 CREATE TABLE spoolme AS SELECT g.i FROM generate_series(1, 5000) g(i);
147 UPDATE tr_etoomuch SET id = -id WHERE id = 5000;
148 UPDATE tr_oddlength SET id = 'x', data = 'quux';
149 UPDATE tr_oddlength SET id = 'yy', data = 'a';
150 DELETE FROM spoolme;
151 DROP TABLE spoolme;
152 COMMIT;
153
154 SELECT data
155 FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1')
156 WHERE data ~ 'UPDATE';
157
158 -- check that a large, spooled, upsert works
159 INSERT INTO tr_etoomuch (id, data)
160 SELECT g.i, -g.i FROM generate_series(8000, 12000) g(i)
161 ON CONFLICT(id) DO UPDATE SET data = EXCLUDED.data;
162
163 SELECT substring(data, 1, 29), count(*)
164 FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1') WITH ORDINALITY
165 GROUP BY 1
166 ORDER BY min(ordinality);
167
168 /*
169 * check whether we decode subtransactions correctly in relation with each
170 * other
171 */
172 CREATE TABLE tr_sub (id serial primary key, path text);
173
174 -- toplevel, subtxn, toplevel, subtxn, subtxn
175 BEGIN;
176 INSERT INTO tr_sub(path) VALUES ('1-top-#1');
177
178 SAVEPOINT a;
179 INSERT INTO tr_sub(path) VALUES ('1-top-1-#1');
180 INSERT INTO tr_sub(path) VALUES ('1-top-1-#2');
181 RELEASE SAVEPOINT a;
182
183 SAVEPOINT b;
184 SAVEPOINT c;
185 INSERT INTO tr_sub(path) VALUES ('1-top-2-1-#1');
186 INSERT INTO tr_sub(path) VALUES ('1-top-2-1-#2');
187 RELEASE SAVEPOINT c;
188 INSERT INTO tr_sub(path) VALUES ('1-top-2-#1');
189 RELEASE SAVEPOINT b;
190 COMMIT;
191
192 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
193
194 -- check that we handle xlog assignments correctly
195 BEGIN;
196 -- nest 80 subtxns
197 SAVEPOINT subtop;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
198 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
199 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
200 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
201 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
202 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
203 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
204 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
205 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
206 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
207 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
208 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
209 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
210 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
211 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
212 SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;SAVEPOINT a;
213 -- assign xid by inserting
214 INSERT INTO tr_sub(path) VALUES ('2-top-1...--#1');
215 INSERT INTO tr_sub(path) VALUES ('2-top-1...--#2');
216 INSERT INTO tr_sub(path) VALUES ('2-top-1...--#3');
217 RELEASE SAVEPOINT subtop;
218 INSERT INTO tr_sub(path) VALUES ('2-top-#1');
219 COMMIT;
220
221 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
222
223 -- make sure rollbacked subtransactions aren't decoded
224 BEGIN;
225 INSERT INTO tr_sub(path) VALUES ('3-top-2-#1');
226 SAVEPOINT a;
227 INSERT INTO tr_sub(path) VALUES ('3-top-2-1-#1');
228 SAVEPOINT b;
229 INSERT INTO tr_sub(path) VALUES ('3-top-2-2-#1');
230 ROLLBACK TO SAVEPOINT b;
231 INSERT INTO tr_sub(path) VALUES ('3-top-2-#2');
232 COMMIT;
233
234 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
235
236 -- test whether a known, but not yet logged toplevel xact, followed by a
237 -- subxact commit is handled correctly
238 BEGIN;
239 SELECT pg_current_xact_id() != '0'; -- so no fixed xid appears in the outfile
240 SAVEPOINT a;
241 INSERT INTO tr_sub(path) VALUES ('4-top-1-#1');
242 RELEASE SAVEPOINT a;
243 COMMIT;
244
245 -- test whether a change in a subtransaction, in an unknown toplevel
246 -- xact is handled correctly.
247 BEGIN;
248 SAVEPOINT a;
249 INSERT INTO tr_sub(path) VALUES ('5-top-1-#1');
250 COMMIT;
251
252
253 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
254
255 -- check that DDL in aborted subtransactions handled correctly
256 CREATE TABLE tr_sub_ddl(data int);
257 BEGIN;
258 SAVEPOINT a;
259 ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE text;
260 INSERT INTO tr_sub_ddl VALUES ('blah-blah');
261 ROLLBACK TO SAVEPOINT a;
262 ALTER TABLE tr_sub_ddl ALTER COLUMN data TYPE bigint;
263 INSERT INTO tr_sub_ddl VALUES(43);
264 COMMIT;
265
266 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
267
268
269 /*
270 * Check whether treating a table as a catalog table works somewhat
271 */
272 CREATE TABLE replication_metadata (
273 id serial primary key,
274 relation name NOT NULL,
275 options text[]
276 )
277 WITH (user_catalog_table = true)
278 ;
279 \d+ replication_metadata
280
281 INSERT INTO replication_metadata(relation, options)
282 VALUES ('foo', ARRAY['a', 'b']);
283
284 ALTER TABLE replication_metadata RESET (user_catalog_table);
285 \d+ replication_metadata
286
287 INSERT INTO replication_metadata(relation, options)
288 VALUES ('bar', ARRAY['a', 'b']);
289
290 ALTER TABLE replication_metadata SET (user_catalog_table = true);
291 \d+ replication_metadata
292
293 INSERT INTO replication_metadata(relation, options)
294 VALUES ('blub', NULL);
295
296 -- make sure rewrites don't work
297 ALTER TABLE replication_metadata ADD COLUMN rewritemeornot int;
298 ALTER TABLE replication_metadata ALTER COLUMN rewritemeornot TYPE text;
299
300 ALTER TABLE replication_metadata SET (user_catalog_table = false);
301 \d+ replication_metadata
302
303 INSERT INTO replication_metadata(relation, options)
304 VALUES ('zaphod', NULL);
305
306 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
307
308 /*
309 * check whether we handle updates/deletes correct with & without a pkey
310 */
311
312 /* we should handle the case without a key at all more gracefully */
313 CREATE TABLE table_without_key(id serial, data int);
314 INSERT INTO table_without_key(data) VALUES(1),(2);
315 DELETE FROM table_without_key WHERE data = 1;
316 -- won't log old keys
317 UPDATE table_without_key SET data = 3 WHERE data = 2;
318 UPDATE table_without_key SET id = -id;
319 UPDATE table_without_key SET id = -id;
320 -- should log the full old row now
321 ALTER TABLE table_without_key REPLICA IDENTITY FULL;
322 UPDATE table_without_key SET data = 3 WHERE data = 2;
323 UPDATE table_without_key SET id = -id;
324 UPDATE table_without_key SET id = -id;
325 -- ensure that FULL correctly deals with new columns
326 ALTER TABLE table_without_key ADD COLUMN new_column text;
327 UPDATE table_without_key SET id = -id;
328 UPDATE table_without_key SET id = -id, new_column = 'someval';
329 DELETE FROM table_without_key WHERE data = 3;
330
331 CREATE TABLE table_with_pkey(id serial primary key, data int);
332 INSERT INTO table_with_pkey(data) VALUES(1), (2);
333 DELETE FROM table_with_pkey WHERE data = 1;
334 -- should log the old pkey
335 UPDATE table_with_pkey SET data = 3 WHERE data = 2;
336 UPDATE table_with_pkey SET id = -id;
337 UPDATE table_with_pkey SET id = -id;
338 -- check that we log nothing despite having a pkey
339 ALTER TABLE table_without_key REPLICA IDENTITY NOTHING;
340 UPDATE table_with_pkey SET id = -id;
341 -- check that we log everything despite having a pkey
342 ALTER TABLE table_without_key REPLICA IDENTITY FULL;
343 UPDATE table_with_pkey SET id = -id;
344 DELETE FROM table_with_pkey WHERE data = 3;
345
346 CREATE TABLE table_with_unique_not_null(id serial unique, data int);
347 ALTER TABLE table_with_unique_not_null ALTER COLUMN id SET NOT NULL; --already set
348 -- won't log anything, replica identity not setup
349 INSERT INTO table_with_unique_not_null(data) VALUES(1), (2);
350 DELETE FROM table_with_unique_not_null WHERE data = 1;
351 UPDATE table_with_unique_not_null SET data = 3 WHERE data = 2;
352 UPDATE table_with_unique_not_null SET id = -id;
353 UPDATE table_with_unique_not_null SET id = -id;
354 DELETE FROM table_with_unique_not_null WHERE data = 3;
355 -- should log old key
356 ALTER TABLE table_with_unique_not_null REPLICA IDENTITY USING INDEX table_with_unique_not_null_id_key;
357 INSERT INTO table_with_unique_not_null(data) VALUES(1), (2);
358 DELETE FROM table_with_unique_not_null WHERE data = 1;
359 UPDATE table_with_unique_not_null SET data = 3 WHERE data = 2;
360 UPDATE table_with_unique_not_null SET id = -id;
361 UPDATE table_with_unique_not_null SET id = -id;
362 DELETE FROM table_with_unique_not_null WHERE data = 3;
363
364 -- check tables with dropped indexes used in REPLICA IDENTITY
365 -- table with primary key
366 CREATE TABLE table_dropped_index_with_pk (a int PRIMARY KEY, b int, c int);
367 CREATE UNIQUE INDEX table_dropped_index_with_pk_idx
368 ON table_dropped_index_with_pk(a);
369 ALTER TABLE table_dropped_index_with_pk REPLICA IDENTITY
370 USING INDEX table_dropped_index_with_pk_idx;
371 DROP INDEX table_dropped_index_with_pk_idx;
372 INSERT INTO table_dropped_index_with_pk VALUES (1,1,1), (2,2,2), (3,3,3);
373 UPDATE table_dropped_index_with_pk SET a = 4 WHERE a = 1;
374 UPDATE table_dropped_index_with_pk SET b = 5 WHERE a = 2;
375 UPDATE table_dropped_index_with_pk SET b = 6, c = 7 WHERE a = 3;
376 DELETE FROM table_dropped_index_with_pk WHERE b = 1;
377 DELETE FROM table_dropped_index_with_pk WHERE a = 3;
378 DROP TABLE table_dropped_index_with_pk;
379
380 -- table without primary key
381 CREATE TABLE table_dropped_index_no_pk (a int NOT NULL, b int, c int);
382 CREATE UNIQUE INDEX table_dropped_index_no_pk_idx
383 ON table_dropped_index_no_pk(a);
384 ALTER TABLE table_dropped_index_no_pk REPLICA IDENTITY
385 USING INDEX table_dropped_index_no_pk_idx;
386 DROP INDEX table_dropped_index_no_pk_idx;
387 INSERT INTO table_dropped_index_no_pk VALUES (1,1,1), (2,2,2), (3,3,3);
388 UPDATE table_dropped_index_no_pk SET a = 4 WHERE a = 1;
389 UPDATE table_dropped_index_no_pk SET b = 5 WHERE a = 2;
390 UPDATE table_dropped_index_no_pk SET b = 6, c = 7 WHERE a = 3;
391 DELETE FROM table_dropped_index_no_pk WHERE b = 1;
392 DELETE FROM table_dropped_index_no_pk WHERE a = 3;
393 DROP TABLE table_dropped_index_no_pk;
394
395 -- check toast support
396 BEGIN;
397 CREATE SEQUENCE toasttable_rand_seq START 79 INCREMENT 1499; -- portable "random"
398 CREATE TABLE toasttable(
399 id serial primary key,
400 toasted_col1 text,
401 rand1 float8 DEFAULT nextval('toasttable_rand_seq'),
402 toasted_col2 text,
403 rand2 float8 DEFAULT nextval('toasttable_rand_seq')
404 );
405 COMMIT;
406 -- uncompressed external toast data
407 INSERT INTO toasttable(toasted_col1) SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i);
408
409 -- compressed external toast data
410 INSERT INTO toasttable(toasted_col2) SELECT repeat(string_agg(to_char(g.i, 'FM0000'), ''), 50) FROM generate_series(1, 500) g(i);
411
412 -- update of existing column
413 UPDATE toasttable
414 SET toasted_col1 = (SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i))
415 WHERE id = 1;
416
417 -- This output is extremely wide, and using aligned mode causes psql to
418 -- produce 200kB of useless dashes. Turn that off temporarily to avoid it.
419 \pset format unaligned
420 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
421 \pset format aligned
422
423 INSERT INTO toasttable(toasted_col1) SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i);
424
425 -- update of second column, first column unchanged
426 UPDATE toasttable
427 SET toasted_col2 = (SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i))
428 WHERE id = 1;
429
430 -- make sure we decode correctly even if the toast table is gone
431 DROP TABLE toasttable;
432
433 \pset format unaligned
434 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
435
436 -- done, free logical replication slot
437 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
438 \pset format aligned
439
440 SELECT pg_drop_replication_slot('regression_slot');
441
442 /* check that the slot is gone */
443 \x
444 SELECT * FROM pg_replication_slots;
445 \x