| 1 |
-- predictability |
| 2 |
SET synchronous_commit = on; |
| 3 |
-- superuser required by default |
| 4 |
CREATE ROLE regress_origin_replication REPLICATION; |
| 5 |
SET ROLE regress_origin_replication; |
| 6 |
SELECT pg_replication_origin_advance('regress_test_decoding: perm', '0/1'); |
| 7 |
ERROR: permission denied for function pg_replication_origin_advance |
| 8 |
SELECT pg_replication_origin_create('regress_test_decoding: perm'); |
| 9 |
ERROR: permission denied for function pg_replication_origin_create |
| 10 |
SELECT pg_replication_origin_drop('regress_test_decoding: perm'); |
| 11 |
ERROR: permission denied for function pg_replication_origin_drop |
| 12 |
SELECT pg_replication_origin_oid('regress_test_decoding: perm'); |
| 13 |
ERROR: permission denied for function pg_replication_origin_oid |
| 14 |
SELECT pg_replication_origin_progress('regress_test_decoding: perm', false); |
| 15 |
ERROR: permission denied for function pg_replication_origin_progress |
| 16 |
SELECT pg_replication_origin_session_is_setup(); |
| 17 |
ERROR: permission denied for function pg_replication_origin_session_is_setup |
| 18 |
SELECT pg_replication_origin_session_progress(false); |
| 19 |
ERROR: permission denied for function pg_replication_origin_session_progress |
| 20 |
SELECT pg_replication_origin_session_reset(); |
| 21 |
ERROR: permission denied for function pg_replication_origin_session_reset |
| 22 |
SELECT pg_replication_origin_session_setup('regress_test_decoding: perm'); |
| 23 |
ERROR: permission denied for function pg_replication_origin_session_setup |
| 24 |
SELECT pg_replication_origin_xact_reset(); |
| 25 |
ERROR: permission denied for function pg_replication_origin_xact_reset |
| 26 |
SELECT pg_replication_origin_xact_setup('0/1', '2013-01-01 00:00'); |
| 27 |
ERROR: permission denied for function pg_replication_origin_xact_setup |
| 28 |
SELECT pg_show_replication_origin_status(); |
| 29 |
ERROR: permission denied for function pg_show_replication_origin_status |
| 30 |
RESET ROLE; |
| 31 |
DROP ROLE regress_origin_replication; |
| 32 |
CREATE TABLE origin_tbl(id serial primary key, data text); |
| 33 |
CREATE TABLE target_tbl(id serial primary key, data text); |
| 34 |
SELECT pg_replication_origin_create('regress_test_decoding: regression_slot'); |
| 35 |
pg_replication_origin_create |
| 36 |
------------------------------ |
| 37 |
1 |
| 38 |
(1 row) |
| 39 |
|
| 40 |
-- ensure duplicate creations fail |
| 41 |
SELECT pg_replication_origin_create('regress_test_decoding: regression_slot'); |
| 42 |
ERROR: duplicate key value violates unique constraint "pg_replication_origin_roname_index" |
| 43 |
DETAIL: Key (roname)=(regress_test_decoding: regression_slot) already exists. |
| 44 |
--ensure deletions work (once) |
| 45 |
SELECT pg_replication_origin_create('regress_test_decoding: temp'); |
| 46 |
pg_replication_origin_create |
| 47 |
------------------------------ |
| 48 |
2 |
| 49 |
(1 row) |
| 50 |
|
| 51 |
SELECT pg_replication_origin_drop('regress_test_decoding: temp'); |
| 52 |
pg_replication_origin_drop |
| 53 |
---------------------------- |
| 54 |
|
| 55 |
(1 row) |
| 56 |
|
| 57 |
SELECT pg_replication_origin_drop('regress_test_decoding: temp'); |
| 58 |
ERROR: replication origin "regress_test_decoding: temp" does not exist |
| 59 |
-- specifying reserved origin names is not supported |
| 60 |
SELECT pg_replication_origin_create('any'); |
| 61 |
ERROR: replication origin name "any" is reserved |
| 62 |
DETAIL: Origin names "any", "none", and names starting with "pg_" are reserved. |
| 63 |
SELECT pg_replication_origin_create('none'); |
| 64 |
ERROR: replication origin name "none" is reserved |
| 65 |
DETAIL: Origin names "any", "none", and names starting with "pg_" are reserved. |
| 66 |
SELECT pg_replication_origin_create('pg_replication_origin'); |
| 67 |
ERROR: replication origin name "pg_replication_origin" is reserved |
| 68 |
DETAIL: Origin names "any", "none", and names starting with "pg_" are reserved. |
| 69 |
-- various failure checks for undefined slots |
| 70 |
select pg_replication_origin_advance('regress_test_decoding: temp', '0/1'); |
| 71 |
ERROR: replication origin "regress_test_decoding: temp" does not exist |
| 72 |
select pg_replication_origin_session_setup('regress_test_decoding: temp'); |
| 73 |
ERROR: replication origin "regress_test_decoding: temp" does not exist |
| 74 |
select pg_replication_origin_progress('regress_test_decoding: temp', true); |
| 75 |
ERROR: replication origin "regress_test_decoding: temp" does not exist |
| 76 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding'); |
| 77 |
?column? |
| 78 |
---------- |
| 79 |
init |
| 80 |
(1 row) |
| 81 |
|
| 82 |
-- origin tx |
| 83 |
INSERT INTO origin_tbl(data) VALUES ('will be replicated and decoded and decoded again'); |
| 84 |
INSERT INTO target_tbl(data) |
| 85 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 86 |
-- as is normal, the insert into target_tbl shows up |
| 87 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 88 |
data |
| 89 |
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 90 |
BEGIN |
| 91 |
table public.target_tbl: INSERT: id[integer]:1 data[text]:'BEGIN' |
| 92 |
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''' |
| 93 |
table public.target_tbl: INSERT: id[integer]:3 data[text]:'COMMIT' |
| 94 |
COMMIT |
| 95 |
(5 rows) |
| 96 |
|
| 97 |
INSERT INTO origin_tbl(data) VALUES ('will be replicated, but not decoded again'); |
| 98 |
-- mark session as replaying |
| 99 |
SELECT pg_replication_origin_session_setup('regress_test_decoding: regression_slot'); |
| 100 |
pg_replication_origin_session_setup |
| 101 |
------------------------------------- |
| 102 |
|
| 103 |
(1 row) |
| 104 |
|
| 105 |
-- ensure we prevent duplicate setup |
| 106 |
SELECT pg_replication_origin_session_setup('regress_test_decoding: regression_slot'); |
| 107 |
ERROR: cannot setup replication origin when one is already setup |
| 108 |
SELECT '' FROM pg_logical_emit_message(false, 'test', 'this message will not be decoded'); |
| 109 |
?column? |
| 110 |
---------- |
| 111 |
|
| 112 |
(1 row) |
| 113 |
|
| 114 |
BEGIN; |
| 115 |
-- setup transaction origin |
| 116 |
SELECT pg_replication_origin_xact_setup('0/aabbccdd', '2013-01-01 00:00'); |
| 117 |
pg_replication_origin_xact_setup |
| 118 |
---------------------------------- |
| 119 |
|
| 120 |
(1 row) |
| 121 |
|
| 122 |
INSERT INTO target_tbl(data) |
| 123 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'only-local', '1'); |
| 124 |
COMMIT; |
| 125 |
-- check replication progress for the session is correct |
| 126 |
SELECT pg_replication_origin_session_progress(false); |
| 127 |
pg_replication_origin_session_progress |
| 128 |
---------------------------------------- |
| 129 |
0/AABBCCDD |
| 130 |
(1 row) |
| 131 |
|
| 132 |
SELECT pg_replication_origin_session_progress(true); |
| 133 |
pg_replication_origin_session_progress |
| 134 |
---------------------------------------- |
| 135 |
0/AABBCCDD |
| 136 |
(1 row) |
| 137 |
|
| 138 |
SELECT pg_replication_origin_session_reset(); |
| 139 |
pg_replication_origin_session_reset |
| 140 |
------------------------------------- |
| 141 |
|
| 142 |
(1 row) |
| 143 |
|
| 144 |
SELECT local_id, external_id, remote_lsn, local_lsn <> '0/0' FROM pg_replication_origin_status; |
| 145 |
local_id | external_id | remote_lsn | ?column? |
| 146 |
----------+----------------------------------------+------------+---------- |
| 147 |
1 | regress_test_decoding: regression_slot | 0/AABBCCDD | t |
| 148 |
(1 row) |
| 149 |
|
| 150 |
-- check replication progress identified by name is correct |
| 151 |
SELECT pg_replication_origin_progress('regress_test_decoding: regression_slot', false); |
| 152 |
pg_replication_origin_progress |
| 153 |
-------------------------------- |
| 154 |
0/AABBCCDD |
| 155 |
(1 row) |
| 156 |
|
| 157 |
SELECT pg_replication_origin_progress('regress_test_decoding: regression_slot', true); |
| 158 |
pg_replication_origin_progress |
| 159 |
-------------------------------- |
| 160 |
0/AABBCCDD |
| 161 |
(1 row) |
| 162 |
|
| 163 |
-- ensure reset requires previously setup state |
| 164 |
SELECT pg_replication_origin_session_reset(); |
| 165 |
ERROR: no replication origin is configured |
| 166 |
-- and magically the replayed xact will be filtered! |
| 167 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'only-local', '1'); |
| 168 |
data |
| 169 |
------ |
| 170 |
(0 rows) |
| 171 |
|
| 172 |
--but new original changes still show up |
| 173 |
INSERT INTO origin_tbl(data) VALUES ('will be replicated'); |
| 174 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'only-local', '1'); |
| 175 |
data |
| 176 |
-------------------------------------------------------------------------------- |
| 177 |
BEGIN |
| 178 |
table public.origin_tbl: INSERT: id[integer]:3 data[text]:'will be replicated' |
| 179 |
COMMIT |
| 180 |
(3 rows) |
| 181 |
|
| 182 |
SELECT pg_drop_replication_slot('regression_slot'); |
| 183 |
pg_drop_replication_slot |
| 184 |
-------------------------- |
| 185 |
|
| 186 |
(1 row) |
| 187 |
|
| 188 |
SELECT pg_replication_origin_drop('regress_test_decoding: regression_slot'); |
| 189 |
pg_replication_origin_drop |
| 190 |
---------------------------- |
| 191 |
|
| 192 |
(1 row) |
| 193 |
|
| 194 |
-- Set of transactions with no origin LSNs and commit timestamps set for |
| 195 |
-- this session. |
| 196 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_no_lsn', 'test_decoding'); |
| 197 |
?column? |
| 198 |
---------- |
| 199 |
init |
| 200 |
(1 row) |
| 201 |
|
| 202 |
SELECT pg_replication_origin_create('regress_test_decoding: regression_slot_no_lsn'); |
| 203 |
pg_replication_origin_create |
| 204 |
------------------------------ |
| 205 |
1 |
| 206 |
(1 row) |
| 207 |
|
| 208 |
-- mark session as replaying |
| 209 |
SELECT pg_replication_origin_session_setup('regress_test_decoding: regression_slot_no_lsn'); |
| 210 |
pg_replication_origin_session_setup |
| 211 |
------------------------------------- |
| 212 |
|
| 213 |
(1 row) |
| 214 |
|
| 215 |
-- Simple transactions |
| 216 |
BEGIN; |
| 217 |
INSERT INTO origin_tbl(data) VALUES ('no_lsn, commit'); |
| 218 |
COMMIT; |
| 219 |
BEGIN; |
| 220 |
INSERT INTO origin_tbl(data) VALUES ('no_lsn, rollback'); |
| 221 |
ROLLBACK; |
| 222 |
-- 2PC transactions |
| 223 |
BEGIN; |
| 224 |
INSERT INTO origin_tbl(data) VALUES ('no_lsn, commit prepared'); |
| 225 |
PREPARE TRANSACTION 'replorigin_prepared'; |
| 226 |
COMMIT PREPARED 'replorigin_prepared'; |
| 227 |
BEGIN; |
| 228 |
INSERT INTO origin_tbl(data) VALUES ('no_lsn, rollback prepared'); |
| 229 |
PREPARE TRANSACTION 'replorigin_prepared'; |
| 230 |
ROLLBACK PREPARED 'replorigin_prepared'; |
| 231 |
SELECT local_id, external_id, |
| 232 |
remote_lsn <> '0/0' AS valid_remote_lsn, |
| 233 |
local_lsn <> '0/0' AS valid_local_lsn |
| 234 |
FROM pg_replication_origin_status; |
| 235 |
local_id | external_id | valid_remote_lsn | valid_local_lsn |
| 236 |
----------+-----------------------------------------------+------------------+----------------- |
| 237 |
1 | regress_test_decoding: regression_slot_no_lsn | f | t |
| 238 |
(1 row) |
| 239 |
|
| 240 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot_no_lsn', NULL, NULL, 'skip-empty-xacts', '1', 'include-xids', '0'); |
| 241 |
data |
| 242 |
------------------------------------------------------------------------------------- |
| 243 |
BEGIN |
| 244 |
table public.origin_tbl: INSERT: id[integer]:4 data[text]:'no_lsn, commit' |
| 245 |
COMMIT |
| 246 |
BEGIN |
| 247 |
table public.origin_tbl: INSERT: id[integer]:6 data[text]:'no_lsn, commit prepared' |
| 248 |
COMMIT |
| 249 |
(6 rows) |
| 250 |
|
| 251 |
-- Clean up |
| 252 |
SELECT pg_replication_origin_session_reset(); |
| 253 |
pg_replication_origin_session_reset |
| 254 |
------------------------------------- |
| 255 |
|
| 256 |
(1 row) |
| 257 |
|
| 258 |
SELECT pg_drop_replication_slot('regression_slot_no_lsn'); |
| 259 |
pg_drop_replication_slot |
| 260 |
-------------------------- |
| 261 |
|
| 262 |
(1 row) |
| 263 |
|
| 264 |
SELECT pg_replication_origin_drop('regress_test_decoding: regression_slot_no_lsn'); |
| 265 |
pg_replication_origin_drop |
| 266 |
---------------------------- |
| 267 |
|
| 268 |
(1 row) |
| 269 |
|
| 270 |
-- Test that the pgoutput correctly filters changes corresponding to the provided origin value. |
| 271 |
SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'pgoutput'); |
| 272 |
?column? |
| 273 |
---------- |
| 274 |
init |
| 275 |
(1 row) |
| 276 |
|
| 277 |
CREATE PUBLICATION pub FOR TABLE target_tbl; |
| 278 |
SELECT pg_replication_origin_create('regress_test_decoding: regression_slot'); |
| 279 |
pg_replication_origin_create |
| 280 |
------------------------------ |
| 281 |
1 |
| 282 |
(1 row) |
| 283 |
|
| 284 |
-- mark session as replaying |
| 285 |
SELECT pg_replication_origin_session_setup('regress_test_decoding: regression_slot'); |
| 286 |
pg_replication_origin_session_setup |
| 287 |
------------------------------------- |
| 288 |
|
| 289 |
(1 row) |
| 290 |
|
| 291 |
INSERT INTO target_tbl(data) VALUES ('test data'); |
| 292 |
-- The replayed change will be filtered. |
| 293 |
SELECT count(*) = 0 FROM pg_logical_slot_peek_binary_changes('regression_slot', NULL, NULL, 'proto_version', '4', 'publication_names', 'pub', 'origin', 'none'); |
| 294 |
?column? |
| 295 |
---------- |
| 296 |
t |
| 297 |
(1 row) |
| 298 |
|
| 299 |
-- The replayed change will be output if the origin value is not specified. |
| 300 |
SELECT count(*) != 0 FROM pg_logical_slot_peek_binary_changes('regression_slot', NULL, NULL, 'proto_version', '4', 'publication_names', 'pub'); |
| 301 |
?column? |
| 302 |
---------- |
| 303 |
t |
| 304 |
(1 row) |
| 305 |
|
| 306 |
-- Clean up |
| 307 |
SELECT pg_replication_origin_session_reset(); |
| 308 |
pg_replication_origin_session_reset |
| 309 |
------------------------------------- |
| 310 |
|
| 311 |
(1 row) |
| 312 |
|
| 313 |
SELECT pg_drop_replication_slot('regression_slot'); |
| 314 |
pg_drop_replication_slot |
| 315 |
-------------------------- |
| 316 |
|
| 317 |
(1 row) |
| 318 |
|
| 319 |
SELECT pg_replication_origin_drop('regress_test_decoding: regression_slot'); |
| 320 |
pg_replication_origin_drop |
| 321 |
---------------------------- |
| 322 |
|
| 323 |
(1 row) |
| 324 |
|
| 325 |
DROP PUBLICATION pub; |