Debian Codegraph @

contrib/test_decoding/sql/slot.sql

raw
1 -- predictability
2 SET synchronous_commit = on;
3
4 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_p', 'test_decoding');
5 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t', 'test_decoding', true);
6
7 SELECT pg_drop_replication_slot('regression_slot_p');
8 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_p', 'test_decoding', false);
9
10 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t2', 'test_decoding', true);
11
12 BEGIN;
13 SET LOCAL output_plugin_libraries = nonexistent;
14 SELECT pg_create_logical_replication_slot('foo', 'nonexistent');
15 ROLLBACK;
16
17 -- here we want to start a new session and wait till old one is gone
18 select pg_backend_pid() as oldpid \gset
19 \c -
20 SET synchronous_commit = on;
21
22 do 'declare c int = 0;
23 begin
24 while (select count(*) from pg_replication_slots where active_pid = '
25 :'oldpid'
26 ') > 0 loop c := c + 1; perform pg_sleep(0.01); end loop;
27 raise log ''slot test looped % times'', c;
28 end';
29
30 -- should fail because the temporary slots were dropped automatically
31 SELECT pg_drop_replication_slot('regression_slot_t');
32 SELECT pg_drop_replication_slot('regression_slot_t2');
33
34 -- monitoring functions for slot directories
35 SELECT count(*) >= 0 AS ok FROM pg_ls_logicalmapdir();
36 SELECT count(*) >= 0 AS ok FROM pg_ls_logicalsnapdir();
37 SELECT count(*) >= 0 AS ok FROM pg_ls_replslotdir('regression_slot_p');
38 SELECT count(*) >= 0 AS ok FROM pg_ls_replslotdir('not_existing_slot'); -- fails
39
40 -- permanent slot has survived
41 SELECT pg_drop_replication_slot('regression_slot_p');
42
43 -- test switching between slots in a session
44 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot1', 'test_decoding', true);
45
46 CREATE TABLE replication_example(id SERIAL PRIMARY KEY, somedata int, text varchar(120));
47 BEGIN;
48 INSERT INTO replication_example(somedata, text) VALUES (1, 1);
49 INSERT INTO replication_example(somedata, text) VALUES (1, 2);
50 COMMIT;
51
52 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot2', 'test_decoding', true);
53
54 INSERT INTO replication_example(somedata, text) VALUES (1, 3);
55
56 SELECT data FROM pg_logical_slot_get_changes('regression_slot1', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
57 SELECT data FROM pg_logical_slot_get_changes('regression_slot2', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
58
59 INSERT INTO replication_example(somedata, text) VALUES (1, 4);
60 INSERT INTO replication_example(somedata, text) VALUES (1, 5);
61
62 SELECT pg_current_wal_lsn() AS wal_lsn \gset
63
64 INSERT INTO replication_example(somedata, text) VALUES (1, 6);
65
66 SELECT end_lsn FROM pg_replication_slot_advance('regression_slot1', :'wal_lsn') \gset
67 SELECT slot_name FROM pg_replication_slot_advance('regression_slot2', pg_current_wal_lsn());
68
69 SELECT :'wal_lsn' = :'end_lsn';
70
71 SELECT data FROM pg_logical_slot_get_changes('regression_slot1', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
72 SELECT data FROM pg_logical_slot_get_changes('regression_slot2', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
73
74 DROP TABLE replication_example;
75
76 -- error
77 SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot1', 'test_decoding', true);
78
79 -- both should error as they should be dropped on error
80 SELECT pg_drop_replication_slot('regression_slot1');
81 SELECT pg_drop_replication_slot('regression_slot2');
82
83 -- slot advance with physical slot, error with non-reserved slot
84 SELECT slot_name FROM pg_create_physical_replication_slot('regression_slot3');
85 SELECT pg_replication_slot_advance('regression_slot3', '0/0'); -- invalid LSN
86 SELECT pg_replication_slot_advance('regression_slot3', '0/1'); -- error
87 SELECT pg_drop_replication_slot('regression_slot3');
88
89 --
90 -- Test copy functions for logical replication slots
91 --
92
93 -- Create and copy logical slots
94 SELECT 'init' FROM pg_create_logical_replication_slot('orig_slot1', 'test_decoding', false);
95 SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_no_change');
96 SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_change_plugin', false, 'pgoutput');
97 SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_change_plugin_temp', true, 'pgoutput');
98
99 -- Check all copied slots status
100 SELECT
101 o.slot_name, o.plugin, o.temporary, c.slot_name, c.plugin, c.temporary
102 FROM
103 (SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
104 LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn AND o.confirmed_flush_lsn = c.confirmed_flush_lsn
105 WHERE
106 o.slot_name != c.slot_name
107 ORDER BY o.slot_name, c.slot_name;
108
109 -- Now we have maximum 4 replication slots. Check slots are properly
110 -- released even when raise error during creating the target slot.
111 SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error
112
113 -- temporary slots were dropped automatically
114 SELECT pg_drop_replication_slot('orig_slot1');
115 SELECT pg_drop_replication_slot('copied_slot1_no_change');
116 SELECT pg_drop_replication_slot('copied_slot1_change_plugin');
117
118 -- Test based on the temporary logical slot
119 SELECT 'init' FROM pg_create_logical_replication_slot('orig_slot2', 'test_decoding', true);
120 SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_no_change');
121 SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_change_plugin', true, 'pgoutput');
122 SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_change_plugin_temp', false, 'pgoutput');
123
124 -- Check all copied slots status
125 SELECT
126 o.slot_name, o.plugin, o.temporary, c.slot_name, c.plugin, c.temporary
127 FROM
128 (SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
129 LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn AND o.confirmed_flush_lsn = c.confirmed_flush_lsn
130 WHERE
131 o.slot_name != c.slot_name
132 ORDER BY o.slot_name, c.slot_name;
133
134 -- Cannot copy a logical slot to a physical slot
135 SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error
136
137 -- temporary slots were dropped automatically
138 SELECT pg_drop_replication_slot('copied_slot2_change_plugin_temp');
139
140 --
141 -- Test copy functions for physical replication slots
142 --
143
144 -- Create and copy physical slots
145 SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot1', true);
146 SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot2', false);
147 SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'copied_slot1_no_change');
148 SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'copied_slot1_temp', true);
149
150 -- Check all copied slots status. Since all slots don't reserve WAL we check only other fields.
151 SELECT slot_name, slot_type, temporary FROM pg_replication_slots;
152
153 -- Cannot copy a physical slot to a logical slot
154 SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error
155
156 -- Cannot copy a physical slot that doesn't reserve WAL
157 SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error
158
159 -- temporary slots were dropped automatically
160 SELECT pg_drop_replication_slot('orig_slot1');
161 SELECT pg_drop_replication_slot('orig_slot2');
162 SELECT pg_drop_replication_slot('copied_slot1_no_change');
163
164 -- Test based on the temporary physical slot
165 SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot2', true, true);
166 SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'copied_slot2_no_change');
167 SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'copied_slot2_notemp', false);
168
169 -- Check all copied slots status
170 SELECT
171 o.slot_name, o.temporary, c.slot_name, c.temporary
172 FROM
173 (SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o
174 LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn
175 WHERE
176 o.slot_name != c.slot_name
177 ORDER BY o.slot_name, c.slot_name;
178
179 SELECT pg_drop_replication_slot('orig_slot2');
180 SELECT pg_drop_replication_slot('copied_slot2_no_change');
181 SELECT pg_drop_replication_slot('copied_slot2_notemp');
182
183 -- Test failover option of slots.
184 SELECT 'init' FROM pg_create_logical_replication_slot('failover_true_slot', 'test_decoding', false, false, true);
185 SELECT 'init' FROM pg_create_logical_replication_slot('failover_false_slot', 'test_decoding', false, false, false);
186 SELECT 'init' FROM pg_create_logical_replication_slot('failover_default_slot', 'test_decoding', false, false);
187 SELECT 'init' FROM pg_create_logical_replication_slot('failover_true_temp_slot', 'test_decoding', true, false, true);
188 SELECT 'init' FROM pg_create_physical_replication_slot('physical_slot');
189
190 SELECT slot_name, slot_type, failover FROM pg_replication_slots;
191
192 SELECT pg_drop_replication_slot('failover_true_slot');
193 SELECT pg_drop_replication_slot('failover_false_slot');
194 SELECT pg_drop_replication_slot('failover_default_slot');
195 SELECT pg_drop_replication_slot('physical_slot');