Skip to content

Commit 7a80e38

Browse files
committed
Skip useless calculation of join RTE column names during EXPLAIN.
There's no need for set_simple_column_names() to compute unique column names for join RTEs, because a finished plan tree will not contain any join alias Vars that we could need names for. Its other, internal callers will not pass it any join RTEs anyway, so the upshot is we can just skip join RTEs here. Aside from getting rid of a klugy against-its-documentation use of set_relation_column_names, this can speed up EXPLAIN substantially when considering many-join queries, because the upper join RTEs tend to have a lot of columns. Sami Imseih, with cosmetic changes by me Discussion: https://postgr.es/m/CAA5RZ0th3q-0p1pri58z9grG8r8azmEBa8o1rtkwhLmJg_cH+g@mail.gmail.com
1 parent dc6acfd commit 7a80e38

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,9 +3773,8 @@ deparse_context_for_plan_tree(PlannedStmt *pstmt, List *rtable_names)
37733773
dpns->appendrels = NULL; /* don't need it */
37743774

37753775
/*
3776-
* Set up column name aliases. We will get rather bogus results for join
3777-
* RTEs, but that doesn't matter because plan trees don't contain any join
3778-
* alias Vars.
3776+
* Set up column name aliases, ignoring any join RTEs; they don't matter
3777+
* because plan trees don't contain any join alias Vars.
37793778
*/
37803779
set_simple_column_names(dpns);
37813780

@@ -4067,8 +4066,10 @@ set_deparse_for_query(deparse_namespace *dpns, Query *query,
40674066
*
40684067
* This handles EXPLAIN and cases where we only have relation RTEs. Without
40694068
* a join tree, we can't do anything smart about join RTEs, but we don't
4070-
* need to (note that EXPLAIN should never see join alias Vars anyway).
4071-
* If we do hit a join RTE we'll just process it like a non-table base RTE.
4069+
* need to, because EXPLAIN should never see join alias Vars anyway.
4070+
* If we find a join RTE we'll just skip it, leaving its deparse_columns
4071+
* struct all-zero. If somehow we try to deparse a join alias Var, we'll
4072+
* error out cleanly because the struct's num_cols will be zero.
40724073
*/
40734074
static void
40744075
set_simple_column_names(deparse_namespace *dpns)
@@ -4082,13 +4083,14 @@ set_simple_column_names(deparse_namespace *dpns)
40824083
dpns->rtable_columns = lappend(dpns->rtable_columns,
40834084
palloc0(sizeof(deparse_columns)));
40844085

4085-
/* Assign unique column aliases within each RTE */
4086+
/* Assign unique column aliases within each non-join RTE */
40864087
forboth(lc, dpns->rtable, lc2, dpns->rtable_columns)
40874088
{
40884089
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
40894090
deparse_columns *colinfo = (deparse_columns *) lfirst(lc2);
40904091

4091-
set_relation_column_names(dpns, rte, colinfo);
4092+
if (rte->rtekind != RTE_JOIN)
4093+
set_relation_column_names(dpns, rte, colinfo);
40924094
}
40934095
}
40944096

0 commit comments

Comments
 (0)