From 26b939f5e1859f2a7556e76bf53f96f1c61d558c Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Thu, 7 Nov 2024 10:58:04 -0500
Subject: [PATCH v1] POC: Don't make up fake eref names. Just use NULL.

---
 .../postgres_fdw/expected/postgres_fdw.out    |   8 +-
 src/backend/executor/functions.c              |   2 +-
 src/backend/nodes/makefuncs.c                 |   3 +-
 src/backend/optimizer/plan/subselect.c        |   2 +-
 src/backend/optimizer/prep/prepjointree.c     |   6 +-
 src/backend/parser/analyze.c                  |   7 +-
 src/backend/parser/parse_merge.c              |   4 +-
 src/backend/parser/parse_relation.c           |   9 +-
 src/backend/rewrite/rewriteSearchCycle.c      |   4 +-
 src/backend/utils/adt/ruleutils.c             |  16 ++-
 src/test/regress/expected/rangefuncs.out      |   8 +-
 src/test/regress/expected/subselect.out       |  18 +--
 src/test/regress/expected/union.out           | 118 +++++++++---------
 13 files changed, 108 insertions(+), 97 deletions(-)

diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index f2bcd6aa98c..9cc8d6d1a4f 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -4961,13 +4961,13 @@ SELECT ft1.c1 FROM ft1 JOIN ft2 on ft1.c1 = ft2.c1 WHERE
 -- ===================================================================
 EXPLAIN (verbose, costs off)
 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
    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
-   ->  Subquery Scan on "*SELECT*"
-         Output: "*SELECT*"."?column?", "*SELECT*"."?column?_1", NULL::integer, "*SELECT*"."?column?_2", NULL::timestamp with time zone, NULL::timestamp without time zone, NULL::character varying, 'ft2       '::character(10), NULL::user_enum
+   ->  Subquery Scan on unnamed_subquery
+         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, 'ft2       '::character(10), NULL::user_enum
          ->  Foreign Scan on public.ft2 ft2_1
                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
diff --git a/src/backend/executor/functions.c b/src/backend/executor/functions.c
index 692854e2b3e..3b08670c062 100644
--- a/src/backend/executor/functions.c
+++ b/src/backend/executor/functions.c
@@ -1962,7 +1962,7 @@ tlist_coercion_finished:
 		rte = makeNode(RangeTblEntry);
 		rte->rtekind = RTE_SUBQUERY;
 		rte->subquery = parse;
-		rte->eref = rte->alias = makeAlias("*SELECT*", colnames);
+		rte->eref = rte->alias = makeAlias(NULL, colnames);
 		rte->lateral = false;
 		rte->inh = false;
 		rte->inFromCl = true;
diff --git a/src/backend/nodes/makefuncs.c b/src/backend/nodes/makefuncs.c
index 9cac3c1c27b..3fe0b68eccf 100644
--- a/src/backend/nodes/makefuncs.c
+++ b/src/backend/nodes/makefuncs.c
@@ -390,7 +390,8 @@ makeAlias(const char *aliasname, List *colnames)
 {
 	Alias	   *a = makeNode(Alias);
 
-	a->aliasname = pstrdup(aliasname);
+	if (aliasname != NULL)
+		a->aliasname = pstrdup(aliasname);
 	a->colnames = colnames;
 
 	return a;
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index 09d5f0f571b..76ed91ffb52 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -1318,7 +1318,7 @@ convert_ANY_sublink_to_join(PlannerInfo *root, SubLink *sublink,
 	 */
 	nsitem = addRangeTableEntryForSubquery(pstate,
 										   subselect,
-										   makeAlias("ANY_subquery", NIL),
+										   makeAlias(NULL, NIL),
 										   use_lateral,
 										   false);
 	rte = nsitem->p_rte;
diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
index 33b10d72cb5..a8f9b9c5a59 100644
--- a/src/backend/optimizer/prep/prepjointree.c
+++ b/src/backend/optimizer/prep/prepjointree.c
@@ -205,7 +205,7 @@ transform_MERGE_to_join(Query *parse)
 	joinrte->join_using_alias = NULL;
 
 	joinrte->alias = NULL;
-	joinrte->eref = makeAlias("*MERGE*", NIL);
+	joinrte->eref = makeAlias(NULL, NIL);
 	joinrte->lateral = false;
 	joinrte->inh = false;
 	joinrte->inFromCl = true;
@@ -393,7 +393,7 @@ replace_empty_jointree(Query *parse)
 	/* Create suitable RTE */
 	rte = makeNode(RangeTblEntry);
 	rte->rtekind = RTE_RESULT;
-	rte->eref = makeAlias("*RESULT*", NIL);
+	rte->eref = makeAlias(NULL, NIL);
 
 	/* Add it to rangetable */
 	parse->rtable = lappend(parse->rtable, rte);
@@ -1842,7 +1842,7 @@ pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
 	/* Create suitable RTE */
 	rte = makeNode(RangeTblEntry);
 	rte->rtekind = RTE_RESULT;
-	rte->eref = makeAlias("*RESULT*", NIL);
+	rte->eref = makeAlias(NULL, NIL);
 
 	/* Replace rangetable */
 	parse->rtable = list_make1(rte);
diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c
index 506e0631615..54fa800eca8 100644
--- a/src/backend/parser/analyze.c
+++ b/src/backend/parser/analyze.c
@@ -823,7 +823,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt)
 		 */
 		nsitem = addRangeTableEntryForSubquery(pstate,
 											   selectQuery,
-											   makeAlias("*SELECT*", NIL),
+											   makeAlias(NULL, NIL),
 											   false,
 											   false);
 		addNSItemToQuery(pstate, nsitem, true, false, false);
@@ -2147,7 +2147,6 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
 	{
 		/* Process leaf SELECT */
 		Query	   *selectQuery;
-		char		selectName[32];
 		ParseNamespaceItem *nsitem;
 		RangeTblRef *rtr;
 		ListCell   *tl;
@@ -2203,11 +2202,9 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt,
 		/*
 		 * 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,
 											   selectQuery,
-											   makeAlias(selectName, NIL),
+											   makeAlias(NULL, NIL),
 											   false,
 											   false);
 
diff --git a/src/backend/parser/parse_merge.c b/src/backend/parser/parse_merge.c
index 87df79027d7..00a9a6071a9 100644
--- a/src/backend/parser/parse_merge.c
+++ b/src/backend/parser/parse_merge.c
@@ -215,7 +215,9 @@ transformMergeStmt(ParseState *pstate, MergeStmt *stmt)
 	 * This would typically be a checkNameSpaceConflicts call, but we want a
 	 * more specific error message.
 	 */
-	if (strcmp(pstate->p_target_nsitem->p_names->aliasname,
+	if (pstate->p_target_nsitem->p_names->aliasname != NULL &&
+		nsitem->p_names->aliasname != NULL &&
+		strcmp(pstate->p_target_nsitem->p_names->aliasname,
 			   nsitem->p_names->aliasname) == 0)
 		ereport(ERROR,
 				errcode(ERRCODE_DUPLICATE_ALIAS),
diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c
index 8075b1b8a1b..dda02ad2ad6 100644
--- a/src/backend/parser/parse_relation.c
+++ b/src/backend/parser/parse_relation.c
@@ -213,7 +213,8 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
 		if (nsitem->p_lateral_only && !pstate->p_lateral_active)
 			continue;
 
-		if (strcmp(nsitem->p_names->aliasname, refname) == 0)
+		if (nsitem->p_names->aliasname != NULL &&
+			strcmp(nsitem->p_names->aliasname, refname) == 0)
 		{
 			if (result)
 				ereport(ERROR,
@@ -1657,7 +1658,7 @@ addRangeTableEntryForSubquery(ParseState *pstate,
 	rte->subquery = subquery;
 	rte->alias = alias;
 
-	eref = alias ? copyObject(alias) : makeAlias("unnamed_subquery", NIL);
+	eref = alias ? copyObject(alias) : makeAlias(NULL, NIL);
 	numaliases = list_length(eref->colnames);
 
 	/* fill in any unspecified alias columns, and extract column type info */
@@ -2253,7 +2254,7 @@ addRangeTableEntryForJoin(ParseState *pstate,
 	rte->join_using_alias = join_using_alias;
 	rte->alias = alias;
 
-	eref = alias ? copyObject(alias) : makeAlias("unnamed_join", NIL);
+	eref = alias ? copyObject(alias) : makeAlias(NULL, NIL);
 	numaliases = list_length(eref->colnames);
 
 	/* fill in any unspecified alias columns */
@@ -2579,7 +2580,7 @@ addRangeTableEntryForGroup(ParseState *pstate,
 	rte->rtekind = RTE_GROUP;
 	rte->alias = NULL;
 
-	eref = makeAlias("*GROUP*", NIL);
+	eref = makeAlias(NULL, NIL);
 
 	/* fill in any unspecified alias columns, and extract column type info */
 	groupexprs = NIL;
diff --git a/src/backend/rewrite/rewriteSearchCycle.c b/src/backend/rewrite/rewriteSearchCycle.c
index 1c58003f9dc..586ff721397 100644
--- a/src/backend/rewrite/rewriteSearchCycle.c
+++ b/src/backend/rewrite/rewriteSearchCycle.c
@@ -282,7 +282,7 @@ rewriteSearchAndCycle(CommonTableExpr *cte)
 
 	newrte = makeNode(RangeTblEntry);
 	newrte->rtekind = RTE_SUBQUERY;
-	newrte->alias = makeAlias("*TLOCRN*", cte->ctecolnames);
+	newrte->alias = makeAlias(NULL, cte->ctecolnames);
 	newrte->eref = newrte->alias;
 	newsubquery = copyObject(rte1->subquery);
 	IncrementVarSublevelsUp((Node *) newsubquery, 1, 1);
@@ -379,7 +379,7 @@ rewriteSearchAndCycle(CommonTableExpr *cte)
 		ewcl = lappend(ewcl, makeString(cte->cycle_clause->cycle_mark_column));
 		ewcl = lappend(ewcl, makeString(cte->cycle_clause->cycle_path_column));
 	}
-	newrte->alias = makeAlias("*TROCRN*", ewcl);
+	newrte->alias = makeAlias(NULL, ewcl);
 	newrte->eref = newrte->alias;
 
 	/*
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 2177d17e278..145cb90b8de 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3902,7 +3902,7 @@ set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
 			/* Ignore unreferenced RTE */
 			refname = NULL;
 		}
-		else if (rte->alias)
+		else if (rte->alias != NULL && rte->alias->aliasname != NULL)
 		{
 			/* If RTE has a user-defined alias, prefer that */
 			refname = rte->alias->aliasname;
@@ -3917,11 +3917,21 @@ set_rtable_names(deparse_namespace *dpns, List *parent_namespaces,
 			/* Unnamed join has no refname */
 			refname = NULL;
 		}
-		else
+		else if (rte->eref->aliasname != NULL)
 		{
-			/* Otherwise use whatever the parser assigned */
+			/* Use whatever the parser assigned */
 			refname = rte->eref->aliasname;
 		}
+		else if (rte->rtekind == RTE_SUBQUERY)
+		{
+			/* refname of last resort */
+			refname = "unnamed_subquery";
+		}
+		else
+		{
+			/* refname of last resort */
+			refname = "unnamed";
+		}
 
 		/*
 		 * If the selected name isn't unique, append digits to make it so, and
diff --git a/src/test/regress/expected/rangefuncs.out b/src/test/regress/expected/rangefuncs.out
index 397a8b35d6d..7f9c441f903 100644
--- a/src/test/regress/expected/rangefuncs.out
+++ b/src/test/regress/expected/rangefuncs.out
@@ -2130,10 +2130,10 @@ select testrngfunc();
 
 explain (verbose, costs off)
 select * from testrngfunc();
-                        QUERY PLAN                        
-----------------------------------------------------------
- Subquery Scan on "*SELECT*"
-   Output: "*SELECT*"."?column?", "*SELECT*"."?column?_1"
+                              QUERY PLAN                              
+----------------------------------------------------------------------
+ Subquery Scan on unnamed_subquery
+   Output: unnamed_subquery."?column?", unnamed_subquery."?column?_1"
    ->  Unique
          Output: (1), (2)
          ->  Sort
diff --git a/src/test/regress/expected/subselect.out b/src/test/regress/expected/subselect.out
index 2d35de3fad6..53426036039 100644
--- a/src/test/regress/expected/subselect.out
+++ b/src/test/regress/expected/subselect.out
@@ -1240,10 +1240,10 @@ where o.ten = 1;
          ->  Subquery Scan on ss
                ->  HashSetOp Except
                      ->  Append
-                           ->  Subquery Scan on "*SELECT* 1"
+                           ->  Subquery Scan on unnamed_subquery
                                  ->  Index Scan using onek_unique1 on onek i1
                                        Index Cond: (unique1 = o.unique1)
-                           ->  Subquery Scan on "*SELECT* 2"
+                           ->  Subquery Scan on unnamed_subquery_1
                                  ->  Index Scan using onek_unique1 on onek i2
                                        Index Cond: (unique1 = o.unique2)
 (13 rows)
@@ -1425,14 +1425,14 @@ select * from int4_tbl o where (f1, f1) in
 -------------------------------------------------------------------
  Nested Loop Semi Join
    Output: o.f1
-   Join Filter: (o.f1 = "ANY_subquery".f1)
+   Join Filter: (o.f1 = unnamed_subquery.f1)
    ->  Seq Scan on public.int4_tbl o
          Output: o.f1
    ->  Materialize
-         Output: "ANY_subquery".f1, "ANY_subquery".g
-         ->  Subquery Scan on "ANY_subquery"
-               Output: "ANY_subquery".f1, "ANY_subquery".g
-               Filter: ("ANY_subquery".f1 = "ANY_subquery".g)
+         Output: unnamed_subquery.f1, unnamed_subquery.g
+         ->  Subquery Scan on unnamed_subquery
+               Output: unnamed_subquery.f1, unnamed_subquery.g
+               Filter: (unnamed_subquery.f1 = unnamed_subquery.g)
                ->  Result
                      Output: i.f1, ((generate_series(1, 50)) / 10)
                      ->  ProjectSet
@@ -2124,8 +2124,8 @@ ON B.hundred in (SELECT min(c.hundred) FROM tenk2 C WHERE c.odd = b.odd);
                ->  Memoize
                      Cache Key: b.hundred, b.odd
                      Cache Mode: binary
-                     ->  Subquery Scan on "ANY_subquery"
-                           Filter: (b.hundred = "ANY_subquery".min)
+                     ->  Subquery Scan on unnamed_subquery
+                           Filter: (b.hundred = unnamed_subquery.min)
                            ->  Result
                                  InitPlan 1
                                    ->  Limit
diff --git a/src/test/regress/expected/union.out b/src/test/regress/expected/union.out
index c73631a9a1d..9baf4f03a9b 100644
--- a/src/test/regress/expected/union.out
+++ b/src/test/regress/expected/union.out
@@ -376,9 +376,9 @@ select count(*) from
    ->  Subquery Scan on ss
          ->  HashSetOp Intersect
                ->  Append
-                     ->  Subquery Scan on "*SELECT* 2"
+                     ->  Subquery Scan on unnamed_subquery_1
                            ->  Seq Scan on tenk1
-                     ->  Subquery Scan on "*SELECT* 1"
+                     ->  Subquery Scan on unnamed_subquery
                            ->  Index Only Scan using tenk1_unique1 on tenk1 tenk1_1
 (8 rows)
 
@@ -395,9 +395,9 @@ select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
 ------------------------------------------------------------------------
  HashSetOp Except
    ->  Append
-         ->  Subquery Scan on "*SELECT* 1"
+         ->  Subquery Scan on unnamed_subquery
                ->  Index Only Scan using tenk1_unique1 on tenk1
-         ->  Subquery Scan on "*SELECT* 2"
+         ->  Subquery Scan on unnamed_subquery_1
                ->  Index Only Scan using tenk1_unique2 on tenk1 tenk1_1
                      Filter: (unique2 <> 10)
 (7 rows)
@@ -440,11 +440,11 @@ select count(*) from
    ->  Subquery Scan on ss
          ->  SetOp Intersect
                ->  Sort
-                     Sort Key: "*SELECT* 2".fivethous
+                     Sort Key: unnamed_subquery_1.fivethous
                      ->  Append
-                           ->  Subquery Scan on "*SELECT* 2"
+                           ->  Subquery Scan on unnamed_subquery_1
                                  ->  Seq Scan on tenk1
-                           ->  Subquery Scan on "*SELECT* 1"
+                           ->  Subquery Scan on unnamed_subquery
                                  ->  Index Only Scan using tenk1_unique1 on tenk1 tenk1_1
 (10 rows)
 
@@ -461,11 +461,11 @@ select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
 ------------------------------------------------------------------------------
  SetOp Except
    ->  Sort
-         Sort Key: "*SELECT* 1".unique1
+         Sort Key: unnamed_subquery.unique1
          ->  Append
-               ->  Subquery Scan on "*SELECT* 1"
+               ->  Subquery Scan on unnamed_subquery
                      ->  Index Only Scan using tenk1_unique1 on tenk1
-               ->  Subquery Scan on "*SELECT* 2"
+               ->  Subquery Scan on unnamed_subquery_1
                      ->  Index Only Scan using tenk1_unique2 on tenk1 tenk1_1
                            Filter: (unique2 <> 10)
 (9 rows)
@@ -528,13 +528,13 @@ select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (va
 
 explain (costs off)
 select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
-                  QUERY PLAN                   
------------------------------------------------
+                   QUERY PLAN                    
+-------------------------------------------------
  HashSetOp Intersect
    ->  Append
-         ->  Subquery Scan on "*SELECT* 1"
+         ->  Subquery Scan on unnamed_subquery
                ->  Values Scan on "*VALUES*"
-         ->  Subquery Scan on "*SELECT* 2"
+         ->  Subquery Scan on unnamed_subquery_1
                ->  Values Scan on "*VALUES*_1"
 (6 rows)
 
@@ -546,13 +546,13 @@ select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from
 
 explain (costs off)
 select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
-                  QUERY PLAN                   
------------------------------------------------
+                   QUERY PLAN                    
+-------------------------------------------------
  HashSetOp Except
    ->  Append
-         ->  Subquery Scan on "*SELECT* 1"
+         ->  Subquery Scan on unnamed_subquery
                ->  Values Scan on "*VALUES*"
-         ->  Subquery Scan on "*SELECT* 2"
+         ->  Subquery Scan on unnamed_subquery_1
                ->  Values Scan on "*VALUES*_1"
 (6 rows)
 
@@ -606,15 +606,15 @@ select x from (values (array[1, 2]), (array[1, 3])) _(x) union select x from (va
 
 explain (costs off)
 select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
-                     QUERY PLAN                      
------------------------------------------------------
+                      QUERY PLAN                       
+-------------------------------------------------------
  SetOp Intersect
    ->  Sort
-         Sort Key: "*SELECT* 1".x
+         Sort Key: unnamed_subquery.x
          ->  Append
-               ->  Subquery Scan on "*SELECT* 1"
+               ->  Subquery Scan on unnamed_subquery
                      ->  Values Scan on "*VALUES*"
-               ->  Subquery Scan on "*SELECT* 2"
+               ->  Subquery Scan on unnamed_subquery_1
                      ->  Values Scan on "*VALUES*_1"
 (8 rows)
 
@@ -626,15 +626,15 @@ select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from
 
 explain (costs off)
 select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
-                     QUERY PLAN                      
------------------------------------------------------
+                      QUERY PLAN                       
+-------------------------------------------------------
  SetOp Except
    ->  Sort
-         Sort Key: "*SELECT* 1".x
+         Sort Key: unnamed_subquery.x
          ->  Append
-               ->  Subquery Scan on "*SELECT* 1"
+               ->  Subquery Scan on unnamed_subquery
                      ->  Values Scan on "*VALUES*"
-               ->  Subquery Scan on "*SELECT* 2"
+               ->  Subquery Scan on unnamed_subquery_1
                      ->  Values Scan on "*VALUES*_1"
 (8 rows)
 
@@ -669,15 +669,15 @@ select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values
 
 explain (costs off)
 select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
-                     QUERY PLAN                      
------------------------------------------------------
+                      QUERY PLAN                       
+-------------------------------------------------------
  SetOp Intersect
    ->  Sort
-         Sort Key: "*SELECT* 1".x
+         Sort Key: unnamed_subquery.x
          ->  Append
-               ->  Subquery Scan on "*SELECT* 1"
+               ->  Subquery Scan on unnamed_subquery
                      ->  Values Scan on "*VALUES*"
-               ->  Subquery Scan on "*SELECT* 2"
+               ->  Subquery Scan on unnamed_subquery_1
                      ->  Values Scan on "*VALUES*_1"
 (8 rows)
 
@@ -689,15 +689,15 @@ select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (va
 
 explain (costs off)
 select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
-                     QUERY PLAN                      
------------------------------------------------------
+                      QUERY PLAN                       
+-------------------------------------------------------
  SetOp Except
    ->  Sort
-         Sort Key: "*SELECT* 1".x
+         Sort Key: unnamed_subquery.x
          ->  Append
-               ->  Subquery Scan on "*SELECT* 1"
+               ->  Subquery Scan on unnamed_subquery
                      ->  Values Scan on "*VALUES*"
-               ->  Subquery Scan on "*SELECT* 2"
+               ->  Subquery Scan on unnamed_subquery_1
                      ->  Values Scan on "*VALUES*_1"
 (8 rows)
 
@@ -777,15 +777,15 @@ select x from (values (row(1, 2)), (row(1, 3))) _(x) union select x from (values
 
 explain (costs off)
 select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (values (row(1, 2)), (row(1, 4))) _(x);
-                     QUERY PLAN                      
------------------------------------------------------
+                      QUERY PLAN                       
+-------------------------------------------------------
  SetOp Intersect
    ->  Sort
-         Sort Key: "*SELECT* 1".x
+         Sort Key: unnamed_subquery.x
          ->  Append
-               ->  Subquery Scan on "*SELECT* 1"
+               ->  Subquery Scan on unnamed_subquery
                      ->  Values Scan on "*VALUES*"
-               ->  Subquery Scan on "*SELECT* 2"
+               ->  Subquery Scan on unnamed_subquery_1
                      ->  Values Scan on "*VALUES*_1"
 (8 rows)
 
@@ -797,15 +797,15 @@ select x from (values (row(1, 2)), (row(1, 3))) _(x) intersect select x from (va
 
 explain (costs off)
 select x from (values (row(1, 2)), (row(1, 3))) _(x) except select x from (values (row(1, 2)), (row(1, 4))) _(x);
-                     QUERY PLAN                      
------------------------------------------------------
+                      QUERY PLAN                       
+-------------------------------------------------------
  SetOp Except
    ->  Sort
-         Sort Key: "*SELECT* 1".x
+         Sort Key: unnamed_subquery.x
          ->  Append
-               ->  Subquery Scan on "*SELECT* 1"
+               ->  Subquery Scan on unnamed_subquery
                      ->  Values Scan on "*VALUES*"
-               ->  Subquery Scan on "*SELECT* 2"
+               ->  Subquery Scan on unnamed_subquery_1
                      ->  Values Scan on "*VALUES*_1"
 (8 rows)
 
@@ -925,7 +925,7 @@ SELECT q1 FROM int8_tbl EXCEPT SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1;
 ERROR:  column "q2" does not exist
 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 "(null)", but it cannot be referenced from this part of the query.
 -- But this should work:
 SELECT q1 FROM int8_tbl EXCEPT (((SELECT q2 FROM int8_tbl ORDER BY q2 LIMIT 1))) ORDER BY 1;
         q1        
@@ -974,9 +974,9 @@ select from generate_series(1,5) intersect select from generate_series(1,3);
 ----------------------------------------------------------------------
  HashSetOp Intersect
    ->  Append
-         ->  Subquery Scan on "*SELECT* 1"
+         ->  Subquery Scan on unnamed_subquery
                ->  Function Scan on generate_series
-         ->  Subquery Scan on "*SELECT* 2"
+         ->  Subquery Scan on unnamed_subquery_1
                ->  Function Scan on generate_series generate_series_1
 (6 rows)
 
@@ -1019,9 +1019,9 @@ select from generate_series(1,5) intersect select from generate_series(1,3);
 ----------------------------------------------------------------------
  SetOp Intersect
    ->  Append
-         ->  Subquery Scan on "*SELECT* 1"
+         ->  Subquery Scan on unnamed_subquery
                ->  Function Scan on generate_series
-         ->  Subquery Scan on "*SELECT* 2"
+         ->  Subquery Scan on unnamed_subquery_1
                ->  Function Scan on generate_series generate_series_1
 (6 rows)
 
@@ -1327,14 +1327,14 @@ where q2 = q2;
 ----------------------------------------------------------
  Unique
    ->  Merge Append
-         Sort Key: "*SELECT* 1".q1
-         ->  Subquery Scan on "*SELECT* 1"
+         Sort Key: unnamed_subquery.q1
+         ->  Subquery Scan on unnamed_subquery
                ->  Unique
                      ->  Sort
                            Sort Key: i81.q1, i81.q2
                            ->  Seq Scan on int8_tbl i81
                                  Filter: (q2 IS NOT NULL)
-         ->  Subquery Scan on "*SELECT* 2"
+         ->  Subquery Scan on unnamed_subquery_1
                ->  Unique
                      ->  Sort
                            Sort Key: i82.q1, i82.q2
@@ -1363,14 +1363,14 @@ where -q1 = q2;
 --------------------------------------------------------
  Unique
    ->  Merge Append
-         Sort Key: "*SELECT* 1".q1
-         ->  Subquery Scan on "*SELECT* 1"
+         Sort Key: unnamed_subquery.q1
+         ->  Subquery Scan on unnamed_subquery
                ->  Unique
                      ->  Sort
                            Sort Key: i81.q1, i81.q2
                            ->  Seq Scan on int8_tbl i81
                                  Filter: ((- q1) = q2)
-         ->  Subquery Scan on "*SELECT* 2"
+         ->  Subquery Scan on unnamed_subquery_1
                ->  Unique
                      ->  Sort
                            Sort Key: i82.q1, i82.q2
-- 
2.39.3 (Apple Git-145)

