Debian Codegraph @

contrib/test_decoding/sql/decoding_in_xact.sql

raw
1 -- predictability
2 SET synchronous_commit = on;
3
4 -- fail because we're creating a slot while in an xact with xid
5 BEGIN;
6 SELECT pg_current_xact_id() = '0';
7 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
8 ROLLBACK;
9
10 -- fail because we're creating a slot while in a subxact whose topxact has an xid
11 BEGIN;
12 SELECT pg_current_xact_id() = '0';
13 SAVEPOINT barf;
14 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
15 ROLLBACK TO SAVEPOINT barf;
16 ROLLBACK;
17
18 -- succeed, outside tx.
19 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
20 SELECT 'stop' FROM pg_drop_replication_slot('regression_slot');
21
22 -- succeed, in tx without xid.
23 BEGIN;
24 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
25 COMMIT;
26
27 CREATE TABLE nobarf(id serial primary key, data text);
28 INSERT INTO nobarf(data) VALUES('1');
29
30 -- decoding works in transaction with xid
31 BEGIN;
32 SELECT pg_current_xact_id() = '0';
33 -- don't show yet, haven't committed
34 INSERT INTO nobarf(data) VALUES('2');
35 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
36 COMMIT;
37
38 INSERT INTO nobarf(data) VALUES('3');
39 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
40
41 -- Decoding works in transaction that issues DDL
42 --
43 -- We had issues handling relcache invalidations with these, see
44 -- https://www.postgresql.org/message-id/[email protected]
45 CREATE TABLE tbl_created_outside_xact(id SERIAL PRIMARY KEY);
46 BEGIN;
47 -- TRUNCATE changes the relfilenode and sends relcache invalidation
48 TRUNCATE tbl_created_outside_xact;
49 INSERT INTO tbl_created_outside_xact(id) VALUES('1');
50
51 -- don't show yet, haven't committed
52 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
53 COMMIT;
54 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
55
56 SET debug_logical_replication_streaming = immediate;
57 BEGIN;
58 CREATE TABLE tbl_created_in_xact(id SERIAL PRIMARY KEY);
59 INSERT INTO tbl_created_in_xact VALUES (1);
60
61 CHECKPOINT; -- Force WAL flush, so that the above changes will be streamed
62
63 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1');
64 COMMIT;
65 RESET debug_logical_replication_streaming;
66 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
67
68 SELECT 'stop' FROM pg_drop_replication_slot('regression_slot');