mirror of
https://github.com/postgres/postgres.git
synced 2025-07-26 01:22:12 +03:00
Fix misbehavior of CTE-used-in-a-subplan during EPQ rechecks.
An updating query that reads a CTE within an InitPlan or SubPlan could get incorrect results if it updates rows that are concurrently being modified. This is caused by CteScanNext supposing that nothing inside its recursive ExecProcNode call could change which read pointer is selected in the CTE's shared tuplestore. While that's normally true because of scoping considerations, it can break down if an EPQ plan tree gets built during the call, because EvalPlanQualStart builds execution trees for all subplans whether they're going to be used during the recheck or not. And it seems like a pretty shaky assumption anyway, so let's just reselect our own read pointer here. Per bug #14870 from Andrei Gorita. This has been broken since CTEs were implemented, so back-patch to all supported branches. Discussion: https://postgr.es/m/20171024155358.1471.82377@wrigleys.postgresql.org
This commit is contained in:
@ -217,3 +217,18 @@ id data id data
|
||||
9 0 9 0
|
||||
10 0 10 0
|
||||
step c1: COMMIT;
|
||||
|
||||
starting permutation: wrtwcte multireadwcte c1 c2
|
||||
step wrtwcte: UPDATE table_a SET value = 'tableAValue2' WHERE id = 1;
|
||||
step multireadwcte:
|
||||
WITH updated AS (
|
||||
UPDATE table_a SET value = 'tableAValue3' WHERE id = 1 RETURNING id
|
||||
)
|
||||
SELECT (SELECT id FROM updated) AS subid, * FROM updated;
|
||||
<waiting ...>
|
||||
step c1: COMMIT;
|
||||
step c2: COMMIT;
|
||||
step multireadwcte: <... completed>
|
||||
subid id
|
||||
|
||||
1 1
|
||||
|
@ -139,6 +139,14 @@ step "readwcte" {
|
||||
SELECT * FROM cte2;
|
||||
}
|
||||
|
||||
# this test exercises a different CTE misbehavior, cf bug #14870
|
||||
step "multireadwcte" {
|
||||
WITH updated AS (
|
||||
UPDATE table_a SET value = 'tableAValue3' WHERE id = 1 RETURNING id
|
||||
)
|
||||
SELECT (SELECT id FROM updated) AS subid, * FROM updated;
|
||||
}
|
||||
|
||||
teardown { COMMIT; }
|
||||
|
||||
permutation "wx1" "wx2" "c1" "c2" "read"
|
||||
@ -151,3 +159,4 @@ permutation "wx2" "lockwithvalues" "c2" "c1" "read"
|
||||
permutation "updateforss" "readforss" "c1" "c2"
|
||||
permutation "wrtwcte" "readwcte" "c1" "c2"
|
||||
permutation "wrjt" "selectjoinforupdate" "c2" "c1"
|
||||
permutation "wrtwcte" "multireadwcte" "c1" "c2"
|
||||
|
Reference in New Issue
Block a user