diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index 3c7fa632b8ebe26c1d91e83e0ba1fa9c03b9e919..1e1a9b236aeb0672645828b8d7dfd458a6ef3710 100644
*** a/src/backend/optimizer/plan/initsplan.c
--- b/src/backend/optimizer/plan/initsplan.c
*************** distribute_qual_to_rels(PlannerInfo *roo
*** 988,993 ****
--- 988,1010 ----
  			if (check_redundant_nullability_qual(root, clause))
  				return;
  		}
+ 		else if (!bms_is_empty(nullable_relids))
+ 		{
+ 			/*
+ 			 * Although the qual doesn't have to be delayed to above its
+ 			 * natural syntactic level, it does contain references to rels
+ 			 * that are nulled by lower outer joins, so don't treat it as a
+ 			 * possible equivalence clause.
+ 			 *
+ 			 * XXX in the future this restriction could be relaxed, but there
+ 			 * are a couple of stumbling blocks.  First, we'd have to verify
+ 			 * that the no-delay property applies to each side of the equality
+ 			 * separately, and second, the equivalence machinery would have to
+ 			 * be improved to create output RestrictInfos with appropriate
+ 			 * (possibly non-empty) nullable_relids values.
+ 			 */
+ 			maybe_equivalence = false;
+ 		}
  		else
  		{
  			/*
diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out
index 624b7455f365fd1b49c582a368e692cc2f584f74..58606fa95af4f9497d06ce9a95d3e57407e46845 100644
*** a/src/test/regress/expected/join.out
--- b/src/test/regress/expected/join.out
*************** SELECT qq, unique1
*** 2554,2560 ****
           ->  Hash
                 ->  Seq Scan on int8_tbl b
     ->  Index Scan using tenk1_unique2 on tenk1 c
!          Index Cond: (unique2 = COALESCE((COALESCE(a.q1, 0::bigint)), (COALESCE(b.q2, (-1)::bigint))))
  (8 rows)
  
  SELECT qq, unique1
--- 2554,2560 ----
           ->  Hash
                 ->  Seq Scan on int8_tbl b
     ->  Index Scan using tenk1_unique2 on tenk1 c
!          Index Cond: (COALESCE((COALESCE(a.q1, 0::bigint)), (COALESCE(b.q2, (-1)::bigint))) = unique2)
  (8 rows)
  
  SELECT qq, unique1
*************** select b.unique1 from
*** 2833,2838 ****
--- 2833,2882 ----
  (5 rows)
  
  --
+ -- test handling of potential equivalence clauses above outer joins
+ --
+ explain (costs off)
+ select q1, unique2, thousand, hundred
+   from int8_tbl a left join tenk1 b on q1 = unique2
+   where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
+                                       QUERY PLAN                                      
+ --------------------------------------------------------------------------------------
+  Nested Loop Left Join
+    Filter: ((COALESCE(b.thousand, 123) = a.q1) AND (a.q1 = COALESCE(b.hundred, 123)))
+    ->  Seq Scan on int8_tbl a
+    ->  Index Scan using tenk1_unique2 on tenk1 b
+          Index Cond: (a.q1 = unique2)
+ (5 rows)
+ 
+ select q1, unique2, thousand, hundred
+   from int8_tbl a left join tenk1 b on q1 = unique2
+   where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
+  q1 | unique2 | thousand | hundred 
+ ----+---------+----------+---------
+ (0 rows)
+ 
+ explain (costs off)
+ select f1, unique2, case when unique2 is null then f1 else 0 end
+   from int4_tbl a left join tenk1 b on f1 = unique2
+   where (case when unique2 is null then f1 else 0 end) = 0;
+                              QUERY PLAN                             
+ --------------------------------------------------------------------
+  Nested Loop Left Join
+    Filter: (CASE WHEN (b.unique2 IS NULL) THEN a.f1 ELSE 0 END = 0)
+    ->  Seq Scan on int4_tbl a
+    ->  Index Only Scan using tenk1_unique2 on tenk1 b
+          Index Cond: (unique2 = a.f1)
+ (5 rows)
+ 
+ select f1, unique2, case when unique2 is null then f1 else 0 end
+   from int4_tbl a left join tenk1 b on f1 = unique2
+   where (case when unique2 is null then f1 else 0 end) = 0;
+  f1 | unique2 | case 
+ ----+---------+------
+   0 |       0 |    0
+ (1 row)
+ 
+ --
  -- test join removal
  --
  begin;
diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql
index 8676e2f76103a4ade2c130e59956e41245903dca..536c4543ec9cd6fc2e3f175e9cd5fa1ecf8d7ff5 100644
*** a/src/test/regress/sql/join.sql
--- b/src/test/regress/sql/join.sql
*************** select b.unique1 from
*** 750,755 ****
--- 750,777 ----
    order by 1;
  
  --
+ -- test handling of potential equivalence clauses above outer joins
+ --
+ 
+ explain (costs off)
+ select q1, unique2, thousand, hundred
+   from int8_tbl a left join tenk1 b on q1 = unique2
+   where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
+ 
+ select q1, unique2, thousand, hundred
+   from int8_tbl a left join tenk1 b on q1 = unique2
+   where coalesce(thousand,123) = q1 and q1 = coalesce(hundred,123);
+ 
+ explain (costs off)
+ select f1, unique2, case when unique2 is null then f1 else 0 end
+   from int4_tbl a left join tenk1 b on f1 = unique2
+   where (case when unique2 is null then f1 else 0 end) = 0;
+ 
+ select f1, unique2, case when unique2 is null then f1 else 0 end
+   from int4_tbl a left join tenk1 b on f1 = unique2
+   where (case when unique2 is null then f1 else 0 end) = 0;
+ 
+ --
  -- test join removal
  --
  
