diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index a7412b0e7204e2c980f3131e302e145fd126e8df..f9bc64953d1b56e117af2178c11de12178f147d9 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -5,7 +5,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994-5, Regents of the University of California
  *
- * $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.78 2002/05/18 21:38:40 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.79 2002/06/13 03:40:49 tgl Exp $
  *
  */
 
@@ -681,7 +681,8 @@ show_scan_qual(List *qual, bool is_or_qual, const char *qlabel,
 		outercontext = NULL;
 
 	context = deparse_context_for_plan(scanrelid, scancontext,
-									   OUTER, outercontext);
+									   OUTER, outercontext,
+									   NIL);
 
 	/* Deparse the expression */
 	exprstr = deparse_expression(node, context, (outercontext != NULL));
@@ -726,7 +727,8 @@ show_upper_qual(List *qual, const char *qlabel,
 	else
 		innercontext = NULL;
 	context = deparse_context_for_plan(outer_varno, outercontext,
-									   inner_varno, innercontext);
+									   inner_varno, innercontext,
+									   NIL);
 
 	/* Deparse the expression */
 	node = (Node *) make_ands_explicit(qual);
@@ -761,11 +763,30 @@ show_sort_keys(List *tlist, int nkeys, const char *qlabel,
 
 	/*
 	 * In this routine we expect that the plan node's tlist has not been
-	 * processed by set_plan_references(), so any Vars will contain valid
-	 * varnos referencing the actual rtable.
+	 * processed by set_plan_references().  Normally, any Vars will contain
+	 * valid varnos referencing the actual rtable.  But we might instead be
+	 * looking at a dummy tlist generated by prepunion.c; if there are
+	 * Vars with zero varno, use the tlist itself to determine their names.
 	 */
-	context = deparse_context_from_rtable(es->rtable);
-	useprefix = length(es->rtable) > 1;
+	if (intMember(0, pull_varnos((Node *) tlist)))
+	{
+		Node	   *outercontext;
+
+		outercontext = deparse_context_for_subplan("sort",
+												   tlist,
+												   es->rtable);
+		context = deparse_context_for_plan(0, outercontext,
+										   0, NULL,
+										   NIL);
+		useprefix = false;
+	}
+	else
+	{
+		context = deparse_context_for_plan(0, NULL,
+										   0, NULL,
+										   es->rtable);
+		useprefix = length(es->rtable) > 1;
+	}
 
 	for (keyno = 1; keyno <= nkeys; keyno++)
 	{
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 6d576349d96a15983b236ea45485966a3af26f60..2c15bf916b9417a012f7e370e1b201449c858fbe 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3,7 +3,7 @@
  *				back to source text
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.107 2002/05/28 22:16:15 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.108 2002/06/13 03:40:49 tgl Exp $
  *
  *	  This software is copyrighted by Jan Wieck - Hamburg.
  *
@@ -685,16 +685,21 @@ deparse_context_for(const char *aliasname, Oid relid)
  * The passed-in Nodes should be made using deparse_context_for_subplan
  * and/or deparse_context_for_relation.  The resulting context will work
  * for deparsing quals, tlists, etc of the plan node.
+ *
+ * An rtable list can also be passed in case plain Vars might be seen.
+ * This is not needed for true upper-level expressions, but is helpful for
+ * Sort nodes and similar cases with slightly bogus targetlists.
  */
 List *
 deparse_context_for_plan(int outer_varno, Node *outercontext,
-						 int inner_varno, Node *innercontext)
+						 int inner_varno, Node *innercontext,
+						 List *rtable)
 {
 	deparse_namespace *dpns;
 
 	dpns = (deparse_namespace *) palloc(sizeof(deparse_namespace));
 
-	dpns->rtable = NIL;
+	dpns->rtable = rtable;
 	dpns->outer_varno = outer_varno;
 	dpns->outer_rte = (RangeTblEntry *) outercontext;
 	dpns->inner_varno = inner_varno;
@@ -779,27 +784,6 @@ deparse_context_for_subplan(const char *name, List *tlist,
 	return (Node *) rte;
 }
 
-/*
- * deparse_context_from_rtable	- Build deparse context given a rangetable
- *
- * This is suitable for deparsing expressions that refer to only a single
- * level of variables (no outer-reference Vars).
- */
-List *
-deparse_context_from_rtable(List *rtable)
-{
-	deparse_namespace *dpns;
-
-	dpns = (deparse_namespace *) palloc(sizeof(deparse_namespace));
-
-	dpns->rtable = rtable;
-	dpns->outer_varno = dpns->inner_varno = 0;
-	dpns->outer_rte = dpns->inner_rte = NULL;
-
-	/* Return a one-deep namespace stack */
-	return makeList1(dpns);
-}
-
 /* ----------
  * make_ruledef			- reconstruct the CREATE RULE command
  *				  for a given pg_rewrite tuple
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index 7676ce5663fdbaf7184ed0675554cbe8306ce001..28ab44ce68b3b3a5e549044e8aa3ecc0f3c78e68 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: builtins.h,v 1.183 2002/06/11 15:41:38 thomas Exp $
+ * $Id: builtins.h,v 1.184 2002/06/13 03:40:49 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -354,11 +354,11 @@ extern char *deparse_expression(Node *expr, List *dpcontext,
 				   bool forceprefix);
 extern List *deparse_context_for(const char *aliasname, Oid relid);
 extern List *deparse_context_for_plan(int outer_varno, Node *outercontext,
-									  int inner_varno, Node *innercontext);
+									  int inner_varno, Node *innercontext,
+									  List *rtable);
 extern Node *deparse_context_for_rte(RangeTblEntry *rte);
 extern Node *deparse_context_for_subplan(const char *name, List *tlist,
 										 List *rtable);
-extern List *deparse_context_from_rtable(List *rtable);
 extern const char *quote_identifier(const char *ident);
 extern char *quote_qualified_identifier(const char *namespace,
 										const char *ident);