diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 9ad54c5..2e87183 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3858,21 +3858,26 @@ get_variable(Var *var, int levelsup, bool showstar, deparse_context *context)
 		return NULL;
 	}
 
 	/* Identify names to use */
 	schemaname = NULL;			/* default assumptions */
 	refname = rte->eref->aliasname;
 
 	/* Exceptions occur only if the RTE is alias-less */
 	if (rte->alias == NULL)
 	{
-		if (rte->rtekind == RTE_RELATION)
+		/*
+		 * If the rel has been renamed since the rule was made, that's
+		 * equivalent to having a alias.
+		 */
+		if (rte->rtekind == RTE_RELATION &&
+			strcmp(refname, get_relation_name(rte->relid)) == 0)
 		{
 			/*
 			 * It's possible that use of the bare refname would find another
 			 * more-closely-nested RTE, or be ambiguous, in which case we need
 			 * to specify the schemaname to avoid these errors.
 			 */
 			if (find_rte_by_refname(rte->eref->aliasname, context) != rte)
 				schemaname = get_namespace_name(get_rel_namespace(rte->relid));
 		}
 		else if (rte->rtekind == RTE_JOIN)
diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expected/inherit.out
index 7e8f572..d47aeea 100644
--- a/src/test/regress/expected/inherit.out
+++ b/src/test/regress/expected/inherit.out
@@ -1001,58 +1001,58 @@ explain (verbose, costs off) select * from matest0 order by 1-id;
 ---------------------------------------------------------------------------------
  Sort
    Output: public.matest0.id, public.matest0.name, ((1 - public.matest0.id))
    Sort Key: ((1 - public.matest0.id))
    ->  Result
          Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
          ->  Append
                ->  Seq Scan on public.matest0
                      Output: public.matest0.id, public.matest0.name
                ->  Seq Scan on public.matest1 matest0
-                     Output: public.matest0.id, public.matest0.name
+                     Output: matest0.id, matest0.name
                ->  Seq Scan on public.matest2 matest0
-                     Output: public.matest0.id, public.matest0.name
+                     Output: matest0.id, matest0.name
                ->  Seq Scan on public.matest3 matest0
-                     Output: public.matest0.id, public.matest0.name
+                     Output: matest0.id, matest0.name
 (14 rows)
 
 select * from matest0 order by 1-id;
  id |  name  
 ----+--------
   6 | Test 6
   5 | Test 5
   4 | Test 4
   3 | Test 3
   2 | Test 2
   1 | Test 1
 (6 rows)
 
 reset enable_indexscan;
 set enable_seqscan = off;  -- plan with fewest seqscans should be merge
 explain (verbose, costs off) select * from matest0 order by 1-id;
-                                         QUERY PLAN                                          
----------------------------------------------------------------------------------------------
+                                      QUERY PLAN                                       
+---------------------------------------------------------------------------------------
  Result
    Output: public.matest0.id, public.matest0.name, ((1 - public.matest0.id))
    ->  Merge Append
          Sort Key: ((1 - public.matest0.id))
          ->  Index Scan using matest0i on public.matest0
                Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
          ->  Index Scan using matest1i on public.matest1 matest0
-               Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
+               Output: matest0.id, matest0.name, (1 - matest0.id)
          ->  Sort
-               Output: public.matest0.id, public.matest0.name, ((1 - public.matest0.id))
-               Sort Key: ((1 - public.matest0.id))
+               Output: matest0.id, matest0.name, ((1 - matest0.id))
+               Sort Key: ((1 - matest0.id))
                ->  Seq Scan on public.matest2 matest0
-                     Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
+                     Output: matest0.id, matest0.name, (1 - matest0.id)
          ->  Index Scan using matest3i on public.matest3 matest0
-               Output: public.matest0.id, public.matest0.name, (1 - public.matest0.id)
+               Output: matest0.id, matest0.name, (1 - matest0.id)
 (15 rows)
 
 select * from matest0 order by 1-id;
  id |  name  
 ----+--------
   6 | Test 6
   5 | Test 5
   4 | Test 4
   3 | Test 3
   2 | Test 2
