Skip to content

Commit 585e31f

Browse files
committed
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
1 parent 3399c26 commit 585e31f

File tree

6 files changed

+20
-23
lines changed

6 files changed

+20
-23
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5086,13 +5086,13 @@ SELECT ft1.c1 FROM ft1 JOIN ft2 on ft1.c1 = ft2.c1 WHERE
50865086
-- ===================================================================
50875087
EXPLAIN (verbose, costs off)
50885088
INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
5089-
QUERY PLAN
5090-
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5089+
QUERY PLAN
5090+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
50915091
Insert on public.ft2
50925092
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)
50935093
Batch Size: 1
5094-
-> Subquery Scan on "*SELECT*"
5095-
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
5094+
-> Subquery Scan on unnamed_subquery
5095+
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
50965096
-> Foreign Scan on public.ft2 ft2_1
50975097
Output: (ft2_1.c1 + 1000), (ft2_1.c2 + 100), (ft2_1.c3 || ft2_1.c3)
50985098
Remote SQL: SELECT "C 1", c2, c3 FROM "S 1"."T 1" LIMIT 20::bigint

src/backend/executor/functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2483,7 +2483,7 @@ check_sql_stmt_retval(List *queryTreeList,
24832483
rte = makeNode(RangeTblEntry);
24842484
rte->rtekind = RTE_SUBQUERY;
24852485
rte->subquery = parse;
2486-
rte->eref = rte->alias = makeAlias("*SELECT*", colnames);
2486+
rte->eref = makeAlias("unnamed_subquery", colnames);
24872487
rte->lateral = false;
24882488
rte->inh = false;
24892489
rte->inFromCl = true;

src/backend/parser/analyze.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
777777
*/
778778
nsitem = addRangeTableEntryForSubquery(pstate,
779779
selectQuery,
780-
makeAlias("*SELECT*", NIL),
780+
NULL,
781781
false,
782782
false);
783783
addNSItemToQuery(pstate, nsitem, true, false, false);
@@ -2100,7 +2100,6 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
21002100
{
21012101
/* Process leaf SELECT */
21022102
Query *selectQuery;
2103-
char selectName[32];
21042103
ParseNamespaceItem *nsitem;
21052104
RangeTblRef *rtr;
21062105
ListCell *tl;
@@ -2156,11 +2155,9 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
21562155
/*
21572156
* Make the leaf query be a subquery in the top-level rangetable.
21582157
*/
2159-
snprintf(selectName, sizeof(selectName), "*SELECT* %d",
2160-
list_length(pstate->p_rtable) + 1);
21612158
nsitem = addRangeTableEntryForSubquery(pstate,
21622159
selectQuery,
2163-
makeAlias(selectName, NIL),
2160+
NULL,
21642161
false,
21652162
false);
21662163

src/test/regress/expected/partition_prune.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4763,7 +4763,7 @@ select min(a) over (partition by a order by a) from part_abc where a >= stable_o
47634763
QUERY PLAN
47644764
----------------------------------------------------------------------------------------------
47654765
Append
4766-
-> Subquery Scan on "*SELECT* 1_1"
4766+
-> Subquery Scan on unnamed_subquery_2
47674767
-> WindowAgg
47684768
Window: w1 AS (PARTITION BY part_abc.a ORDER BY part_abc.a)
47694769
-> Append
@@ -4780,7 +4780,7 @@ select min(a) over (partition by a order by a) from part_abc where a >= stable_o
47804780
-> Index Scan using part_abc_3_2_a_idx on part_abc_3_2 part_abc_4
47814781
Index Cond: (a >= (stable_one() + 1))
47824782
Filter: (d <= stable_one())
4783-
-> Subquery Scan on "*SELECT* 2"
4783+
-> Subquery Scan on unnamed_subquery_1
47844784
-> WindowAgg
47854785
Window: w1 AS (PARTITION BY part_abc_5.a ORDER BY part_abc_5.a)
47864786
-> Append

src/test/regress/expected/rangefuncs.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,10 +2130,10 @@ select testrngfunc();
21302130

21312131
explain (verbose, costs off)
21322132
select * from testrngfunc();
2133-
QUERY PLAN
2134-
----------------------------------------------------------
2135-
Subquery Scan on "*SELECT*"
2136-
Output: "*SELECT*"."?column?", "*SELECT*"."?column?_1"
2133+
QUERY PLAN
2134+
----------------------------------------------------------------------
2135+
Subquery Scan on unnamed_subquery
2136+
Output: unnamed_subquery."?column?", unnamed_subquery."?column?_1"
21372137
-> Unique
21382138
Output: (1), (2)
21392139
-> Sort

src/test/regress/expected/union.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1;
942942
ERROR: column "q2" does not exist
943943
LINE 1: ... int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1...
944944
^
945-
DETAIL: There is a column named "q2" in table "*SELECT* 2", but it cannot be referenced from this part of the query.
945+
DETAIL: There is a column named "q2" in table "unnamed_subquery", but it cannot be referenced from this part of the query.
946946
-- But this should work:
947947
SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1;
948948
q1
@@ -1338,14 +1338,14 @@ where q2 = q2;
13381338
----------------------------------------------------------
13391339
Unique
13401340
-> Merge Append
1341-
Sort Key: "*SELECT* 1".q1
1342-
-> Subquery Scan on "*SELECT* 1"
1341+
Sort Key: unnamed_subquery.q1
1342+
-> Subquery Scan on unnamed_subquery
13431343
-> Unique
13441344
-> Sort
13451345
Sort Key: i81.q1, i81.q2
13461346
-> Seq Scan on int8_tbl i81
13471347
Filter: (q2 IS NOT NULL)
1348-
-> Subquery Scan on "*SELECT* 2"
1348+
-> Subquery Scan on unnamed_subquery_1
13491349
-> Unique
13501350
-> Sort
13511351
Sort Key: i82.q1, i82.q2
@@ -1374,14 +1374,14 @@ where -q1 = q2;
13741374
--------------------------------------------------------
13751375
Unique
13761376
-> Merge Append
1377-
Sort Key: "*SELECT* 1".q1
1378-
-> Subquery Scan on "*SELECT* 1"
1377+
Sort Key: unnamed_subquery.q1
1378+
-> Subquery Scan on unnamed_subquery
13791379
-> Unique
13801380
-> Sort
13811381
Sort Key: i81.q1, i81.q2
13821382
-> Seq Scan on int8_tbl i81
13831383
Filter: ((- q1) = q2)
1384-
-> Subquery Scan on "*SELECT* 2"
1384+
-> Subquery Scan on unnamed_subquery_1
13851385
-> Unique
13861386
-> Sort
13871387
Sort Key: i82.q1, i82.q2

0 commit comments

Comments
 (0)