| 1 |
-- predictability |
| 2 |
SET synchronous_commit = on; |
| 3 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_p', 'test_decoding'); |
| 4 |
?column? |
| 5 |
---------- |
| 6 |
init |
| 7 |
(1 row) |
| 8 |
|
| 9 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t', 'test_decoding', true); |
| 10 |
?column? |
| 11 |
---------- |
| 12 |
init |
| 13 |
(1 row) |
| 14 |
|
| 15 |
SELECT pg_drop_replication_slot('regression_slot_p'); |
| 16 |
pg_drop_replication_slot |
| 17 |
-------------------------- |
| 18 |
|
| 19 |
(1 row) |
| 20 |
|
| 21 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_p', 'test_decoding', false); |
| 22 |
?column? |
| 23 |
---------- |
| 24 |
init |
| 25 |
(1 row) |
| 26 |
|
| 27 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t2', 'test_decoding', true); |
| 28 |
?column? |
| 29 |
---------- |
| 30 |
init |
| 31 |
(1 row) |
| 32 |
|
| 33 |
BEGIN; |
| 34 |
SET LOCAL output_plugin_libraries = nonexistent; |
| 35 |
SELECT pg_create_logical_replication_slot('foo', 'nonexistent'); |
| 36 |
ERROR: could not access file "nonexistent": No such file or directory |
| 37 |
ROLLBACK; |
| 38 |
-- here we want to start a new session and wait till old one is gone |
| 39 |
select pg_backend_pid() as oldpid \gset |
| 40 |
\c - |
| 41 |
SET synchronous_commit = on; |
| 42 |
do 'declare c int = 0; |
| 43 |
begin |
| 44 |
while (select count(*) from pg_replication_slots where active_pid = ' |
| 45 |
:'oldpid' |
| 46 |
') > 0 loop c := c + 1; perform pg_sleep(0.01); end loop; |
| 47 |
raise log ''slot test looped % times'', c; |
| 48 |
end'; |
| 49 |
-- should fail because the temporary slots were dropped automatically |
| 50 |
SELECT pg_drop_replication_slot('regression_slot_t'); |
| 51 |
ERROR: replication slot "regression_slot_t" does not exist |
| 52 |
SELECT pg_drop_replication_slot('regression_slot_t2'); |
| 53 |
ERROR: replication slot "regression_slot_t2" does not exist |
| 54 |
-- monitoring functions for slot directories |
| 55 |
SELECT count(*) >= 0 AS ok FROM pg_ls_logicalmapdir(); |
| 56 |
ok |
| 57 |
---- |
| 58 |
t |
| 59 |
(1 row) |
| 60 |
|
| 61 |
SELECT count(*) >= 0 AS ok FROM pg_ls_logicalsnapdir(); |
| 62 |
ok |
| 63 |
---- |
| 64 |
t |
| 65 |
(1 row) |
| 66 |
|
| 67 |
SELECT count(*) >= 0 AS ok FROM pg_ls_replslotdir('regression_slot_p'); |
| 68 |
ok |
| 69 |
---- |
| 70 |
t |
| 71 |
(1 row) |
| 72 |
|
| 73 |
SELECT count(*) >= 0 AS ok FROM pg_ls_replslotdir('not_existing_slot'); -- fails |
| 74 |
ERROR: replication slot "not_existing_slot" does not exist |
| 75 |
-- permanent slot has survived |
| 76 |
SELECT pg_drop_replication_slot('regression_slot_p'); |
| 77 |
pg_drop_replication_slot |
| 78 |
-------------------------- |
| 79 |
|
| 80 |
(1 row) |
| 81 |
|
| 82 |
-- test switching between slots in a session |
| 83 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot1', 'test_decoding', true); |
| 84 |
?column? |
| 85 |
---------- |
| 86 |
init |
| 87 |
(1 row) |
| 88 |
|
| 89 |
CREATE TABLE replication_example(id SERIAL PRIMARY KEY, somedata int, text varchar(120)); |
| 90 |
BEGIN; |
| 91 |
INSERT INTO replication_example(somedata, text) VALUES (1, 1); |
| 92 |
INSERT INTO replication_example(somedata, text) VALUES (1, 2); |
| 93 |
COMMIT; |
| 94 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot2', 'test_decoding', true); |
| 95 |
?column? |
| 96 |
---------- |
| 97 |
init |
| 98 |
(1 row) |
| 99 |
|
| 100 |
INSERT INTO replication_example(somedata, text) VALUES (1, 3); |
| 101 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot1', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 102 |
data |
| 103 |
--------------------------------------------------------------------------------------------------------- |
| 104 |
BEGIN |
| 105 |
table public.replication_example: INSERT: id[integer]:1 somedata[integer]:1 text[character varying]:'1' |
| 106 |
table public.replication_example: INSERT: id[integer]:2 somedata[integer]:1 text[character varying]:'2' |
| 107 |
COMMIT |
| 108 |
BEGIN |
| 109 |
table public.replication_example: INSERT: id[integer]:3 somedata[integer]:1 text[character varying]:'3' |
| 110 |
COMMIT |
| 111 |
(7 rows) |
| 112 |
|
| 113 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot2', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 114 |
data |
| 115 |
--------------------------------------------------------------------------------------------------------- |
| 116 |
BEGIN |
| 117 |
table public.replication_example: INSERT: id[integer]:3 somedata[integer]:1 text[character varying]:'3' |
| 118 |
COMMIT |
| 119 |
(3 rows) |
| 120 |
|
| 121 |
INSERT INTO replication_example(somedata, text) VALUES (1, 4); |
| 122 |
INSERT INTO replication_example(somedata, text) VALUES (1, 5); |
| 123 |
SELECT pg_current_wal_lsn() AS wal_lsn \gset |
| 124 |
INSERT INTO replication_example(somedata, text) VALUES (1, 6); |
| 125 |
SELECT end_lsn FROM pg_replication_slot_advance('regression_slot1', :'wal_lsn') \gset |
| 126 |
SELECT slot_name FROM pg_replication_slot_advance('regression_slot2', pg_current_wal_lsn()); |
| 127 |
slot_name |
| 128 |
------------------ |
| 129 |
regression_slot2 |
| 130 |
(1 row) |
| 131 |
|
| 132 |
SELECT :'wal_lsn' = :'end_lsn'; |
| 133 |
?column? |
| 134 |
---------- |
| 135 |
t |
| 136 |
(1 row) |
| 137 |
|
| 138 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot1', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 139 |
data |
| 140 |
--------------------------------------------------------------------------------------------------------- |
| 141 |
BEGIN |
| 142 |
table public.replication_example: INSERT: id[integer]:6 somedata[integer]:1 text[character varying]:'6' |
| 143 |
COMMIT |
| 144 |
(3 rows) |
| 145 |
|
| 146 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot2', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 147 |
data |
| 148 |
------ |
| 149 |
(0 rows) |
| 150 |
|
| 151 |
DROP TABLE replication_example; |
| 152 |
-- error |
| 153 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot1', 'test_decoding', true); |
| 154 |
ERROR: replication slot "regression_slot1" already exists |
| 155 |
-- both should error as they should be dropped on error |
| 156 |
SELECT pg_drop_replication_slot('regression_slot1'); |
| 157 |
ERROR: replication slot "regression_slot1" does not exist |
| 158 |
SELECT pg_drop_replication_slot('regression_slot2'); |
| 159 |
ERROR: replication slot "regression_slot2" does not exist |
| 160 |
-- slot advance with physical slot, error with non-reserved slot |
| 161 |
SELECT slot_name FROM pg_create_physical_replication_slot('regression_slot3'); |
| 162 |
slot_name |
| 163 |
------------------ |
| 164 |
regression_slot3 |
| 165 |
(1 row) |
| 166 |
|
| 167 |
SELECT pg_replication_slot_advance('regression_slot3', '0/0'); -- invalid LSN |
| 168 |
ERROR: invalid target WAL LSN |
| 169 |
SELECT pg_replication_slot_advance('regression_slot3', '0/1'); -- error |
| 170 |
ERROR: replication slot "regression_slot3" cannot be advanced |
| 171 |
DETAIL: This slot has never previously reserved WAL, or it has been invalidated. |
| 172 |
SELECT pg_drop_replication_slot('regression_slot3'); |
| 173 |
pg_drop_replication_slot |
| 174 |
-------------------------- |
| 175 |
|
| 176 |
(1 row) |
| 177 |
|
| 178 |
-- |
| 179 |
-- Test copy functions for logical replication slots |
| 180 |
-- |
| 181 |
-- Create and copy logical slots |
| 182 |
SELECT 'init' FROM pg_create_logical_replication_slot('orig_slot1', 'test_decoding', false); |
| 183 |
?column? |
| 184 |
---------- |
| 185 |
init |
| 186 |
(1 row) |
| 187 |
|
| 188 |
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_no_change'); |
| 189 |
?column? |
| 190 |
---------- |
| 191 |
copy |
| 192 |
(1 row) |
| 193 |
|
| 194 |
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_change_plugin', false, 'pgoutput'); |
| 195 |
?column? |
| 196 |
---------- |
| 197 |
copy |
| 198 |
(1 row) |
| 199 |
|
| 200 |
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'copied_slot1_change_plugin_temp', true, 'pgoutput'); |
| 201 |
?column? |
| 202 |
---------- |
| 203 |
copy |
| 204 |
(1 row) |
| 205 |
|
| 206 |
-- Check all copied slots status |
| 207 |
SELECT |
| 208 |
o.slot_name, o.plugin, o.temporary, c.slot_name, c.plugin, c.temporary |
| 209 |
FROM |
| 210 |
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o |
| 211 |
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn AND o.confirmed_flush_lsn = c.confirmed_flush_lsn |
| 212 |
WHERE |
| 213 |
o.slot_name != c.slot_name |
| 214 |
ORDER BY o.slot_name, c.slot_name; |
| 215 |
slot_name | plugin | temporary | slot_name | plugin | temporary |
| 216 |
------------+---------------+-----------+---------------------------------+---------------+----------- |
| 217 |
orig_slot1 | test_decoding | f | copied_slot1_change_plugin | pgoutput | f |
| 218 |
orig_slot1 | test_decoding | f | copied_slot1_change_plugin_temp | pgoutput | t |
| 219 |
orig_slot1 | test_decoding | f | copied_slot1_no_change | test_decoding | f |
| 220 |
(3 rows) |
| 221 |
|
| 222 |
-- Now we have maximum 4 replication slots. Check slots are properly |
| 223 |
-- released even when raise error during creating the target slot. |
| 224 |
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error |
| 225 |
ERROR: all replication slots are in use |
| 226 |
HINT: Free one or increase "max_replication_slots". |
| 227 |
-- temporary slots were dropped automatically |
| 228 |
SELECT pg_drop_replication_slot('orig_slot1'); |
| 229 |
pg_drop_replication_slot |
| 230 |
-------------------------- |
| 231 |
|
| 232 |
(1 row) |
| 233 |
|
| 234 |
SELECT pg_drop_replication_slot('copied_slot1_no_change'); |
| 235 |
pg_drop_replication_slot |
| 236 |
-------------------------- |
| 237 |
|
| 238 |
(1 row) |
| 239 |
|
| 240 |
SELECT pg_drop_replication_slot('copied_slot1_change_plugin'); |
| 241 |
pg_drop_replication_slot |
| 242 |
-------------------------- |
| 243 |
|
| 244 |
(1 row) |
| 245 |
|
| 246 |
-- Test based on the temporary logical slot |
| 247 |
SELECT 'init' FROM pg_create_logical_replication_slot('orig_slot2', 'test_decoding', true); |
| 248 |
?column? |
| 249 |
---------- |
| 250 |
init |
| 251 |
(1 row) |
| 252 |
|
| 253 |
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_no_change'); |
| 254 |
?column? |
| 255 |
---------- |
| 256 |
copy |
| 257 |
(1 row) |
| 258 |
|
| 259 |
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_change_plugin', true, 'pgoutput'); |
| 260 |
?column? |
| 261 |
---------- |
| 262 |
copy |
| 263 |
(1 row) |
| 264 |
|
| 265 |
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot2', 'copied_slot2_change_plugin_temp', false, 'pgoutput'); |
| 266 |
?column? |
| 267 |
---------- |
| 268 |
copy |
| 269 |
(1 row) |
| 270 |
|
| 271 |
-- Check all copied slots status |
| 272 |
SELECT |
| 273 |
o.slot_name, o.plugin, o.temporary, c.slot_name, c.plugin, c.temporary |
| 274 |
FROM |
| 275 |
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o |
| 276 |
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn AND o.confirmed_flush_lsn = c.confirmed_flush_lsn |
| 277 |
WHERE |
| 278 |
o.slot_name != c.slot_name |
| 279 |
ORDER BY o.slot_name, c.slot_name; |
| 280 |
slot_name | plugin | temporary | slot_name | plugin | temporary |
| 281 |
------------+---------------+-----------+---------------------------------+---------------+----------- |
| 282 |
orig_slot2 | test_decoding | t | copied_slot2_change_plugin | pgoutput | t |
| 283 |
orig_slot2 | test_decoding | t | copied_slot2_change_plugin_temp | pgoutput | f |
| 284 |
orig_slot2 | test_decoding | t | copied_slot2_no_change | test_decoding | t |
| 285 |
(3 rows) |
| 286 |
|
| 287 |
-- Cannot copy a logical slot to a physical slot |
| 288 |
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error |
| 289 |
ERROR: cannot copy physical replication slot "orig_slot2" as a logical replication slot |
| 290 |
-- temporary slots were dropped automatically |
| 291 |
SELECT pg_drop_replication_slot('copied_slot2_change_plugin_temp'); |
| 292 |
pg_drop_replication_slot |
| 293 |
-------------------------- |
| 294 |
|
| 295 |
(1 row) |
| 296 |
|
| 297 |
-- |
| 298 |
-- Test copy functions for physical replication slots |
| 299 |
-- |
| 300 |
-- Create and copy physical slots |
| 301 |
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot1', true); |
| 302 |
?column? |
| 303 |
---------- |
| 304 |
init |
| 305 |
(1 row) |
| 306 |
|
| 307 |
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot2', false); |
| 308 |
?column? |
| 309 |
---------- |
| 310 |
init |
| 311 |
(1 row) |
| 312 |
|
| 313 |
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'copied_slot1_no_change'); |
| 314 |
?column? |
| 315 |
---------- |
| 316 |
copy |
| 317 |
(1 row) |
| 318 |
|
| 319 |
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot1', 'copied_slot1_temp', true); |
| 320 |
?column? |
| 321 |
---------- |
| 322 |
copy |
| 323 |
(1 row) |
| 324 |
|
| 325 |
-- Check all copied slots status. Since all slots don't reserve WAL we check only other fields. |
| 326 |
SELECT slot_name, slot_type, temporary FROM pg_replication_slots; |
| 327 |
slot_name | slot_type | temporary |
| 328 |
------------------------+-----------+----------- |
| 329 |
orig_slot1 | physical | f |
| 330 |
orig_slot2 | physical | f |
| 331 |
copied_slot1_no_change | physical | f |
| 332 |
copied_slot1_temp | physical | t |
| 333 |
(4 rows) |
| 334 |
|
| 335 |
-- Cannot copy a physical slot to a logical slot |
| 336 |
SELECT 'copy' FROM pg_copy_logical_replication_slot('orig_slot1', 'failed'); -- error |
| 337 |
ERROR: cannot copy logical replication slot "orig_slot1" as a physical replication slot |
| 338 |
-- Cannot copy a physical slot that doesn't reserve WAL |
| 339 |
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'failed'); -- error |
| 340 |
ERROR: cannot copy a replication slot that doesn't reserve WAL |
| 341 |
-- temporary slots were dropped automatically |
| 342 |
SELECT pg_drop_replication_slot('orig_slot1'); |
| 343 |
pg_drop_replication_slot |
| 344 |
-------------------------- |
| 345 |
|
| 346 |
(1 row) |
| 347 |
|
| 348 |
SELECT pg_drop_replication_slot('orig_slot2'); |
| 349 |
pg_drop_replication_slot |
| 350 |
-------------------------- |
| 351 |
|
| 352 |
(1 row) |
| 353 |
|
| 354 |
SELECT pg_drop_replication_slot('copied_slot1_no_change'); |
| 355 |
pg_drop_replication_slot |
| 356 |
-------------------------- |
| 357 |
|
| 358 |
(1 row) |
| 359 |
|
| 360 |
-- Test based on the temporary physical slot |
| 361 |
SELECT 'init' FROM pg_create_physical_replication_slot('orig_slot2', true, true); |
| 362 |
?column? |
| 363 |
---------- |
| 364 |
init |
| 365 |
(1 row) |
| 366 |
|
| 367 |
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'copied_slot2_no_change'); |
| 368 |
?column? |
| 369 |
---------- |
| 370 |
copy |
| 371 |
(1 row) |
| 372 |
|
| 373 |
SELECT 'copy' FROM pg_copy_physical_replication_slot('orig_slot2', 'copied_slot2_notemp', false); |
| 374 |
?column? |
| 375 |
---------- |
| 376 |
copy |
| 377 |
(1 row) |
| 378 |
|
| 379 |
-- Check all copied slots status |
| 380 |
SELECT |
| 381 |
o.slot_name, o.temporary, c.slot_name, c.temporary |
| 382 |
FROM |
| 383 |
(SELECT * FROM pg_replication_slots WHERE slot_name LIKE 'orig%') as o |
| 384 |
LEFT JOIN pg_replication_slots as c ON o.restart_lsn = c.restart_lsn |
| 385 |
WHERE |
| 386 |
o.slot_name != c.slot_name |
| 387 |
ORDER BY o.slot_name, c.slot_name; |
| 388 |
slot_name | temporary | slot_name | temporary |
| 389 |
------------+-----------+------------------------+----------- |
| 390 |
orig_slot2 | t | copied_slot2_no_change | t |
| 391 |
orig_slot2 | t | copied_slot2_notemp | f |
| 392 |
(2 rows) |
| 393 |
|
| 394 |
SELECT pg_drop_replication_slot('orig_slot2'); |
| 395 |
pg_drop_replication_slot |
| 396 |
-------------------------- |
| 397 |
|
| 398 |
(1 row) |
| 399 |
|
| 400 |
SELECT pg_drop_replication_slot('copied_slot2_no_change'); |
| 401 |
pg_drop_replication_slot |
| 402 |
-------------------------- |
| 403 |
|
| 404 |
(1 row) |
| 405 |
|
| 406 |
SELECT pg_drop_replication_slot('copied_slot2_notemp'); |
| 407 |
pg_drop_replication_slot |
| 408 |
-------------------------- |
| 409 |
|
| 410 |
(1 row) |
| 411 |
|
| 412 |
-- Test failover option of slots. |
| 413 |
SELECT 'init' FROM pg_create_logical_replication_slot('failover_true_slot', 'test_decoding', false, false, true); |
| 414 |
?column? |
| 415 |
---------- |
| 416 |
init |
| 417 |
(1 row) |
| 418 |
|
| 419 |
SELECT 'init' FROM pg_create_logical_replication_slot('failover_false_slot', 'test_decoding', false, false, false); |
| 420 |
?column? |
| 421 |
---------- |
| 422 |
init |
| 423 |
(1 row) |
| 424 |
|
| 425 |
SELECT 'init' FROM pg_create_logical_replication_slot('failover_default_slot', 'test_decoding', false, false); |
| 426 |
?column? |
| 427 |
---------- |
| 428 |
init |
| 429 |
(1 row) |
| 430 |
|
| 431 |
SELECT 'init' FROM pg_create_logical_replication_slot('failover_true_temp_slot', 'test_decoding', true, false, true); |
| 432 |
ERROR: cannot enable failover for a temporary replication slot |
| 433 |
SELECT 'init' FROM pg_create_physical_replication_slot('physical_slot'); |
| 434 |
?column? |
| 435 |
---------- |
| 436 |
init |
| 437 |
(1 row) |
| 438 |
|
| 439 |
SELECT slot_name, slot_type, failover FROM pg_replication_slots; |
| 440 |
slot_name | slot_type | failover |
| 441 |
-----------------------+-----------+---------- |
| 442 |
failover_true_slot | logical | t |
| 443 |
failover_false_slot | logical | f |
| 444 |
failover_default_slot | logical | f |
| 445 |
physical_slot | physical | f |
| 446 |
(4 rows) |
| 447 |
|
| 448 |
SELECT pg_drop_replication_slot('failover_true_slot'); |
| 449 |
pg_drop_replication_slot |
| 450 |
-------------------------- |
| 451 |
|
| 452 |
(1 row) |
| 453 |
|
| 454 |
SELECT pg_drop_replication_slot('failover_false_slot'); |
| 455 |
pg_drop_replication_slot |
| 456 |
-------------------------- |
| 457 |
|
| 458 |
(1 row) |
| 459 |
|
| 460 |
SELECT pg_drop_replication_slot('failover_default_slot'); |
| 461 |
pg_drop_replication_slot |
| 462 |
-------------------------- |
| 463 |
|
| 464 |
(1 row) |
| 465 |
|
| 466 |
SELECT pg_drop_replication_slot('physical_slot'); |
| 467 |
pg_drop_replication_slot |
| 468 |
-------------------------- |
| 469 |
|
| 470 |
(1 row) |
| 471 |
|