Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 96dd77d

Browse files
committed
Be sure to rewind the tuplestore read pointer in non-leader CTEScan nodes.
ExecInitCteScan supposed that it didn't have to do anything to the extra tuplestore read pointer it gets from tuplestore_alloc_read_pointer. However, it needs this read pointer to be positioned at the start of the tuplestore, while tuplestore_alloc_read_pointer is actually defined as cloning the current position of read pointer 0. In normal situations that accidentally works because we initialize the whole plan tree at once, before anything gets read. But it fails in an EvalPlanQual recheck, as illustrated in bug #14328 from Dima Pavlov. To fix, just forcibly rewind the pointer after tuplestore_alloc_read_pointer. The cost of doing so is negligible unless the tuplestore is already in TSS_READFILE state, which wouldn't happen in normal cases. We could consider altering tuplestore's API to make that case cheaper, but that would make for a more invasive back-patch and it doesn't seem worth it. This has been broken probably for as long as we've had CTEs, so back-patch to all supported branches. Discussion: <32468.1474548308@sss.pgh.pa.us>
1 parent 8b84552 commit 96dd77d

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/backend/executor/nodeCtescan.c

+4
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,13 @@ ExecInitCteScan(CteScan *node, EState *estate, int eflags)
224224
{
225225
/* Not the leader */
226226
Assert(IsA(scanstate->leader, CteScanState));
227+
/* Create my own read pointer, and ensure it is at start */
227228
scanstate->readptr =
228229
tuplestore_alloc_read_pointer(scanstate->leader->cte_table,
229230
scanstate->eflags);
231+
tuplestore_select_read_pointer(scanstate->leader->cte_table,
232+
scanstate->readptr);
233+
tuplestore_rescan(scanstate->leader->cte_table);
230234
}
231235

232236
/*

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

+21
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,24 @@ ta_id ta_value tb_row
163163

164164
1 newTableAValue (1,tableBValue)
165165
step c2: COMMIT;
166+
167+
starting permutation: wrtwcte readwcte c1 c2
168+
step wrtwcte: UPDATE table_a SET value = 'tableAValue2' WHERE id = 1;
169+
step readwcte:
170+
WITH
171+
cte1 AS (
172+
SELECT id FROM table_b WHERE value = 'tableBValue'
173+
),
174+
cte2 AS (
175+
SELECT * FROM table_a
176+
WHERE id = (SELECT id FROM cte1)
177+
FOR UPDATE
178+
)
179+
SELECT * FROM cte2;
180+
<waiting ...>
181+
step c1: COMMIT;
182+
step c2: COMMIT;
183+
step readwcte: <... completed>
184+
id value
185+
186+
1 tableAValue2

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

+17
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,27 @@ step "readforss" {
103103
FROM table_a ta
104104
WHERE ta.id = 1 FOR UPDATE OF ta;
105105
}
106+
step "wrtwcte" { UPDATE table_a SET value = 'tableAValue2' WHERE id = 1; }
106107
step "c2" { COMMIT; }
107108

108109
session "s3"
109110
setup { BEGIN ISOLATION LEVEL READ COMMITTED; }
110111
step "read" { SELECT * FROM accounts ORDER BY accountid; }
112+
113+
# this test exercises EvalPlanQual with a CTE, cf bug #14328
114+
step "readwcte" {
115+
WITH
116+
cte1 AS (
117+
SELECT id FROM table_b WHERE value = 'tableBValue'
118+
),
119+
cte2 AS (
120+
SELECT * FROM table_a
121+
WHERE id = (SELECT id FROM cte1)
122+
FOR UPDATE
123+
)
124+
SELECT * FROM cte2;
125+
}
126+
111127
teardown { COMMIT; }
112128

113129
permutation "wx1" "wx2" "c1" "c2" "read"
@@ -118,3 +134,4 @@ permutation "writep2" "returningp1" "c1" "c2"
118134
permutation "wx2" "partiallock" "c2" "c1" "read"
119135
permutation "wx2" "lockwithvalues" "c2" "c1" "read"
120136
permutation "updateforss" "readforss" "c1" "c2"
137+
permutation "wrtwcte" "readwcte" "c1" "c2"

0 commit comments

Comments
 (0)