Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit340d63b

Browse files
committed
Fix misbehavior of CTE-used-in-a-subplan during EPQ rechecks.
An updating query that reads a CTE within an InitPlan or SubPlan could getincorrect results if it updates rows that are concurrently being modified.This is caused by CteScanNext supposing that nothing inside its recursiveExecProcNode call could change which read pointer is selected in the CTE'sshared tuplestore. While that's normally true because of scopingconsiderations, it can break down if an EPQ plan tree gets built during thecall, because EvalPlanQualStart builds execution trees for all subplanswhether they're going to be used during the recheck or not. And it seemslike a pretty shaky assumption anyway, so let's just reselect our own readpointer here.Per bug #14870 from Andrei Gorita. This has been broken since CTEs wereimplemented, so back-patch to all supported branches.Discussion:https://postgr.es/m/20171024155358.1471.82377@wrigleys.postgresql.org
1 parent4957398 commit340d63b

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

‎src/backend/executor/nodeCtescan.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ CteScanNext(CteScanState *node)
107107
returnNULL;
108108
}
109109

110+
/*
111+
* There are corner cases where the subplan could change which
112+
* tuplestore read pointer is active, so be sure to reselect ours
113+
* before storing the tuple we got.
114+
*/
115+
tuplestore_select_read_pointer(tuplestorestate,node->readptr);
116+
110117
/*
111118
* Append a copy of the returned tuple to tuplestore. NOTE: because
112119
* our read pointer is certainly in EOF state, its read position will
@@ -176,6 +183,12 @@ ExecInitCteScan(CteScan *node, EState *estate, int eflags)
176183
* we might be asked to rescan the CTE even though upper levels didn't
177184
* tell us to be prepared to do it efficiently. Annoying, since this
178185
* prevents truncation of the tuplestore. XXX FIXME
186+
*
187+
* Note: if we are in an EPQ recheck plan tree, it's likely that no access
188+
* to the tuplestore is needed at all, making this even more annoying.
189+
* It's not worth improving that as long as all the read pointers would
190+
* have REWIND anyway, but if we ever improve this logic then that aspect
191+
* should be considered too.
179192
*/
180193
eflags |=EXEC_FLAG_REWIND;
181194

‎src/test/isolation/expected/eval-plan-qual.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,18 @@ step readwcte: <... completed>
184184
id value
185185

186186
1 tableAValue2
187+
188+
starting permutation: wrtwcte multireadwcte c1 c2
189+
step wrtwcte: UPDATE table_a SET value = 'tableAValue2' WHERE id = 1;
190+
step multireadwcte:
191+
WITH updated AS (
192+
UPDATE table_a SET value = 'tableAValue3' WHERE id = 1 RETURNING id
193+
)
194+
SELECT (SELECT id FROM updated) AS subid, * FROM updated;
195+
<waiting ...>
196+
step c1: COMMIT;
197+
step c2: COMMIT;
198+
step multireadwcte: <... completed>
199+
subid id
200+
201+
1 1

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ step "readwcte"{
124124
SELECT*FROMcte2;
125125
}
126126

127+
# this test exercises a different CTE misbehavior, cf bug #14870
128+
step"multireadwcte"{
129+
WITHupdatedAS (
130+
UPDATEtable_aSETvalue='tableAValue3'WHEREid=1RETURNINGid
131+
)
132+
SELECT (SELECTidFROMupdated)ASsubid,*FROMupdated;
133+
}
134+
127135
teardown{COMMIT; }
128136

129137
permutation"wx1""wx2""c1""c2""read"
@@ -135,3 +143,4 @@ permutation "wx2" "partiallock" "c2" "c1" "read"
135143
permutation"wx2""lockwithvalues""c2""c1""read"
136144
permutation"updateforss""readforss""c1""c2"
137145
permutation"wrtwcte""readwcte""c1""c2"
146+
permutation"wrtwcte""multireadwcte""c1""c2"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp