]> granicus.if.org Git - postgresql/blobdiff - src/backend/optimizer/path/indxpath.c
For some reason access/tupmacs.h has been #including utils/memutils.h,
[postgresql] / src / backend / optimizer / path / indxpath.c
index 4fc86295e882ef15dd6638acec06da17afc8f700..a6a780f4778554ab717ad4a8a0f58262164652b7 100644 (file)
@@ -1,15 +1,15 @@
 /*-------------------------------------------------------------------------
  *
  * indxpath.c
- *       Routines to determine which indices are usable for scanning a
- *       given relation, and create IndexPaths accordingly.
+ *       Routines to determine which indexes are usable for scanning a
+ *       given relation, and create Paths accordingly.
  *
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.167 2004/12/31 22:00:04 pgsql Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.180 2005/05/06 17:24:54 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -37,6 +37,7 @@
 #include "utils/builtins.h"
 #include "utils/catcache.h"
 #include "utils/lsyscache.h"
+#include "utils/memutils.h"
 #include "utils/pg_locale.h"
 #include "utils/selfuncs.h"
 #include "utils/syscache.h"
 #define is_indexable_operator(clause,opclass,indexkey_on_left) \
        (indexable_operator(clause,opclass,indexkey_on_left) != InvalidOid)
 
+#define IsBooleanOpclass(opclass) \
+       ((opclass) == BOOL_BTREE_OPS_OID || (opclass) == BOOL_HASH_OPS_OID)
 
-static List *group_clauses_by_indexkey(RelOptInfo *rel, IndexOptInfo *index);
-static List *group_clauses_by_indexkey_for_join(Query *root,
-                                                                  RelOptInfo *rel, IndexOptInfo *index,
-                                                                  Relids outer_relids,
-                                                                  JoinType jointype, bool isouterjoin);
-static bool match_clause_to_indexcol(RelOptInfo *rel, IndexOptInfo *index,
+
+static List *find_usable_indexes(Query *root, RelOptInfo *rel,
+                                                                List *clauses, List *outer_clauses,
+                                                                bool istoplevel, bool isjoininner,
+                                                                Relids outer_relids);
+static Path *choose_bitmap_and(Query *root, RelOptInfo *rel, List *paths);
+static int     bitmap_path_comparator(const void *a, const void *b);
+static Cost bitmap_and_cost_est(Query *root, RelOptInfo *rel, List *paths);
+static bool match_clause_to_indexcol(IndexOptInfo *index,
                                                 int indexcol, Oid opclass,
-                                                RestrictInfo *rinfo);
-static bool match_join_clause_to_indexcol(RelOptInfo *rel, IndexOptInfo *index,
-                                                         int indexcol, Oid opclass,
-                                                         RestrictInfo *rinfo);
+                                                RestrictInfo *rinfo,
+                                                Relids outer_relids);
 static Oid indexable_operator(Expr *clause, Oid opclass,
                                   bool indexkey_on_left);
-static bool pred_test_recurse_pred(Expr *predicate, List *restrictinfo_list);
-static bool pred_test_restrict_list(Expr *predicate, List *restrictinfo_list);
-static bool pred_test_recurse_restrict(Expr *predicate, Node *clause);
+static bool pred_test_recurse(Node *clause, Node *predicate);
 static bool pred_test_simple_clause(Expr *predicate, Node *clause);
-static Relids indexable_outerrelids(RelOptInfo *rel, IndexOptInfo *index);
-static Path *make_innerjoin_index_path(Query *root,
-                                                 RelOptInfo *rel, IndexOptInfo *index,
-                                                 List *clausegroups);
-static bool match_index_to_operand(Node *operand, int indexcol,
-                                          RelOptInfo *rel, IndexOptInfo *index);
+static Relids indexable_outerrelids(RelOptInfo *rel);
+static bool list_matches_any_index(List *clauses, RelOptInfo *rel,
+                                                                  Relids outer_relids);
+static bool matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel,
+                                                         Relids outer_relids);
+static List *find_clauses_for_join(Query *root, RelOptInfo *rel,
+                                                                  Relids outer_relids, bool isouterjoin);
+static bool match_boolean_index_clause(Node *clause, int indexcol,
+                                                                          IndexOptInfo *index);
 static bool match_special_index_operator(Expr *clause, Oid opclass,
                                                         bool indexkey_on_left);
+static Expr *expand_boolean_index_clause(Node *clause, int indexcol,
+                                                                                IndexOptInfo *index);
 static List *expand_indexqual_condition(RestrictInfo *rinfo, Oid opclass);
 static List *prefix_quals(Node *leftop, Oid opclass,
                         Const *prefix, Pattern_Prefix_Status pstatus);
@@ -119,37 +126,176 @@ static Const *string_to_const(const char *str, Oid datatype);
 void
 create_index_paths(Query *root, RelOptInfo *rel)
 {
-       Relids          all_join_outerrelids = NULL;
+       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)
+{
+       List       *result = NIL;
+       List       *all_clauses = NIL;          /* not computed till needed */
        ListCell   *ilist;
 
        foreach(ilist, rel->indexlist)
        {
                IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
+               IndexPath  *ipath;
                List       *restrictclauses;
                List       *index_pathkeys;
                List       *useful_pathkeys;
                bool            index_is_ordered;
-               Relids          join_outerrelids;
 
-               /* Ignore partial indexes that do not match the query */
+               /*
+                * Ignore partial indexes that do not match the query.  If a partial
+                * index is marked predOK then we know it's OK; otherwise, if we
+                * are at top level we know it's not OK (since predOK is exactly
+                * whether its predicate could be proven from the toplevel clauses).
+                * Otherwise, we have to test whether the added clauses are
+                * sufficient to imply the predicate.  If so, we could use
+                * the index in the current context.
+                */
                if (index->indpred != NIL && !index->predOK)
-                       continue;
+               {
+                       if (istoplevel)
+                               continue;               /* no point in trying to prove it */
+
+                       /* Form all_clauses if not done already */
+                       if (all_clauses == NIL)
+                               all_clauses = list_concat(list_copy(clauses),
+                                                                                 outer_clauses);
+
+                       if (!pred_test(index->indpred, all_clauses) ||
+                               pred_test(index->indpred, outer_clauses))
+                               continue;
+               }
 
                /*
-                * 1. Match the index against non-OR restriction clauses. (OR
-                * clauses will be considered later by orindxpath.c.)
+                * 1. Match the index against the available restriction clauses.
                 */
-               restrictclauses = group_clauses_by_indexkey(rel, index);
+               restrictclauses = group_clauses_by_indexkey(index,
+                                                                                                       clauses,
+                                                                                                       outer_clauses,
+                                                                                                       outer_relids);
 
                /*
                 * 2. Compute pathkeys describing index's ordering, if any, then
-                * see how many of them are actually useful for this query.
+                * see how many of them are actually useful for this query.  This
+                * is not relevant unless we are at top level.
                 */
-               index_pathkeys = build_index_pathkeys(root, rel, index,
-                                                                                         ForwardScanDirection);
-               index_is_ordered = (index_pathkeys != NIL);
-               useful_pathkeys = truncate_useless_pathkeys(root, rel,
-                                                                                                       index_pathkeys);
+               index_is_ordered = OidIsValid(index->ordering[0]);
+               if (istoplevel && index_is_ordered && !isjoininner)
+               {
+                       index_pathkeys = build_index_pathkeys(root, index,
+                                                                                                 ForwardScanDirection);
+                       useful_pathkeys = truncate_useless_pathkeys(root, rel,
+                                                                                                               index_pathkeys);
+               }
+               else
+                       useful_pathkeys = NIL;
 
                /*
                 * 3. Generate an indexscan path if there are relevant restriction
@@ -159,338 +305,395 @@ create_index_paths(Query *root, RelOptInfo *rel)
                 * If there is a predicate, consider it anyway since the index
                 * predicate has already been found to match the query.  The
                 * selectivity of the predicate might alone make the index useful.
+                *
+                * Note: not all index AMs support scans with no restriction clauses.
+                * We assume here that the AM does so if and only if it supports
+                * ordered scans.  (It would probably be better if there were a
+                * specific flag for this in pg_am, but there's not.)
                 */
                if (restrictclauses != NIL ||
                        useful_pathkeys != NIL ||
-                       index->indpred != NIL)
-                       add_path(rel, (Path *)
-                                        create_index_path(root, rel, index,
-                                                                          restrictclauses,
-                                                                          useful_pathkeys,
-                                                                          index_is_ordered ?
-                                                                          ForwardScanDirection :
-                                                                          NoMovementScanDirection));
+                       (index->indpred != NIL && index_is_ordered))
+               {
+                       ipath = create_index_path(root, index,
+                                                                         restrictclauses,
+                                                                         useful_pathkeys,
+                                                                         index_is_ordered ?
+                                                                         ForwardScanDirection :
+                                                                         NoMovementScanDirection,
+                                                                         isjoininner);
+                       result = lappend(result, ipath);
+               }
 
                /*
                 * 4. If the index is ordered, a backwards scan might be
                 * interesting. Currently this is only possible for a DESC query
                 * result ordering.
                 */
-               if (index_is_ordered)
+               if (istoplevel && index_is_ordered && !isjoininner)
                {
-                       index_pathkeys = build_index_pathkeys(root, rel, index,
+                       index_pathkeys = build_index_pathkeys(root, index,
                                                                                                  BackwardScanDirection);
                        useful_pathkeys = truncate_useless_pathkeys(root, rel,
                                                                                                                index_pathkeys);
                        if (useful_pathkeys != NIL)
-                               add_path(rel, (Path *)
-                                                create_index_path(root, rel, index,
-                                                                                  restrictclauses,
-                                                                                  useful_pathkeys,
-                                                                                  BackwardScanDirection));
+                       {
+                               ipath = create_index_path(root, index,
+                                                                                 restrictclauses,
+                                                                                 useful_pathkeys,
+                                                                                 BackwardScanDirection,
+                                                                                 false);
+                               result = lappend(result, ipath);
+                       }
                }
-
-               /*
-                * 5. Examine join clauses to see which ones are potentially
-                * usable with this index, 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. We compute both per-index and
-                * overall-for-relation sets.
-                */
-               join_outerrelids = indexable_outerrelids(rel, index);
-               index->outer_relids = join_outerrelids;
-               all_join_outerrelids = bms_add_members(all_join_outerrelids,
-                                                                                          join_outerrelids);
        }
 
-       rel->index_outer_relids = all_join_outerrelids;
+       return result;
 }
 
 
-/****************************************************************************
- *                             ----  ROUTINES TO CHECK RESTRICTIONS  ----
- ****************************************************************************/
-
-
 /*
- * group_clauses_by_indexkey
- *       Find restriction clauses that can be used with an index.
- *
- * 'rel' is the node of the relation itself.
- * 'index' is a index on 'rel'.
+ * generate_bitmap_or_paths
+ *             Look through the list of clauses to find OR clauses, and generate
+ *             a BitmapOrPath for each one we can handle that way.  Return a list
+ *             of the generated BitmapOrPaths.
  *
- * Returns a list of sublists of RestrictInfo nodes for clauses that can be
- * used with this index.  Each sublist contains clauses that can be used
- * with one index key (in no particular order); the top list is ordered by
- * index key.  (This is depended on by expand_indexqual_conditions().)
- *
- * Note that in a multi-key index, we stop if we find a key that cannot be
- * used with any clause.  For example, given an index on (A,B,C), we might
- * return ((C1 C2) (C3 C4)) if we find that clauses C1 and C2 use column A,
- * clauses C3 and C4 use column B, and no clauses use column C.  But if
- * no clauses match B we will return ((C1 C2)), whether or not there are
- * clauses matching column C, because the executor couldn't use them anyway.
- * Therefore, there are no empty sublists in the result.
+ * outer_clauses is a list of additional clauses that can be assumed true
+ * for the purpose of generating indexquals, but are not to be searched for
+ * ORs.  (See find_usable_indexes() for motivation.)
  */
-static List *
-group_clauses_by_indexkey(RelOptInfo *rel, IndexOptInfo *index)
+List *
+generate_bitmap_or_paths(Query *root, RelOptInfo *rel,
+                                                List *clauses, List *outer_clauses,
+                                                bool isjoininner,
+                                                Relids outer_relids)
 {
-       List       *clausegroup_list = NIL;
-       List       *restrictinfo_list = rel->baserestrictinfo;
-       int                     indexcol = 0;
-       Oid                *classes = index->classlist;
+       List       *result = NIL;
+       List       *all_clauses;
+       ListCell   *l;
 
-       if (restrictinfo_list == NIL)
-               return NIL;
+       /*
+        * We can use both the current and outer clauses as context for
+        * find_usable_indexes
+        */
+       all_clauses = list_concat(list_copy(clauses), outer_clauses);
 
-       do
+       foreach(l, clauses)
        {
-               Oid                     curClass = classes[0];
-               List       *clausegroup = NIL;
-               ListCell   *l;
+               RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
+               List   *pathlist;
+               Path   *bitmapqual;
+               ListCell *j;
+
+               Assert(IsA(rinfo, RestrictInfo));
+               /* Ignore RestrictInfos that aren't ORs */
+               if (!restriction_is_or_clause(rinfo))
+                       continue;
 
-               foreach(l, restrictinfo_list)
+               /*
+                * We must be able to match at least one index to each of the arms
+                * of the OR, else we can't use it.
+                */
+               pathlist = NIL;
+               foreach(j, ((BoolExpr *) rinfo->orclause)->args)
                {
-                       RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
+                       Node   *orarg = (Node *) lfirst(j);
+                       List   *indlist;
 
-                       if (match_clause_to_indexcol(rel,
-                                                                                index,
-                                                                                indexcol,
-                                                                                curClass,
-                                                                                rinfo))
-                               clausegroup = lappend(clausegroup, rinfo);
+                       /* OR arguments should be ANDs or sub-RestrictInfos */
+                       if (and_clause(orarg))
+                       {
+                               List   *andargs = ((BoolExpr *) orarg)->args;
+
+                               indlist = find_usable_indexes(root, rel,
+                                                                                         andargs,
+                                                                                         all_clauses,
+                                                                                         false,
+                                                                                         isjoininner,
+                                                                                         outer_relids);
+                               /* Recurse in case there are sub-ORs */
+                               indlist = list_concat(indlist,
+                                                                         generate_bitmap_or_paths(root, rel,
+                                                                                                                          andargs,
+                                                                                                                          all_clauses,
+                                                                                                                          isjoininner,
+                                                                                                                          outer_relids));
+                       }
+                       else
+                       {
+                               Assert(IsA(orarg, RestrictInfo));
+                               Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
+                               indlist = find_usable_indexes(root, rel,
+                                                                                         list_make1(orarg),
+                                                                                         all_clauses,
+                                                                                         false,
+                                                                                         isjoininner,
+                                                                                         outer_relids);
+                       }
+                       /*
+                        * If nothing matched this arm, we can't do anything
+                        * with this OR clause.
+                        */
+                       if (indlist == NIL)
+                       {
+                               pathlist = NIL;
+                               break;
+                       }
+                       /*
+                        * OK, pick the most promising AND combination,
+                        * and add it to pathlist.
+                        */
+                       bitmapqual = choose_bitmap_and(root, rel, indlist);
+                       pathlist = lappend(pathlist, bitmapqual);
                }
-
                /*
-                * If no clauses match this key, we're done; we don't want to look
-                * at keys to its right.
+                * If we have a match for every arm, then turn them
+                * into a BitmapOrPath, and add to result list.
                 */
-               if (clausegroup == NIL)
-                       break;
-
-               clausegroup_list = lappend(clausegroup_list, clausegroup);
-
-               indexcol++;
-               classes++;
-
-       } while (!DoneMatchingIndexKeys(classes));
+               if (pathlist != NIL)
+               {
+                       bitmapqual = (Path *) create_bitmap_or_path(root, rel, pathlist);
+                       result = lappend(result, bitmapqual);
+               }
+       }
 
-       return clausegroup_list;
+       return result;
 }
 
+
 /*
- * group_clauses_by_indexkey_for_join
- *       Generate a list of sublists of clauses that can be used with an index
- *       to scan the inner side of a nestloop join.
- *
- * This is much like group_clauses_by_indexkey(), but we consider both
- * join and restriction clauses.  Any joinclause that uses only otherrels
- * in the specified outer_relids is fair game. But there must be at least
- * one such joinclause in the final list, otherwise we return NIL indicating
- * that this index isn't interesting as an inner indexscan.  (A scan using
- * only restriction clauses shouldn't be created here, because a regular Path
- * will already have been generated for it.)
+ * choose_bitmap_and
+ *             Given a nonempty list of bitmap paths, AND them into one path.
+ *
+ * This is a nontrivial decision since we can legally use any subset of the
+ * given path set.  We want to choose a good tradeoff between selectivity
+ * and cost of computing the bitmap.
+ *
+ * The result is either a single one of the inputs, or a BitmapAndPath
+ * combining multiple inputs.
  */
-static List *
-group_clauses_by_indexkey_for_join(Query *root,
-                                                                  RelOptInfo *rel, IndexOptInfo *index,
-                                                                  Relids outer_relids,
-                                                                  JoinType jointype, bool isouterjoin)
+static Path *
+choose_bitmap_and(Query *root, RelOptInfo *rel, List *paths)
 {
-       List       *clausegroup_list = NIL;
-       bool            jfound = false;
-       int                     indexcol = 0;
-       Oid                *classes = index->classlist;
+       int                     npaths = list_length(paths);
+       Path      **patharray;
+       Cost            costsofar;
+       List       *qualsofar;
+       ListCell   *lastcell;
+       int                     i;
+       ListCell   *l;
 
-       do
-       {
-               Oid                     curClass = classes[0];
-               List       *clausegroup = NIL;
-               int                     numsources;
-               ListCell   *l;
+       Assert(npaths > 0);                                                     /* else caller error */
+       if (npaths == 1)
+               return (Path *) linitial(paths);                /* easy case */
 
-               /*
-                * We can always use plain restriction clauses for the rel.  We
-                * scan these first because we want them first in the clausegroup
-                * list for the convenience of remove_redundant_join_clauses,
-                * which can never remove non-join clauses and hence won't be able
-                * to get rid of a non-join clause if it appears after a join
-                * clause it is redundant with.
-                */
-               foreach(l, rel->baserestrictinfo)
-               {
-                       RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
+       /*
+        * In theory we should consider every nonempty subset of the given paths.
+        * In practice that seems like overkill, given the crude nature of the
+        * estimates, not to mention the possible effects of higher-level AND and
+        * OR clauses.  As a compromise, we sort the paths by selectivity.
+        * We always take the first, and sequentially add on paths that result
+        * in a lower estimated cost.
+        *
+        * We also make some effort to detect directly redundant input paths,
+        * as can happen if there are multiple possibly usable indexes.  For
+        * this we look only at plain IndexPath inputs, not at sub-OR clauses.
+        * And we consider an index redundant if all its index conditions were
+        * already used by earlier indexes.  (We could use pred_test() to have
+        * a more intelligent, but much more expensive, check --- but in most
+        * cases simple pointer equality should suffice, since after all the
+        * index conditions are all coming from the same RestrictInfo lists.)
+        *
+        * XXX is there any risk of throwing away a useful partial index here
+        * because we don't explicitly look at indpred?  At least in simple
+        * cases, the partial index will sort before competing non-partial
+        * indexes and so it makes the right choice, but perhaps we need to
+        * work harder.
+        */
 
-                       /* Can't use pushed-down clauses in outer join */
-                       if (isouterjoin && rinfo->is_pushed_down)
-                               continue;
+       /* Convert list to array so we can apply qsort */
+       patharray = (Path **) palloc(npaths * sizeof(Path *));
+       i = 0;
+       foreach(l, paths)
+       {
+               patharray[i++] = (Path *) lfirst(l);
+       }
+       qsort(patharray, npaths, sizeof(Path *), bitmap_path_comparator);
 
-                       if (match_clause_to_indexcol(rel,
-                                                                                index,
-                                                                                indexcol,
-                                                                                curClass,
-                                                                                rinfo))
-                               clausegroup = lappend(clausegroup, rinfo);
-               }
+       paths = list_make1(patharray[0]);
+       costsofar = bitmap_and_cost_est(root, rel, paths);
+       if (IsA(patharray[0], IndexPath))
+               qualsofar = list_copy(((IndexPath *) patharray[0])->indexclauses);
+       else
+               qualsofar = NIL;
+       lastcell = list_head(paths);            /* for quick deletions */
 
-               /* found anything in base restrict list? */
-               numsources = (clausegroup != NIL) ? 1 : 0;
+       for (i = 1; i < npaths; i++)
+       {
+               Path   *newpath = patharray[i];
+               List   *newqual = NIL;
+               Cost    newcost;
 
-               /* Look for joinclauses that are usable with given outer_relids */
-               foreach(l, rel->joininfo)
+               if (IsA(newpath, IndexPath))
                {
-                       JoinInfo   *joininfo = (JoinInfo *) lfirst(l);
-                       bool            jfoundhere = false;
-                       ListCell   *j;
-
-                       if (!bms_is_subset(joininfo->unjoined_relids, outer_relids))
-                               continue;
-
-                       foreach(j, joininfo->jinfo_restrictinfo)
-                       {
-                               RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
-
-                               /* Can't use pushed-down clauses in outer join */
-                               if (isouterjoin && rinfo->is_pushed_down)
-                                       continue;
-
-                               if (match_join_clause_to_indexcol(rel,
-                                                                                                 index,
-                                                                                                 indexcol,
-                                                                                                 curClass,
-                                                                                                 rinfo))
-                               {
-                                       clausegroup = lappend(clausegroup, rinfo);
-                                       if (!jfoundhere)
-                                       {
-                                               jfoundhere = true;
-                                               jfound = true;
-                                               numsources++;
-                                       }
-                               }
-                       }
+                       newqual = ((IndexPath *) newpath)->indexclauses;
+                       if (list_difference_ptr(newqual, qualsofar) == NIL)
+                               continue;               /* redundant */
                }
 
-               /*
-                * If we found clauses in more than one list, we may now have
-                * clauses that are known redundant.  Get rid of 'em.
-                */
-               if (numsources > 1)
+               paths = lappend(paths, newpath);
+               newcost = bitmap_and_cost_est(root, rel, paths);
+               if (newcost < costsofar)
                {
-                       clausegroup = remove_redundant_join_clauses(root,
-                                                                                                               clausegroup,
-                                                                                                               jointype);
+                       costsofar = newcost;
+                       if (newqual)
+                               qualsofar = list_concat(qualsofar, list_copy(newqual));
+                       lastcell = lnext(lastcell);
                }
+               else
+               {
+                       paths = list_delete_cell(paths, lnext(lastcell), lastcell);
+               }
+               Assert(lnext(lastcell) == NULL);
+       }
 
-               /*
-                * If no clauses match this key, we're done; we don't want to look
-                * at keys to its right.
-                */
-               if (clausegroup == NIL)
-                       break;
+       if (list_length(paths) == 1)
+               return (Path *) linitial(paths);                /* no need for AND */
+       return (Path *) create_bitmap_and_path(root, rel, paths);
+}
 
-               clausegroup_list = lappend(clausegroup_list, clausegroup);
+/* qsort comparator to sort in increasing selectivity order */
+static int
+bitmap_path_comparator(const void *a, const void *b)
+{
+       Path       *pa = *(Path * const *) a;
+       Path       *pb = *(Path * const *) b;
+       Cost            acost;
+       Cost            bcost;
+       Selectivity     aselec;
+       Selectivity     bselec;
+
+       cost_bitmap_tree_node(pa, &acost, &aselec);
+       cost_bitmap_tree_node(pb, &bcost, &bselec);
+
+       if (aselec < bselec)
+               return -1;
+       if (aselec > bselec)
+               return 1;
+       /* if identical selectivity, sort by cost */
+       if (acost < bcost)
+               return -1;
+       if (acost > bcost)
+               return 1;
+       return 0;
+}
 
-               indexcol++;
-               classes++;
+/*
+ * Estimate the cost of actually executing a BitmapAnd with the given
+ * inputs.
+ */
+static Cost
+bitmap_and_cost_est(Query *root, RelOptInfo *rel, List *paths)
+{
+       BitmapAndPath apath;
+       Path            bpath;
 
-       } while (!DoneMatchingIndexKeys(classes));
+       /* Set up a dummy BitmapAndPath */
+       apath.path.type = T_BitmapAndPath;
+       apath.path.parent = rel;
+       apath.bitmapquals = paths;
+       cost_bitmap_and_node(&apath, root);
 
-       /* if no join clause was matched then forget it, per comments above */
-       if (!jfound)
-               return NIL;
+       /* Now we can do cost_bitmap_heap_scan */
+       cost_bitmap_heap_scan(&bpath, root, rel, (Path *) &apath, false);
 
-       return clausegroup_list;
+       return bpath.total_cost;
 }
 
 
+/****************************************************************************
+ *                             ----  ROUTINES TO CHECK RESTRICTIONS  ----
+ ****************************************************************************/
+
+
 /*
- * group_clauses_by_indexkey_for_or
- *       Generate a list of sublists of clauses that can be used with an index
- *       to find rows matching an OR subclause.
- *
- * This is essentially just like group_clauses_by_indexkey() except that
- * we can use the given clause (or any AND subclauses of it) as well as
- * top-level restriction clauses of the relation.  Furthermore, we demand
- * that at least one such use be made, otherwise we fail and return NIL.
- * (Any path we made without such a use would be redundant with non-OR
- * indexscans. Compare also group_clauses_by_indexkey_for_join.)
- *
- * XXX When we generate an indexqual list that uses both the OR subclause
- * and top-level restriction clauses, we end up with a slightly inefficient
- * plan because create_indexscan_plan is not very bright about figuring out
- * which restriction clauses are implied by the generated indexqual condition.
- * Currently we'll end up rechecking both the OR clause and the top-level
- * restriction clause as qpquals.  FIXME someday.
+ * group_clauses_by_indexkey
+ *       Find restriction clauses that can be used with an index.
+ *
+ * As explained in the comments for find_usable_indexes(), we can use
+ * clauses from either of the given lists, but the result is required to
+ * use at least one clause from the "current clauses" list.  We return
+ * NIL if we don't find any such clause.
+ *
+ * outer_relids determines what Vars will be allowed on the other side
+ * of a possible index qual; see match_clause_to_indexcol().
+ *
+ * Returns a list of sublists of RestrictInfo nodes for clauses that can be
+ * used with this index.  Each sublist contains clauses that can be used
+ * with one index key (in no particular order); the top list is ordered by
+ * index key.  (This is depended on by expand_indexqual_conditions().)
+ *
+ * Note that in a multi-key index, we stop if we find a key that cannot be
+ * used with any clause.  For example, given an index on (A,B,C), we might
+ * return ((C1 C2) (C3 C4)) if we find that clauses C1 and C2 use column A,
+ * clauses C3 and C4 use column B, and no clauses use column C.  But if
+ * no clauses match B we will return ((C1 C2)), whether or not there are
+ * clauses matching column C, because the executor couldn't use them anyway.
+ * Therefore, there are no empty sublists in the result.
  */
 List *
-group_clauses_by_indexkey_for_or(RelOptInfo *rel,
-                                                                IndexOptInfo *index,
-                                                                Expr *orsubclause)
+group_clauses_by_indexkey(IndexOptInfo *index,
+                                                 List *clauses, List *outer_clauses,
+                                                 Relids outer_relids)
 {
        List       *clausegroup_list = NIL;
-       bool            matched = false;
+       bool            found_clause = false;
        int                     indexcol = 0;
        Oid                *classes = index->classlist;
 
+       if (clauses == NIL)
+               return NIL;                             /* cannot succeed */
+
        do
        {
                Oid                     curClass = classes[0];
                List       *clausegroup = NIL;
-               ListCell   *item;
+               ListCell   *l;
 
-               /* Try to match the OR subclause to the index key */
-               if (IsA(orsubclause, RestrictInfo))
-               {
-                       if (match_clause_to_indexcol(rel, index,
-                                                                                indexcol, curClass,
-                                                                                (RestrictInfo *) orsubclause))
-                       {
-                               clausegroup = lappend(clausegroup, orsubclause);
-                               matched = true;
-                       }
-               }
-               else if (and_clause((Node *) orsubclause))
+               /* check the current clauses */
+               foreach(l, clauses)
                {
-                       foreach(item, ((BoolExpr *) orsubclause)->args)
-                       {
-                               RestrictInfo *subsubclause = (RestrictInfo *) lfirst(item);
+                       RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
-                               if (IsA(subsubclause, RestrictInfo) &&
-                                       match_clause_to_indexcol(rel, index,
-                                                                                        indexcol, curClass,
-                                                                                        subsubclause))
-                               {
-                                       clausegroup = lappend(clausegroup, subsubclause);
-                                       matched = true;
-                               }
+                       Assert(IsA(rinfo, RestrictInfo));
+                       if (match_clause_to_indexcol(index,
+                                                                                indexcol,
+                                                                                curClass,
+                                                                                rinfo,
+                                                                                outer_relids))
+                       {
+                               clausegroup = lappend(clausegroup, rinfo);
+                               found_clause = true;
                        }
                }
 
-               /*
-                * If we found no clauses for this indexkey in the OR subclause
-                * itself, try looking in the rel's top-level restriction list.
-                *
-                * XXX should we always search the top-level list?      Slower but could
-                * sometimes yield a better plan.
-                */
-               if (clausegroup == NIL)
+               /* check the outer clauses */
+               foreach(l, outer_clauses)
                {
-                       foreach(item, rel->baserestrictinfo)
-                       {
-                               RestrictInfo *rinfo = (RestrictInfo *) lfirst(item);
+                       RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
-                               if (match_clause_to_indexcol(rel, index,
-                                                                                        indexcol, curClass,
-                                                                                        rinfo))
-                                       clausegroup = lappend(clausegroup, rinfo);
-                       }
+                       Assert(IsA(rinfo, RestrictInfo));
+                       if (match_clause_to_indexcol(index,
+                                                                                indexcol,
+                                                                                curClass,
+                                                                                rinfo,
+                                                                                outer_relids))
+                               clausegroup = lappend(clausegroup, rinfo);
                }
 
                /*
-                * If still no clauses match this key, we're done; we don't want
-                * to look at keys to its right.
+                * If no clauses match this key, we're done; we don't want to look
+                * at keys to its right.
                 */
                if (clausegroup == NIL)
                        break;
@@ -499,10 +702,10 @@ group_clauses_by_indexkey_for_or(RelOptInfo *rel,
 
                indexcol++;
                classes++;
+
        } while (!DoneMatchingIndexKeys(classes));
 
-       /* if OR clause was not used then forget it, per comments above */
-       if (!matched)
+       if (!found_clause)
                return NIL;
 
        return clausegroup_list;
@@ -513,7 +716,7 @@ group_clauses_by_indexkey_for_or(RelOptInfo *rel,
  * match_clause_to_indexcol()
  *       Determines whether a restriction clause matches a column of an index.
  *
- *       To match, the clause:
+ *       To match a normal index, the clause:
  *
  *       (1)  must be in the form (indexkey op const) or (const op indexkey);
  *                and
@@ -521,14 +724,29 @@ group_clauses_by_indexkey_for_or(RelOptInfo *rel,
  *                operator for this column, or is a "special" operator as recognized
  *                by match_special_index_operator().
  *
+ *       Our definition of "const" is pretty liberal: we allow Vars belonging
+ *       to the caller-specified outer_relids relations (which had better not
+ *       include the relation whose index is being tested).  outer_relids should
+ *       be NULL when checking simple restriction clauses, and the outer side
+ *       of the join when building a join inner scan.  Other than that, the
+ *       only thing we don't like is volatile functions.
+ *
+ *       Note: in most cases we already know that the clause as a whole uses
+ *       vars from the interesting set of relations.  The reason for the
+ *       outer_relids test is to reject clauses like (a.f1 OP (b.f2 OP a.f3));
+ *       that's not processable by an indexscan nestloop join on A, whereas
+ *       (a.f1 OP (b.f2 OP c.f3)) is.
+ *
  *       Presently, the executor can only deal with indexquals that have the
  *       indexkey on the left, so we can only use clauses that have the indexkey
  *       on the right if we can commute the clause to put the key on the left.
  *       We do not actually do the commuting here, but we check whether a
  *       suitable commutator operator is available.
  *
- * 'rel' is the relation of interest.
- * 'index' is an index on 'rel'.
+ *       For boolean indexes, it is also possible to match the clause directly
+ *       to the indexkey; or perhaps the clause is (NOT indexkey).
+ *
+ * 'index' is the index of interest.
  * 'indexcol' is a column number of 'index' (counting from 0).
  * 'opclass' is the corresponding operator class.
  * 'rinfo' is the clause to be tested (as a RestrictInfo node).
@@ -539,17 +757,24 @@ group_clauses_by_indexkey_for_or(RelOptInfo *rel,
  * responsibility of higher-level routines to cope with those.
  */
 static bool
-match_clause_to_indexcol(RelOptInfo *rel,
-                                                IndexOptInfo *index,
+match_clause_to_indexcol(IndexOptInfo *index,
                                                 int indexcol,
                                                 Oid opclass,
-                                                RestrictInfo *rinfo)
+                                                RestrictInfo *rinfo,
+                                                Relids outer_relids)
 {
        Expr       *clause = rinfo->clause;
        Node       *leftop,
                           *rightop;
 
-       /* Clause must be a binary opclause. */
+       /* First check for boolean-index cases. */
+       if (IsBooleanOpclass(opclass))
+       {
+               if (match_boolean_index_clause((Node *) clause, indexcol, index))
+                       return true;
+       }
+
+       /* Else clause must be a binary opclause. */
        if (!is_opclause(clause))
                return false;
        leftop = get_leftop(clause);
@@ -559,11 +784,11 @@ match_clause_to_indexcol(RelOptInfo *rel,
 
        /*
         * Check for clauses of the form: (indexkey operator constant) or
-        * (constant operator indexkey). Anything that is a "pseudo constant"
-        * expression will do.
+        * (constant operator indexkey).  See above notes about const-ness.
         */
-       if (match_index_to_operand(leftop, indexcol, rel, index) &&
-               is_pseudo_constant_clause_relids(rightop, rinfo->right_relids))
+       if (match_index_to_operand(leftop, indexcol, index) &&
+               bms_is_subset(rinfo->right_relids, outer_relids) &&
+               !contain_volatile_functions(rightop))
        {
                if (is_indexable_operator(clause, opclass, true))
                        return true;
@@ -577,8 +802,9 @@ match_clause_to_indexcol(RelOptInfo *rel,
                return false;
        }
 
-       if (match_index_to_operand(rightop, indexcol, rel, index) &&
-               is_pseudo_constant_clause_relids(leftop, rinfo->left_relids))
+       if (match_index_to_operand(rightop, indexcol, index) &&
+               bms_is_subset(rinfo->left_relids, outer_relids) &&
+               !contain_volatile_functions(leftop))
        {
                if (is_indexable_operator(clause, opclass, false))
                        return true;
@@ -595,90 +821,6 @@ match_clause_to_indexcol(RelOptInfo *rel,
        return false;
 }
 
-/*
- * match_join_clause_to_indexcol()
- *       Determines whether a join clause matches a column of an index.
- *
- *       To match, the clause:
- *
- *       (1)  must be in the form (indexkey op others) or (others op indexkey),
- *                where others is an expression involving only vars of the other
- *                relation(s); and
- *       (2)  must contain an operator which is in the same class as the index
- *                operator for this column, or is a "special" operator as recognized
- *                by match_special_index_operator().
- *
- *       As above, we must be able to commute the clause to put the indexkey
- *       on the left.
- *
- *       Note that we already know that the clause as a whole uses vars from
- *       the interesting set of relations.  But we need to defend against
- *       expressions like (a.f1 OP (b.f2 OP a.f3)); that's not processable by
- *       an indexscan nestloop join, whereas (a.f1 OP (b.f2 OP c.f3)) is.
- *
- * 'rel' is the relation of interest.
- * 'index' is an index on 'rel'.
- * 'indexcol' is a column number of 'index' (counting from 0).
- * 'opclass' is the corresponding operator class.
- * 'rinfo' is the clause to be tested (as a RestrictInfo node).
- *
- * Returns true if the clause can be used with this index key.
- *
- * NOTE:  returns false if clause is an OR or AND clause; it is the
- * responsibility of higher-level routines to cope with those.
- */
-static bool
-match_join_clause_to_indexcol(RelOptInfo *rel,
-                                                         IndexOptInfo *index,
-                                                         int indexcol,
-                                                         Oid opclass,
-                                                         RestrictInfo *rinfo)
-{
-       Expr       *clause = rinfo->clause;
-       Node       *leftop,
-                          *rightop;
-
-       /* Clause must be a binary opclause. */
-       if (!is_opclause(clause))
-               return false;
-       leftop = get_leftop(clause);
-       rightop = get_rightop(clause);
-       if (!leftop || !rightop)
-               return false;
-
-       /*
-        * Check for an indexqual that could be handled by a nestloop join. We
-        * need the index key to be compared against an expression that uses
-        * none of the indexed relation's vars and contains no volatile
-        * functions.
-        */
-       if (match_index_to_operand(leftop, indexcol, rel, index))
-       {
-               Relids          othervarnos = rinfo->right_relids;
-               bool            isIndexable;
-
-               isIndexable =
-                       !bms_overlap(rel->relids, othervarnos) &&
-                       !contain_volatile_functions(rightop) &&
-                       is_indexable_operator(clause, opclass, true);
-               return isIndexable;
-       }
-
-       if (match_index_to_operand(rightop, indexcol, rel, index))
-       {
-               Relids          othervarnos = rinfo->left_relids;
-               bool            isIndexable;
-
-               isIndexable =
-                       !bms_overlap(rel->relids, othervarnos) &&
-                       !contain_volatile_functions(leftop) &&
-                       is_indexable_operator(clause, opclass, false);
-               return isIndexable;
-       }
-
-       return false;
-}
-
 /*
  * indexable_operator
  *       Does a binary opclause contain an operator matching the index opclass?
@@ -749,16 +891,17 @@ check_partial_indexes(Query *root, RelOptInfo *rel)
  *       Recursively checks whether the clauses in restrictinfo_list imply
  *       that the given predicate is true.
  *
- *       This routine (together with the routines it calls) first breaks down
- *       the predicate to its constituent AND/OR elements, then similarly
- *       breaks down the restriction clauses to AND/OR elements in an effort
- *       to prove that each predicate element is implied.  The top-level
- *       List structure of each list corresponds to an AND list.
+ *       The top-level List structure of each list corresponds to an AND list.
+ *       We assume that eval_const_expressions() has been applied and so there
+ *       are no un-flattened ANDs or ORs (e.g., no AND immediately within an AND,
+ *       including AND just below the top-level List structure).
+ *       If this is not true we might fail to prove an implication that is
+ *       valid, but no worse consequences will ensue.
  */
 bool
 pred_test(List *predicate_list, List *restrictinfo_list)
 {
-       ListCell   *pred;
+       ListCell   *item;
 
        /*
         * Note: if Postgres tried to optimize queries by forming equivalence
@@ -779,132 +922,189 @@ pred_test(List *predicate_list, List *restrictinfo_list)
                return false;                   /* no restriction clauses: the test must
                                                                 * fail */
 
-       foreach(pred, predicate_list)
+       /*
+        * In all cases where the predicate is an AND-clause, pred_test_recurse()
+        * will prefer to iterate over the predicate's components.  So we can
+        * just do that to start with here, and eliminate the need for
+        * pred_test_recurse() to handle a bare List on the predicate side.
+        *
+        * Logic is: restriction must imply each of the AND'ed predicate items.
+        */
+       foreach(item, predicate_list)
        {
-               /*
-                * if any clause is not implied, the whole predicate is not
-                * implied.
-                */
-               if (!pred_test_recurse_pred(lfirst(pred), restrictinfo_list))
+               if (!pred_test_recurse((Node *) restrictinfo_list, lfirst(item)))
                        return false;
        }
        return true;
 }
 
 
-/*
- * pred_test_recurse_pred
- *       Does the "predicate inclusion test" for one conjunct of a predicate
- *       expression.  Here we recursively deal with the possibility that the
- *       predicate conjunct is itself an AND or OR structure.
- */
-static bool
-pred_test_recurse_pred(Expr *predicate, List *restrictinfo_list)
-{
-       List       *items;
-       ListCell   *item;
-
-       Assert(predicate != NULL);
-       if (or_clause((Node *) predicate))
-       {
-               items = ((BoolExpr *) predicate)->args;
-               foreach(item, items)
-               {
-                       /* if any item is implied, the whole predicate is implied */
-                       if (pred_test_recurse_pred(lfirst(item), restrictinfo_list))
-                               return true;
-               }
-               return false;
-       }
-       else if (and_clause((Node *) predicate))
-       {
-               items = ((BoolExpr *) predicate)->args;
-               foreach(item, items)
-               {
-                       /*
-                        * if any item is not implied, the whole predicate is not
-                        * implied
-                        */
-                       if (!pred_test_recurse_pred(lfirst(item), restrictinfo_list))
-                               return false;
-               }
-               return true;
-       }
-       else
-               return pred_test_restrict_list(predicate, restrictinfo_list);
-}
-
-
-/*
- * pred_test_restrict_list
- *       Does the "predicate inclusion test" for one element of a predicate
- *       expression.  Here we take care of the AND semantics of the top-level
- *       restrictinfo list.
- */
-static bool
-pred_test_restrict_list(Expr *predicate, List *restrictinfo_list)
-{
-       ListCell   *item;
-
-       foreach(item, restrictinfo_list)
-       {
-               /* if any clause implies the predicate, return true */
-               if (pred_test_recurse_restrict(predicate,
-                                                                          (Node *) lfirst(item)))
-                       return true;
-       }
-       return false;
-}
-
-
-/*
- * pred_test_recurse_restrict
- *       Does the "predicate inclusion test" for one element of a predicate
- *       expression.  Here we recursively deal with the possibility that the
- *       restriction-list element is itself an AND or OR structure; also,
- *       we strip off RestrictInfo nodes to find bare predicate expressions.
+/*----------
+ * pred_test_recurse
+ *       Does the "predicate inclusion test" for non-NULL restriction and
+ *       predicate clauses.
+ *
+ * The logic followed here is ("=>" means "implies"):
+ *     atom A => atom B iff:                   pred_test_simple_clause says so
+ *     atom A => AND-expr B iff:               A => each of B's components
+ *     atom A => OR-expr B iff:                A => any of B's components
+ *     AND-expr A => atom B iff:               any of A's components => B
+ *     AND-expr A => AND-expr B iff:   A => each of B's components
+ *     AND-expr A => OR-expr B iff:    A => any of B's components,
+ *                                                                     *or* any of A's components => B
+ *     OR-expr A => atom B iff:                each of A's components => B
+ *     OR-expr A => AND-expr B iff:    A => each of B's components
+ *     OR-expr A => OR-expr B iff:             each of A's components => any of B's
+ *
+ * An "atom" is anything other than an AND or OR node.  Notice that we don't
+ * have any special logic to handle NOT nodes; these should have been pushed
+ * down or eliminated where feasible by prepqual.c.
+ *
+ * We can't recursively expand either side first, but have to interleave
+ * the expansions per the above rules, to be sure we handle all of these
+ * examples:
+ *             (x OR y) => (x OR y OR z)
+ *             (x AND y AND z) => (x AND y)
+ *             (x AND y) => ((x AND y) OR z)
+ *             ((x OR y) AND z) => (x OR y)
+ * This is still not an exhaustive test, but it handles most normal cases
+ * under the assumption that both inputs have been AND/OR flattened.
+ *
+ * A bare List node on the restriction side is interpreted as an AND clause,
+ * in order to handle the top-level restriction List properly.  However we
+ * need not consider a List on the predicate side since pred_test() already
+ * expanded it.
+ *
+ * We have to be prepared to handle RestrictInfo nodes in the restrictinfo
+ * tree, though not in the predicate tree.
+ *----------
  */
 static bool
-pred_test_recurse_restrict(Expr *predicate, Node *clause)
+pred_test_recurse(Node *clause, Node *predicate)
 {
-       List       *items;
        ListCell   *item;
 
        Assert(clause != NULL);
+       /* skip through RestrictInfo */
        if (IsA(clause, RestrictInfo))
        {
-               RestrictInfo *restrictinfo = (RestrictInfo *) clause;
-
-               return pred_test_recurse_restrict(predicate,
-                                                                                 (Node *) restrictinfo->clause);
+               clause = (Node *) ((RestrictInfo *) clause)->clause;
+               Assert(clause != NULL);
+               Assert(!IsA(clause, RestrictInfo));
        }
-       else if (or_clause(clause))
+       Assert(predicate != NULL);
+
+       /*
+        * Since a restriction List clause is handled the same as an AND clause,
+        * we can avoid duplicate code like this:
+        */
+       if (and_clause(clause))
+               clause = (Node *) ((BoolExpr *) clause)->args;
+
+       if (IsA(clause, List))
        {
-               items = ((BoolExpr *) clause)->args;
-               foreach(item, items)
+               if (and_clause(predicate))
                {
-                       /* if any OR item doesn't imply the predicate, clause doesn't */
-                       if (!pred_test_recurse_restrict(predicate, lfirst(item)))
-                               return false;
+                       /* AND-clause => AND-clause if A implies each of B's items */
+                       foreach(item, ((BoolExpr *) predicate)->args)
+                       {
+                               if (!pred_test_recurse(clause, lfirst(item)))
+                                       return false;
+                       }
+                       return true;
+               }
+               else if (or_clause(predicate))
+               {
+                       /* AND-clause => OR-clause if A implies any of B's items */
+                       /* Needed to handle (x AND y) => ((x AND y) OR z) */
+                       foreach(item, ((BoolExpr *) predicate)->args)
+                       {
+                               if (pred_test_recurse(clause, lfirst(item)))
+                                       return true;
+                       }
+                       /* Also check if any of A's items implies B */
+                       /* Needed to handle ((x OR y) AND z) => (x OR y) */
+                       foreach(item, (List *) clause)
+                       {
+                               if (pred_test_recurse(lfirst(item), predicate))
+                                       return true;
+                       }
+                       return false;
+               }
+               else
+               {
+                       /* AND-clause => atom if any of A's items implies B */
+                       foreach(item, (List *) clause)
+                       {
+                               if (pred_test_recurse(lfirst(item), predicate))
+                                       return true;
+                       }
+                       return false;
                }
-               return true;
        }
-       else if (and_clause(clause))
+       else if (or_clause(clause))
        {
-               items = ((BoolExpr *) clause)->args;
-               foreach(item, items)
+               if (or_clause(predicate))
                {
                        /*
-                        * if any AND item implies the predicate, the whole clause
-                        * does
+                        * OR-clause => OR-clause if each of A's items implies any of
+                        * B's items.  Messy but can't do it any more simply.
                         */
-                       if (pred_test_recurse_restrict(predicate, lfirst(item)))
-                               return true;
+                       foreach(item, ((BoolExpr *) clause)->args)
+                       {
+                               Node       *citem = lfirst(item);
+                               ListCell   *item2;
+
+                               foreach(item2, ((BoolExpr *) predicate)->args)
+                               {
+                                       if (pred_test_recurse(citem, lfirst(item2)))
+                                               break;
+                               }
+                               if (item2 == NULL)
+                                       return false; /* doesn't imply any of B's */
+                       }
+                       return true;
+               }
+               else
+               {
+                       /* OR-clause => AND-clause if each of A's items implies B */
+                       /* OR-clause => atom if each of A's items implies B */
+                       foreach(item, ((BoolExpr *) clause)->args)
+                       {
+                               if (!pred_test_recurse(lfirst(item), predicate))
+                                       return false;
+                       }
+                       return true;
                }
-               return false;
        }
        else
-               return pred_test_simple_clause(predicate, clause);
+       {
+               if (and_clause(predicate))
+               {
+                       /* atom => AND-clause if A implies each of B's items */
+                       foreach(item, ((BoolExpr *) predicate)->args)
+                       {
+                               if (!pred_test_recurse(clause, lfirst(item)))
+                                       return false;
+                       }
+                       return true;
+               }
+               else if (or_clause(predicate))
+               {
+                       /* atom => OR-clause if A implies any of B's items */
+                       foreach(item, ((BoolExpr *) predicate)->args)
+                       {
+                               if (pred_test_recurse(clause, lfirst(item)))
+                                       return true;
+                       }
+                       return false;
+               }
+               else
+               {
+                       /* atom => atom is the base case */
+                       return pred_test_simple_clause((Expr *) predicate, clause);
+               }
+       }
 }
 
 
@@ -1342,12 +1542,10 @@ pred_test_simple_clause(Expr *predicate, Node *clause)
 /*
  * indexable_outerrelids
  *       Finds all other relids that participate in any indexable join clause
- *       for the specified index.      Returns a set of relids.
- *
- * 'rel' is the relation for which 'index' is defined
+ *       for the specified table.  Returns a set of relids.
  */
 static Relids
-indexable_outerrelids(RelOptInfo *rel, IndexOptInfo *index)
+indexable_outerrelids(RelOptInfo *rel)
 {
        Relids          outer_relids = NULL;
        ListCell   *l;
@@ -1355,53 +1553,112 @@ indexable_outerrelids(RelOptInfo *rel, IndexOptInfo *index)
        foreach(l, rel->joininfo)
        {
                JoinInfo   *joininfo = (JoinInfo *) lfirst(l);
-               bool            match_found = false;
-               ListCell   *j;
 
                /*
                 * Examine each joinclause in the JoinInfo node's list to see if
-                * it matches any key of the index.  If so, add the JoinInfo's
+                * it matches any key of any index.  If so, add the JoinInfo's
                 * otherrels to the result.  We can skip examining other
-                * joinclauses in the same list as soon as we find a match (since
-                * by definition they all have the same otherrels).
+                * joinclauses in the same list as soon as we find a matchsince
+                * by definition they all have the same otherrels.
                 */
-               foreach(j, joininfo->jinfo_restrictinfo)
-               {
-                       RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
-                       int                     indexcol = 0;
-                       Oid                *classes = index->classlist;
+               if (list_matches_any_index(joininfo->jinfo_restrictinfo,
+                                                                  rel,
+                                                                  joininfo->unjoined_relids))
+                       outer_relids = bms_add_members(outer_relids,
+                                                                                  joininfo->unjoined_relids);
+       }
 
-                       do
-                       {
-                               Oid                     curClass = classes[0];
+       return outer_relids;
+}
 
-                               if (match_join_clause_to_indexcol(rel,
-                                                                                                 index,
-                                                                                                 indexcol,
-                                                                                                 curClass,
-                                                                                                 rinfo))
-                               {
-                                       match_found = true;
-                                       break;
-                               }
+/*
+ * list_matches_any_index
+ *       Workhorse for indexable_outerrelids: given a list of RestrictInfos,
+ *       see if any of them match any index of the given rel.
+ *
+ * We define it like this so that we can recurse into OR subclauses.
+ */
+static bool
+list_matches_any_index(List *clauses, RelOptInfo *rel, Relids outer_relids)
+{
+       ListCell   *l;
 
-                               indexcol++;
-                               classes++;
+       foreach(l, clauses)
+       {
+               RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
+               ListCell *j;
 
-                       } while (!DoneMatchingIndexKeys(classes));
+               Assert(IsA(rinfo, RestrictInfo));
 
-                       if (match_found)
-                               break;
+               /* RestrictInfos that aren't ORs are easy */
+               if (!restriction_is_or_clause(rinfo))
+               {
+                       if (matches_any_index(rinfo, rel, outer_relids))
+                               return true;
+                       continue;
                }
 
-               if (match_found)
+               foreach(j, ((BoolExpr *) rinfo->orclause)->args)
                {
-                       outer_relids = bms_add_members(outer_relids,
-                                                                                  joininfo->unjoined_relids);
+                       Node   *orarg = (Node *) lfirst(j);
+
+                       /* OR arguments should be ANDs or sub-RestrictInfos */
+                       if (and_clause(orarg))
+                       {
+                               List   *andargs = ((BoolExpr *) orarg)->args;
+
+                               /* Recurse to examine AND items and sub-ORs */
+                               if (list_matches_any_index(andargs, rel, outer_relids))
+                                       return true;
+                       }
+                       else
+                       {
+                               Assert(IsA(orarg, RestrictInfo));
+                               Assert(!restriction_is_or_clause((RestrictInfo *) orarg));
+                               if (matches_any_index((RestrictInfo *) orarg, rel,
+                                                                          outer_relids))
+                                       return true;
+                       }
                }
        }
 
-       return outer_relids;
+       return false;
+}
+
+/*
+ * matches_any_index
+ *       Workhorse for indexable_outerrelids: see if a simple joinclause can be
+ *       matched to any index of the given rel.
+ */
+static bool
+matches_any_index(RestrictInfo *rinfo, RelOptInfo *rel, Relids outer_relids)
+{
+       ListCell   *l;
+
+       /* Normal case for a simple restriction clause */
+       foreach(l, rel->indexlist)
+       {
+               IndexOptInfo *index = (IndexOptInfo *) lfirst(l);
+               int                     indexcol = 0;
+               Oid                *classes = index->classlist;
+
+               do
+               {
+                       Oid                     curClass = classes[0];
+
+                       if (match_clause_to_indexcol(index,
+                                                                                indexcol,
+                                                                                curClass,
+                                                                                rinfo,
+                                                                                outer_relids))
+                               return true;
+
+                       indexcol++;
+                       classes++;
+               } while (!DoneMatchingIndexKeys(classes));
+       }
+
+       return false;
 }
 
 /*
@@ -1421,10 +1678,12 @@ Path *
 best_inner_indexscan(Query *root, RelOptInfo *rel,
                                         Relids outer_relids, JoinType jointype)
 {
-       Path       *cheapest = NULL;
+       Path       *cheapest;
        bool            isouterjoin;
-       ListCell   *ilist;
-       ListCell   *jlist;
+       List       *clause_list;
+       List       *indexpaths;
+       List       *bitindexpaths;
+       ListCell   *l;
        InnerIndexscanInfo *info;
        MemoryContext oldcontext;
 
@@ -1461,7 +1720,7 @@ best_inner_indexscan(Query *root, RelOptInfo *rel,
 
        /*
         * Intersect the given outer_relids with index_outer_relids to find
-        * the set of outer relids actually relevant for this index. If there
+        * the set of outer relids actually relevant for this rel. If there
         * are none, again we can fail immediately.
         */
        outer_relids = bms_intersect(rel->index_outer_relids, outer_relids);
@@ -1479,9 +1738,9 @@ best_inner_indexscan(Query *root, RelOptInfo *rel,
         * necessary because it should always be the same for a given
         * innerrel.)
         */
-       foreach(jlist, rel->index_inner_paths)
+       foreach(l, rel->index_inner_paths)
        {
-               info = (InnerIndexscanInfo *) lfirst(jlist);
+               info = (InnerIndexscanInfo *) lfirst(l);
                if (bms_equal(info->other_relids, outer_relids) &&
                        info->isouterjoin == isouterjoin)
                {
@@ -1492,71 +1751,57 @@ best_inner_indexscan(Query *root, RelOptInfo *rel,
        }
 
        /*
-        * For each index of the rel, find the best path; then choose the best
-        * overall.  We cache the per-index results as well as the overall
-        * result.      (This is useful because different indexes may have
-        * different relevant outerrel sets, so different overall outerrel
-        * sets might still map to the same computation for a given index.)
+        * Find all the relevant restriction and join clauses.
         */
-       foreach(ilist, rel->indexlist)
-       {
-               IndexOptInfo *index = (IndexOptInfo *) lfirst(ilist);
-               Relids          index_outer_relids;
-               Path       *path = NULL;
+       clause_list = find_clauses_for_join(root, rel, outer_relids, isouterjoin);
 
-               /* identify set of relevant outer relids for this index */
-               index_outer_relids = bms_intersect(index->outer_relids, outer_relids);
-               /* skip if none */
-               if (bms_is_empty(index_outer_relids))
-               {
-                       bms_free(index_outer_relids);
-                       continue;
-               }
+       /*
+        * Find all the index paths that are usable for this join, except for
+        * stuff involving OR clauses.
+        */
+       indexpaths = find_usable_indexes(root, rel,
+                                                                        clause_list, NIL,
+                                                                        false, true,
+                                                                        outer_relids);
 
-               /*
-                * Look to see if we already computed the result for this index.
-                */
-               foreach(jlist, index->inner_paths)
-               {
-                       info = (InnerIndexscanInfo *) lfirst(jlist);
-                       if (bms_equal(info->other_relids, index_outer_relids) &&
-                               info->isouterjoin == isouterjoin)
-                       {
-                               path = info->best_innerpath;
-                               bms_free(index_outer_relids);   /* not needed anymore */
-                               break;
-                       }
-               }
+       /*
+        * Generate BitmapOrPaths for any suitable OR-clauses present in the
+        * clause list.
+        */
+       bitindexpaths = generate_bitmap_or_paths(root, rel,
+                                                                                        clause_list, NIL,
+                                                                                        true,
+                                                                                        outer_relids);
 
-               if (jlist == NULL)              /* failed to find a match? */
-               {
-                       List       *clausegroups;
-
-                       /* find useful clauses for this index and outerjoin set */
-                       clausegroups = group_clauses_by_indexkey_for_join(root,
-                                                                                                                         rel,
-                                                                                                                         index,
-                                                                                                         index_outer_relids,
-                                                                                                                         jointype,
-                                                                                                                       isouterjoin);
-                       if (clausegroups)
-                       {
-                               /* make the path */
-                               path = make_innerjoin_index_path(root, rel, index,
-                                                                                                clausegroups);
-                       }
+       /*
+        * Include the regular index paths in bitindexpaths.
+        */
+       bitindexpaths = list_concat(bitindexpaths, list_copy(indexpaths));
 
-                       /* Cache the result --- whether positive or negative */
-                       info = makeNode(InnerIndexscanInfo);
-                       info->other_relids = index_outer_relids;
-                       info->isouterjoin = isouterjoin;
-                       info->best_innerpath = path;
-                       index->inner_paths = lcons(info, index->inner_paths);
-               }
+       /*
+        * 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, true);
+               indexpaths = lappend(indexpaths, bpath);
+       }
+
+       /*
+        * Now choose the cheapest member of indexpaths.
+        */
+       cheapest = NULL;
+       foreach(l, indexpaths)
+       {
+               Path       *path = (Path *) lfirst(l);
 
-               if (path != NULL &&
-                       (cheapest == NULL ||
-                        compare_path_costs(path, cheapest, TOTAL_COST) < 0))
+               if (cheapest == NULL ||
+                       compare_path_costs(path, cheapest, TOTAL_COST) < 0)
                        cheapest = path;
        }
 
@@ -1572,95 +1817,103 @@ best_inner_indexscan(Query *root, RelOptInfo *rel,
        return cheapest;
 }
 
-/****************************************************************************
- *                             ----  PATH CREATION UTILITIES  ----
- ****************************************************************************/
-
 /*
- * make_innerjoin_index_path
- *       Create an index path node for a path to be used as an inner
- *       relation in a nestloop join.
+ * find_clauses_for_join
+ *       Generate a list of clauses that are potentially useful for
+ *       scanning rel as the inner side of a nestloop join.
  *
- * 'rel' is the relation for which 'index' is defined
- * 'clausegroups' is a list of lists of RestrictInfos that can use 'index'
+ * We consider both join and restriction clauses.  Any joinclause that uses
+ * only otherrels in the specified outer_relids is fair game.  But there must
+ * be at least one such joinclause in the final list, otherwise we return NIL
+ * indicating that there isn't any potential win here.
  */
-static Path *
-make_innerjoin_index_path(Query *root,
-                                                 RelOptInfo *rel, IndexOptInfo *index,
-                                                 List *clausegroups)
+static List *
+find_clauses_for_join(Query *root, RelOptInfo *rel,
+                                         Relids outer_relids, bool isouterjoin)
 {
-       IndexPath  *pathnode = makeNode(IndexPath);
-       List       *indexquals,
-                          *allclauses;
-
-       /* XXX perhaps this code should be merged with create_index_path? */
-
-       pathnode->path.pathtype = T_IndexScan;
-       pathnode->path.parent = rel;
+       List       *clause_list = NIL;
+       bool            jfound = false;
+       int                     numsources;
+       ListCell   *l;
 
        /*
-        * There's no point in marking the path with any pathkeys, since it
-        * will only ever be used as the inner path of a nestloop, and so its
-        * ordering does not matter.
+        * We can always use plain restriction clauses for the rel.  We
+        * scan these first because we want them first in the clause
+        * list for the convenience of remove_redundant_join_clauses,
+        * which can never remove non-join clauses and hence won't be able
+        * to get rid of a non-join clause if it appears after a join
+        * clause it is redundant with.
         */
-       pathnode->path.pathkeys = NIL;
+       foreach(l, rel->baserestrictinfo)
+       {
+               RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
-       /* Convert clauses to indexquals the executor can handle */
-       indexquals = expand_indexqual_conditions(index, clausegroups);
+               /* Can't use pushed-down clauses in outer join */
+               if (isouterjoin && rinfo->is_pushed_down)
+                       continue;
+               clause_list = lappend(clause_list, rinfo);
+       }
 
-       /* Flatten the clausegroups list to produce indexclauses list */
-       allclauses = flatten_clausegroups_list(clausegroups);
+       /* found anything in base restrict list? */
+       numsources = (clause_list != NIL) ? 1 : 0;
 
-       /*
-        * Note that we are making a pathnode for a single-scan indexscan;
-        * therefore, indexinfo etc should be single-element lists.
-        */
-       pathnode->indexinfo = list_make1(index);
-       pathnode->indexclauses = list_make1(allclauses);
-       pathnode->indexquals = list_make1(indexquals);
+       /* Look for joinclauses that are usable with given outer_relids */
+       foreach(l, rel->joininfo)
+       {
+               JoinInfo   *joininfo = (JoinInfo *) lfirst(l);
+               bool            jfoundhere = false;
+               ListCell   *j;
+
+               if (!bms_is_subset(joininfo->unjoined_relids, outer_relids))
+                       continue;
+
+               foreach(j, joininfo->jinfo_restrictinfo)
+               {
+                       RestrictInfo *rinfo = (RestrictInfo *) lfirst(j);
+
+                       /* Can't use pushed-down clauses in outer join */
+                       if (isouterjoin && rinfo->is_pushed_down)
+                               continue;
 
-       pathnode->isjoininner = true;
+                       clause_list = lappend(clause_list, rinfo);
+                       if (!jfoundhere)
+                       {
+                               jfoundhere = true;
+                               jfound = true;
+                               numsources++;
+                       }
+               }
+       }
 
-       /* We don't actually care what order the index scans in ... */
-       pathnode->indexscandir = NoMovementScanDirection;
+       /* if no join clause was matched then forget it, per comments above */
+       if (!jfound)
+               return NIL;
 
        /*
-        * We must compute the estimated number of output rows for the
-        * indexscan.  This is less than rel->rows because of the additional
-        * selectivity of the join clauses.  Since clausegroups may contain
-        * both restriction and join clauses, we have to do a set union to get
-        * the full set of clauses that must be considered to compute the
-        * correct selectivity.  (Without the union operation, we might have
-        * some restriction clauses appearing twice, which'd mislead
-        * clauselist_selectivity into double-counting their selectivity.
-        * However, since RestrictInfo nodes aren't copied when linking them
-        * into different lists, it should be sufficient to use pointer
-        * comparison to remove duplicates.)
-        *
-        * Always assume the join type is JOIN_INNER; even if some of the join
-        * clauses come from other contexts, that's not our problem.
+        * If we found clauses in more than one list, we may now have
+        * clauses that are known redundant.  Get rid of 'em.
         */
-       allclauses = list_union_ptr(rel->baserestrictinfo, allclauses);
-       pathnode->rows = rel->tuples *
-               clauselist_selectivity(root,
-                                                          allclauses,
-                                                          rel->relid,          /* do not use 0! */
-                                                          JOIN_INNER);
-       /* Like costsize.c, force estimate to be at least one row */
-       pathnode->rows = clamp_row_est(pathnode->rows);
-
-       cost_index(&pathnode->path, root, rel, index, indexquals, true);
-
-       return (Path *) pathnode;
+       if (numsources > 1)
+       {
+               clause_list = remove_redundant_join_clauses(root,
+                                                                                                       clause_list,
+                                                                                                       isouterjoin);
+       }
+
+       return clause_list;
 }
 
+/****************************************************************************
+ *                             ----  PATH CREATION UTILITIES  ----
+ ****************************************************************************/
+
 /*
  * flatten_clausegroups_list
  *       Given a list of lists of RestrictInfos, flatten it to a list
  *       of RestrictInfos.
  *
  * This is used to flatten out the result of group_clauses_by_indexkey()
- * or one of its sibling routines, to produce an indexclauses list.
+ * to produce an indexclauses list.
  */
 List *
 flatten_clausegroups_list(List *clausegroups)
@@ -1673,39 +1926,6 @@ flatten_clausegroups_list(List *clausegroups)
        return allclauses;
 }
 
-/*
- * make_expr_from_indexclauses()
- *       Given an indexclauses structure, produce an ordinary boolean expression.
- *
- * This consists of stripping out the RestrictInfo nodes and inserting
- * explicit AND and OR nodes as needed.  There's not much to it, but
- * the functionality is needed in a few places, so centralize the logic.
- */
-Expr *
-make_expr_from_indexclauses(List *indexclauses)
-{
-       List       *orclauses = NIL;
-       ListCell   *orlist;
-
-       /* There's no such thing as an indexpath with zero scans */
-       Assert(indexclauses != NIL);
-
-       foreach(orlist, indexclauses)
-       {
-               List       *andlist = (List *) lfirst(orlist);
-
-               /* Strip RestrictInfos */
-               andlist = get_actual_clauses(andlist);
-               /* Insert AND node if needed, and add to orclauses list */
-               orclauses = lappend(orclauses, make_ands_explicit(andlist));
-       }
-
-       if (list_length(orclauses) > 1)
-               return make_orclause(orclauses);
-       else
-               return (Expr *) linitial(orclauses);
-}
-
 
 /****************************************************************************
  *                             ----  ROUTINES TO CHECK OPERANDS  ----
@@ -1718,13 +1938,11 @@ make_expr_from_indexclauses(List *indexclauses)
  *
  * operand: the nodetree to be compared to the index
  * indexcol: the column number of the index (counting from 0)
- * rel: the parent relation
  * index: the index of interest
  */
-static bool
+bool
 match_index_to_operand(Node *operand,
                                           int indexcol,
-                                          RelOptInfo *rel,
                                           IndexOptInfo *index)
 {
        int                     indkey;
@@ -1745,7 +1963,7 @@ match_index_to_operand(Node *operand,
                 * Simple index column; operand must be a matching Var.
                 */
                if (operand && IsA(operand, Var) &&
-                       rel->relid == ((Var *) operand)->varno &&
+                       index->rel->relid == ((Var *) operand)->varno &&
                        indkey == ((Var *) operand)->varattno)
                        return true;
        }
@@ -1812,21 +2030,77 @@ match_index_to_operand(Node *operand,
  * from LIKE to indexscan limits rather harder than one might think ...
  * but that's the basic idea.)
  *
- * Two routines are provided here, match_special_index_operator() and
- * expand_indexqual_conditions().  match_special_index_operator() is
- * just an auxiliary function for match_clause_to_indexcol(); after
- * the latter fails to recognize a restriction opclause's operator
- * as a member of an index's opclass, it asks match_special_index_operator()
- * whether the clause should be considered an indexqual anyway.
+ * Another thing that we do with this machinery is to provide special
+ * smarts for "boolean" indexes (that is, indexes on boolean columns
+ * that support boolean equality).  We can transform a plain reference
+ * to the indexkey into "indexkey = true", or "NOT indexkey" into
+ * "indexkey = false", so as to make the expression indexable using the
+ * regular index operators.  (As of Postgres 8.1, we must do this here
+ * because constant simplification does the reverse transformation;
+ * without this code there'd be no way to use such an index at all.)
+ *
+ * Three routines are provided here:
+ *
+ * match_special_index_operator() is just an auxiliary function for
+ * match_clause_to_indexcol(); after the latter fails to recognize a
+ * restriction opclause's operator as a member of an index's opclass,
+ * it asks match_special_index_operator() whether the clause should be
+ * considered an indexqual anyway.
+ *
+ * match_boolean_index_clause() similarly detects clauses that can be
+ * converted into boolean equality operators.
+ *
  * expand_indexqual_conditions() converts a list of lists of RestrictInfo
  * nodes (with implicit AND semantics across list elements) into
  * a list of clauses that the executor can actually handle.  For operators
  * that are members of the index's opclass this transformation is a no-op,
- * but operators recognized by match_special_index_operator() must be
- * converted into one or more "regular" indexqual conditions.
+ * but clauses recognized by match_special_index_operator() or
+ * match_boolean_index_clause() must be converted into one or more "regular"
+ * indexqual conditions.
  *----------
  */
 
+/*
+ * match_boolean_index_clause
+ *       Recognize restriction clauses that can be matched to a boolean index.
+ *
+ * This should be called only when IsBooleanOpclass() recognizes the
+ * index's operator class.  We check to see if the clause matches the
+ * index's key.
+ */
+static bool
+match_boolean_index_clause(Node *clause,
+                                                  int indexcol,
+                                                  IndexOptInfo *index)
+{
+       /* Direct match? */
+       if (match_index_to_operand(clause, indexcol, index))
+               return true;
+       /* NOT clause? */
+       if (not_clause(clause))
+       {
+               if (match_index_to_operand((Node *) get_notclausearg((Expr *) clause),
+                                                                  indexcol, index))
+                       return true;
+       }
+       /*
+        * Since we only consider clauses at top level of WHERE, we can convert
+        * indexkey IS TRUE and indexkey IS FALSE to index searches as well.
+        * The different meaning for NULL isn't important.
+        */
+       else if (clause && IsA(clause, BooleanTest))
+       {
+               BooleanTest        *btest = (BooleanTest *) clause;
+
+               if (btest->booltesttype == IS_TRUE ||
+                       btest->booltesttype == IS_FALSE)
+                       if (match_index_to_operand((Node *) btest->arg,
+                                                                          indexcol, index))
+                               return true;
+       }
+       return false;
+}
+
 /*
  * match_special_index_operator
  *       Recognize restriction clauses that can be used to generate
@@ -1986,13 +2260,13 @@ match_special_index_operator(Expr *clause, Oid opclass,
  * expand_indexqual_conditions
  *       Given a list of sublists of RestrictInfo nodes, produce a flat list
  *       of index qual clauses.  Standard qual clauses (those in the index's
- *       opclass) are passed through unchanged.  "Special" index operators
- *       are expanded into clauses that the indexscan machinery will know
- *       what to do with.
+ *       opclass) are passed through unchanged.  Boolean clauses and "special"
+ *       index operators are expanded into clauses that the indexscan machinery
+ *       will know what to do with.
  *
  * The input list is ordered by index key, and so the output list is too.
  * (The latter is not depended on by any part of the planner, so far as I can
- * tell; but some parts of the executor do assume that the indxqual list
+ * tell; but some parts of the executor do assume that the indexqual list
  * ultimately delivered to the executor is so ordered. One such place is
  * _bt_preprocess_keys() in the btree support. Perhaps that ought to be fixed
  * someday --- tgl 7/00)
@@ -2002,6 +2276,7 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
 {
        List       *resultquals = NIL;
        ListCell   *clausegroup_item;
+       int                     indexcol = 0;
        Oid                *classes = index->classlist;
 
        if (clausegroups == NIL)
@@ -2017,12 +2292,31 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
                {
                        RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
+                       /* First check for boolean cases */
+                       if (IsBooleanOpclass(curClass))
+                       {
+                               Expr   *boolqual;
+
+                               boolqual = expand_boolean_index_clause((Node *) rinfo->clause,
+                                                                                                          indexcol,
+                                                                                                          index);
+                               if (boolqual)
+                               {
+                                       resultquals = lappend(resultquals,
+                                                                                 make_restrictinfo(boolqual,
+                                                                                                                       true, true));
+                                       continue;
+                               }
+                       }
+
                        resultquals = list_concat(resultquals,
                                                                          expand_indexqual_condition(rinfo,
-                                                                                                                         curClass));
+                                                                                                                                curClass));
                }
 
                clausegroup_item = lnext(clausegroup_item);
+
+               indexcol++;
                classes++;
        } while (clausegroup_item != NULL && !DoneMatchingIndexKeys(classes));
 
@@ -2031,8 +2325,69 @@ expand_indexqual_conditions(IndexOptInfo *index, List *clausegroups)
        return resultquals;
 }
 
+/*
+ * expand_boolean_index_clause
+ *       Convert a clause recognized by match_boolean_index_clause into
+ *       a boolean equality operator clause.
+ *
+ * Returns NULL if the clause isn't a boolean index qual.
+ */
+static Expr *
+expand_boolean_index_clause(Node *clause,
+                                                       int indexcol,
+                                                       IndexOptInfo *index)
+{
+       /* Direct match? */
+       if (match_index_to_operand(clause, indexcol, index))
+       {
+               /* convert to indexkey = TRUE */
+               return make_opclause(BooleanEqualOperator, BOOLOID, false,
+                                                        (Expr *) clause,
+                                                        (Expr *) makeBoolConst(true, false));
+       }
+       /* NOT clause? */
+       if (not_clause(clause))
+       {
+               Node   *arg = (Node *) get_notclausearg((Expr *) clause);
+
+               /* It must have matched the indexkey */
+               Assert(match_index_to_operand(arg, indexcol, index));
+               /* convert to indexkey = FALSE */
+               return make_opclause(BooleanEqualOperator, BOOLOID, false,
+                                                        (Expr *) arg,
+                                                        (Expr *) makeBoolConst(false, false));
+       }
+       if (clause && IsA(clause, BooleanTest))
+       {
+               BooleanTest        *btest = (BooleanTest *) clause;
+               Node   *arg = (Node *) btest->arg;
+
+               /* It must have matched the indexkey */
+               Assert(match_index_to_operand(arg, indexcol, index));
+               if (btest->booltesttype == IS_TRUE)
+               {
+                       /* convert to indexkey = TRUE */
+                       return make_opclause(BooleanEqualOperator, BOOLOID, false,
+                                                                (Expr *) arg,
+                                                                (Expr *) makeBoolConst(true, false));
+               }
+               if (btest->booltesttype == IS_FALSE)
+               {
+                       /* convert to indexkey = FALSE */
+                       return make_opclause(BooleanEqualOperator, BOOLOID, false,
+                                                                (Expr *) arg,
+                                                                (Expr *) makeBoolConst(false, false));
+               }
+               /* Oops */
+               Assert(false);
+       }
+
+       return NULL;
+}
+
 /*
  * expand_indexqual_condition --- expand a single indexqual condition
+ *             (other than a boolean-qual case)
  *
  * The input is a single RestrictInfo, the output a list of RestrictInfos
  */
@@ -2040,7 +2395,6 @@ static List *
 expand_indexqual_condition(RestrictInfo *rinfo, Oid opclass)
 {
        Expr       *clause = rinfo->clause;
-
        /* we know these will succeed */
        Node       *leftop = get_leftop(clause);
        Node       *rightop = get_rightop(clause);