Debian Codegraph @

contrib/test_decoding/sql/twophase.sql

raw
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
6 CREATE TABLE test_prepared1(id integer primary key);
7 CREATE TABLE test_prepared2(id integer primary key);
8
9 -- Test that decoding happens at PREPARE time when two-phase-commit is enabled.
10 -- Decoding after COMMIT PREPARED must have all the commands in the transaction.
11 BEGIN;
12 INSERT INTO test_prepared1 VALUES (1);
13 INSERT INTO test_prepared1 VALUES (2);
14 -- should show nothing because the xact has not been prepared yet.
15 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
16 PREPARE TRANSACTION 'test_prepared#1';
17 -- should show both the above inserts and the PREPARE TRANSACTION.
18 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
19 COMMIT PREPARED 'test_prepared#1';
20 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
21
22 -- Test that rollback of a prepared xact is decoded.
23 BEGIN;
24 INSERT INTO test_prepared1 VALUES (3);
25 PREPARE TRANSACTION 'test_prepared#2';
26 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
27 ROLLBACK PREPARED 'test_prepared#2';
28 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
29
30 -- Test prepare of a xact containing ddl. Leaving xact uncommitted for next test.
31 BEGIN;
32 ALTER TABLE test_prepared1 ADD COLUMN data text;
33 INSERT INTO test_prepared1 VALUES (4, 'frakbar');
34 PREPARE TRANSACTION 'test_prepared#3';
35 -- confirm that exclusive lock from the ALTER command is held on test_prepared1 table
36 SELECT 'test_prepared_1' AS relation, locktype, mode
37 FROM pg_locks
38 WHERE locktype = 'relation'
39 AND relation = 'test_prepared1'::regclass;
40 -- The insert should show the newly altered column but not the DDL.
41 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
42
43 -- Test that we decode correctly while an uncommitted prepared xact
44 -- with ddl exists.
45 --
46 -- Use a separate table for the concurrent transaction because the lock from
47 -- the ALTER will stop us inserting into the other one.
48 --
49 INSERT INTO test_prepared2 VALUES (5);
50 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
51
52 COMMIT PREPARED 'test_prepared#3';
53 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
54 -- make sure stuff still works
55 INSERT INTO test_prepared1 VALUES (6);
56 INSERT INTO test_prepared2 VALUES (7);
57 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
58
59 -- Check 'CLUSTER' (as operation that hold exclusive lock) doesn't block
60 -- logical decoding.
61 BEGIN;
62 INSERT INTO test_prepared1 VALUES (8, 'othercol');
63 CLUSTER test_prepared1 USING test_prepared1_pkey;
64 INSERT INTO test_prepared1 VALUES (9, 'othercol2');
65 PREPARE TRANSACTION 'test_prepared_lock';
66
67 SELECT 'test_prepared1' AS relation, locktype, mode
68 FROM pg_locks
69 WHERE locktype = 'relation'
70 AND relation = 'test_prepared1'::regclass;
71 -- The above CLUSTER command shouldn't cause a timeout on 2pc decoding.
72 \set env_timeout ''
73 \getenv env_timeout PG_TEST_TIMEOUT_DEFAULT
74 SELECT COALESCE(NULLIF(:'env_timeout', ''), '180') || 's' AS timeout \gset
75 SET statement_timeout = :'timeout';
76 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
77 RESET statement_timeout;
78 COMMIT PREPARED 'test_prepared_lock';
79 -- consume the commit
80 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
81
82 -- Test savepoints and sub-xacts. Creating savepoints will create
83 -- sub-xacts implicitly.
84 BEGIN;
85 CREATE TABLE test_prepared_savepoint (a int);
86 INSERT INTO test_prepared_savepoint VALUES (1);
87 SAVEPOINT test_savepoint;
88 INSERT INTO test_prepared_savepoint VALUES (2);
89 ROLLBACK TO SAVEPOINT test_savepoint;
90 PREPARE TRANSACTION 'test_prepared_savepoint';
91 -- should show only 1, not 2
92 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
93 COMMIT PREPARED 'test_prepared_savepoint';
94 -- consume the commit
95 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
96
97 -- Test that a GID containing "_nodecode" gets decoded at commit prepared time.
98 BEGIN;
99 INSERT INTO test_prepared1 VALUES (20);
100 PREPARE TRANSACTION 'test_prepared_nodecode';
101 -- should show nothing
102 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
103 COMMIT PREPARED 'test_prepared_nodecode';
104 -- should be decoded now
105 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
106
107 -- Test that accessing a TOAST table is permitted during the decoding of a
108 -- prepared transaction.
109
110 -- Create a table with a column that uses a TOASTed default value.
111 -- (temporarily hide query, to avoid the long CREATE TABLE stmt)
112 \set ECHO none
113 SELECT 'CREATE TABLE test_tab (a text DEFAULT ''' || string_agg('toast value', '') || ''');' FROM generate_series(1, 4000)
114 \gexec
115 \set ECHO all
116
117 BEGIN;
118 INSERT INTO test_tab VALUES('test');
119 PREPARE TRANSACTION 'test_toast_table_access';
120
121 SELECT count(*) FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1');
122
123 COMMIT PREPARED 'test_toast_table_access';
124
125 -- consume commit prepared
126 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1');
127
128 -- Test that an empty prepared transaction should not be decoded, whether it
129 -- is committed or rolled back.
130 BEGIN;
131 SELECT * FROM test_prepared1 WHERE id = 1 FOR SHARE;
132 PREPARE TRANSACTION 'test_empty_transaction';
133 COMMIT PREPARED 'test_empty_transaction';
134 BEGIN;
135 SELECT * FROM test_prepared1 WHERE id = 1 FOR SHARE;
136 PREPARE TRANSACTION 'test_empty_transaction';
137 ROLLBACK PREPARED 'test_empty_transaction';
138 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
139
140 -- Test 8:
141 -- cleanup and make sure results are also empty
142 DROP TABLE test_prepared1;
143 DROP TABLE test_prepared2;
144 DROP TABLE test_prepared_savepoint;
145 DROP TABLE test_tab;
146 -- show results. There should be nothing to show
147 SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
148
149 SELECT pg_drop_replication_slot('regression_slot');