@@ -317,8 +317,7 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
317
317
bool useful_predicate ;
318
318
bool found_clause ;
319
319
bool index_is_ordered ;
320
- bool index_only_scan = false;
321
- bool checked_index_only = false;
320
+ bool index_only_scan ;
322
321
323
322
/*
324
323
* Check that index supports the desired scan type(s)
@@ -436,17 +435,20 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
436
435
}
437
436
438
437
/*
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
440
444
* clauses in the current clauses, OR the index ordering is
441
445
* 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.
443
448
*/
444
- if (found_clause || useful_pathkeys != NIL || useful_predicate )
449
+ if (found_clause || useful_pathkeys != NIL || useful_predicate ||
450
+ index_only_scan )
445
451
{
446
- /* First, detect whether index-only scan is possible */
447
- index_only_scan = check_index_only (rel , index );
448
- checked_index_only = true;
449
-
450
452
ipath = create_index_path (root , index ,
451
453
restrictclauses ,
452
454
orderbyclauses ,
@@ -460,7 +462,7 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
460
462
}
461
463
462
464
/*
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.
464
466
* Again, this is only interesting at top level.
465
467
*/
466
468
if (index_is_ordered && possibly_useful_pathkeys &&
@@ -472,9 +474,6 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
472
474
index_pathkeys );
473
475
if (useful_pathkeys != NIL )
474
476
{
475
- if (!checked_index_only )
476
- index_only_scan = check_index_only (rel , index );
477
-
478
477
ipath = create_index_path (root , index ,
479
478
restrictclauses ,
480
479
NIL ,
0 commit comments