postgresql/src/include/optimizer
Tom Lane 5ac340602e Account for optimized MinMax aggregates during SS_finalize_plan.
We are capable of optimizing MIN() and MAX() aggregates on indexed
columns into subqueries that exploit the index, rather than the normal
thing of scanning the whole table.  When we do this, we replace the
Aggref node(s) with Params referencing subquery outputs.  Such Params
really ought to be included in the per-plan-node extParam/allParam
sets computed by SS_finalize_plan.  However, we've never done so
up to now because of an ancient implementation choice to perform
that substitution during set_plan_references, which runs after
SS_finalize_plan, so that SS_finalize_plan never sees these Params.

The cleanest fix would be to perform a separate tree walk to do
these substitutions before SS_finalize_plan runs.  That seems
unattractive, first because a whole-tree mutation pass is expensive,
and second because we lack infrastructure for visiting expression
subtrees in a Plan tree, so that we'd need a new function knowing
as much as SS_finalize_plan knows about that.  I also considered
swapping the order of SS_finalize_plan and set_plan_references,
but that fell foul of various assumptions that seem tricky to fix.
So the approach adopted here is to teach SS_finalize_plan itself
to check for such Aggrefs.  I refactored things a bit in setrefs.c
to avoid having three copies of the code that does that.

Back-patch of v17 commits d0d44049d and 779ac2c74.  When d0d44049d
went in, there was no evidence that it was fixing a reachable bug,
so I refrained from back-patching.  Now we have such evidence.

Per bug #18465 from Hal Takahara.  Back-patch to all supported
branches.

Discussion: https://postgr.es/m/18465-2fae927718976b22@postgresql.org
Discussion: https://postgr.es/m/2391880.1689025003@sss.pgh.pa.us
2024-05-18 14:31:35 -04:00
..
appendinfo.h Rework planning and execution of UPDATE and DELETE. 2021-03-31 11:52:37 -04:00
clauses.h Flush Memoize cache when non-key parameters change, take 2 2021-11-24 23:29:56 +13:00
cost.h Change the name of the Result Cache node to Memoize 2021-07-14 12:45:00 +12:00
geqo.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
geqo_copy.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
geqo_gene.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
geqo_misc.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
geqo_mutation.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
geqo_pool.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
geqo_random.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
geqo_recombination.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
geqo_selection.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
inherit.h Fix calculation of which GENERATED columns need to be updated. 2023-01-05 14:12:17 -05:00
joininfo.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
optimizer.h Ensure we preprocess expressions before checking their volatility. 2023-11-16 10:05:14 -05:00
orclauses.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
paramassign.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
pathnode.h Allow Memoize to operate in binary comparison mode 2021-11-24 10:07:38 +13:00
paths.h Rename find_em_expr_usable_for_sorting_rel. 2021-04-20 11:37:36 -04:00
placeholder.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
plancat.h Fix calculation of which GENERATED columns need to be updated. 2023-01-05 14:12:17 -05:00
planmain.h Account for optimized MinMax aggregates during SS_finalize_plan. 2024-05-18 14:31:35 -04:00
planner.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
prep.h Fix mishandling of resjunk columns in ON CONFLICT ... UPDATE tlists. 2021-05-10 11:02:29 -04:00
restrictinfo.h Disallow replacing joins with scans in problematic cases. 2023-07-28 15:45:04 +09:00
subselect.h Update copyright for 2021 2021-01-02 13:06:25 -05:00
tlist.h Fix planner failure in some cases of sorting by an aggregate. 2021-04-20 11:32:02 -04:00