diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2016-12-13 13:20:16 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2016-12-13 13:20:37 -0500 |
commit | 1f542a2eacca030c676cbb594f3b362d43f2f857 (patch) | |
tree | 57830258483cd4d8c849448aae4dab577621236a /src | |
parent | 501c7b94bcb00cfa0faad60135cf6af82fd56a3a (diff) | |
download | postgresql-1f542a2eacca030c676cbb594f3b362d43f2f857.tar.gz postgresql-1f542a2eacca030c676cbb594f3b362d43f2f857.zip |
Prevent planagg.c from failing on queries containing CTEs.
The existing tests in preprocess_minmax_aggregates() usually prevent it
from trying to do anything with queries containing CTEs, but there's an
exception: a CTE could be present as a member of an appendrel, if we
flattened a UNION ALL that contains CTE references. If it did try to
generate 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 commit 3fc6e2d7f to clear
subroot->cte_plan_ids in build_minmax_path(). That left the subroot's
cte_plan_ids list out of step with its parse->cteList.
Removing the "subroot->cte_plan_ids = NIL;" assignment is enough to let
the case work again, but really it's pretty silly to be expending any
cycles at all in this module when there are CTEs: we always treat their
outputs 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
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/optimizer/plan/planagg.c | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/backend/optimizer/plan/planagg.c b/src/backend/optimizer/plan/planagg.c index 805aae7ee7a..4216fc4a6ea 100644 --- a/src/backend/optimizer/plan/planagg.c +++ b/src/backend/optimizer/plan/planagg.c @@ -104,6 +104,14 @@ preprocess_minmax_aggregates(PlannerInfo *root, List *tlist) return; /* + * Reject if query contains any CTEs; there's no way to build an indexscan + * on one so we couldn't succeed here. (If the CTEs are unreferenced, + * that's not true, but it doesn't seem worth expending cycles to check.) + */ + if (parse->cteList) + return; + + /* * We also restrict the query to reference exactly one table, since join * conditions can't be handled reasonably. (We could perhaps handle a * query containing cartesian-product joins, but it hardly seems worth the @@ -360,7 +368,6 @@ build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo, subroot->plan_params = NIL; subroot->outer_params = NULL; subroot->init_plans = NIL; - subroot->cte_plan_ids = NIL; subroot->parse = parse = (Query *) copyObject(root->parse); IncrementVarSublevelsUp((Node *) parse, 1, 1); |