Skip to content

Commit 904f6a5

Browse files
author
Richard Guo
committed
Centralize collection of catalog info needed early in the planner
There are several pieces of catalog information that need to be retrieved for a relation during the early stage of planning. These include relhassubclass, which is used to clear the inh flag if the relation has no children, as well as a column's attgenerated and default value, which are needed to expand virtual generated columns. More such information may be required in the future. Currently, these pieces of catalog data are collected in multiple places, resulting in repeated table_open/table_close calls for each relation in the rangetable. This patch centralizes the collection of all required early-stage catalog information into a single loop over the rangetable, allowing each relation to be opened and closed only once. Author: Richard Guo <guofenglinux@gmail.com> Reviewed-by: Robert Haas <robertmhaas@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAMbWs4-bFJ1At4btk5wqbezdu8PLtQ3zv-aiaY3ry9Ymm=jgFQ@mail.gmail.com
1 parent e0d0529 commit 904f6a5

File tree

4 files changed

+190
-151
lines changed

4 files changed

+190
-151
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -721,13 +721,15 @@ subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root,
721721
transform_MERGE_to_join(parse);
722722

723723
/*
724-
* Scan the rangetable for relations with virtual generated columns, and
725-
* replace all Var nodes in the query that reference these columns with
726-
* the generation expressions. Note that this step does not descend into
727-
* sublinks and subqueries; if we pull up any sublinks or subqueries
728-
* below, their rangetables are processed just before pulling them up.
724+
* Scan the rangetable for relation RTEs and retrieve the necessary
725+
* catalog information for each relation. Using this information, clear
726+
* the inh flag for any relation that has no children, and expand virtual
727+
* generated columns for any relation that contains them. Note that this
728+
* step does not descend into sublinks and subqueries; if we pull up any
729+
* sublinks or subqueries below, their relation RTEs are processed just
730+
* before pulling them up.
729731
*/
730-
parse = root->parse = expand_virtual_generated_columns(root);
732+
parse = root->parse = preprocess_relation_rtes(root);
731733

732734
/*
733735
* If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
@@ -788,23 +790,6 @@ subquery_planner(PlannerGlobal *glob, Query *parse, PlannerInfo *parent_root,
788790

789791
switch (rte->rtekind)
790792
{
791-
case RTE_RELATION:
792-
if (rte->inh)
793-
{
794-
/*
795-
* Check to see if the relation actually has any children;
796-
* if not, clear the inh flag so we can treat it as a
797-
* plain base relation.
798-
*
799-
* Note: this could give a false-positive result, if the
800-
* rel once had children but no longer does. We used to
801-
* be able to clear rte->inh later on when we discovered
802-
* that, but no more; we have to handle such cases as
803-
* full-fledged inheritance.
804-
*/
805-
rte->inh = has_subclass(rte->relid);
806-
}
807-
break;
808793
case RTE_JOIN:
809794
root->hasJoinRTEs = true;
810795
if (IS_OUTER_JOIN(rte->jointype))

src/backend/optimizer/plan/subselect.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,9 +1517,10 @@ convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
15171517
return NULL;
15181518

15191519
/*
1520-
* Scan the rangetable for relations with virtual generated columns, and
1521-
* replace all Var nodes in the subquery that reference these columns with
1522-
* the generation expressions.
1520+
* Scan the rangetable for relation RTEs and retrieve the necessary
1521+
* catalog information for each relation. Using this information, clear
1522+
* the inh flag for any relation that has no children, and expand virtual
1523+
* generated columns for any relation that contains them.
15231524
*
15241525
* Note: we construct up an entirely dummy PlannerInfo for use here. This
15251526
* is fine because only the "glob" and "parse" links will be used in this
@@ -1534,7 +1535,7 @@ convert_EXISTS_sublink_to_join(PlannerInfo *root, SubLink *sublink,
15341535
subroot.glob = root->glob;
15351536
subroot.parse = subselect;
15361537
subselect->jointree->quals = whereClause;
1537-
subselect = expand_virtual_generated_columns(&subroot);
1538+
subselect = preprocess_relation_rtes(&subroot);
15381539

15391540
/*
15401541
* Now separate out the WHERE clause again.

0 commit comments

Comments
 (0)