|
| 1 | +-- Test prepared transactions. When two-phase-commit is enabled, transactions are |
| 2 | +-- decoded at PREPARE time rather than at COMMIT PREPARED time. |
| 3 | +SET synchronous_commit = on; |
| 4 | +SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding'); |
| 5 | + ?column? |
| 6 | +---------- |
| 7 | + init |
| 8 | +(1 row) |
| 9 | + |
| 10 | +CREATE TABLE test_prepared1(id integer primary key); |
| 11 | +CREATE TABLE test_prepared2(id integer primary key); |
| 12 | +-- Test that decoding happens at PREPARE time when two-phase-commit is enabled. |
| 13 | +-- Decoding after COMMIT PREPARED must have all the commands in the transaction. |
| 14 | +BEGIN; |
| 15 | +INSERT INTO test_prepared1 VALUES (1); |
| 16 | +INSERT INTO test_prepared1 VALUES (2); |
| 17 | +-- should show nothing because the xact has not been prepared yet. |
| 18 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 19 | + data |
| 20 | +------ |
| 21 | +(0 rows) |
| 22 | + |
| 23 | +PREPARE TRANSACTION 'test_prepared#1'; |
| 24 | +-- should show both the above inserts and the PREPARE TRANSACTION. |
| 25 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 26 | + data |
| 27 | +---------------------------------------------------- |
| 28 | + BEGIN |
| 29 | + table public.test_prepared1: INSERT: id[integer]:1 |
| 30 | + table public.test_prepared1: INSERT: id[integer]:2 |
| 31 | + PREPARE TRANSACTION 'test_prepared#1' |
| 32 | +(4 rows) |
| 33 | + |
| 34 | +COMMIT PREPARED 'test_prepared#1'; |
| 35 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 36 | + data |
| 37 | +---------------------------------------------------- |
| 38 | + BEGIN |
| 39 | + table public.test_prepared1: INSERT: id[integer]:1 |
| 40 | + table public.test_prepared1: INSERT: id[integer]:2 |
| 41 | + PREPARE TRANSACTION 'test_prepared#1' |
| 42 | + COMMIT PREPARED 'test_prepared#1' |
| 43 | +(5 rows) |
| 44 | + |
| 45 | +-- Test that rollback of a prepared xact is decoded. |
| 46 | +BEGIN; |
| 47 | +INSERT INTO test_prepared1 VALUES (3); |
| 48 | +PREPARE TRANSACTION 'test_prepared#2'; |
| 49 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 50 | + data |
| 51 | +---------------------------------------------------- |
| 52 | + BEGIN |
| 53 | + table public.test_prepared1: INSERT: id[integer]:3 |
| 54 | + PREPARE TRANSACTION 'test_prepared#2' |
| 55 | +(3 rows) |
| 56 | + |
| 57 | +ROLLBACK PREPARED 'test_prepared#2'; |
| 58 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 59 | + data |
| 60 | +------------------------------------- |
| 61 | + ROLLBACK PREPARED 'test_prepared#2' |
| 62 | +(1 row) |
| 63 | + |
| 64 | +-- Test prepare of a xact containing ddl. Leaving xact uncommitted for next test. |
| 65 | +BEGIN; |
| 66 | +ALTER TABLE test_prepared1 ADD COLUMN data text; |
| 67 | +INSERT INTO test_prepared1 VALUES (4, 'frakbar'); |
| 68 | +PREPARE TRANSACTION 'test_prepared#3'; |
| 69 | +-- confirm that exclusive lock from the ALTER command is held on test_prepared1 table |
| 70 | +SELECT 'test_prepared_1' AS relation, locktype, mode |
| 71 | +FROM pg_locks |
| 72 | +WHERE locktype = 'relation' |
| 73 | + AND relation = 'test_prepared1'::regclass; |
| 74 | + relation | locktype | mode |
| 75 | +-----------------+----------+--------------------- |
| 76 | + test_prepared_1 | relation | RowExclusiveLock |
| 77 | + test_prepared_1 | relation | AccessExclusiveLock |
| 78 | +(2 rows) |
| 79 | + |
| 80 | +-- The insert should show the newly altered column but not the DDL. |
| 81 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 82 | + data |
| 83 | +------------------------------------------------------------------------- |
| 84 | + BEGIN |
| 85 | + table public.test_prepared1: INSERT: id[integer]:4 data[text]:'frakbar' |
| 86 | + PREPARE TRANSACTION 'test_prepared#3' |
| 87 | +(3 rows) |
| 88 | + |
| 89 | +-- Test that we decode correctly while an uncommitted prepared xact |
| 90 | +-- with ddl exists. |
| 91 | +-- |
| 92 | +-- Use a separate table for the concurrent transaction because the lock from |
| 93 | +-- the ALTER will stop us inserting into the other one. |
| 94 | +-- |
| 95 | +INSERT INTO test_prepared2 VALUES (5); |
| 96 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 97 | + data |
| 98 | +---------------------------------------------------- |
| 99 | + BEGIN |
| 100 | + table public.test_prepared2: INSERT: id[integer]:5 |
| 101 | + COMMIT |
| 102 | +(3 rows) |
| 103 | + |
| 104 | +COMMIT PREPARED 'test_prepared#3'; |
| 105 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 106 | + data |
| 107 | +------------------------------------------------------------------------- |
| 108 | + BEGIN |
| 109 | + table public.test_prepared1: INSERT: id[integer]:4 data[text]:'frakbar' |
| 110 | + PREPARE TRANSACTION 'test_prepared#3' |
| 111 | + COMMIT PREPARED 'test_prepared#3' |
| 112 | +(4 rows) |
| 113 | + |
| 114 | +-- make sure stuff still works |
| 115 | +INSERT INTO test_prepared1 VALUES (6); |
| 116 | +INSERT INTO test_prepared2 VALUES (7); |
| 117 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 118 | + data |
| 119 | +-------------------------------------------------------------------- |
| 120 | + BEGIN |
| 121 | + table public.test_prepared1: INSERT: id[integer]:6 data[text]:null |
| 122 | + COMMIT |
| 123 | + BEGIN |
| 124 | + table public.test_prepared2: INSERT: id[integer]:7 |
| 125 | + COMMIT |
| 126 | +(6 rows) |
| 127 | + |
| 128 | +-- Check 'CLUSTER' (as operation that hold exclusive lock) doesn't block |
| 129 | +-- logical decoding. |
| 130 | +BEGIN; |
| 131 | +INSERT INTO test_prepared1 VALUES (8, 'othercol'); |
| 132 | +CLUSTER test_prepared1 USING test_prepared1_pkey; |
| 133 | +INSERT INTO test_prepared1 VALUES (9, 'othercol2'); |
| 134 | +PREPARE TRANSACTION 'test_prepared_lock'; |
| 135 | +SELECT 'test_prepared1' AS relation, locktype, mode |
| 136 | +FROM pg_locks |
| 137 | +WHERE locktype = 'relation' |
| 138 | + AND relation = 'test_prepared1'::regclass; |
| 139 | + relation | locktype | mode |
| 140 | +----------------+----------+--------------------- |
| 141 | + test_prepared1 | relation | RowExclusiveLock |
| 142 | + test_prepared1 | relation | ShareLock |
| 143 | + test_prepared1 | relation | AccessExclusiveLock |
| 144 | +(3 rows) |
| 145 | + |
| 146 | +-- The above CLUSTER command shouldn't cause a timeout on 2pc decoding. The |
| 147 | +-- call should return within a second. |
| 148 | +SET statement_timeout = '1s'; |
| 149 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 150 | + data |
| 151 | +--------------------------------------------------------------------------- |
| 152 | + BEGIN |
| 153 | + table public.test_prepared1: INSERT: id[integer]:8 data[text]:'othercol' |
| 154 | + table public.test_prepared1: INSERT: id[integer]:9 data[text]:'othercol2' |
| 155 | + PREPARE TRANSACTION 'test_prepared_lock' |
| 156 | +(4 rows) |
| 157 | + |
| 158 | +RESET statement_timeout; |
| 159 | +COMMIT PREPARED 'test_prepared_lock'; |
| 160 | +-- consume the commit |
| 161 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 162 | + data |
| 163 | +--------------------------------------------------------------------------- |
| 164 | + BEGIN |
| 165 | + table public.test_prepared1: INSERT: id[integer]:8 data[text]:'othercol' |
| 166 | + table public.test_prepared1: INSERT: id[integer]:9 data[text]:'othercol2' |
| 167 | + PREPARE TRANSACTION 'test_prepared_lock' |
| 168 | + COMMIT PREPARED 'test_prepared_lock' |
| 169 | +(5 rows) |
| 170 | + |
| 171 | +-- Test savepoints and sub-xacts. Creating savepoints will create |
| 172 | +-- sub-xacts implicitly. |
| 173 | +BEGIN; |
| 174 | +CREATE TABLE test_prepared_savepoint (a int); |
| 175 | +INSERT INTO test_prepared_savepoint VALUES (1); |
| 176 | +SAVEPOINT test_savepoint; |
| 177 | +INSERT INTO test_prepared_savepoint VALUES (2); |
| 178 | +ROLLBACK TO SAVEPOINT test_savepoint; |
| 179 | +PREPARE TRANSACTION 'test_prepared_savepoint'; |
| 180 | +-- should show only 1, not 2 |
| 181 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 182 | + data |
| 183 | +------------------------------------------------------------ |
| 184 | + BEGIN |
| 185 | + table public.test_prepared_savepoint: INSERT: a[integer]:1 |
| 186 | + PREPARE TRANSACTION 'test_prepared_savepoint' |
| 187 | +(3 rows) |
| 188 | + |
| 189 | +COMMIT PREPARED 'test_prepared_savepoint'; |
| 190 | +-- consume the commit |
| 191 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 192 | + data |
| 193 | +------------------------------------------------------------ |
| 194 | + BEGIN |
| 195 | + table public.test_prepared_savepoint: INSERT: a[integer]:1 |
| 196 | + PREPARE TRANSACTION 'test_prepared_savepoint' |
| 197 | + COMMIT PREPARED 'test_prepared_savepoint' |
| 198 | +(4 rows) |
| 199 | + |
| 200 | +-- Test that a GID containing "_nodecode" gets decoded at commit prepared time. |
| 201 | +BEGIN; |
| 202 | +INSERT INTO test_prepared1 VALUES (20); |
| 203 | +PREPARE TRANSACTION 'test_prepared_nodecode'; |
| 204 | +-- should show nothing |
| 205 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 206 | + data |
| 207 | +------ |
| 208 | +(0 rows) |
| 209 | + |
| 210 | +COMMIT PREPARED 'test_prepared_nodecode'; |
| 211 | +-- should be decoded now |
| 212 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 213 | + data |
| 214 | +--------------------------------------------------------------------- |
| 215 | + BEGIN |
| 216 | + table public.test_prepared1: INSERT: id[integer]:20 data[text]:null |
| 217 | + COMMIT |
| 218 | +(3 rows) |
| 219 | + |
| 220 | +-- Test 8: |
| 221 | +-- cleanup and make sure results are also empty |
| 222 | +DROP TABLE test_prepared1; |
| 223 | +DROP TABLE test_prepared2; |
| 224 | +-- show results. There should be nothing to show |
| 225 | +SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'two-phase-commit', '1', 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 226 | + data |
| 227 | +------ |
| 228 | +(0 rows) |
| 229 | + |
| 230 | +SELECT pg_drop_replication_slot('regression_slot'); |
| 231 | + pg_drop_replication_slot |
| 232 | +-------------------------- |
| 233 | + |
| 234 | +(1 row) |
| 235 | + |
0 commit comments