Skip to content

Commit 73df86a

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 46bee23 commit 73df86a

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/backend/executor/nodeCtescan.c

Lines changed: 4 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,24 @@ a b c
104104
2 2 2
105105
2 3 0
106106
step c2: COMMIT;
107+
108+
starting permutation: wrtwcte readwcte c1 c2
109+
step wrtwcte: UPDATE table_a SET value = 'tableAValue2' WHERE id = 1;
110+
step readwcte:
111+
WITH
112+
cte1 AS (
113+
SELECT id FROM table_b WHERE value = 'tableBValue'
114+
),
115+
cte2 AS (
116+
SELECT * FROM table_a
117+
WHERE id = (SELECT id FROM cte1)
118+
FOR UPDATE
119+
)
120+
SELECT * FROM cte2;
121+
<waiting ...>
122+
step c1: COMMIT;
123+
step c2: COMMIT;
124+
step readwcte: <... completed>
125+
id value
126+
127+
1 tableAValue2

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ setup
1616
INSERT INTO c1 SELECT 0, a / 3, a % 3 FROM generate_series(0, 9) a;
1717
INSERT INTO c2 SELECT 1, a / 3, a % 3 FROM generate_series(0, 9) a;
1818
INSERT INTO c3 SELECT 2, a / 3, a % 3 FROM generate_series(0, 9) a;
19+
20+
CREATE TABLE table_a (id integer, value text);
21+
CREATE TABLE table_b (id integer, value text);
22+
INSERT INTO table_a VALUES (1, 'tableAValue');
23+
INSERT INTO table_b VALUES (1, 'tableBValue');
1924
}
2025

2126
teardown
2227
{
2328
DROP TABLE accounts;
2429
DROP TABLE p CASCADE;
30+
DROP TABLE table_a, table_b;
2531
}
2632

2733
session "s1"
@@ -67,15 +73,32 @@ step "returningp1" {
6773
WITH u AS ( UPDATE p SET b = b WHERE a > 0 RETURNING * )
6874
SELECT * FROM u;
6975
}
76+
step "wrtwcte" { UPDATE table_a SET value = 'tableAValue2' WHERE id = 1; }
7077
step "c2" { COMMIT; }
7178

7279
session "s3"
7380
setup { BEGIN ISOLATION LEVEL READ COMMITTED; }
7481
step "read" { SELECT * FROM accounts ORDER BY accountid; }
82+
83+
# this test exercises EvalPlanQual with a CTE, cf bug #14328
84+
step "readwcte" {
85+
WITH
86+
cte1 AS (
87+
SELECT id FROM table_b WHERE value = 'tableBValue'
88+
),
89+
cte2 AS (
90+
SELECT * FROM table_a
91+
WHERE id = (SELECT id FROM cte1)
92+
FOR UPDATE
93+
)
94+
SELECT * FROM cte2;
95+
}
96+
7597
teardown { COMMIT; }
7698

7799
permutation "wx1" "wx2" "c1" "c2" "read"
78100
permutation "wy1" "wy2" "c1" "c2" "read"
79101
permutation "upsert1" "upsert2" "c1" "c2" "read"
80102
permutation "readp1" "writep1" "readp2" "c1" "c2"
81103
permutation "writep2" "returningp1" "c1" "c2"
104+
permutation "wrtwcte" "readwcte" "c1" "c2"

0 commit comments

Comments
 (0)