|
| 1 | +-- predictability |
| 2 | +SET synchronous_commit = on; |
| 3 | +CREATE TABLE origin_tbl(id serial primary key, data text); |
| 4 | +CREATE TABLE target_tbl(id serial primary key, data text); |
| 5 | +SELECT pg_replication_origin_create('test_decoding: regression_slot'); |
| 6 | + pg_replication_origin_create |
| 7 | +------------------------------ |
| 8 | + 1 |
| 9 | +(1 row) |
| 10 | + |
| 11 | +-- ensure duplicate creations fail |
| 12 | +SELECT pg_replication_origin_create('test_decoding: regression_slot'); |
| 13 | +ERROR: duplicate key value violates unique constraint "pg_replication_origin_roname_index" |
| 14 | +DETAIL: Key (roname)=(test_decoding: regression_slot) already exists. |
| 15 | +--ensure deletions work (once) |
| 16 | +SELECT pg_replication_origin_create('test_decoding: temp'); |
| 17 | + pg_replication_origin_create |
| 18 | +------------------------------ |
| 19 | + 2 |
| 20 | +(1 row) |
| 21 | + |
| 22 | +SELECT pg_replication_origin_drop('test_decoding: temp'); |
| 23 | + pg_replication_origin_drop |
| 24 | +---------------------------- |
| 25 | + |
| 26 | +(1 row) |
| 27 | + |
| 28 | +SELECT pg_replication_origin_drop('test_decoding: temp'); |
| 29 | +ERROR: cache lookup failed for replication origin 'test_decoding: temp' |
| 30 | +SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding'); |
| 31 | + ?column? |
| 32 | +---------- |
| 33 | + init |
| 34 | +(1 row) |
| 35 | + |
| 36 | +-- origin tx |
| 37 | +INSERT INTO origin_tbl(data) VALUES ('will be replicated and decoded and decoded again'); |
| 38 | +INSERT INTO target_tbl(data) |
| 39 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 40 | +-- as is normal, the insert into target_tbl shows up |
| 41 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 42 | + data |
| 43 | +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 44 | + BEGIN |
| 45 | + table public.target_tbl: INSERT: id[integer]:1 data[text]:'BEGIN' |
| 46 | + table public.target_tbl: INSERT: id[integer]:2 data[text]:'table public.origin_tbl: INSERT: id[integer]:1 data[text]:''will be replicated and decoded and decoded again''' |
| 47 | + table public.target_tbl: INSERT: id[integer]:3 data[text]:'COMMIT' |
| 48 | + COMMIT |
| 49 | +(5 rows) |
| 50 | + |
| 51 | +INSERT INTO origin_tbl(data) VALUES ('will be replicated, but not decoded again'); |
| 52 | +-- mark session as replaying |
| 53 | +SELECT pg_replication_origin_session_setup('test_decoding: regression_slot'); |
| 54 | + pg_replication_origin_session_setup |
| 55 | +------------------------------------- |
| 56 | + |
| 57 | +(1 row) |
| 58 | + |
| 59 | +-- ensure we prevent duplicate setup |
| 60 | +SELECT pg_replication_origin_session_setup('test_decoding: regression_slot'); |
| 61 | +ERROR: cannot setup replication origin when one is already setup |
| 62 | +BEGIN; |
| 63 | +-- setup transaction origin |
| 64 | +SELECT pg_replication_origin_xact_setup('0/aabbccdd', '2013-01-01 00:00'); |
| 65 | + pg_replication_origin_xact_setup |
| 66 | +---------------------------------- |
| 67 | + |
| 68 | +(1 row) |
| 69 | + |
| 70 | +INSERT INTO target_tbl(data) |
| 71 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'only-local', '1'); |
| 72 | +COMMIT; |
| 73 | +-- check replication progress for the session is correct |
| 74 | +SELECT pg_replication_origin_session_progress(false); |
| 75 | + pg_replication_origin_session_progress |
| 76 | +---------------------------------------- |
| 77 | + 0/AABBCCDD |
| 78 | +(1 row) |
| 79 | + |
| 80 | +SELECT pg_replication_origin_session_progress(true); |
| 81 | + pg_replication_origin_session_progress |
| 82 | +---------------------------------------- |
| 83 | + 0/AABBCCDD |
| 84 | +(1 row) |
| 85 | + |
| 86 | +SELECT pg_replication_origin_session_reset(); |
| 87 | + pg_replication_origin_session_reset |
| 88 | +------------------------------------- |
| 89 | + |
| 90 | +(1 row) |
| 91 | + |
| 92 | +SELECT local_id, external_id, remote_lsn, local_lsn <> '0/0' FROM pg_replication_origin_status; |
| 93 | + local_id | external_id | remote_lsn | ?column? |
| 94 | +----------+--------------------------------+------------+---------- |
| 95 | + 1 | test_decoding: regression_slot | 0/AABBCCDD | t |
| 96 | +(1 row) |
| 97 | + |
| 98 | +-- check replication progress identified by name is correct |
| 99 | +SELECT pg_replication_origin_progress('test_decoding: regression_slot', false); |
| 100 | + pg_replication_origin_progress |
| 101 | +-------------------------------- |
| 102 | + 0/AABBCCDD |
| 103 | +(1 row) |
| 104 | + |
| 105 | +SELECT pg_replication_origin_progress('test_decoding: regression_slot', true); |
| 106 | + pg_replication_origin_progress |
| 107 | +-------------------------------- |
| 108 | + 0/AABBCCDD |
| 109 | +(1 row) |
| 110 | + |
| 111 | +-- ensure reset requires previously setup state |
| 112 | +SELECT pg_replication_origin_session_reset(); |
| 113 | +ERROR: no replication origin is configured |
| 114 | +-- and magically the replayed xact will be filtered! |
| 115 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'only-local', '1'); |
| 116 | + data |
| 117 | +------ |
| 118 | +(0 rows) |
| 119 | + |
| 120 | +--but new original changes still show up |
| 121 | +INSERT INTO origin_tbl(data) VALUES ('will be replicated'); |
| 122 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'only-local', '1'); |
| 123 | + data |
| 124 | +-------------------------------------------------------------------------------- |
| 125 | + BEGIN |
| 126 | + table public.origin_tbl: INSERT: id[integer]:3 data[text]:'will be replicated' |
| 127 | + COMMIT |
| 128 | +(3 rows) |
| 129 | + |
| 130 | +SELECT pg_drop_replication_slot('regression_slot'); |
| 131 | + pg_drop_replication_slot |
| 132 | +-------------------------- |
| 133 | + |
| 134 | +(1 row) |
| 135 | + |
| 136 | +SELECT pg_replication_origin_drop('test_decoding: regression_slot'); |
| 137 | + pg_replication_origin_drop |
| 138 | +---------------------------- |
| 139 | + |
| 140 | +(1 row) |
| 141 | + |
0 commit comments