| 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', false, true); |
| 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, '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, '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, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 36 |
data |
| 37 |
----------------------------------- |
| 38 |
COMMIT PREPARED 'test_prepared#1' |
| 39 |
(1 row) |
| 40 |
|
| 41 |
-- Test that rollback of a prepared xact is decoded. |
| 42 |
BEGIN; |
| 43 |
INSERT INTO test_prepared1 VALUES (3); |
| 44 |
PREPARE TRANSACTION 'test_prepared#2'; |
| 45 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 46 |
data |
| 47 |
---------------------------------------------------- |
| 48 |
BEGIN |
| 49 |
table public.test_prepared1: INSERT: id[integer]:3 |
| 50 |
PREPARE TRANSACTION 'test_prepared#2' |
| 51 |
(3 rows) |
| 52 |
|
| 53 |
ROLLBACK PREPARED 'test_prepared#2'; |
| 54 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 55 |
data |
| 56 |
------------------------------------- |
| 57 |
ROLLBACK PREPARED 'test_prepared#2' |
| 58 |
(1 row) |
| 59 |
|
| 60 |
-- Test prepare of a xact containing ddl. Leaving xact uncommitted for next test. |
| 61 |
BEGIN; |
| 62 |
ALTER TABLE test_prepared1 ADD COLUMN data text; |
| 63 |
INSERT INTO test_prepared1 VALUES (4, 'frakbar'); |
| 64 |
PREPARE TRANSACTION 'test_prepared#3'; |
| 65 |
-- confirm that exclusive lock from the ALTER command is held on test_prepared1 table |
| 66 |
SELECT 'test_prepared_1' AS relation, locktype, mode |
| 67 |
FROM pg_locks |
| 68 |
WHERE locktype = 'relation' |
| 69 |
AND relation = 'test_prepared1'::regclass; |
| 70 |
relation | locktype | mode |
| 71 |
-----------------+----------+--------------------- |
| 72 |
test_prepared_1 | relation | RowExclusiveLock |
| 73 |
test_prepared_1 | relation | AccessExclusiveLock |
| 74 |
(2 rows) |
| 75 |
|
| 76 |
-- The insert should show the newly altered column but not the DDL. |
| 77 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 78 |
data |
| 79 |
------------------------------------------------------------------------- |
| 80 |
BEGIN |
| 81 |
table public.test_prepared1: INSERT: id[integer]:4 data[text]:'frakbar' |
| 82 |
PREPARE TRANSACTION 'test_prepared#3' |
| 83 |
(3 rows) |
| 84 |
|
| 85 |
-- Test that we decode correctly while an uncommitted prepared xact |
| 86 |
-- with ddl exists. |
| 87 |
-- |
| 88 |
-- Use a separate table for the concurrent transaction because the lock from |
| 89 |
-- the ALTER will stop us inserting into the other one. |
| 90 |
-- |
| 91 |
INSERT INTO test_prepared2 VALUES (5); |
| 92 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 93 |
data |
| 94 |
---------------------------------------------------- |
| 95 |
BEGIN |
| 96 |
table public.test_prepared2: INSERT: id[integer]:5 |
| 97 |
COMMIT |
| 98 |
(3 rows) |
| 99 |
|
| 100 |
COMMIT PREPARED 'test_prepared#3'; |
| 101 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 102 |
data |
| 103 |
----------------------------------- |
| 104 |
COMMIT PREPARED 'test_prepared#3' |
| 105 |
(1 row) |
| 106 |
|
| 107 |
-- make sure stuff still works |
| 108 |
INSERT INTO test_prepared1 VALUES (6); |
| 109 |
INSERT INTO test_prepared2 VALUES (7); |
| 110 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 111 |
data |
| 112 |
-------------------------------------------------------------------- |
| 113 |
BEGIN |
| 114 |
table public.test_prepared1: INSERT: id[integer]:6 data[text]:null |
| 115 |
COMMIT |
| 116 |
BEGIN |
| 117 |
table public.test_prepared2: INSERT: id[integer]:7 |
| 118 |
COMMIT |
| 119 |
(6 rows) |
| 120 |
|
| 121 |
-- Check 'CLUSTER' (as operation that hold exclusive lock) doesn't block |
| 122 |
-- logical decoding. |
| 123 |
BEGIN; |
| 124 |
INSERT INTO test_prepared1 VALUES (8, 'othercol'); |
| 125 |
CLUSTER test_prepared1 USING test_prepared1_pkey; |
| 126 |
INSERT INTO test_prepared1 VALUES (9, 'othercol2'); |
| 127 |
PREPARE TRANSACTION 'test_prepared_lock'; |
| 128 |
SELECT 'test_prepared1' AS relation, locktype, mode |
| 129 |
FROM pg_locks |
| 130 |
WHERE locktype = 'relation' |
| 131 |
AND relation = 'test_prepared1'::regclass; |
| 132 |
relation | locktype | mode |
| 133 |
----------------+----------+--------------------- |
| 134 |
test_prepared1 | relation | RowExclusiveLock |
| 135 |
test_prepared1 | relation | ShareLock |
| 136 |
test_prepared1 | relation | AccessExclusiveLock |
| 137 |
(3 rows) |
| 138 |
|
| 139 |
-- The above CLUSTER command shouldn't cause a timeout on 2pc decoding. |
| 140 |
\set env_timeout '' |
| 141 |
\getenv env_timeout PG_TEST_TIMEOUT_DEFAULT |
| 142 |
SELECT COALESCE(NULLIF(:'env_timeout', ''), '180') || 's' AS timeout \gset |
| 143 |
SET statement_timeout = :'timeout'; |
| 144 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 145 |
data |
| 146 |
--------------------------------------------------------------------------- |
| 147 |
BEGIN |
| 148 |
table public.test_prepared1: INSERT: id[integer]:8 data[text]:'othercol' |
| 149 |
table public.test_prepared1: INSERT: id[integer]:9 data[text]:'othercol2' |
| 150 |
PREPARE TRANSACTION 'test_prepared_lock' |
| 151 |
(4 rows) |
| 152 |
|
| 153 |
RESET statement_timeout; |
| 154 |
COMMIT PREPARED 'test_prepared_lock'; |
| 155 |
-- consume the commit |
| 156 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 157 |
data |
| 158 |
-------------------------------------- |
| 159 |
COMMIT PREPARED 'test_prepared_lock' |
| 160 |
(1 row) |
| 161 |
|
| 162 |
-- Test savepoints and sub-xacts. Creating savepoints will create |
| 163 |
-- sub-xacts implicitly. |
| 164 |
BEGIN; |
| 165 |
CREATE TABLE test_prepared_savepoint (a int); |
| 166 |
INSERT INTO test_prepared_savepoint VALUES (1); |
| 167 |
SAVEPOINT test_savepoint; |
| 168 |
INSERT INTO test_prepared_savepoint VALUES (2); |
| 169 |
ROLLBACK TO SAVEPOINT test_savepoint; |
| 170 |
PREPARE TRANSACTION 'test_prepared_savepoint'; |
| 171 |
-- should show only 1, not 2 |
| 172 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 173 |
data |
| 174 |
------------------------------------------------------------ |
| 175 |
BEGIN |
| 176 |
table public.test_prepared_savepoint: INSERT: a[integer]:1 |
| 177 |
PREPARE TRANSACTION 'test_prepared_savepoint' |
| 178 |
(3 rows) |
| 179 |
|
| 180 |
COMMIT PREPARED 'test_prepared_savepoint'; |
| 181 |
-- consume the commit |
| 182 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 183 |
data |
| 184 |
------------------------------------------- |
| 185 |
COMMIT PREPARED 'test_prepared_savepoint' |
| 186 |
(1 row) |
| 187 |
|
| 188 |
-- Test that a GID containing "_nodecode" gets decoded at commit prepared time. |
| 189 |
BEGIN; |
| 190 |
INSERT INTO test_prepared1 VALUES (20); |
| 191 |
PREPARE TRANSACTION 'test_prepared_nodecode'; |
| 192 |
-- should show nothing |
| 193 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 194 |
data |
| 195 |
------ |
| 196 |
(0 rows) |
| 197 |
|
| 198 |
COMMIT PREPARED 'test_prepared_nodecode'; |
| 199 |
-- should be decoded now |
| 200 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 201 |
data |
| 202 |
--------------------------------------------------------------------- |
| 203 |
BEGIN |
| 204 |
table public.test_prepared1: INSERT: id[integer]:20 data[text]:null |
| 205 |
COMMIT |
| 206 |
(3 rows) |
| 207 |
|
| 208 |
-- Test that accessing a TOAST table is permitted during the decoding of a |
| 209 |
-- prepared transaction. |
| 210 |
-- Create a table with a column that uses a TOASTed default value. |
| 211 |
-- (temporarily hide query, to avoid the long CREATE TABLE stmt) |
| 212 |
\set ECHO none |
| 213 |
BEGIN; |
| 214 |
INSERT INTO test_tab VALUES('test'); |
| 215 |
PREPARE TRANSACTION 'test_toast_table_access'; |
| 216 |
SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1'); |
| 217 |
count |
| 218 |
------- |
| 219 |
3 |
| 220 |
(1 row) |
| 221 |
|
| 222 |
COMMIT PREPARED 'test_toast_table_access'; |
| 223 |
-- consume commit prepared |
| 224 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1'); |
| 225 |
data |
| 226 |
------------------------------------------- |
| 227 |
COMMIT PREPARED 'test_toast_table_access' |
| 228 |
(1 row) |
| 229 |
|
| 230 |
-- Test that an empty prepared transaction should not be decoded, whether it |
| 231 |
-- is committed or rolled back. |
| 232 |
BEGIN; |
| 233 |
SELECT * FROM test_prepared1 WHERE id = 1 FOR SHARE; |
| 234 |
id | data |
| 235 |
----+------ |
| 236 |
1 | |
| 237 |
(1 row) |
| 238 |
|
| 239 |
PREPARE TRANSACTION 'test_empty_transaction'; |
| 240 |
COMMIT PREPARED 'test_empty_transaction'; |
| 241 |
BEGIN; |
| 242 |
SELECT * FROM test_prepared1 WHERE id = 1 FOR SHARE; |
| 243 |
id | data |
| 244 |
----+------ |
| 245 |
1 | |
| 246 |
(1 row) |
| 247 |
|
| 248 |
PREPARE TRANSACTION 'test_empty_transaction'; |
| 249 |
ROLLBACK PREPARED 'test_empty_transaction'; |
| 250 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 251 |
data |
| 252 |
------ |
| 253 |
(0 rows) |
| 254 |
|
| 255 |
-- Test 8: |
| 256 |
-- cleanup and make sure results are also empty |
| 257 |
DROP TABLE test_prepared1; |
| 258 |
DROP TABLE test_prepared2; |
| 259 |
DROP TABLE test_prepared_savepoint; |
| 260 |
DROP TABLE test_tab; |
| 261 |
-- show results. There should be nothing to show |
| 262 |
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1'); |
| 263 |
data |
| 264 |
------ |
| 265 |
(0 rows) |
| 266 |
|
| 267 |
SELECT pg_drop_replication_slot('regression_slot'); |
| 268 |
pg_drop_replication_slot |
| 269 |
-------------------------- |
| 270 |
|
| 271 |
(1 row) |
| 272 |
|