Skip to content

Commit d17cf76

Browse files
committed
Fix rescan logic in nodeCtescan.
The previous coding essentially assumed that nodes would be rescanned in the same order they were initialized in; or at least that the "leader" of a group of CTEscans would be rescanned before any others were required to execute. Unfortunately, that isn't even a little bit true. It's possible to devise queries in which the leader isn't rescanned until other CTEscans on the same CTE have run to completion, or even in which the leader never gets a rescan call at all. The fix makes the leader specially responsible only for initial creation and final destruction of the tuplestore; rescan resets are now a symmetrically shared responsibility. This means that we might reset the tuplestore multiple times when restarting a plan subtree containing multiple CTEscans; but resetting an already-empty tuplestore is cheap enough that that doesn't seem like a problem. Per report from Adam Mackler; the new regression test cases are based on his example query. Back-patch to 8.4 where CTE scans were introduced.
1 parent 3b849db commit d17cf76

File tree

3 files changed

+151
-17
lines changed

3 files changed

+151
-17
lines changed

src/backend/executor/nodeCtescan.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ ExecInitCteScan(CteScan *node, EState *estate, int eflags)
205205
* The Param slot associated with the CTE query is used to hold a pointer
206206
* to the CteState of the first CteScan node that initializes for this
207207
* CTE. This node will be the one that holds the shared state for all the
208-
* CTEs.
208+
* CTEs, particularly the shared tuplestore.
209209
*/
210210
prmdata = &(estate->es_param_exec_vals[node->cteParam]);
211211
Assert(prmdata->execPlan == NULL);
@@ -294,7 +294,10 @@ ExecEndCteScan(CteScanState *node)
294294
* If I am the leader, free the tuplestore.
295295
*/
296296
if (node->leader == node)
297+
{
297298
tuplestore_end(node->cte_table);
299+
node->cte_table = NULL;
300+
}
298301
}
299302

300303
/* ----------------------------------------------------------------
@@ -312,26 +315,26 @@ ExecCteScanReScan(CteScanState *node, ExprContext *exprCtxt)
312315

313316
ExecScanReScan(&node->ss);
314317

315-
if (node->leader == node)
318+
/*
319+
* Clear the tuplestore if a new scan of the underlying CTE is required.
320+
* This implicitly resets all the tuplestore's read pointers. Note that
321+
* multiple CTE nodes might redundantly clear the tuplestore; that's OK,
322+
* and not unduly expensive. We'll stop taking this path as soon as
323+
* somebody has attempted to read something from the underlying CTE
324+
* (thereby causing its chgParam to be cleared).
325+
*/
326+
if (node->leader->cteplanstate->chgParam != NULL)
316327
{
317-
/*
318-
* The leader is responsible for clearing the tuplestore if a new scan
319-
* of the underlying CTE is required.
320-
*/
321-
if (node->cteplanstate->chgParam != NULL)
322-
{
323-
tuplestore_clear(tuplestorestate);
324-
node->eof_cte = false;
325-
}
326-
else
327-
{
328-
tuplestore_select_read_pointer(tuplestorestate, node->readptr);
329-
tuplestore_rescan(tuplestorestate);
330-
}
328+
tuplestore_clear(tuplestorestate);
329+
node->leader->eof_cte = false;
331330
}
332331
else
333332
{
334-
/* Not leader, so just rewind my own pointer */
333+
/*
334+
* Else, just rewind my own pointer. Either the underlying CTE
335+
* doesn't need a rescan (and we can re-read what's in the tuplestore
336+
* now), or somebody else already took care of it.
337+
*/
335338
tuplestore_select_read_pointer(tuplestorestate, node->readptr);
336339
tuplestore_rescan(tuplestorestate);
337340
}

src/test/regress/expected/with.out

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,3 +1077,78 @@ SELECT * FROM outermost;
10771077
ERROR: recursive reference to query "outermost" must not appear within a subquery
10781078
LINE 2: WITH innermost as (SELECT 2 FROM outermost)
10791079
^
1080+
--
1081+
-- Test CTEs read in non-initialization orders
1082+
--
1083+
WITH RECURSIVE
1084+
tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
1085+
iter (id_key, row_type, link) AS (
1086+
SELECT 0, 'base', 17
1087+
UNION ALL (
1088+
WITH remaining(id_key, row_type, link, min) AS (
1089+
SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
1090+
FROM tab INNER JOIN iter USING (link)
1091+
WHERE tab.id_key > iter.id_key
1092+
),
1093+
first_remaining AS (
1094+
SELECT id_key, row_type, link
1095+
FROM remaining
1096+
WHERE id_key=min
1097+
),
1098+
effect AS (
1099+
SELECT tab.id_key, 'new'::text, tab.link
1100+
FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
1101+
WHERE e.row_type = 'false'
1102+
)
1103+
SELECT * FROM first_remaining
1104+
UNION ALL SELECT * FROM effect
1105+
)
1106+
)
1107+
SELECT * FROM iter;
1108+
id_key | row_type | link
1109+
--------+----------+------
1110+
0 | base | 17
1111+
1 | true | 17
1112+
2 | true | 17
1113+
3 | true | 17
1114+
4 | true | 17
1115+
5 | true | 17
1116+
6 | true | 17
1117+
(7 rows)
1118+
1119+
WITH RECURSIVE
1120+
tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
1121+
iter (id_key, row_type, link) AS (
1122+
SELECT 0, 'base', 17
1123+
UNION (
1124+
WITH remaining(id_key, row_type, link, min) AS (
1125+
SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
1126+
FROM tab INNER JOIN iter USING (link)
1127+
WHERE tab.id_key > iter.id_key
1128+
),
1129+
first_remaining AS (
1130+
SELECT id_key, row_type, link
1131+
FROM remaining
1132+
WHERE id_key=min
1133+
),
1134+
effect AS (
1135+
SELECT tab.id_key, 'new'::text, tab.link
1136+
FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
1137+
WHERE e.row_type = 'false'
1138+
)
1139+
SELECT * FROM first_remaining
1140+
UNION ALL SELECT * FROM effect
1141+
)
1142+
)
1143+
SELECT * FROM iter;
1144+
id_key | row_type | link
1145+
--------+----------+------
1146+
0 | base | 17
1147+
1 | true | 17
1148+
2 | true | 17
1149+
3 | true | 17
1150+
4 | true | 17
1151+
5 | true | 17
1152+
6 | true | 17
1153+
(7 rows)
1154+

src/test/regress/sql/with.sql

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,59 @@ WITH RECURSIVE outermost(x) AS (
535535
UNION SELECT * from outermost
536536
)
537537
SELECT * FROM outermost;
538+
539+
--
540+
-- Test CTEs read in non-initialization orders
541+
--
542+
543+
WITH RECURSIVE
544+
tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
545+
iter (id_key, row_type, link) AS (
546+
SELECT 0, 'base', 17
547+
UNION ALL (
548+
WITH remaining(id_key, row_type, link, min) AS (
549+
SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
550+
FROM tab INNER JOIN iter USING (link)
551+
WHERE tab.id_key > iter.id_key
552+
),
553+
first_remaining AS (
554+
SELECT id_key, row_type, link
555+
FROM remaining
556+
WHERE id_key=min
557+
),
558+
effect AS (
559+
SELECT tab.id_key, 'new'::text, tab.link
560+
FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
561+
WHERE e.row_type = 'false'
562+
)
563+
SELECT * FROM first_remaining
564+
UNION ALL SELECT * FROM effect
565+
)
566+
)
567+
SELECT * FROM iter;
568+
569+
WITH RECURSIVE
570+
tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
571+
iter (id_key, row_type, link) AS (
572+
SELECT 0, 'base', 17
573+
UNION (
574+
WITH remaining(id_key, row_type, link, min) AS (
575+
SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
576+
FROM tab INNER JOIN iter USING (link)
577+
WHERE tab.id_key > iter.id_key
578+
),
579+
first_remaining AS (
580+
SELECT id_key, row_type, link
581+
FROM remaining
582+
WHERE id_key=min
583+
),
584+
effect AS (
585+
SELECT tab.id_key, 'new'::text, tab.link
586+
FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
587+
WHERE e.row_type = 'false'
588+
)
589+
SELECT * FROM first_remaining
590+
UNION ALL SELECT * FROM effect
591+
)
592+
)
593+
SELECT * FROM iter;

0 commit comments

Comments
 (0)