1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-15 05:46:52 +03:00
Files
postgres/src/include/optimizer/planner.h
Robert Haas 8c49a484e8 Assign each subquery a unique name prior to planning it.
Previously, subqueries were given names only after they were planned,
which makes it difficult to use information from a previous execution of
the query to guide future planning. If, for example, you knew something
about how you want "InitPlan 2" to be planned, you won't know whether
the subquery you're currently planning will end up being "InitPlan 2"
until after you've finished planning it, by which point it's too late to
use the information that you had.

To fix this, assign each subplan a unique name before we begin planning
it. To improve consistency, use textual names for all subplans, rather
than, as we did previously, a mix of numbers (such as "InitPlan 1") and
names (such as "CTE foo"), and make sure that the same name is never
assigned more than once.

We adopt the somewhat arbitrary convention of using the type of sublink
to set the plan name; for example, a query that previously had two
expression sublinks shown as InitPlan 2 and InitPlan 1 will now end up
named expr_1 and expr_2. Because names are assigned before rather than
after planning, some of the regression test outputs show the numerical
part of the name switching positions: what was previously SubPlan 2 was
actually the first one encountered, but we finished planning it later.

We assign names even to subqueries that aren't shown as such within the
EXPLAIN output. These include subqueries that are a FROM clause item or
a branch of a set operation, rather than something that will be turned
into an InitPlan or SubPlan. The purpose of this is to make sure that,
below the topmost query level, there's always a name for each subquery
that is stable from one planning cycle to the next (assuming no changes
to the query or the database schema).

Author: Robert Haas <rhaas@postgresql.org>
Co-authored-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Alexandra Wang <alexandra.wang.oss@gmail.com>
Reviewed-by: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Junwang Zhao <zhjwpku@gmail.com>
Discussion: http://postgr.es/m/3641043.1758751399@sss.pgh.pa.us
2025-10-07 09:18:54 -04:00

70 lines
2.3 KiB
C

/*-------------------------------------------------------------------------
*
* planner.h
* prototypes for planner.c.
*
* Note that the primary entry points for planner.c are declared in
* optimizer/optimizer.h, because they're intended to be called from
* non-planner code. Declarations here are meant for use by other
* planner modules.
*
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/include/optimizer/planner.h
*
*-------------------------------------------------------------------------
*/
#ifndef PLANNER_H
#define PLANNER_H
#include "nodes/pathnodes.h"
#include "nodes/plannodes.h"
/* Hook for plugins to get control in planner() */
typedef PlannedStmt *(*planner_hook_type) (Query *parse,
const char *query_string,
int cursorOptions,
ParamListInfo boundParams);
extern PGDLLIMPORT planner_hook_type planner_hook;
/* Hook for plugins to get control when grouping_planner() plans upper rels */
typedef void (*create_upper_paths_hook_type) (PlannerInfo *root,
UpperRelationKind stage,
RelOptInfo *input_rel,
RelOptInfo *output_rel,
void *extra);
extern PGDLLIMPORT create_upper_paths_hook_type create_upper_paths_hook;
extern PlannedStmt *standard_planner(Query *parse, const char *query_string,
int cursorOptions,
ParamListInfo boundParams);
extern PlannerInfo *subquery_planner(PlannerGlobal *glob, Query *parse,
char *plan_name,
PlannerInfo *parent_root,
bool hasRecursion, double tuple_fraction,
SetOperationStmt *setops);
extern RowMarkType select_rowmark_type(RangeTblEntry *rte,
LockClauseStrength strength);
extern bool limit_needed(Query *parse);
extern void mark_partial_aggref(Aggref *agg, AggSplit aggsplit);
extern Path *get_cheapest_fractional_path(RelOptInfo *rel,
double tuple_fraction);
extern Expr *preprocess_phv_expression(PlannerInfo *root, Expr *expr);
extern RelOptInfo *create_unique_paths(PlannerInfo *root, RelOptInfo *rel,
SpecialJoinInfo *sjinfo);
extern char *choose_plan_name(PlannerGlobal *glob, const char *name,
bool always_number);
#endif /* PLANNER_H */