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

Commit1f542a2

Browse files
committed
Prevent planagg.c from failing on queries containing CTEs.
The existing tests in preprocess_minmax_aggregates() usually prevent itfrom trying to do anything with queries containing CTEs, but there's anexception: a CTE could be present as a member of an appendrel, if weflattened a UNION ALL that contains CTE references. If it did try togenerate an optimized path for a query using a CTE, it failed with"could not find plan for CTE", as reported by Torsten Förtsch.The proximate cause is an unwise decision in commit3fc6e2d to clearsubroot->cte_plan_ids in build_minmax_path(). That left the subroot'scte_plan_ids list out of step with its parse->cteList.Removing the "subroot->cte_plan_ids = NIL;" assignment is enough to letthe case work again, but really it's pretty silly to be expending anycycles at all in this module when there are CTEs: we always treat theiroutputs as unordered so there's no way for the optimization to win.Hence, also add an early-exit test so we don't waste time like that.Back-patch to 9.6 where the misbehavior was introduced.Report:https://postgr.es/m/CAKkG4_=gjY5QiHtqSZyWMwDuTd_CftKoTaCqxjJ7uUz1-Gw=qw@mail.gmail.com
1 parent501c7b9 commit1f542a2

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

‎src/backend/optimizer/plan/planagg.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ preprocess_minmax_aggregates(PlannerInfo *root, List *tlist)
103103
parse->hasWindowFuncs)
104104
return;
105105

106+
/*
107+
* Reject if query contains any CTEs; there's no way to build an indexscan
108+
* on one so we couldn't succeed here. (If the CTEs are unreferenced,
109+
* that's not true, but it doesn't seem worth expending cycles to check.)
110+
*/
111+
if (parse->cteList)
112+
return;
113+
106114
/*
107115
* We also restrict the query to reference exactly one table, since join
108116
* conditions can't be handled reasonably. (We could perhaps handle a
@@ -360,7 +368,6 @@ build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo,
360368
subroot->plan_params=NIL;
361369
subroot->outer_params=NULL;
362370
subroot->init_plans=NIL;
363-
subroot->cte_plan_ids=NIL;
364371

365372
subroot->parse=parse= (Query*)copyObject(root->parse);
366373
IncrementVarSublevelsUp((Node*)parse,1,1);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp