diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c
index 8c5714a..ea4a9e0 100644
--- a/src/backend/optimizer/plan/analyzejoins.c
+++ b/src/backend/optimizer/plan/analyzejoins.c
@@ -34,8 +34,8 @@
 
 /* local functions */
 static bool join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo);
-static bool	sortclause_is_unique_on_restrictinfo(Query *query,
-					  List *clause_list, List *sortclause);
+static bool	sortlist_is_unique_on_restrictinfo(Query *query,
+					  List *clause_list, List *sortlist);
 static void remove_rel_from_query(PlannerInfo *root, int relid,
 					  Relids joinrelids);
 static List *remove_rel_from_joinlist(List *joinlist, int relid, int *nremoved);
@@ -160,15 +160,26 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
 	int			attroff;
 
 	/*
-	 * Currently, we only know how to remove left joins to a baserel with
-	 * unique indexes and left joins to a subquery where the subquery is
-	 * unique on the join condition. We can check most of these criteria
-	 * pretty trivially to avoid doing useless extra work.  But checking
-	 * whether any of the indexes are unique would require iterating over
-	 * the indexlist, so for now, if we're joining to a relation, we'll just
-	 * ensure that we have at least 1 index, it won't matter if that index
-	 * is unique at this stage, we'll check those details later.
+	 * Assuming none of the variables from the join are needed by the query,
+	 * it is possible here to remove a left join providing we can determine
+	 * that the join will never produce more than 1 row that matches the join
+	 * condition.
+	 *
+	 * There are a few ways that we can do this:
+	 *
+	 * 1. When joining to a baserel we can check if a unique index exists
+	 *    where all of the columns of the index are seen in the join condition
+	 *    with equality operators.
+	 *
+	 * 2. When joining to a subquery we can check if the subquery contains a
+	 *    GROUP BY or DISTINCT clause where all of the columns of the clause
+	 *    appear in the join condition with equality operators.
+	 *
+	 * The code below is written with the assumption that join removal is more
+	 * likely not to happen, for this reason there are fast paths for both of
+	 * the cases above to try to save on unnecessary processing.
 	 */
+
 	if (sjinfo->jointype != JOIN_LEFT ||
 		sjinfo->delay_upper_joins ||
 		bms_membership(sjinfo->min_righthand) != BMS_SINGLETON)
@@ -182,8 +193,12 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
 
 	if (innerrel->rtekind == RTE_RELATION)
 	{
+		/*
+		 * If there are no indexes then there's certainly no unique indexes
+		 * so there's no need to go any further.
+		 */
 		if (innerrel->indexlist == NIL)
-			return false; /* no possibility of a unique index */
+			return false;
 	}
 	else if (innerrel->rtekind == RTE_SUBQUERY)
 	{
@@ -348,7 +363,7 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
 		 * have matching items in the join condition.
 		 */
 		if (subquery->groupClause != NIL &&
-			sortclause_is_unique_on_restrictinfo(subquery, clause_list, subquery->groupClause))
+			sortlist_is_unique_on_restrictinfo(subquery, clause_list, subquery->groupClause))
 			return true;
 
 		/*
@@ -356,40 +371,40 @@ join_is_removable(PlannerInfo *root, SpecialJoinInfo *sjinfo)
 		 * items in the join condition.
 		 */
 		if (subquery->distinctClause != NIL &&
-			sortclause_is_unique_on_restrictinfo(subquery, clause_list, subquery->distinctClause))
+			sortlist_is_unique_on_restrictinfo(subquery, clause_list, subquery->distinctClause))
 			return true;
 	}
+
 	/*
 	 * Some day it would be nice to check for other methods of establishing
-	 * distinctness.  XXX is this comment still true??
+	 * distinctness.
 	 */
 	return false;
 }
 
 /*
- * sortclause_is_unique_on_restrictinfo
+ * sortlist_is_unique_on_restrictinfo
  *
- * Checks to see if all items in sortclause also exist in clause_list.
+ * Checks to see if all items in sortlist also exist in clause_list.
  * The function will return true if clause_list is the same as or a superset
- * of the sortclause. If the sortclause has columns that don't exist in the
- * clause_list then the query can't be guaranteed unique on the clause_list
- * columns.
+ * of sortlist. If the sortlist has Vars that don't exist in the clause_list
+ * then the query can't be guaranteed unique on the clause_list columns.
  *
- * Note: The calling function must ensure that sortclause is not NIL.
+ * Note: The calling function must ensure that sortlist is not NIL.
  */
 static bool
-sortclause_is_unique_on_restrictinfo(Query *query, List *clause_list, List *sortclause)
+sortlist_is_unique_on_restrictinfo(Query *query, List *clause_list, List *sortlist)
 {
 	ListCell *l;
 
-	Assert(sortclause != NIL);
+	Assert(sortlist != NIL);
 
 	/*
-	 * Loop over each sort clause to ensure that we have an item in the join
-	 * conditions that matches it. Note that it does not matter if we have more
-	 * items in the join condition than we have in the sort clause.
+	 * Loop over each sortlist item to ensure that we have an item in the
+	 * clause_list that matches it. Note that it does not matter if we have
+	 * more items in the clause_list than we have in the sortlist.
 	 */
-	foreach(l, sortclause)
+	foreach(l, sortlist)
 	{
 		ListCell		*ri;
 		SortGroupClause *scl = (SortGroupClause *) lfirst(l);
@@ -400,8 +415,8 @@ sortclause_is_unique_on_restrictinfo(Query *query, List *clause_list, List *sort
 		sortTarget = get_sortgroupref_tle(scl->tleSortGroupRef, query->targetList);
 
 		/*
-		 * Since a constant only has 1 value the existence of one here will
-		 * not cause any duplication of the results. We'll simply ignore it!
+		 * We can ignore constants since they have only one value and don't
+		 * affect uniqueness of results.
 		 */
 		if (IsA(sortTarget->expr, Const))
 			continue;
diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out
index 202ac0a..4959e5f 100644
--- a/src/test/regress/expected/join.out
+++ b/src/test/regress/expected/join.out
@@ -3171,23 +3171,37 @@ select id from a where id in (
          ->  Seq Scan on b
 (5 rows)
 
--- check optimization of outer join when joining a unique sub query using group by
+-- check that join removal works for a left join when joining a subquery
+-- that is guaranteed to be unique on the join condition by the GROUP BY clause
 EXPLAIN (COSTS OFF)
-SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id) b ON a.id = b.id;
+SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id) b ON a.b_id = b.id;
   QUERY PLAN   
 ---------------
  Seq Scan on a
 (1 row)
 
--- check optimization of outer join when joining a unique sub query using distinct
+-- check that join removal works for a left join when joining a subquery
+-- that is guaranteed to be unique on the join condition by the GROUP BY clause
+-- which contains more than 1 column.
 EXPLAIN (COSTS OFF)
-SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.c_id FROM b) b ON a.id = b.c_id;
+SELECT a.id FROM a LEFT JOIN (SELECT b.id,b.c_id FROM b GROUP BY b.id,b.c_id) b ON a.b_id = b.id AND a.id = b.c_id;
   QUERY PLAN   
 ---------------
  Seq Scan on a
 (1 row)
 
--- check optimization of outer join when joining a unique sub query using distinct
+-- check that join removal works for a left join when joining a subquery
+-- where the join condition is a superset of the columns in the GROUP BY.
+EXPLAIN (COSTS OFF)
+SELECT a.id FROM a
+LEFT JOIN (SELECT b.id,c_id FROM b GROUP BY b.id) b ON a.id = b.id AND b.c_id = 1;
+  QUERY PLAN   
+---------------
+ Seq Scan on a
+(1 row)
+
+-- check that join removal works for a left join when joining a subquery
+-- that is guaranteed to be unique on the join condition by the DISTINCT clause
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT a+b AS ab FROM d) d ON a.id = d.ab;
   QUERY PLAN   
@@ -3195,7 +3209,7 @@ SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT a+b AS ab FROM d) d ON a.id = d.ab
  Seq Scan on a
 (1 row)
 
--- optimization is not possible when distinct contains a volatile function
+-- join removal is not possible when distinct contains a volatile function
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT a+b+random() AS abr FROM d) d ON a.id = d.abr;
                                         QUERY PLAN                                        
@@ -3209,8 +3223,8 @@ SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT a+b+random() AS abr FROM d) d ON a
                ->  Seq Scan on d
 (7 rows)
 
--- check optimization of outer join when joining a unique sub query using distinct
--- and a constant expression.
+-- check that join removal works for a left join when joining a subquery that
+-- is guaranteed to be unique on the join condition even if it contains a Const.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.c_id,1 AS dummy FROM b) b ON a.id = b.c_id;
   QUERY PLAN   
@@ -3218,15 +3232,8 @@ SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.c_id,1 AS dummy FROM b) b ON a.i
  Seq Scan on a
 (1 row)
 
--- check optimization of outer join when joining a unique sub query which contains 2 tables
-EXPLAIN (COSTS OFF)
-SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id = b.id;
-  QUERY PLAN   
----------------
- Seq Scan on a
-(1 row)
-
--- check optimization of outer join when joining a unique sub query which contains 2 tables
+-- check join removal works when joining to a subquery that is guaranteed to be
+-- unique on the join condition even when the subquery itself involves a join. 
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id + 10 = b.id;
   QUERY PLAN   
@@ -3234,8 +3241,9 @@ SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b INNER JOIN c ON b.id = c.id GRO
  Seq Scan on a
 (1 row)
 
--- check optimization of outer join when joining a unique sub query which contains 2 tables
--- and contains a redundant join clause
+-- check join removal works with a left join when joining a unique subquery which
+-- contains 2 tables where the uniqueness enforced by the GROUP BY clause is a
+-- subset of the join condition.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a
 LEFT JOIN (SELECT b.id,1 as dummy FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id = b.id AND b.dummy = 1;
@@ -3244,7 +3252,8 @@ LEFT JOIN (SELECT b.id,1 as dummy FROM b INNER JOIN c ON b.id = c.id GROUP BY b.
  Seq Scan on a
 (1 row)
 
--- optimization is not possible when the group by contains a column which is not in the join condition.
+-- join removal is not possible when the GROUP BY contains a column which is
+-- not in the join condition.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id,b.c_id) b ON a.id = b.id;
            QUERY PLAN            
@@ -3258,7 +3267,8 @@ SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id,b.c_id) b ON a.id
          ->  Seq Scan on a
 (7 rows)
 
--- optimization is not possible when distinct clause contains an item that is not in the join clause
+-- join removal is not possible when DISTINCT clause contains a column which is
+-- not in the join condition.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.id,c_id FROM b) b ON a.id = b.id;
            QUERY PLAN            
@@ -3272,7 +3282,7 @@ SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.id,c_id FROM b) b ON a.id = b.id
          ->  Seq Scan on a
 (7 rows)
 
--- optimization is not possible when distinct contains a volatile function
+-- join removal is not possible when DISTINCT contains a volatile function
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.id,random() AS r FROM b) b ON a.id = b.id AND r = random();
                    QUERY PLAN                    
@@ -3288,7 +3298,7 @@ SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.id,random() AS r FROM b) b ON a.
                      ->  Seq Scan on b b_1
 (9 rows)
 
--- optimization is not possible when there are any volatile functions in the target list.
+-- join removal is not possible when there are any volatile functions in the target list.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT id,AVG(c_id),SUM(random()) FROM b GROUP BY id) b ON a.id = b.id;
          QUERY PLAN         
@@ -3302,7 +3312,7 @@ SELECT a.id FROM a LEFT JOIN (SELECT id,AVG(c_id),SUM(random()) FROM b GROUP BY
          ->  Seq Scan on a
 (7 rows)
 
--- optimization is not possible when there are set returning functions in the target list.
+-- join removal is not possible when there are set returning functions in the target list.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT id,generate_series(1,2) FROM b GROUP BY id) b ON a.id = b.id;
          QUERY PLAN         
diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql
index ba75912..21e29d2 100644
--- a/src/test/regress/sql/join.sql
+++ b/src/test/regress/sql/join.sql
@@ -938,58 +938,68 @@ select id from a where id in (
 	select b.id from b left join c on b.id = c.id
 );
 
--- check optimization of outer join when joining a unique sub query using group by
+-- check that join removal works for a left join when joining a subquery
+-- that is guaranteed to be unique on the join condition by the GROUP BY clause
 EXPLAIN (COSTS OFF)
-SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id) b ON a.id = b.id;
+SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id) b ON a.b_id = b.id;
 
--- check optimization of outer join when joining a unique sub query using distinct
+-- check that join removal works for a left join when joining a subquery
+-- that is guaranteed to be unique on the join condition by the GROUP BY clause
+-- which contains more than 1 column.
 EXPLAIN (COSTS OFF)
-SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.c_id FROM b) b ON a.id = b.c_id;
+SELECT a.id FROM a LEFT JOIN (SELECT b.id,b.c_id FROM b GROUP BY b.id,b.c_id) b ON a.b_id = b.id AND a.id = b.c_id;
 
--- check optimization of outer join when joining a unique sub query using distinct
+-- check that join removal works for a left join when joining a subquery
+-- where the join condition is a superset of the columns in the GROUP BY.
+EXPLAIN (COSTS OFF)
+SELECT a.id FROM a
+LEFT JOIN (SELECT b.id,c_id FROM b GROUP BY b.id) b ON a.id = b.id AND b.c_id = 1;
+
+-- check that join removal works for a left join when joining a subquery
+-- that is guaranteed to be unique on the join condition by the DISTINCT clause
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT a+b AS ab FROM d) d ON a.id = d.ab;
 
--- optimization is not possible when distinct contains a volatile function
+-- join removal is not possible when distinct contains a volatile function
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT a+b+random() AS abr FROM d) d ON a.id = d.abr;
 
--- check optimization of outer join when joining a unique sub query using distinct
--- and a constant expression.
+-- check that join removal works for a left join when joining a subquery that
+-- is guaranteed to be unique on the join condition even if it contains a Const.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.c_id,1 AS dummy FROM b) b ON a.id = b.c_id;
 
--- check optimization of outer join when joining a unique sub query which contains 2 tables
-EXPLAIN (COSTS OFF)
-SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id = b.id;
-
--- check optimization of outer join when joining a unique sub query which contains 2 tables
+-- check join removal works when joining to a subquery that is guaranteed to be
+-- unique on the join condition even when the subquery itself involves a join.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id + 10 = b.id;
 
--- check optimization of outer join when joining a unique sub query which contains 2 tables
--- and contains a redundant join clause
+-- check join removal works with a left join when joining a unique subquery which
+-- contains 2 tables where the uniqueness enforced by the GROUP BY clause is a
+-- subset of the join condition.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a
 LEFT JOIN (SELECT b.id,1 as dummy FROM b INNER JOIN c ON b.id = c.id GROUP BY b.id) b ON a.id = b.id AND b.dummy = 1;
 
--- optimization is not possible when the group by contains a column which is not in the join condition.
+-- join removal is not possible when the GROUP BY contains a column which is
+-- not in the join condition.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT b.id FROM b GROUP BY b.id,b.c_id) b ON a.id = b.id;
 
--- optimization is not possible when distinct clause contains an item that is not in the join clause
+-- join removal is not possible when DISTINCT clause contains a column which is
+-- not in the join condition.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.id,c_id FROM b) b ON a.id = b.id;
 
--- optimization is not possible when distinct contains a volatile function
+-- join removal is not possible when DISTINCT contains a volatile function
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT DISTINCT b.id,random() AS r FROM b) b ON a.id = b.id AND r = random();
 
--- optimization is not possible when there are any volatile functions in the target list.
+-- join removal is not possible when there are any volatile functions in the target list.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT id,AVG(c_id),SUM(random()) FROM b GROUP BY id) b ON a.id = b.id;
 
--- optimization is not possible when there are set returning functions in the target list.
+-- join removal is not possible when there are set returning functions in the target list.
 EXPLAIN (COSTS OFF)
 SELECT a.id FROM a LEFT JOIN (SELECT id,generate_series(1,2) FROM b GROUP BY id) b ON a.id = b.id;
 
