Don't generate fake "*SELECT*" or "*SELECT* %d" subquery aliases.

rte->alias should point only to a user-written alias, but in these
cases that principle was violated. Fixing this causes some regression
test output changes: wherever rte->alias previously had a value and
is now NULL, rte->eref is now set to a generated name rather than to
rte->alias; and the scheme used to generate eref names differs from
what we were doing for aliases.

The upshot is that instead of "*SELECT*" or "*SELECT* %d",
EXPLAIN will now emit "unnamed_subquery" or "unnamed_subquery_%d".
But that's a reasonable descriptor, and we were already producing
that in yet other cases, so this seems not too objectionable.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Co-authored-by: Robert Haas <rhaas@postgresql.org>
Discussion: https://postgr.es/m/CA+TgmoYSYmDA2GvanzPMci084n+mVucv0bJ0HPbs6uhmMN6HMg@mail.gmail.com
master
Robert Haas 3 days ago
parent 3399c26554
commit 585e31fcb6
  1. 8
      contrib/postgres_fdw/expected/postgres_fdw.out
  2. 2
      src/backend/executor/functions.c
  3. 7
      src/backend/parser/analyze.c
  4. 4
      src/test/regress/expected/partition_prune.out
  5. 8
      src/test/regress/expected/rangefuncs.out
  6. 14
      src/test/regress/expected/union.out

@ -5086,13 +5086,13 @@ SELECT ft1.c1 FROM ft1 JOIN ft2 on ft1.c1 = ft2.c1 WHERE
-- =================================================================== -- ===================================================================
EXPLAIN (verbose, costs off) EXPLAIN (verbose, costs off)
INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20; INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
QUERY PLAN QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Insert on public.ft2 Insert on public.ft2
Remote SQL: INSERT INTO "S 1"."T 1"("C 1", c2, c3, c4, c5, c6, c7, c8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) Remote SQL: INSERT INTO "S 1"."T 1"("C 1", c2, c3, c4, c5, c6, c7, c8) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
Batch Size: 1 Batch Size: 1
-> Subquery Scan on "*SELECT*" -> Subquery Scan on unnamed_subquery
Output: "*SELECT*"."?column?", "*SELECT*"."?column?_1", NULL::integer, "*SELECT*"."?column?_2", NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying(10), 'ft2 '::character(10), NULL::user_enum Output: unnamed_subquery."?column?", unnamed_subquery."?column?_1", NULL::integer, unnamed_subquery."?column?_2", NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying(10), 'ft2 '::character(10), NULL::user_enum
-> Foreign Scan on public.ft2 ft2_1 -> Foreign Scan on public.ft2 ft2_1
Output: (ft2_1.c1 + 1000), (ft2_1.c2 + 100), (ft2_1.c3 || ft2_1.c3) Output: (ft2_1.c1 + 1000), (ft2_1.c2 + 100), (ft2_1.c3 || ft2_1.c3)
Remote SQL: SELECT "C 1", c2, c3 FROM "S 1"."T 1" LIMIT 20::bigint Remote SQL: SELECT "C 1", c2, c3 FROM "S 1"."T 1" LIMIT 20::bigint

@ -2483,7 +2483,7 @@ tlist_coercion_finished:
rte = makeNode(RangeTblEntry); rte = makeNode(RangeTblEntry);
rte->rtekind = RTE_SUBQUERY; rte->rtekind = RTE_SUBQUERY;
rte->subquery = parse; rte->subquery = parse;
rte->eref = rte->alias = makeAlias("*SELECT*", colnames); rte->eref = makeAlias("unnamed_subquery", colnames);
rte->lateral = false; rte->lateral = false;
rte->inh = false; rte->inh = false;
rte->inFromCl = true; rte->inFromCl = true;

@ -777,7 +777,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
*/ */
nsitem = addRangeTableEntryForSubquery(pstate, nsitem = addRangeTableEntryForSubquery(pstate,
selectQuery, selectQuery,
makeAlias("*SELECT*", NIL), NULL,
false, false,
false); false);
addNSItemToQuery(pstate, nsitem, true, false, false); addNSItemToQuery(pstate, nsitem, true, false, false);
@ -2100,7 +2100,6 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
{ {
/* Process leaf SELECT */ /* Process leaf SELECT */
Query *selectQuery; Query *selectQuery;
char selectName[32];
ParseNamespaceItem *nsitem; ParseNamespaceItem *nsitem;
RangeTblRef *rtr; RangeTblRef *rtr;
ListCell *tl; ListCell *tl;
@ -2156,11 +2155,9 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
/* /*
* Make the leaf query be a subquery in the top-level rangetable. * Make the leaf query be a subquery in the top-level rangetable.
*/ */
snprintf(selectName, sizeof(selectName), "*SELECT* %d",
list_length(pstate->p_rtable) + 1);
nsitem = addRangeTableEntryForSubquery(pstate, nsitem = addRangeTableEntryForSubquery(pstate,
selectQuery, selectQuery,
makeAlias(selectName, NIL), NULL,
false, false,
false); false);

@ -4763,7 +4763,7 @@ select min(a) over (partition by a order by a) from part_abc where a >= stable_o
QUERY PLAN QUERY PLAN
---------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------
Append Append
-> Subquery Scan on "*SELECT* 1_1" -> Subquery Scan on unnamed_subquery_2
-> WindowAgg -> WindowAgg
Window: w1 AS (PARTITION BY part_abc.a ORDER BY part_abc.a) Window: w1 AS (PARTITION BY part_abc.a ORDER BY part_abc.a)
-> Append -> Append
@ -4780,7 +4780,7 @@ select min(a) over (partition by a order by a) from part_abc where a >= stable_o
-> Index Scan using part_abc_3_2_a_idx on part_abc_3_2 part_abc_4 -> Index Scan using part_abc_3_2_a_idx on part_abc_3_2 part_abc_4
Index Cond: (a >= (stable_one() + 1)) Index Cond: (a >= (stable_one() + 1))
Filter: (d <= stable_one()) Filter: (d <= stable_one())
-> Subquery Scan on "*SELECT* 2" -> Subquery Scan on unnamed_subquery_1
-> WindowAgg -> WindowAgg
Window: w1 AS (PARTITION BY part_abc_5.a ORDER BY part_abc_5.a) Window: w1 AS (PARTITION BY part_abc_5.a ORDER BY part_abc_5.a)
-> Append -> Append

@ -2130,10 +2130,10 @@ select testrngfunc();
explain (verbose, costs off) explain (verbose, costs off)
select * from testrngfunc(); select * from testrngfunc();
QUERY PLAN QUERY PLAN
---------------------------------------------------------- ----------------------------------------------------------------------
Subquery Scan on "*SELECT*" Subquery Scan on unnamed_subquery
Output: "*SELECT*"."?column?", "*SELECT*"."?column?_1" Output: unnamed_subquery."?column?", unnamed_subquery."?column?_1"
-> Unique -> Unique
Output: (1), (2) Output: (1), (2)
-> Sort -> Sort

@ -942,7 +942,7 @@ SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1;
ERROR: column "q2" does not exist ERROR: column "q2" does not exist
LINE 1: ... int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1... LINE 1: ... int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1...
^ ^
DETAIL: There is a column named "q2" in table "*SELECT* 2", but it cannot be referenced from this part of the query. DETAIL: There is a column named "q2" in table "unnamed_subquery", but it cannot be referenced from this part of the query.
-- But this should work: -- But this should work:
SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1; SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1;
q1 q1
@ -1338,14 +1338,14 @@ where q2 = q2;
---------------------------------------------------------- ----------------------------------------------------------
Unique Unique
-> Merge Append -> Merge Append
Sort Key: "*SELECT* 1".q1 Sort Key: unnamed_subquery.q1
-> Subquery Scan on "*SELECT* 1" -> Subquery Scan on unnamed_subquery
-> Unique -> Unique
-> Sort -> Sort
Sort Key: i81.q1, i81.q2 Sort Key: i81.q1, i81.q2
-> Seq Scan on int8_tbl i81 -> Seq Scan on int8_tbl i81
Filter: (q2 IS NOT NULL) Filter: (q2 IS NOT NULL)
-> Subquery Scan on "*SELECT* 2" -> Subquery Scan on unnamed_subquery_1
-> Unique -> Unique
-> Sort -> Sort
Sort Key: i82.q1, i82.q2 Sort Key: i82.q1, i82.q2
@ -1374,14 +1374,14 @@ where -q1 = q2;
-------------------------------------------------------- --------------------------------------------------------
Unique Unique
-> Merge Append -> Merge Append
Sort Key: "*SELECT* 1".q1 Sort Key: unnamed_subquery.q1
-> Subquery Scan on "*SELECT* 1" -> Subquery Scan on unnamed_subquery
-> Unique -> Unique
-> Sort -> Sort
Sort Key: i81.q1, i81.q2 Sort Key: i81.q1, i81.q2
-> Seq Scan on int8_tbl i81 -> Seq Scan on int8_tbl i81
Filter: ((- q1) = q2) Filter: ((- q1) = q2)
-> Subquery Scan on "*SELECT* 2" -> Subquery Scan on unnamed_subquery_1
-> Unique -> Unique
-> Sort -> Sort
Sort Key: i82.q1, i82.q2 Sort Key: i82.q1, i82.q2

Loading…
Cancel
Save