Debian Codegraph @

src/test/isolation/specs/eval-plan-qual.spec

raw
1 # Tests for the EvalPlanQual mechanism
2 #
3 # EvalPlanQual is used in READ COMMITTED isolation level to attempt to
4 # re-execute UPDATE and DELETE operations against rows that were updated
5 # by some concurrent transaction.
6
7 setup
8 {
9 CREATE TABLE accounts (accountid text PRIMARY KEY, balance numeric not null,
10 balance2 numeric GENERATED ALWAYS AS (balance * 2) STORED);
11 INSERT INTO accounts VALUES ('checking', 600), ('savings', 600);
12
13 CREATE FUNCTION update_checking(int) RETURNS bool LANGUAGE sql AS $$
14 UPDATE accounts SET balance = balance + 1 WHERE accountid = 'checking'; SELECT true;$$;
15
16 CREATE TABLE accounts_ext (accountid text PRIMARY KEY, balance numeric not null, other text);
17 INSERT INTO accounts_ext VALUES ('checking', 600, 'other'), ('savings', 700, null);
18 ALTER TABLE accounts_ext ADD COLUMN newcol int DEFAULT 42;
19 ALTER TABLE accounts_ext ADD COLUMN newcol2 text DEFAULT NULL;
20
21 CREATE TABLE p (a int, b int, c int);
22 CREATE TABLE c1 () INHERITS (p);
23 CREATE TABLE c2 () INHERITS (p);
24 CREATE TABLE c3 () INHERITS (p);
25 INSERT INTO c1 SELECT 0, a / 3, a % 3 FROM generate_series(0, 9) a;
26 INSERT INTO c2 SELECT 1, a / 3, a % 3 FROM generate_series(0, 9) a;
27 INSERT INTO c3 SELECT 2, a / 3, a % 3 FROM generate_series(0, 9) a;
28
29 CREATE TABLE table_a (id integer, value text);
30 CREATE TABLE table_b (id integer, value text);
31 INSERT INTO table_a VALUES (1, 'tableAValue');
32 INSERT INTO table_b VALUES (1, 'tableBValue');
33
34 CREATE TABLE jointest AS SELECT generate_series(1,10) AS id, 0 AS data;
35 CREATE INDEX ON jointest(id);
36
37 CREATE TABLE parttbl (a int, b int, c int,
38 d int GENERATED ALWAYS AS (a + b) STORED) PARTITION BY LIST (a);
39 CREATE TABLE parttbl1 PARTITION OF parttbl FOR VALUES IN (1);
40 CREATE TABLE parttbl2 PARTITION OF parttbl
41 (d WITH OPTIONS GENERATED ALWAYS AS (a + b + 1000) STORED)
42 FOR VALUES IN (2);
43 INSERT INTO parttbl VALUES (1, 1, 1), (2, 2, 2);
44
45 CREATE TABLE another_parttbl (a int, b int, c int) PARTITION BY LIST (a);
46 CREATE TABLE another_parttbl1 PARTITION OF another_parttbl FOR VALUES IN (1);
47 CREATE TABLE another_parttbl2 PARTITION OF another_parttbl FOR VALUES IN (2);
48 INSERT INTO another_parttbl VALUES (1, 1, 1);
49
50 CREATE FUNCTION noisy_oper(p_comment text, p_a anynonarray, p_op text, p_b anynonarray)
51 RETURNS bool LANGUAGE plpgsql AS $$
52 DECLARE
53 r bool;
54 BEGIN
55 EXECUTE format('SELECT $1 %s $2', p_op) INTO r USING p_a, p_b;
56 RAISE NOTICE '%: % % % % %: %', p_comment, pg_typeof(p_a), p_a, p_op, pg_typeof(p_b), p_b, r;
57 RETURN r;
58 END;$$;
59 }
60
61 teardown
62 {
63 DROP TABLE accounts;
64 DROP FUNCTION update_checking(int);
65 DROP TABLE accounts_ext;
66 DROP TABLE p CASCADE;
67 DROP TABLE table_a, table_b, jointest;
68 DROP TABLE parttbl;
69 DROP TABLE another_parttbl;
70 DROP FUNCTION noisy_oper(text, anynonarray, text, anynonarray)
71 }
72
73 session s1
74 setup { BEGIN ISOLATION LEVEL READ COMMITTED; }
75 # wx1 then wx2 checks the basic case of re-fetching up-to-date values
76 step wx1 { UPDATE accounts SET balance = balance - 200 WHERE accountid = 'checking' RETURNING balance; }
77 # wy1 then wy2 checks the case where quals pass then fail
78 step wy1 { UPDATE accounts SET balance = balance + 500 WHERE accountid = 'checking' RETURNING balance; }
79 # wx2 then wb1 checks the case of re-fetching up-to-date values for DELETE ... RETURNING ...
80 step wb1 { DELETE FROM accounts WHERE balance = 600 RETURNING *; }
81
82 step wxext1 { UPDATE accounts_ext SET balance = balance - 200 WHERE accountid = 'checking' RETURNING balance; }
83 step tocds1 { UPDATE accounts SET accountid = 'cds' WHERE accountid = 'checking'; }
84 step tocdsext1 { UPDATE accounts_ext SET accountid = 'cds' WHERE accountid = 'checking'; }
85
86 # d1 then wx1 checks that update can deal with the updated row vanishing
87 # wx2 then d1 checks that the delete affects the updated row
88 # wx2, wx2 then d1 checks that the delete checks the quals correctly (balance too high)
89 # wx2, d2, then d1 checks that delete handles a vanishing row correctly
90 step d1 { DELETE FROM accounts WHERE accountid = 'checking' AND balance < 1500 RETURNING balance; }
91
92 # upsert tests are to check writable-CTE cases
93 step upsert1 {
94 WITH upsert AS
95 (UPDATE accounts SET balance = balance + 500
96 WHERE accountid = 'savings'
97 RETURNING accountid)
98 INSERT INTO accounts SELECT 'savings', 500
99 WHERE NOT EXISTS (SELECT 1 FROM upsert);
100 }
101
102 # Tests for Tid / Tid Range Scan
103 step tid1 { UPDATE accounts SET balance = balance + 100 WHERE ctid = '(0,1)' RETURNING accountid, balance; }
104 step tidrange1 { UPDATE accounts SET balance = balance + 100 WHERE ctid BETWEEN '(0,1)' AND '(0,1)' RETURNING accountid, balance; }
105
106 # tests with table p check inheritance cases:
107 # readp1/writep1/readp2 tests a bug where nodeLockRows did the wrong thing
108 # when the first updated tuple was in a non-first child table.
109 # writep2/returningp1 tests a memory allocation issue
110 # writep3a/writep3b tests updates touching more than one table
111 # writep4a/writep4b tests a case where matches in another table confused EPQ
112 # writep4a/deletep4 tests the same case in the DELETE path
113
114 step readp { SELECT tableoid::regclass, ctid, * FROM p; }
115 step readp1 { SELECT tableoid::regclass, ctid, * FROM p WHERE b IN (0, 1) AND c = 0 FOR UPDATE; }
116 step writep1 { UPDATE p SET b = -1 WHERE a = 1 AND b = 1 AND c = 0; }
117 step writep2 { UPDATE p SET b = -b WHERE a = 1 AND c = 0; }
118 step writep3a { UPDATE p SET b = -b WHERE c = 0; }
119 step writep4a { UPDATE p SET c = 4 WHERE c = 0; }
120 step c1 { COMMIT; }
121 step r1 { ROLLBACK; }
122
123 # these tests are meant to exercise EvalPlanQualFetchRowMark,
124 # ie, handling non-locked tables in an EvalPlanQual recheck
125
126 step partiallock {
127 SELECT * FROM accounts a1, accounts a2
128 WHERE a1.accountid = a2.accountid
129 FOR UPDATE OF a1;
130 }
131 step lockwithvalues {
132 -- Reference rowmark column that differs in type from targetlist at some attno.
133 -- See CAHU7rYZo_C4ULsAx_LAj8az9zqgrD8WDd4hTegDTMM1LMqrBsg@mail.gmail.com
134 SELECT a1.*, v.id FROM accounts a1, (values('checking'::text, 'nan'::text),('savings', 'nan')) v(id, notnumeric)
135 WHERE a1.accountid = v.id AND v.notnumeric != 'einszwei'
136 FOR UPDATE OF a1;
137 }
138 step partiallock_ext {
139 SELECT * FROM accounts_ext a1, accounts_ext a2
140 WHERE a1.accountid = a2.accountid
141 FOR UPDATE OF a1;
142 }
143
144 # these tests exercise EvalPlanQual with a SubLink sub-select (which should be
145 # unaffected by any EPQ recheck behavior in the outer query); cf bug #14034
146
147 step updateforss {
148 UPDATE table_a SET value = 'newTableAValue' WHERE id = 1;
149 UPDATE table_b SET value = 'newTableBValue' WHERE id = 1;
150 }
151
152 # these tests exercise EvalPlanQual with conditional InitPlans which
153 # have not been executed prior to the EPQ
154
155 step updateforcip {
156 UPDATE table_a SET value = NULL WHERE id = 1;
157 }
158
159 # these tests exercise mark/restore during EPQ recheck, cf bug #15032
160
161 step selectjoinforupdate {
162 set local enable_nestloop to 0;
163 set local enable_hashjoin to 0;
164 set local enable_seqscan to 0;
165 explain (costs off)
166 select * from jointest a join jointest b on a.id=b.id for update;
167 select * from jointest a join jointest b on a.id=b.id for update;
168 }
169
170 # these tests exercise Result plan nodes participating in EPQ
171
172 step selectresultforupdate {
173 select * from (select 1 as x) ss1 join (select 7 as y) ss2 on true
174 left join table_a a on a.id = x, jointest jt
175 where jt.id = y;
176 explain (verbose, costs off)
177 select * from (select 1 as x) ss1 join (select 7 as y) ss2 on true
178 left join table_a a on a.id = x, jointest jt
179 where jt.id = y for update of jt, ss1, ss2;
180 select * from (select 1 as x) ss1 join (select 7 as y) ss2 on true
181 left join table_a a on a.id = x, jointest jt
182 where jt.id = y for update of jt, ss1, ss2;
183 }
184
185 # test for EPQ on a partitioned result table
186
187 step simplepartupdate {
188 update parttbl set b = b + 10;
189 }
190
191 # test scenarios where update may cause row movement
192
193 step simplepartupdate_route1to2 {
194 update parttbl set a = 2 where c = 1 returning *;
195 }
196
197 step simplepartupdate_noroute {
198 update parttbl set b = 2 where c = 1 returning *;
199 }
200
201 # test system class LockTuple()
202
203 step sys1 {
204 UPDATE pg_class SET reltuples = 123 WHERE oid = 'accounts'::regclass;
205 }
206
207 step s1pp1 { UPDATE another_parttbl SET b = b + 1 WHERE a = 1; }
208
209 step updateformergevalues { UPDATE accounts SET balance = balance + 100; }
210
211 session s2
212 setup { BEGIN ISOLATION LEVEL READ COMMITTED; }
213 step wx2 { UPDATE accounts SET balance = balance + 450 WHERE accountid = 'checking' RETURNING balance; }
214 step wy2 { UPDATE accounts SET balance = balance + 1000 WHERE accountid = 'checking' AND balance < 1000 RETURNING balance; }
215 step d2 { DELETE FROM accounts WHERE accountid = 'checking'; }
216
217 step upsert2 {
218 WITH upsert AS
219 (UPDATE accounts SET balance = balance + 1234
220 WHERE accountid = 'savings'
221 RETURNING accountid)
222 INSERT INTO accounts SELECT 'savings', 1234
223 WHERE NOT EXISTS (SELECT 1 FROM upsert);
224 }
225 step wx2_ext { UPDATE accounts_ext SET balance = balance + 450; }
226 step readp2 { SELECT tableoid::regclass, ctid, * FROM p WHERE b IN (0, 1) AND c = 0 FOR UPDATE; }
227 step returningp1 {
228 WITH u AS ( UPDATE p SET b = b WHERE a > 0 RETURNING * )
229 SELECT * FROM u;
230 }
231 step writep3b { UPDATE p SET b = -b WHERE c = 0; }
232 step writep4b { UPDATE p SET b = -4 WHERE c = 0; }
233 step deletep4 { DELETE FROM p WHERE c = 0; }
234 step readforss {
235 SELECT ta.id AS ta_id, ta.value AS ta_value,
236 (SELECT ROW(tb.id, tb.value)
237 FROM table_b tb WHERE ta.id = tb.id) AS tb_row
238 FROM table_a ta
239 WHERE ta.id = 1 FOR UPDATE OF ta;
240 }
241 step updateforcip2 {
242 UPDATE table_a SET value = COALESCE(value, (SELECT text 'newValue')) WHERE id = 1;
243 }
244 step updateforcip3 {
245 WITH d(val) AS (SELECT text 'newValue' FROM generate_series(1,1))
246 UPDATE table_a SET value = COALESCE(value, (SELECT val FROM d)) WHERE id = 1;
247 }
248 step wrtwcte { UPDATE table_a SET value = 'tableAValue2' WHERE id = 1; }
249 step wrjt { UPDATE jointest SET data = 42 WHERE id = 7; }
250
251 step tid2 { UPDATE accounts SET balance = balance + 200 WHERE ctid = '(0,1)' RETURNING accountid, balance; }
252 step tidrange2 { UPDATE accounts SET balance = balance + 200 WHERE ctid BETWEEN '(0,1)' AND '(0,1)' RETURNING accountid, balance; }
253 # here, recheck succeeds; (0,3) is the id that step tid1 will assign
254 step tidsucceed2 { UPDATE accounts SET balance = balance + 200 WHERE ctid = '(0,1)' OR ctid = '(0,3)' RETURNING accountid, balance; }
255
256 step conditionalpartupdate {
257 update parttbl set c = -c where b < 10;
258 }
259
260 step complexpartupdate {
261 with u as (update parttbl set b = b + 1 returning parttbl.*)
262 update parttbl p set b = u.b + 100 from u where p.a = u.a;
263 }
264
265 step complexpartupdate_route_err1 {
266 with u as (update another_parttbl set a = 1 returning another_parttbl.*)
267 update parttbl p set a = u.a from u where p.a = u.a and p.c = 1 returning p.*;
268 }
269
270 step complexpartupdate_route {
271 with u as (update another_parttbl set a = 1 returning another_parttbl.*)
272 update parttbl p set a = p.b from u where p.a = u.a and p.c = 1 returning p.*;
273 }
274
275 step complexpartupdate_doesnt_route {
276 with u as (update another_parttbl set a = 1 returning another_parttbl.*)
277 update parttbl p set a = 3 - p.b from u where p.a = u.a and p.c = 1 returning p.*;
278 }
279
280 # Use writable CTEs to create self-updated rows, that then are
281 # (updated|deleted). The *fail versions of the tests additionally
282 # perform an update, via a function, in a different command, to test
283 # behaviour relating to that.
284 step updwcte { WITH doup AS (UPDATE accounts SET balance = balance + 1100 WHERE accountid = 'checking' RETURNING *) UPDATE accounts a SET balance = doup.balance + 100 FROM doup RETURNING *; }
285 step updwctefail { WITH doup AS (UPDATE accounts SET balance = balance + 1100 WHERE accountid = 'checking' RETURNING *, update_checking(999)) UPDATE accounts a SET balance = doup.balance + 100 FROM doup RETURNING *; }
286 step delwcte { WITH doup AS (UPDATE accounts SET balance = balance + 1100 WHERE accountid = 'checking' RETURNING *) DELETE FROM accounts a USING doup RETURNING *; }
287 step delwctefail { WITH doup AS (UPDATE accounts SET balance = balance + 1100 WHERE accountid = 'checking' RETURNING *, update_checking(999)) DELETE FROM accounts a USING doup RETURNING *; }
288
289 # Check that nested EPQ works correctly
290 step wnested2 {
291 UPDATE accounts SET balance = balance - 1200
292 WHERE noisy_oper('upid', accountid, '=', 'checking')
293 AND noisy_oper('up', balance, '>', 200.0)
294 AND EXISTS (
295 SELECT accountid
296 FROM accounts_ext ae
297 WHERE noisy_oper('lock_id', ae.accountid, '=', accounts.accountid)
298 AND noisy_oper('lock_bal', ae.balance, '>', 200.0)
299 FOR UPDATE
300 );
301 }
302
303 step sysupd2 {
304 UPDATE pg_class SET reltuples = reltuples * 2
305 WHERE oid = 'accounts'::regclass;
306 }
307
308 step sysmerge2 {
309 MERGE INTO pg_class
310 USING (SELECT 'accounts'::regclass AS o) j
311 ON o = oid
312 WHEN MATCHED THEN UPDATE SET reltuples = reltuples * 2;
313 }
314
315 step c2 { COMMIT; }
316 step r2 { ROLLBACK; }
317
318 step s2pp1 { SET plan_cache_mode TO force_generic_plan; }
319 step s2pp2 { PREPARE epd AS DELETE FROM another_parttbl WHERE a = $1; }
320 step s2pp3 { EXECUTE epd(1); }
321 step s2pp4 { DELETE FROM another_parttbl WHERE a = (SELECT 1); }
322
323 step mergevalues {
324 MERGE INTO accounts
325 USING (VALUES ('checking', 610), ('savings', 620)) v(accountid, balance)
326 ON v.accountid = accounts.accountid
327 WHEN MATCHED THEN UPDATE SET balance = v.balance
328 WHEN NOT MATCHED THEN INSERT VALUES ('unmatched', -1);
329 }
330
331 session s3
332 setup { BEGIN ISOLATION LEVEL READ COMMITTED; }
333 step read { SELECT * FROM accounts ORDER BY accountid; }
334 step read_ext { SELECT * FROM accounts_ext ORDER BY accountid; }
335 step read_a { SELECT * FROM table_a ORDER BY id; }
336 step read_part { SELECT * FROM parttbl ORDER BY a, c; }
337
338 # this test exercises EvalPlanQual with a CTE, cf bug #14328
339 step readwcte {
340 WITH
341 cte1 AS (
342 SELECT id FROM table_b WHERE value = 'tableBValue'
343 ),
344 cte2 AS (
345 SELECT * FROM table_a
346 WHERE id = (SELECT id FROM cte1)
347 FOR UPDATE
348 )
349 SELECT * FROM cte2;
350 }
351
352 # this test exercises a different CTE misbehavior, cf bug #14870
353 step multireadwcte {
354 WITH updated AS (
355 UPDATE table_a SET value = 'tableAValue3' WHERE id = 1 RETURNING id
356 )
357 SELECT (SELECT id FROM updated) AS subid, * FROM updated;
358 }
359
360 teardown { COMMIT; }
361
362 # test that normal update follows update chains, and reverifies quals
363 permutation wx1 wx2 c1 c2 read
364 permutation wy1 wy2 c1 c2 read
365 permutation wx1 wx2 r1 c2 read
366 permutation wy1 wy2 r1 c2 read
367
368 # test that deletes follow chains, and if necessary reverifies quals
369 permutation wx1 d1 wx2 c1 c2 read
370 permutation wx2 d1 c2 c1 read
371 permutation wx2 wx2 d1 c2 c1 read
372 permutation wx2 d2 d1 c2 c1 read
373 permutation wx1 d1 wx2 r1 c2 read
374 permutation wx2 d1 r2 c1 read
375 permutation wx2 wx2 d1 r2 c1 read
376 permutation wx2 d2 d1 r2 c1 read
377 permutation d1 wx2 c1 c2 read
378 permutation d1 wx2 r1 c2 read
379
380 # Check that nested EPQ works correctly
381 permutation wnested2 c1 c2 read
382 permutation wx1 wxext1 wnested2 c1 c2 read
383 permutation wx1 wx1 wxext1 wnested2 c1 c2 read
384 permutation wx1 wx1 wxext1 wxext1 wnested2 c1 c2 read
385 permutation wx1 wxext1 wxext1 wnested2 c1 c2 read
386 permutation wx1 tocds1 wnested2 c1 c2 read
387 permutation wx1 tocdsext1 wnested2 c1 c2 read
388
389 # test that an update to a self-modified row is ignored when
390 # previously updated by the same cid
391 permutation wx1 updwcte c1 c2 read
392 # test that an update to a self-modified row throws error when
393 # previously updated by a different cid
394 permutation wx1 updwctefail c1 c2 read
395 # test that a delete to a self-modified row is ignored when
396 # previously updated by the same cid
397 permutation wx1 delwcte c1 c2 read
398 # test that a delete to a self-modified row throws error when
399 # previously updated by a different cid
400 permutation wx1 delwctefail c1 c2 read
401 # test that a delete re-fetches up-to-date values for returning clause
402 permutation read wx2 wb1 c2 c1 read
403
404 permutation upsert1 upsert2 c1 c2 read
405 permutation readp1 writep1 readp2 c1 c2
406 permutation writep2 returningp1 c1 c2
407 permutation writep3a writep3b c1 c2
408 permutation writep4a writep4b c1 c2 readp
409 permutation writep4a deletep4 c1 c2 readp
410 permutation wx2 partiallock c2 c1 read
411 permutation wx2 lockwithvalues c2 c1 read
412 permutation wx2_ext partiallock_ext c2 c1 read_ext
413 permutation updateforss readforss c1 c2
414 permutation updateforcip updateforcip2 c1 c2 read_a
415 permutation updateforcip updateforcip3 c1 c2 read_a
416 permutation wrtwcte readwcte c1 c2
417 permutation wrjt selectjoinforupdate c2 c1
418 permutation wrjt selectresultforupdate c2 c1
419 permutation wrtwcte multireadwcte c1 c2
420 permutation tid1 tid2 c1 c2 read
421 permutation tid1 tidsucceed2 c1 c2 read
422 permutation tidrange1 tidrange2 c1 c2 read
423 # test that a rollback on s1 has s2 perform the update on the original row
424 permutation tid1 tid2 r1 c2 read
425
426 permutation simplepartupdate conditionalpartupdate c1 c2 read_part
427 permutation simplepartupdate complexpartupdate c1 c2 read_part
428 permutation simplepartupdate_route1to2 complexpartupdate_route_err1 c1 c2 read_part
429 permutation simplepartupdate_noroute complexpartupdate_route c1 c2 read_part
430 permutation simplepartupdate_noroute complexpartupdate_doesnt_route c1 c2 read_part
431
432 permutation sys1 sysupd2 c1 c2
433 permutation sys1 sysmerge2 c1 c2
434
435 # Exercise run-time partition pruning code in an EPQ recheck
436 permutation s1pp1 s2pp1 s2pp2 s2pp3 c1 c2
437 permutation s1pp1 s2pp4 c1 c2
438
439 # test EPQ recheck in MERGE from VALUES_RTE, cf bug #19355
440 permutation updateformergevalues mergevalues c1 c2 read