@@ -317,8 +317,7 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
317317 bool useful_predicate ;
318318 bool found_clause ;
319319 bool index_is_ordered ;
320- bool index_only_scan = false;
321- bool checked_index_only = false;
320+ bool index_only_scan ;
322321
323322 /*
324323 * Check that index supports the desired scan type(s)
@@ -436,17 +435,20 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
436435 }
437436
438437 /*
439- * 3. Generate an indexscan path if there are relevant restriction
438+ * 3. Check if an index-only scan is possible.
439+ */
440+ index_only_scan = check_index_only (rel , index );
441+
442+ /*
443+ * 4. Generate an indexscan path if there are relevant restriction
440444 * clauses in the current clauses, OR the index ordering is
441445 * potentially useful for later merging or final output ordering, OR
442- * the index has a predicate that was proven by the current clauses.
446+ * the index has a predicate that was proven by the current clauses,
447+ * OR an index-only scan is possible.
443448 */
444- if (found_clause || useful_pathkeys != NIL || useful_predicate )
449+ if (found_clause || useful_pathkeys != NIL || useful_predicate ||
450+ index_only_scan )
445451 {
446- /* First, detect whether index-only scan is possible */
447- index_only_scan = check_index_only (rel , index );
448- checked_index_only = true;
449-
450452 ipath = create_index_path (root , index ,
451453 restrictclauses ,
452454 orderbyclauses ,
@@ -460,7 +462,7 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
460462 }
461463
462464 /*
463- * 4 . If the index is ordered, a backwards scan might be interesting.
465+ * 5 . If the index is ordered, a backwards scan might be interesting.
464466 * Again, this is only interesting at top level.
465467 */
466468 if (index_is_ordered && possibly_useful_pathkeys &&
@@ -472,9 +474,6 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
472474 index_pathkeys );
473475 if (useful_pathkeys != NIL )
474476 {
475- if (!checked_index_only )
476- index_only_scan = check_index_only (rel , index );
477-
478477 ipath = create_index_path (root , index ,
479478 restrictclauses ,
480479 NIL ,
0 commit comments