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

Commit 4c53169

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 083b913 commit 4c53169

File tree

3 files changed

+151
-17
lines changed

3 files changed

+151
-17
lines changed

src/backend/executor/nodeCtescan.c

+20-17
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 @@ ExecReScanCteScan(CteScanState *node)
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

+75
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,81 @@ SELECT * FROM outermost;
12091209
ERROR: recursive reference to query "outermost" must not appear within a subquery
12101210
LINE 2: WITH innermost as (SELECT 2 FROM outermost)
12111211
^
1212+
--
1213+
-- Test CTEs read in non-initialization orders
1214+
--
1215+
WITH RECURSIVE
1216+
tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
1217+
iter (id_key, row_type, link) AS (
1218+
SELECT 0, 'base', 17
1219+
UNION ALL (
1220+
WITH remaining(id_key, row_type, link, min) AS (
1221+
SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
1222+
FROM tab INNER JOIN iter USING (link)
1223+
WHERE tab.id_key > iter.id_key
1224+
),
1225+
first_remaining AS (
1226+
SELECT id_key, row_type, link
1227+
FROM remaining
1228+
WHERE id_key=min
1229+
),
1230+
effect AS (
1231+
SELECT tab.id_key, 'new'::text, tab.link
1232+
FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
1233+
WHERE e.row_type = 'false'
1234+
)
1235+
SELECT * FROM first_remaining
1236+
UNION ALL SELECT * FROM effect
1237+
)
1238+
)
1239+
SELECT * FROM iter;
1240+
id_key | row_type | link
1241+
--------+----------+------
1242+
0 | base | 17
1243+
1 | true | 17
1244+
2 | true | 17
1245+
3 | true | 17
1246+
4 | true | 17
1247+
5 | true | 17
1248+
6 | true | 17
1249+
(7 rows)
1250+
1251+
WITH RECURSIVE
1252+
tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
1253+
iter (id_key, row_type, link) AS (
1254+
SELECT 0, 'base', 17
1255+
UNION (
1256+
WITH remaining(id_key, row_type, link, min) AS (
1257+
SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
1258+
FROM tab INNER JOIN iter USING (link)
1259+
WHERE tab.id_key > iter.id_key
1260+
),
1261+
first_remaining AS (
1262+
SELECT id_key, row_type, link
1263+
FROM remaining
1264+
WHERE id_key=min
1265+
),
1266+
effect AS (
1267+
SELECT tab.id_key, 'new'::text, tab.link
1268+
FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
1269+
WHERE e.row_type = 'false'
1270+
)
1271+
SELECT * FROM first_remaining
1272+
UNION ALL SELECT * FROM effect
1273+
)
1274+
)
1275+
SELECT * FROM iter;
1276+
id_key | row_type | link
1277+
--------+----------+------
1278+
0 | base | 17
1279+
1 | true | 17
1280+
2 | true | 17
1281+
3 | true | 17
1282+
4 | true | 17
1283+
5 | true | 17
1284+
6 | true | 17
1285+
(7 rows)
1286+
12121287
--
12131288
-- Data-modifying statements in WITH
12141289
--

src/test/regress/sql/with.sql

+56
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,62 @@ WITH RECURSIVE outermost(x) AS (
574574
)
575575
SELECT * FROM outermost;
576576

577+
--
578+
-- Test CTEs read in non-initialization orders
579+
--
580+
581+
WITH RECURSIVE
582+
tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
583+
iter (id_key, row_type, link) AS (
584+
SELECT 0, 'base', 17
585+
UNION ALL (
586+
WITH remaining(id_key, row_type, link, min) AS (
587+
SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
588+
FROM tab INNER JOIN iter USING (link)
589+
WHERE tab.id_key > iter.id_key
590+
),
591+
first_remaining AS (
592+
SELECT id_key, row_type, link
593+
FROM remaining
594+
WHERE id_key=min
595+
),
596+
effect AS (
597+
SELECT tab.id_key, 'new'::text, tab.link
598+
FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
599+
WHERE e.row_type = 'false'
600+
)
601+
SELECT * FROM first_remaining
602+
UNION ALL SELECT * FROM effect
603+
)
604+
)
605+
SELECT * FROM iter;
606+
607+
WITH RECURSIVE
608+
tab(id_key,link) AS (VALUES (1,17), (2,17), (3,17), (4,17), (6,17), (5,17)),
609+
iter (id_key, row_type, link) AS (
610+
SELECT 0, 'base', 17
611+
UNION (
612+
WITH remaining(id_key, row_type, link, min) AS (
613+
SELECT tab.id_key, 'true'::text, iter.link, MIN(tab.id_key) OVER ()
614+
FROM tab INNER JOIN iter USING (link)
615+
WHERE tab.id_key > iter.id_key
616+
),
617+
first_remaining AS (
618+
SELECT id_key, row_type, link
619+
FROM remaining
620+
WHERE id_key=min
621+
),
622+
effect AS (
623+
SELECT tab.id_key, 'new'::text, tab.link
624+
FROM first_remaining e INNER JOIN tab ON e.id_key=tab.id_key
625+
WHERE e.row_type = 'false'
626+
)
627+
SELECT * FROM first_remaining
628+
UNION ALL SELECT * FROM effect
629+
)
630+
)
631+
SELECT * FROM iter;
632+
577633
--
578634
-- Data-modifying statements in WITH
579635
--

0 commit comments

Comments
 (0)