diff --git a/src/backend/optimizer/path/indxpath.c b/src/backend/optimizer/path/indxpath.c
index c62e3f87724..c2687dec425 100644
--- a/src/backend/optimizer/path/indxpath.c
+++ b/src/backend/optimizer/path/indxpath.c
@@ -4038,6 +4038,7 @@ check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
 	foreach(lc, rel->indexlist)
 	{
 		IndexOptInfo *index = (IndexOptInfo *) lfirst(lc);
+		List	   *newrestrictinfo;
 		ListCell   *lcr;
 
 		if (index->indpred == NIL)
@@ -4051,8 +4052,8 @@ check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
 		if (is_target_rel)
 			continue;
 
-		/* Else compute indrestrictinfo as the non-implied quals */
-		index->indrestrictinfo = NIL;
+		/* Else compute new indrestrictinfo as the non-implied quals */
+		newrestrictinfo = NIL;
 		foreach(lcr, rel->baserestrictinfo)
 		{
 			RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcr);
@@ -4061,8 +4062,18 @@ check_index_predicates(PlannerInfo *root, RelOptInfo *rel)
 			if (contain_mutable_functions((Node *) rinfo->clause) ||
 				!predicate_implied_by(list_make1(rinfo->clause),
 									  index->indpred, false))
-				index->indrestrictinfo = lappend(index->indrestrictinfo, rinfo);
+				newrestrictinfo = lappend(newrestrictinfo, rinfo);
 		}
+
+		/*
+		 * If we excluded every qual as implied by the predicate, and the
+		 * index cannot do full-index scans, then it's better to leave
+		 * indrestrictinfo alone so that we can still build a scan on this
+		 * index.  XXX We will underestimate the cost of such an indexscan;
+		 * what can we do about that?
+		 */
+		if (!(newrestrictinfo == NIL && !index->amoptionalkey))
+			index->indrestrictinfo = newrestrictinfo;
 	}
 }
 
