*** a/src/backend/optimizer/path/indxpath.c
--- b/src/backend/optimizer/path/indxpath.c
***************
*** 1866,1871 **** check_index_only(RelOptInfo *rel, IndexOptInfo *index)
--- 1866,1872 ----
  	bool		result;
  	Bitmapset  *attrs_used = NULL;
  	Bitmapset  *index_canreturn_attrs = NULL;
+ 	Bitmapset  *index_cannotreturn_attrs = NULL;
  	ListCell   *lc;
  	int			i;
  
***************
*** 1905,1911 **** check_index_only(RelOptInfo *rel, IndexOptInfo *index)
  
  	/*
  	 * Construct a bitmapset of columns that the index can return back in an
! 	 * index-only scan.
  	 */
  	for (i = 0; i < index->ncolumns; i++)
  	{
--- 1906,1913 ----
  
  	/*
  	 * Construct a bitmapset of columns that the index can return back in an
! 	 * index-only scan. We must have a value for all occurances of the same
! 	 * attribute since it can be used for rechecking.
  	 */
  	for (i = 0; i < index->ncolumns; i++)
  	{
***************
*** 1922,1932 **** check_index_only(RelOptInfo *rel, IndexOptInfo *index)
--- 1924,1942 ----
  			index_canreturn_attrs =
  				bms_add_member(index_canreturn_attrs,
  							   attno - FirstLowInvalidHeapAttributeNumber);
+ 		else
+ 			index_cannotreturn_attrs =
+ 				bms_add_member(index_cannotreturn_attrs,
+ 							   attno - FirstLowInvalidHeapAttributeNumber);
  	}
  
+ 	index_canreturn_attrs = bms_del_members(index_canreturn_attrs,
+ 											index_cannotreturn_attrs);
+ 
  	/* Do we have all the necessary attributes? */
  	result = bms_is_subset(attrs_used, index_canreturn_attrs);
  
+ 
  	bms_free(attrs_used);
  	bms_free(index_canreturn_attrs);
  
