mirror of
https://github.com/postgres/postgres.git
synced 2025-04-25 21:42:33 +03:00
Previously, pull_varnos() took the relids of a PlaceHolderVar as being equal to the relids in its contents, but that fails to account for the possibility that we have to postpone evaluation of the PHV due to outer joins. This could result in a malformed plan. The known cases end up triggering the "failed to assign all NestLoopParams to plan nodes" sanity check in createplan.c, but other symptoms may be possible. The right value to use is the join level we actually intend to evaluate the PHV at. We can get that from the ph_eval_at field of the associated PlaceHolderInfo. However, there are some places that call pull_varnos() before the PlaceHolderInfos have been created; in that case, fall back to the conservative assumption that the PHV will be evaluated at its syntactic level. (In principle this might result in missing some legal optimization, but I'm not aware of any cases where it's an issue in practice.) Things are also a bit ticklish for calls occurring during deconstruct_jointree(), but AFAICS the ph_eval_at fields should have reached their final values by the time we need them. The main problem in making this work is that pull_varnos() has no way to get at the PlaceHolderInfos. We can fix that easily, if a bit tediously, in HEAD by passing it the planner "root" pointer. In the back branches that'd cause an unacceptable API/ABI break for extensions, so leave the existing entry points alone and add new ones with the additional parameter. (If an old entry point is called and encounters a PHV, it'll fall back to using the syntactic level, again possibly missing some valid optimization.) Back-patch to v12. The computation is surely also wrong before that, but it appears that we cannot reach a bad plan thanks to join order restrictions imposed on the subquery that the PlaceHolderVar came from. The error only became reachable when commit 4be058fe9 allowed trivial subqueries to be collapsed out completely, eliminating their join order restrictions. Per report from Stephan Springl. Discussion: https://postgr.es/m/171041.1610849523@sss.pgh.pa.us
117 lines
4.1 KiB
C
117 lines
4.1 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* planmain.h
|
|
* prototypes for various files in optimizer/plan
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/optimizer/planmain.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef PLANMAIN_H
|
|
#define PLANMAIN_H
|
|
|
|
#include "nodes/pathnodes.h"
|
|
#include "nodes/plannodes.h"
|
|
|
|
/* GUC parameters */
|
|
#define DEFAULT_CURSOR_TUPLE_FRACTION 0.1
|
|
extern double cursor_tuple_fraction;
|
|
|
|
/* query_planner callback to compute query_pathkeys */
|
|
typedef void (*query_pathkeys_callback) (PlannerInfo *root, void *extra);
|
|
|
|
/*
|
|
* prototypes for plan/planmain.c
|
|
*/
|
|
extern RelOptInfo *query_planner(PlannerInfo *root,
|
|
query_pathkeys_callback qp_callback, void *qp_extra);
|
|
|
|
/*
|
|
* prototypes for plan/planagg.c
|
|
*/
|
|
extern void preprocess_minmax_aggregates(PlannerInfo *root);
|
|
|
|
/*
|
|
* prototypes for plan/createplan.c
|
|
*/
|
|
extern Plan *create_plan(PlannerInfo *root, Path *best_path);
|
|
extern ForeignScan *make_foreignscan(List *qptlist, List *qpqual,
|
|
Index scanrelid, List *fdw_exprs, List *fdw_private,
|
|
List *fdw_scan_tlist, List *fdw_recheck_quals,
|
|
Plan *outer_plan);
|
|
extern Plan *change_plan_targetlist(Plan *subplan, List *tlist,
|
|
bool tlist_parallel_safe);
|
|
extern Plan *materialize_finished_plan(Plan *subplan);
|
|
extern bool is_projection_capable_path(Path *path);
|
|
extern bool is_projection_capable_plan(Plan *plan);
|
|
|
|
/* External use of these functions is deprecated: */
|
|
extern Sort *make_sort_from_sortclauses(List *sortcls, Plan *lefttree);
|
|
extern Agg *make_agg(List *tlist, List *qual,
|
|
AggStrategy aggstrategy, AggSplit aggsplit,
|
|
int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators, Oid *grpCollations,
|
|
List *groupingSets, List *chain,
|
|
double dNumGroups, Plan *lefttree);
|
|
extern Limit *make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount);
|
|
|
|
/*
|
|
* prototypes for plan/initsplan.c
|
|
*/
|
|
extern int from_collapse_limit;
|
|
extern int join_collapse_limit;
|
|
|
|
extern void add_base_rels_to_query(PlannerInfo *root, Node *jtnode);
|
|
extern void add_other_rels_to_query(PlannerInfo *root);
|
|
extern void build_base_rel_tlists(PlannerInfo *root, List *final_tlist);
|
|
extern void add_vars_to_targetlist(PlannerInfo *root, List *vars,
|
|
Relids where_needed, bool create_new_ph);
|
|
extern void find_lateral_references(PlannerInfo *root);
|
|
extern void create_lateral_join_info(PlannerInfo *root);
|
|
extern List *deconstruct_jointree(PlannerInfo *root);
|
|
extern void distribute_restrictinfo_to_rels(PlannerInfo *root,
|
|
RestrictInfo *restrictinfo);
|
|
extern void process_implied_equality(PlannerInfo *root,
|
|
Oid opno,
|
|
Oid collation,
|
|
Expr *item1,
|
|
Expr *item2,
|
|
Relids qualscope,
|
|
Relids nullable_relids,
|
|
Index security_level,
|
|
bool below_outer_join,
|
|
bool both_const);
|
|
extern RestrictInfo *build_implied_join_equality(PlannerInfo *root,
|
|
Oid opno,
|
|
Oid collation,
|
|
Expr *item1,
|
|
Expr *item2,
|
|
Relids qualscope,
|
|
Relids nullable_relids,
|
|
Index security_level);
|
|
extern void match_foreign_keys_to_quals(PlannerInfo *root);
|
|
|
|
/*
|
|
* prototypes for plan/analyzejoins.c
|
|
*/
|
|
extern List *remove_useless_joins(PlannerInfo *root, List *joinlist);
|
|
extern void reduce_unique_semijoins(PlannerInfo *root);
|
|
extern bool query_supports_distinctness(Query *query);
|
|
extern bool query_is_distinct_for(Query *query, List *colnos, List *opids);
|
|
extern bool innerrel_is_unique(PlannerInfo *root,
|
|
Relids joinrelids, Relids outerrelids, RelOptInfo *innerrel,
|
|
JoinType jointype, List *restrictlist, bool force_cache);
|
|
|
|
/*
|
|
* prototypes for plan/setrefs.c
|
|
*/
|
|
extern Plan *set_plan_references(PlannerInfo *root, Plan *plan);
|
|
extern void record_plan_function_dependency(PlannerInfo *root, Oid funcid);
|
|
extern void record_plan_type_dependency(PlannerInfo *root, Oid typid);
|
|
extern bool extract_query_dependencies_walker(Node *node, PlannerInfo *root);
|
|
|
|
#endif /* PLANMAIN_H */
|