+create_index_paths(Query *root, RelOptInfo *rel)
+{
+ List *indexpaths;
+ List *bitindexpaths;
+ ListCell *l;
+
+ /* Skip the whole mess if no indexes */
+ if (rel->indexlist == NIL)
+ {
+ rel->index_outer_relids = NULL;
+ return;
+ }
+
+ /*
+ * Examine join clauses to see which ones are potentially usable with
+ * indexes of this rel, and generate the set of all other relids that
+ * participate in such join clauses. We'll use this set later to
+ * recognize outer rels that are equivalent for joining purposes.
+ */
+ rel->index_outer_relids = indexable_outerrelids(rel);
+
+ /*
+ * Find all the index paths that are directly usable for this relation
+ * (ie, are valid without considering OR or JOIN clauses).
+ */
+ indexpaths = find_usable_indexes(root, rel,
+ rel->baserestrictinfo, NIL,
+ true, false, NULL);
+
+ /*
+ * We can submit them all to add_path. (This generates access paths for
+ * plain IndexScan plans.) However, for the next step we will only want
+ * the ones that have some selectivity; we must discard anything that was
+ * generated solely for ordering purposes.
+ */
+ bitindexpaths = NIL;
+ foreach(l, indexpaths)
+ {
+ IndexPath *ipath = (IndexPath *) lfirst(l);
+
+ add_path(rel, (Path *) ipath);
+
+ if (ipath->indexselectivity < 1.0 &&
+ !ScanDirectionIsBackward(ipath->indexscandir))
+ bitindexpaths = lappend(bitindexpaths, ipath);
+ }
+
+ /*
+ * Generate BitmapOrPaths for any suitable OR-clauses present in the
+ * restriction list. Add these to bitindexpaths.
+ */
+ indexpaths = generate_bitmap_or_paths(root, rel,
+ rel->baserestrictinfo, NIL,
+ false, NULL);
+ bitindexpaths = list_concat(bitindexpaths, indexpaths);
+
+ /*
+ * If we found anything usable, generate a BitmapHeapPath for the
+ * most promising combination of bitmap index paths.
+ */
+ if (bitindexpaths != NIL)
+ {
+ Path *bitmapqual;
+ BitmapHeapPath *bpath;
+
+ bitmapqual = choose_bitmap_and(root, rel, bitindexpaths);
+ bpath = create_bitmap_heap_path(root, rel, bitmapqual, false);
+ add_path(rel, (Path *) bpath);
+ }
+}
+
+
+/*----------
+ * find_usable_indexes
+ * Given a list of restriction clauses, find all the potentially usable
+ * indexes for the given relation, and return a list of IndexPaths.
+ *
+ * The caller actually supplies two lists of restriction clauses: some
+ * "current" ones and some "outer" ones. Both lists can be used freely
+ * to match keys of the index, but an index must use at least one of the
+ * "current" clauses to be considered usable. The motivation for this is
+ * examples like
+ * WHERE (x = 42) AND (... OR (y = 52 AND z = 77) OR ....)
+ * While we are considering the y/z subclause of the OR, we can use "x = 42"
+ * as one of the available index conditions; but we shouldn't match the
+ * subclause to any index on x alone, because such a Path would already have
+ * been generated at the upper level. So we could use an index on x,y,z
+ * or an index on x,y for the OR subclause, but not an index on just x.
+ *
+ * If istoplevel is true (indicating we are considering the top level of a
+ * rel's restriction clauses), we will include indexes in the result that
+ * have an interesting sort order, even if they have no matching restriction
+ * clauses.
+ *
+ * 'rel' is the relation for which we want to generate index paths
+ * 'clauses' is the current list of clauses (RestrictInfo nodes)
+ * 'outer_clauses' is the list of additional upper-level clauses
+ * 'istoplevel' is true if clauses are the rel's top-level restriction list
+ * 'isjoininner' is true if forming an inner indexscan (so some of the
+ * given clauses are join clauses)
+ * 'outer_relids' identifies the outer side of the join (pass NULL
+ * if not isjoininner)
+ *
+ * Note: check_partial_indexes() must have been run previously.
+ *----------
+ */
+static List *
+find_usable_indexes(Query *root, RelOptInfo *rel,
+ List *clauses, List *outer_clauses,
+ bool istoplevel, bool isjoininner,
+ Relids outer_relids)