]> granicus.if.org Git - postgresql/blob - contrib/test_decoding/sql/ddl.sql
Add support for INSERT ... ON CONFLICT DO NOTHING/UPDATE.
[postgresql] / contrib / test_decoding / sql / ddl.sql
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, 'nonexistant-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_xlog_location_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 -- 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');
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, 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)
97 ORDER BY 1,2;
98
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');
105
106 INSERT INTO tr_pkey(data) VALUES(1);
107 --show deletion with primary key
108 DELETE FROM tr_pkey;
109
110 /* display results */
111 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
112
113 /*
114  * check that disk spooling works
115  */
116 BEGIN;
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;
121 COMMIT;
122
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)
127 ORDER BY 1,2;
128
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;
133
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')
136 GROUP BY 1
137 ORDER BY min(location - '0/0');
138
139 /*
140  * check whether we decode subtransactions correctly in relation with each
141  * other
142  */
143 CREATE TABLE tr_sub (id serial primary key, path text);
144
145 -- toplevel, subtxn, toplevel, subtxn, subtxn
146 BEGIN;
147 INSERT INTO tr_sub(path) VALUES ('1-top-#1');
148
149 SAVEPOINT a;
150 INSERT INTO tr_sub(path) VALUES ('1-top-1-#1');
151 INSERT INTO tr_sub(path) VALUES ('1-top-1-#2');
152 RELEASE SAVEPOINT a;
153
154 SAVEPOINT b;
155 SAVEPOINT c;
156 INSERT INTO tr_sub(path) VALUES ('1-top-2-1-#1');
157 INSERT INTO tr_sub(path) VALUES ('1-top-2-1-#2');
158 RELEASE SAVEPOINT c;
159 INSERT INTO tr_sub(path) VALUES ('1-top-2-#1');
160 RELEASE SAVEPOINT b;
161 COMMIT;
162
163 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
164
165 -- check that we handle xlog assignments correctly
166 BEGIN;
167 -- nest 80 subtxns
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');
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 -- make sure rollbacked subtransactions aren't decoded
195 BEGIN;
196 INSERT INTO tr_sub(path) VALUES ('3-top-2-#1');
197 SAVEPOINT a;
198 INSERT INTO tr_sub(path) VALUES ('3-top-2-1-#1');
199 SAVEPOINT b;
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');
203 COMMIT;
204
205 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
206
207 -- test whether a known, but not yet logged toplevel xact, followed by a
208 -- subxact commit is handled correctly
209 BEGIN;
210 SELECT txid_current() != 0; -- so no fixed xid apears in the outfile
211 SAVEPOINT a;
212 INSERT INTO tr_sub(path) VALUES ('4-top-1-#1');
213 RELEASE SAVEPOINT a;
214 COMMIT;
215
216 -- test whether a change in a subtransaction, in an unknown toplevel
217 -- xact is handled correctly.
218 BEGIN;
219 SAVEPOINT a;
220 INSERT INTO tr_sub(path) VALUES ('5-top-1-#1');
221 COMMIT;
222
223
224 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
225
226
227 /*
228  * Check whether treating a table as a catalog table works somewhat
229  */
230 CREATE TABLE replication_metadata (
231     id serial primary key,
232     relation name NOT NULL,
233     options text[]
234 )
235 WITH (user_catalog_table = true)
236 ;
237 \d+ replication_metadata
238
239 INSERT INTO replication_metadata(relation, options)
240 VALUES ('foo', ARRAY['a', 'b']);
241
242 ALTER TABLE replication_metadata RESET (user_catalog_table);
243 \d+ replication_metadata
244
245 INSERT INTO replication_metadata(relation, options)
246 VALUES ('bar', ARRAY['a', 'b']);
247
248 ALTER TABLE replication_metadata SET (user_catalog_table = true);
249 \d+ replication_metadata
250
251 INSERT INTO replication_metadata(relation, options)
252 VALUES ('blub', NULL);
253
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;
257
258 ALTER TABLE replication_metadata SET (user_catalog_table = false);
259 \d+ replication_metadata
260
261 INSERT INTO replication_metadata(relation, options)
262 VALUES ('zaphod', NULL);
263
264 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
265
266 /*
267  * check whether we handle updates/deletes correct with & without a pkey
268  */
269
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;
284
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;
299
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;
317
318 -- check toast support
319 BEGIN;
320 CREATE SEQUENCE toasttable_rand_seq START 79 INCREMENT 1499; -- portable "random"
321 CREATE TABLE toasttable(
322        id serial primary key,
323        toasted_col1 text,
324        rand1 float8 DEFAULT nextval('toasttable_rand_seq'),
325        toasted_col2 text,
326        rand2 float8 DEFAULT nextval('toasttable_rand_seq')
327        );
328 COMMIT;
329 -- uncompressed external toast data
330 INSERT INTO toasttable(toasted_col1) SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i);
331
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);
334
335 -- update of existing column
336 UPDATE toasttable
337     SET toasted_col1 = (SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i))
338 WHERE id = 1;
339
340 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
341
342 INSERT INTO toasttable(toasted_col1) SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i);
343
344 -- update of second column, first column unchanged
345 UPDATE toasttable
346     SET toasted_col2 = (SELECT string_agg(g.i::text, '') FROM generate_series(1, 2000) g(i))
347 WHERE id = 1;
348
349 -- make sure we decode correctly even if the toast table is gone
350 DROP TABLE toasttable;
351
352 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
353
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');
356
357 SELECT pg_drop_replication_slot('regression_slot');
358
359 /* check that the slot is gone */
360 SELECT * FROM pg_replication_slots;