]> granicus.if.org Git - postgresql/blobdiff - src/backend/access/nbtree/nbtutils.c
Adjust INCLUDE index truncation comments and code.
[postgresql] / src / backend / access / nbtree / nbtutils.c
index 9c227d7f6c656650503b77b0fd1767ea3144633c..0cecbf8e389837bbfc1e489df3d2547bac81eb3b 100644 (file)
@@ -3,12 +3,12 @@
  * nbtutils.c
  *       Utility code for Postgres btree implementation.
  *
- * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/access/nbtree/nbtutils.c,v 1.83 2007/03/30 00:12:59 tgl Exp $
+ *       src/backend/access/nbtree/nbtutils.c
  *
  *-------------------------------------------------------------------------
  */
 
 #include <time.h>
 
-#include "access/genam.h"
 #include "access/nbtree.h"
 #include "access/reloptions.h"
-#include "executor/execdebug.h"
+#include "access/relscan.h"
 #include "miscadmin.h"
-#include "storage/lwlock.h"
-#include "storage/shmem.h"
+#include "utils/array.h"
 #include "utils/lsyscache.h"
+#include "utils/memutils.h"
+#include "utils/rel.h"
 
 
+typedef struct BTSortArrayContext
+{
+       FmgrInfo        flinfo;
+       Oid                     collation;
+       bool            reverse;
+} BTSortArrayContext;
+
+static Datum _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey,
+                                                StrategyNumber strat,
+                                                Datum *elems, int nelems);
+static int _bt_sort_array_elements(IndexScanDesc scan, ScanKey skey,
+                                               bool reverse,
+                                               Datum *elems, int nelems);
+static int     _bt_compare_array_elements(const void *a, const void *b, void *arg);
 static bool _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
                                                 ScanKey leftarg, ScanKey rightarg,
                                                 bool *result);
-static void _bt_mark_scankey_with_indoption(ScanKey skey, int16 *indoption);
+static bool _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption);
 static void _bt_mark_scankey_required(ScanKey skey);
 static bool _bt_check_rowcompare(ScanKey skey,
                                         IndexTuple tuple, TupleDesc tupdesc,
@@ -49,17 +63,28 @@ _bt_mkscankey(Relation rel, IndexTuple itup)
 {
        ScanKey         skey;
        TupleDesc       itupdesc;
-       int                     natts;
+       int                     indnatts PG_USED_FOR_ASSERTS_ONLY;
+       int                     indnkeyatts;
        int16      *indoption;
        int                     i;
 
        itupdesc = RelationGetDescr(rel);
-       natts = RelationGetNumberOfAttributes(rel);
+       indnatts = IndexRelationGetNumberOfAttributes(rel);
+       indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
        indoption = rel->rd_indoption;
 
-       skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
+       Assert(indnkeyatts > 0);
+       Assert(indnkeyatts <= indnatts);
+       Assert(BTreeTupleGetNAtts(itup, rel) == indnatts ||
+                  BTreeTupleGetNAtts(itup, rel) == indnkeyatts);
+
+       /*
+        * We'll execute search using scan key constructed on key columns. Non-key
+        * (INCLUDE index) columns are always omitted from scan keys.
+        */
+       skey = (ScanKey) palloc(indnkeyatts * sizeof(ScanKeyData));
 
-       for (i = 0; i < natts; i++)
+       for (i = 0; i < indnkeyatts; i++)
        {
                FmgrInfo   *procinfo;
                Datum           arg;
@@ -78,6 +103,7 @@ _bt_mkscankey(Relation rel, IndexTuple itup)
                                                                           (AttrNumber) (i + 1),
                                                                           InvalidStrategy,
                                                                           InvalidOid,
+                                                                          rel->rd_indcollation[i],
                                                                           procinfo,
                                                                           arg);
        }
@@ -92,7 +118,7 @@ _bt_mkscankey(Relation rel, IndexTuple itup)
  *             comparison data ultimately used must match the key datatypes.
  *
  *             The result cannot be used with _bt_compare(), unless comparison
- *             data is first stored into the key entries.      Currently this
+ *             data is first stored into the key entries.  Currently this
  *             routine is only called by nbtsort.c and tuplesort.c, which have
  *             their own comparison routines.
  */
@@ -100,16 +126,16 @@ ScanKey
 _bt_mkscankey_nodata(Relation rel)
 {
        ScanKey         skey;
-       int                     natts;
+       int                     indnkeyatts;
        int16      *indoption;
        int                     i;
 
-       natts = RelationGetNumberOfAttributes(rel);
+       indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
        indoption = rel->rd_indoption;
 
-       skey = (ScanKey) palloc(natts * sizeof(ScanKeyData));
+       skey = (ScanKey) palloc(indnkeyatts * sizeof(ScanKeyData));
 
-       for (i = 0; i < natts; i++)
+       for (i = 0; i < indnkeyatts; i++)
        {
                FmgrInfo   *procinfo;
                int                     flags;
@@ -125,6 +151,7 @@ _bt_mkscankey_nodata(Relation rel)
                                                                           (AttrNumber) (i + 1),
                                                                           InvalidStrategy,
                                                                           InvalidOid,
+                                                                          rel->rd_indcollation[i],
                                                                           procinfo,
                                                                           (Datum) 0);
        }
@@ -158,13 +185,496 @@ _bt_freestack(BTStack stack)
 }
 
 
+/*
+ *     _bt_preprocess_array_keys() -- Preprocess SK_SEARCHARRAY scan keys
+ *
+ * If there are any SK_SEARCHARRAY scan keys, deconstruct the array(s) and
+ * set up BTArrayKeyInfo info for each one that is an equality-type key.
+ * Prepare modified scan keys in so->arrayKeyData, which will hold the current
+ * array elements during each primitive indexscan operation.  For inequality
+ * array keys, it's sufficient to find the extreme element value and replace
+ * the whole array with that scalar value.
+ *
+ * Note: the reason we need so->arrayKeyData, rather than just scribbling
+ * on scan->keyData, is that callers are permitted to call btrescan without
+ * supplying a new set of scankey data.
+ */
+void
+_bt_preprocess_array_keys(IndexScanDesc scan)
+{
+       BTScanOpaque so = (BTScanOpaque) scan->opaque;
+       int                     numberOfKeys = scan->numberOfKeys;
+       int16      *indoption = scan->indexRelation->rd_indoption;
+       int                     numArrayKeys;
+       ScanKey         cur;
+       int                     i;
+       MemoryContext oldContext;
+
+       /* Quick check to see if there are any array keys */
+       numArrayKeys = 0;
+       for (i = 0; i < numberOfKeys; i++)
+       {
+               cur = &scan->keyData[i];
+               if (cur->sk_flags & SK_SEARCHARRAY)
+               {
+                       numArrayKeys++;
+                       Assert(!(cur->sk_flags & (SK_ROW_HEADER | SK_SEARCHNULL | SK_SEARCHNOTNULL)));
+                       /* If any arrays are null as a whole, we can quit right now. */
+                       if (cur->sk_flags & SK_ISNULL)
+                       {
+                               so->numArrayKeys = -1;
+                               so->arrayKeyData = NULL;
+                               return;
+                       }
+               }
+       }
+
+       /* Quit if nothing to do. */
+       if (numArrayKeys == 0)
+       {
+               so->numArrayKeys = 0;
+               so->arrayKeyData = NULL;
+               return;
+       }
+
+       /*
+        * Make a scan-lifespan context to hold array-associated data, or reset it
+        * if we already have one from a previous rescan cycle.
+        */
+       if (so->arrayContext == NULL)
+               so->arrayContext = AllocSetContextCreate(CurrentMemoryContext,
+                                                                                                "BTree array context",
+                                                                                                ALLOCSET_SMALL_SIZES);
+       else
+               MemoryContextReset(so->arrayContext);
+
+       oldContext = MemoryContextSwitchTo(so->arrayContext);
+
+       /* Create modifiable copy of scan->keyData in the workspace context */
+       so->arrayKeyData = (ScanKey) palloc(scan->numberOfKeys * sizeof(ScanKeyData));
+       memcpy(so->arrayKeyData,
+                  scan->keyData,
+                  scan->numberOfKeys * sizeof(ScanKeyData));
+
+       /* Allocate space for per-array data in the workspace context */
+       so->arrayKeys = (BTArrayKeyInfo *) palloc0(numArrayKeys * sizeof(BTArrayKeyInfo));
+
+       /* Now process each array key */
+       numArrayKeys = 0;
+       for (i = 0; i < numberOfKeys; i++)
+       {
+               ArrayType  *arrayval;
+               int16           elmlen;
+               bool            elmbyval;
+               char            elmalign;
+               int                     num_elems;
+               Datum      *elem_values;
+               bool       *elem_nulls;
+               int                     num_nonnulls;
+               int                     j;
+
+               cur = &so->arrayKeyData[i];
+               if (!(cur->sk_flags & SK_SEARCHARRAY))
+                       continue;
+
+               /*
+                * First, deconstruct the array into elements.  Anything allocated
+                * here (including a possibly detoasted array value) is in the
+                * workspace context.
+                */
+               arrayval = DatumGetArrayTypeP(cur->sk_argument);
+               /* We could cache this data, but not clear it's worth it */
+               get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
+                                                        &elmlen, &elmbyval, &elmalign);
+               deconstruct_array(arrayval,
+                                                 ARR_ELEMTYPE(arrayval),
+                                                 elmlen, elmbyval, elmalign,
+                                                 &elem_values, &elem_nulls, &num_elems);
+
+               /*
+                * Compress out any null elements.  We can ignore them since we assume
+                * all btree operators are strict.
+                */
+               num_nonnulls = 0;
+               for (j = 0; j < num_elems; j++)
+               {
+                       if (!elem_nulls[j])
+                               elem_values[num_nonnulls++] = elem_values[j];
+               }
+
+               /* We could pfree(elem_nulls) now, but not worth the cycles */
+
+               /* If there's no non-nulls, the scan qual is unsatisfiable */
+               if (num_nonnulls == 0)
+               {
+                       numArrayKeys = -1;
+                       break;
+               }
+
+               /*
+                * If the comparison operator is not equality, then the array qual
+                * degenerates to a simple comparison against the smallest or largest
+                * non-null array element, as appropriate.
+                */
+               switch (cur->sk_strategy)
+               {
+                       case BTLessStrategyNumber:
+                       case BTLessEqualStrategyNumber:
+                               cur->sk_argument =
+                                       _bt_find_extreme_element(scan, cur,
+                                                                                        BTGreaterStrategyNumber,
+                                                                                        elem_values, num_nonnulls);
+                               continue;
+                       case BTEqualStrategyNumber:
+                               /* proceed with rest of loop */
+                               break;
+                       case BTGreaterEqualStrategyNumber:
+                       case BTGreaterStrategyNumber:
+                               cur->sk_argument =
+                                       _bt_find_extreme_element(scan, cur,
+                                                                                        BTLessStrategyNumber,
+                                                                                        elem_values, num_nonnulls);
+                               continue;
+                       default:
+                               elog(ERROR, "unrecognized StrategyNumber: %d",
+                                        (int) cur->sk_strategy);
+                               break;
+               }
+
+               /*
+                * Sort the non-null elements and eliminate any duplicates.  We must
+                * sort in the same ordering used by the index column, so that the
+                * successive primitive indexscans produce data in index order.
+                */
+               num_elems = _bt_sort_array_elements(scan, cur,
+                                                                                       (indoption[cur->sk_attno - 1] & INDOPTION_DESC) != 0,
+                                                                                       elem_values, num_nonnulls);
+
+               /*
+                * And set up the BTArrayKeyInfo data.
+                */
+               so->arrayKeys[numArrayKeys].scan_key = i;
+               so->arrayKeys[numArrayKeys].num_elems = num_elems;
+               so->arrayKeys[numArrayKeys].elem_values = elem_values;
+               numArrayKeys++;
+       }
+
+       so->numArrayKeys = numArrayKeys;
+
+       MemoryContextSwitchTo(oldContext);
+}
+
+/*
+ * _bt_find_extreme_element() -- get least or greatest array element
+ *
+ * scan and skey identify the index column, whose opfamily determines the
+ * comparison semantics.  strat should be BTLessStrategyNumber to get the
+ * least element, or BTGreaterStrategyNumber to get the greatest.
+ */
+static Datum
+_bt_find_extreme_element(IndexScanDesc scan, ScanKey skey,
+                                                StrategyNumber strat,
+                                                Datum *elems, int nelems)
+{
+       Relation        rel = scan->indexRelation;
+       Oid                     elemtype,
+                               cmp_op;
+       RegProcedure cmp_proc;
+       FmgrInfo        flinfo;
+       Datum           result;
+       int                     i;
+
+       /*
+        * Determine the nominal datatype of the array elements.  We have to
+        * support the convention that sk_subtype == InvalidOid means the opclass
+        * input type; this is a hack to simplify life for ScanKeyInit().
+        */
+       elemtype = skey->sk_subtype;
+       if (elemtype == InvalidOid)
+               elemtype = rel->rd_opcintype[skey->sk_attno - 1];
+
+       /*
+        * Look up the appropriate comparison operator in the opfamily.
+        *
+        * Note: it's possible that this would fail, if the opfamily is
+        * incomplete, but it seems quite unlikely that an opfamily would omit
+        * non-cross-type comparison operators for any datatype that it supports
+        * at all.
+        */
+       cmp_op = get_opfamily_member(rel->rd_opfamily[skey->sk_attno - 1],
+                                                                elemtype,
+                                                                elemtype,
+                                                                strat);
+       if (!OidIsValid(cmp_op))
+               elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
+                        strat, elemtype, elemtype,
+                        rel->rd_opfamily[skey->sk_attno - 1]);
+       cmp_proc = get_opcode(cmp_op);
+       if (!RegProcedureIsValid(cmp_proc))
+               elog(ERROR, "missing oprcode for operator %u", cmp_op);
+
+       fmgr_info(cmp_proc, &flinfo);
+
+       Assert(nelems > 0);
+       result = elems[0];
+       for (i = 1; i < nelems; i++)
+       {
+               if (DatumGetBool(FunctionCall2Coll(&flinfo,
+                                                                                  skey->sk_collation,
+                                                                                  elems[i],
+                                                                                  result)))
+                       result = elems[i];
+       }
+
+       return result;
+}
+
+/*
+ * _bt_sort_array_elements() -- sort and de-dup array elements
+ *
+ * The array elements are sorted in-place, and the new number of elements
+ * after duplicate removal is returned.
+ *
+ * scan and skey identify the index column, whose opfamily determines the
+ * comparison semantics.  If reverse is true, we sort in descending order.
+ */
+static int
+_bt_sort_array_elements(IndexScanDesc scan, ScanKey skey,
+                                               bool reverse,
+                                               Datum *elems, int nelems)
+{
+       Relation        rel = scan->indexRelation;
+       Oid                     elemtype;
+       RegProcedure cmp_proc;
+       BTSortArrayContext cxt;
+       int                     last_non_dup;
+       int                     i;
+
+       if (nelems <= 1)
+               return nelems;                  /* no work to do */
+
+       /*
+        * Determine the nominal datatype of the array elements.  We have to
+        * support the convention that sk_subtype == InvalidOid means the opclass
+        * input type; this is a hack to simplify life for ScanKeyInit().
+        */
+       elemtype = skey->sk_subtype;
+       if (elemtype == InvalidOid)
+               elemtype = rel->rd_opcintype[skey->sk_attno - 1];
+
+       /*
+        * Look up the appropriate comparison function in the opfamily.
+        *
+        * Note: it's possible that this would fail, if the opfamily is
+        * incomplete, but it seems quite unlikely that an opfamily would omit
+        * non-cross-type support functions for any datatype that it supports at
+        * all.
+        */
+       cmp_proc = get_opfamily_proc(rel->rd_opfamily[skey->sk_attno - 1],
+                                                                elemtype,
+                                                                elemtype,
+                                                                BTORDER_PROC);
+       if (!RegProcedureIsValid(cmp_proc))
+               elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
+                        BTORDER_PROC, elemtype, elemtype,
+                        rel->rd_opfamily[skey->sk_attno - 1]);
+
+       /* Sort the array elements */
+       fmgr_info(cmp_proc, &cxt.flinfo);
+       cxt.collation = skey->sk_collation;
+       cxt.reverse = reverse;
+       qsort_arg((void *) elems, nelems, sizeof(Datum),
+                         _bt_compare_array_elements, (void *) &cxt);
+
+       /* Now scan the sorted elements and remove duplicates */
+       last_non_dup = 0;
+       for (i = 1; i < nelems; i++)
+       {
+               int32           compare;
+
+               compare = DatumGetInt32(FunctionCall2Coll(&cxt.flinfo,
+                                                                                                 cxt.collation,
+                                                                                                 elems[last_non_dup],
+                                                                                                 elems[i]));
+               if (compare != 0)
+                       elems[++last_non_dup] = elems[i];
+       }
+
+       return last_non_dup + 1;
+}
+
+/*
+ * qsort_arg comparator for sorting array elements
+ */
+static int
+_bt_compare_array_elements(const void *a, const void *b, void *arg)
+{
+       Datum           da = *((const Datum *) a);
+       Datum           db = *((const Datum *) b);
+       BTSortArrayContext *cxt = (BTSortArrayContext *) arg;
+       int32           compare;
+
+       compare = DatumGetInt32(FunctionCall2Coll(&cxt->flinfo,
+                                                                                         cxt->collation,
+                                                                                         da, db));
+       if (cxt->reverse)
+               compare = -compare;
+       return compare;
+}
+
+/*
+ * _bt_start_array_keys() -- Initialize array keys at start of a scan
+ *
+ * Set up the cur_elem counters and fill in the first sk_argument value for
+ * each array scankey.  We can't do this until we know the scan direction.
+ */
+void
+_bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
+{
+       BTScanOpaque so = (BTScanOpaque) scan->opaque;
+       int                     i;
+
+       for (i = 0; i < so->numArrayKeys; i++)
+       {
+               BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
+               ScanKey         skey = &so->arrayKeyData[curArrayKey->scan_key];
+
+               Assert(curArrayKey->num_elems > 0);
+               if (ScanDirectionIsBackward(dir))
+                       curArrayKey->cur_elem = curArrayKey->num_elems - 1;
+               else
+                       curArrayKey->cur_elem = 0;
+               skey->sk_argument = curArrayKey->elem_values[curArrayKey->cur_elem];
+       }
+}
+
+/*
+ * _bt_advance_array_keys() -- Advance to next set of array elements
+ *
+ * Returns true if there is another set of values to consider, false if not.
+ * On true result, the scankeys are initialized with the next set of values.
+ */
+bool
+_bt_advance_array_keys(IndexScanDesc scan, ScanDirection dir)
+{
+       BTScanOpaque so = (BTScanOpaque) scan->opaque;
+       bool            found = false;
+       int                     i;
+
+       /*
+        * We must advance the last array key most quickly, since it will
+        * correspond to the lowest-order index column among the available
+        * qualifications. This is necessary to ensure correct ordering of output
+        * when there are multiple array keys.
+        */
+       for (i = so->numArrayKeys - 1; i >= 0; i--)
+       {
+               BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
+               ScanKey         skey = &so->arrayKeyData[curArrayKey->scan_key];
+               int                     cur_elem = curArrayKey->cur_elem;
+               int                     num_elems = curArrayKey->num_elems;
+
+               if (ScanDirectionIsBackward(dir))
+               {
+                       if (--cur_elem < 0)
+                       {
+                               cur_elem = num_elems - 1;
+                               found = false;  /* need to advance next array key */
+                       }
+                       else
+                               found = true;
+               }
+               else
+               {
+                       if (++cur_elem >= num_elems)
+                       {
+                               cur_elem = 0;
+                               found = false;  /* need to advance next array key */
+                       }
+                       else
+                               found = true;
+               }
+
+               curArrayKey->cur_elem = cur_elem;
+               skey->sk_argument = curArrayKey->elem_values[cur_elem];
+               if (found)
+                       break;
+       }
+
+       /* advance parallel scan */
+       if (scan->parallel_scan != NULL)
+               _bt_parallel_advance_array_keys(scan);
+
+       return found;
+}
+
+/*
+ * _bt_mark_array_keys() -- Handle array keys during btmarkpos
+ *
+ * Save the current state of the array keys as the "mark" position.
+ */
+void
+_bt_mark_array_keys(IndexScanDesc scan)
+{
+       BTScanOpaque so = (BTScanOpaque) scan->opaque;
+       int                     i;
+
+       for (i = 0; i < so->numArrayKeys; i++)
+       {
+               BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
+
+               curArrayKey->mark_elem = curArrayKey->cur_elem;
+       }
+}
+
+/*
+ * _bt_restore_array_keys() -- Handle array keys during btrestrpos
+ *
+ * Restore the array keys to where they were when the mark was set.
+ */
+void
+_bt_restore_array_keys(IndexScanDesc scan)
+{
+       BTScanOpaque so = (BTScanOpaque) scan->opaque;
+       bool            changed = false;
+       int                     i;
+
+       /* Restore each array key to its position when the mark was set */
+       for (i = 0; i < so->numArrayKeys; i++)
+       {
+               BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
+               ScanKey         skey = &so->arrayKeyData[curArrayKey->scan_key];
+               int                     mark_elem = curArrayKey->mark_elem;
+
+               if (curArrayKey->cur_elem != mark_elem)
+               {
+                       curArrayKey->cur_elem = mark_elem;
+                       skey->sk_argument = curArrayKey->elem_values[mark_elem];
+                       changed = true;
+               }
+       }
+
+       /*
+        * If we changed any keys, we must redo _bt_preprocess_keys.  That might
+        * sound like overkill, but in cases with multiple keys per index column
+        * it seems necessary to do the full set of pushups.
+        */
+       if (changed)
+       {
+               _bt_preprocess_keys(scan);
+               /* The mark should have been set on a consistent set of keys... */
+               Assert(so->qual_ok);
+       }
+}
+
+
 /*
  *     _bt_preprocess_keys() -- Preprocess scan keys
  *
- * The caller-supplied search-type keys (in scan->keyData[]) are copied to
- * so->keyData[] with possible transformation. scan->numberOfKeys is
- * the number of input keys, so->numberOfKeys gets the number of output
- * keys (possibly less, never greater).
+ * The given search-type keys (in scan->keyData[] or so->arrayKeyData[])
+ * are copied to so->keyData[] with possible transformation.
+ * scan->numberOfKeys is the number of input keys, so->numberOfKeys gets
+ * the number of output keys (possibly less, never greater).
  *
  * The output keys are marked with additional sk_flag bits beyond the
  * system-standard bits supplied by the caller.  The DESC and NULLS_FIRST
@@ -172,15 +682,15 @@ _bt_freestack(BTStack stack)
  * Also, for a DESC column, we commute (flip) all the sk_strategy numbers
  * so that the index sorts in the desired direction.
  *
- * One key purpose of this routine is to discover how many scan keys
- * must be satisfied to continue the scan.     It also attempts to eliminate
- * redundant keys and detect contradictory keys.  (If the index opfamily
- * provides incomplete sets of cross-type operators, we may fail to detect
- * redundant or contradictory keys, but we can survive that.)
+ * One key purpose of this routine is to discover which scan keys must be
+ * satisfied to continue the scan.  It also attempts to eliminate redundant
+ * keys and detect contradictory keys.  (If the index opfamily provides
+ * incomplete sets of cross-type operators, we may fail to detect redundant
+ * or contradictory keys, but we can survive that.)
  *
  * The output keys must be sorted by index attribute.  Presently we expect
  * (but verify) that the input keys are already so sorted --- this is done
- * by group_clauses_by_indexkey() in indxpath.c.  Some reordering of the keys
+ * by match_clauses_to_index() in indxpath.c.  Some reordering of the keys
  * within each attribute may be done as a byproduct of the processing here,
  * but no other code depends on that.
  *
@@ -211,8 +721,21 @@ _bt_freestack(BTStack stack)
  * </<= keys if we can't compare them.  The logic about required keys still
  * works if we don't eliminate redundant keys.
  *
+ * Note that one reason we need direction-sensitive required-key flags is
+ * precisely that we may not be able to eliminate redundant keys.  Suppose
+ * we have "x > 4::int AND x > 10::bigint", and we are unable to determine
+ * which key is more restrictive for lack of a suitable cross-type operator.
+ * _bt_first will arbitrarily pick one of the keys to do the initial
+ * positioning with.  If it picks x > 4, then the x > 10 condition will fail
+ * until we reach index entries > 10; but we can't stop the scan just because
+ * x > 10 is failing.  On the other hand, if we are scanning backwards, then
+ * failure of either key is indeed enough to stop the scan.  (In general, when
+ * inequality keys are present, the initial-positioning code only promises to
+ * position before the first possible match, not exactly at the first match,
+ * for a forward scan; or after the last match for a backward scan.)
+ *
  * As a byproduct of this work, we can detect contradictory quals such
- * as "x = 1 AND x > 2".  If we see that, we return so->qual_ok = FALSE,
+ * as "x = 1 AND x > 2".  If we see that, we return so->qual_ok = false,
  * indicating the scan need not be run at all since no tuples can match.
  * (In this case we do not bother completing the output key array!)
  * Again, missing cross-type operators might cause us to fail to prove the
@@ -226,8 +749,8 @@ _bt_freestack(BTStack stack)
  *
  * Note: the reason we have to copy the preprocessed scan keys into private
  * storage is that we are modifying the array based on comparisons of the
- * key argument values, which could change on a rescan.  Therefore we can't
- * overwrite the caller's data structure.
+ * key argument values, which could change on a rescan or after moving to
+ * new elements of array keys.  Therefore we can't overwrite the source data.
  */
 void
 _bt_preprocess_keys(IndexScanDesc scan)
@@ -253,7 +776,14 @@ _bt_preprocess_keys(IndexScanDesc scan)
        if (numberOfKeys < 1)
                return;                                 /* done if qual-less scan */
 
-       inkeys = scan->keyData;
+       /*
+        * Read so->arrayKeyData if array keys are present, else scan->keyData
+        */
+       if (so->arrayKeyData != NULL)
+               inkeys = so->arrayKeyData;
+       else
+               inkeys = scan->keyData;
+
        outkeys = so->keyData;
        cur = &inkeys[0];
        /* we check that input keys are correctly ordered */
@@ -263,14 +793,9 @@ _bt_preprocess_keys(IndexScanDesc scan)
        /* We can short-circuit most of the work if there's just one key */
        if (numberOfKeys == 1)
        {
-               /*
-                * We don't use indices for 'A is null' and 'A is not null' currently
-                * and 'A < = > <> NULL' will always fail - so qual is not OK if
-                * comparison value is NULL.      - vadim 03/21/97
-                */
-               if (cur->sk_flags & SK_ISNULL)
+               /* Apply indoption to scankey (might change sk_strategy!) */
+               if (!_bt_fix_scankey_strategy(cur, indoption))
                        so->qual_ok = false;
-               _bt_mark_scankey_with_indoption(cur, indoption);
                memcpy(outkeys, cur, sizeof(ScanKeyData));
                so->numberOfKeys = 1;
                /* We can mark the qual as required if it's for first index col */
@@ -288,8 +813,8 @@ _bt_preprocess_keys(IndexScanDesc scan)
        /*
         * Initialize for processing of keys for attr 1.
         *
-        * xform[i] points to the currently best scan key of strategy type i+1;
-        * it is NULL if we haven't yet found such a key for this attr.
+        * xform[i] points to the currently best scan key of strategy type i+1; it
+        * is NULL if we haven't yet found such a key for this attr.
         */
        attno = 1;
        memset(xform, 0, sizeof(xform));
@@ -303,16 +828,11 @@ _bt_preprocess_keys(IndexScanDesc scan)
        {
                if (i < numberOfKeys)
                {
-                       /* See comments above: any NULL implies cannot match qual */
-                       /* Note: we assume SK_ISNULL is never set in a row header key */
-                       if (cur->sk_flags & SK_ISNULL)
+                       /* Apply indoption to scankey (might change sk_strategy!) */
+                       if (!_bt_fix_scankey_strategy(cur, indoption))
                        {
+                               /* NULL can't be matched, so give up */
                                so->qual_ok = false;
-
-                               /*
-                                * Quit processing so we don't try to invoke comparison
-                                * routines on NULLs.
-                                */
                                return;
                        }
                }
@@ -331,8 +851,14 @@ _bt_preprocess_keys(IndexScanDesc scan)
 
                        /*
                         * If = has been specified, all other keys can be eliminated as
-                        * redundant.  In case of key > 2 && key == 1 we can set qual_ok
-                        * to false and abandon further processing.
+                        * redundant.  If we have a case like key = 1 AND key > 2, we can
+                        * set qual_ok to false and abandon further processing.
+                        *
+                        * We also have to deal with the case of "key IS NULL", which is
+                        * unsatisfiable in combination with any other index condition. By
+                        * the time we get here, that's been classified as an equality
+                        * check, and we've rejected any combination of it with a regular
+                        * equality condition; but not with other types of conditions.
                         */
                        if (xform[BTEqualStrategyNumber - 1])
                        {
@@ -344,6 +870,14 @@ _bt_preprocess_keys(IndexScanDesc scan)
 
                                        if (!chk || j == (BTEqualStrategyNumber - 1))
                                                continue;
+
+                                       if (eq->sk_flags & SK_SEARCHNULL)
+                                       {
+                                               /* IS NULL is contradictory to anything else */
+                                               so->qual_ok = false;
+                                               return;
+                                       }
+
                                        if (_bt_compare_scankey_args(scan, chk, eq, chk,
                                                                                                 &test_result))
                                        {
@@ -398,7 +932,7 @@ _bt_preprocess_keys(IndexScanDesc scan)
 
                        /*
                         * Emit the cleaned-up keys into the outkeys[] array, and then
-                        * mark them if they are required.      They are required (possibly
+                        * mark them if they are required.  They are required (possibly
                         * only in one direction) if all attrs before this one had "=".
                         */
                        for (j = BTMaxStrategyNumber; --j >= 0;)
@@ -424,9 +958,6 @@ _bt_preprocess_keys(IndexScanDesc scan)
                        memset(xform, 0, sizeof(xform));
                }
 
-               /* apply indoption to scankey (might change sk_strategy!) */
-               _bt_mark_scankey_with_indoption(cur, indoption);
-
                /* check strategy this key's operator corresponds to */
                j = cur->sk_strategy - 1;
 
@@ -438,6 +969,7 @@ _bt_preprocess_keys(IndexScanDesc scan)
                        memcpy(outkey, cur, sizeof(ScanKeyData));
                        if (numberOfEqualCols == attno - 1)
                                _bt_mark_scankey_required(outkey);
+
                        /*
                         * We don't support RowCompare using equality; such a qual would
                         * mess up the numberOfEqualCols tracking.
@@ -471,9 +1003,9 @@ _bt_preprocess_keys(IndexScanDesc scan)
                        else
                        {
                                /*
-                                * We can't determine which key is more restrictive.  Keep
-                                * the previous one in xform[j] and push this one directly
-                                * to the output array.
+                                * We can't determine which key is more restrictive.  Keep the
+                                * previous one in xform[j] and push this one directly to the
+                                * output array.
                                 */
                                ScanKey         outkey = &outkeys[new_numberOfKeys++];
 
@@ -488,8 +1020,7 @@ _bt_preprocess_keys(IndexScanDesc scan)
 }
 
 /*
- * Compare two scankey values using a specified operator.  Both values
- * must be already known non-NULL.
+ * Compare two scankey values using a specified operator.
  *
  * The test we want to perform is logically "leftarg op rightarg", where
  * leftarg and rightarg are the sk_argument values in those ScanKeys, and
@@ -500,7 +1031,7 @@ _bt_preprocess_keys(IndexScanDesc scan)
  *
  * If the opfamily doesn't supply a complete set of cross-type operators we
  * may not be able to make the comparison.  If we can make the comparison
- * we store the operator result in *result and return TRUE.  We return FALSE
+ * we store the operator result in *result and return true.  We return false
  * if the comparison could not be made.
  *
  * Note: op always points at the same ScanKey as either leftarg or rightarg.
@@ -509,8 +1040,7 @@ _bt_preprocess_keys(IndexScanDesc scan)
  *
  * Note: this routine needs to be insensitive to any DESC option applied
  * to the index column.  For example, "x < 4" is a tighter constraint than
- * "x < 5" regardless of which way the index is sorted.  We don't worry about
- * NULLS FIRST/LAST either, since the given values are never nulls.
+ * "x < 5" regardless of which way the index is sorted.
  */
 static bool
 _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
@@ -525,6 +1055,64 @@ _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
                                cmp_op;
        StrategyNumber strat;
 
+       /*
+        * First, deal with cases where one or both args are NULL.  This should
+        * only happen when the scankeys represent IS NULL/NOT NULL conditions.
+        */
+       if ((leftarg->sk_flags | rightarg->sk_flags) & SK_ISNULL)
+       {
+               bool            leftnull,
+                                       rightnull;
+
+               if (leftarg->sk_flags & SK_ISNULL)
+               {
+                       Assert(leftarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
+                       leftnull = true;
+               }
+               else
+                       leftnull = false;
+               if (rightarg->sk_flags & SK_ISNULL)
+               {
+                       Assert(rightarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
+                       rightnull = true;
+               }
+               else
+                       rightnull = false;
+
+               /*
+                * We treat NULL as either greater than or less than all other values.
+                * Since true > false, the tests below work correctly for NULLS LAST
+                * logic.  If the index is NULLS FIRST, we need to flip the strategy.
+                */
+               strat = op->sk_strategy;
+               if (op->sk_flags & SK_BT_NULLS_FIRST)
+                       strat = BTCommuteStrategyNumber(strat);
+
+               switch (strat)
+               {
+                       case BTLessStrategyNumber:
+                               *result = (leftnull < rightnull);
+                               break;
+                       case BTLessEqualStrategyNumber:
+                               *result = (leftnull <= rightnull);
+                               break;
+                       case BTEqualStrategyNumber:
+                               *result = (leftnull == rightnull);
+                               break;
+                       case BTGreaterEqualStrategyNumber:
+                               *result = (leftnull >= rightnull);
+                               break;
+                       case BTGreaterStrategyNumber:
+                               *result = (leftnull > rightnull);
+                               break;
+                       default:
+                               elog(ERROR, "unrecognized StrategyNumber: %d", (int) strat);
+                               *result = false;        /* keep compiler quiet */
+                               break;
+               }
+               return true;
+       }
+
        /*
         * The opfamily we need to worry about is identified by the index column.
         */
@@ -553,9 +1141,10 @@ _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
         */
        if (lefttype == opcintype && righttype == optype)
        {
-               *result = DatumGetBool(FunctionCall2(&op->sk_func,
-                                                                                        leftarg->sk_argument,
-                                                                                        rightarg->sk_argument));
+               *result = DatumGetBool(FunctionCall2Coll(&op->sk_func,
+                                                                                                op->sk_collation,
+                                                                                                leftarg->sk_argument,
+                                                                                                rightarg->sk_argument));
                return true;
        }
 
@@ -565,8 +1154,8 @@ _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
         * indexscan initiated by syscache lookup will use cross-data-type
         * operators.)
         *
-        * If the sk_strategy was flipped by _bt_mark_scankey_with_indoption,
-        * we have to un-flip it to get the correct opfamily member.
+        * If the sk_strategy was flipped by _bt_fix_scankey_strategy, we have to
+        * un-flip it to get the correct opfamily member.
         */
        strat = op->sk_strategy;
        if (op->sk_flags & SK_BT_DESC)
@@ -582,9 +1171,10 @@ _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
 
                if (RegProcedureIsValid(cmp_proc))
                {
-                       *result = DatumGetBool(OidFunctionCall2(cmp_proc,
-                                                                                                       leftarg->sk_argument,
-                                                                                                       rightarg->sk_argument));
+                       *result = DatumGetBool(OidFunctionCall2Coll(cmp_proc,
+                                                                                                               op->sk_collation,
+                                                                                                               leftarg->sk_argument,
+                                                                                                               rightarg->sk_argument));
                        return true;
                }
        }
@@ -595,12 +1185,20 @@ _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
 }
 
 /*
- * Mark a scankey with info from the index's indoption array.
+ * Adjust a scankey's strategy and flags setting as needed for indoptions.
  *
  * We copy the appropriate indoption value into the scankey sk_flags
  * (shifting to avoid clobbering system-defined flag bits).  Also, if
  * the DESC option is set, commute (flip) the operator strategy number.
  *
+ * A secondary purpose is to check for IS NULL/NOT NULL scankeys and set up
+ * the strategy field correctly for them.
+ *
+ * Lastly, for ordinary scankeys (not IS NULL/NOT NULL), we check for a
+ * NULL comparison value.  Since all btree operators are assumed strict,
+ * a NULL means that the qual cannot be satisfied.  We return true if the
+ * comparison value isn't NULL, or false if the scan should be abandoned.
+ *
  * This function is applied to the *input* scankey structure; therefore
  * on a rescan we will be looking at already-processed scankeys.  Hence
  * we have to be careful not to re-commute the strategy if we already did it.
@@ -608,16 +1206,73 @@ _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
  * there shouldn't be any problem, since the index's indoptions are certainly
  * not going to change while the scankey survives.
  */
-static void
-_bt_mark_scankey_with_indoption(ScanKey skey, int16 *indoption)
+static bool
+_bt_fix_scankey_strategy(ScanKey skey, int16 *indoption)
 {
-       int             addflags;
+       int                     addflags;
 
        addflags = indoption[skey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
+
+       /*
+        * We treat all btree operators as strict (even if they're not so marked
+        * in pg_proc). This means that it is impossible for an operator condition
+        * with a NULL comparison constant to succeed, and we can reject it right
+        * away.
+        *
+        * However, we now also support "x IS NULL" clauses as search conditions,
+        * so in that case keep going. The planner has not filled in any
+        * particular strategy in this case, so set it to BTEqualStrategyNumber
+        * --- we can treat IS NULL as an equality operator for purposes of search
+        * strategy.
+        *
+        * Likewise, "x IS NOT NULL" is supported.  We treat that as either "less
+        * than NULL" in a NULLS LAST index, or "greater than NULL" in a NULLS
+        * FIRST index.
+        *
+        * Note: someday we might have to fill in sk_collation from the index
+        * column's collation.  At the moment this is a non-issue because we'll
+        * never actually call the comparison operator on a NULL.
+        */
+       if (skey->sk_flags & SK_ISNULL)
+       {
+               /* SK_ISNULL shouldn't be set in a row header scankey */
+               Assert(!(skey->sk_flags & SK_ROW_HEADER));
+
+               /* Set indoption flags in scankey (might be done already) */
+               skey->sk_flags |= addflags;
+
+               /* Set correct strategy for IS NULL or NOT NULL search */
+               if (skey->sk_flags & SK_SEARCHNULL)
+               {
+                       skey->sk_strategy = BTEqualStrategyNumber;
+                       skey->sk_subtype = InvalidOid;
+                       skey->sk_collation = InvalidOid;
+               }
+               else if (skey->sk_flags & SK_SEARCHNOTNULL)
+               {
+                       if (skey->sk_flags & SK_BT_NULLS_FIRST)
+                               skey->sk_strategy = BTGreaterStrategyNumber;
+                       else
+                               skey->sk_strategy = BTLessStrategyNumber;
+                       skey->sk_subtype = InvalidOid;
+                       skey->sk_collation = InvalidOid;
+               }
+               else
+               {
+                       /* regular qual, so it cannot be satisfied */
+                       return false;
+               }
+
+               /* Needn't do the rest */
+               return true;
+       }
+
+       /* Adjust strategy for DESC, if we didn't already */
        if ((addflags & SK_BT_DESC) && !(skey->sk_flags & SK_BT_DESC))
                skey->sk_strategy = BTCommuteStrategyNumber(skey->sk_strategy);
        skey->sk_flags |= addflags;
 
+       /* If it's a row header, fix row member flags and strategies similarly */
        if (skey->sk_flags & SK_ROW_HEADER)
        {
                ScanKey         subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
@@ -634,25 +1289,24 @@ _bt_mark_scankey_with_indoption(ScanKey skey, int16 *indoption)
                        subkey++;
                }
        }
+
+       return true;
 }
 
 /*
  * Mark a scankey as "required to continue the scan".
  *
  * Depending on the operator type, the key may be required for both scan
- * directions or just one.     Also, if the key is a row comparison header,
- * we have to mark the appropriate subsidiary ScanKeys as required.  In
- * such cases, the first subsidiary key is required, but subsequent ones
- * are required only as long as they correspond to successive index columns
- * and match the leading column as to sort direction.
- * Otherwise the row comparison ordering is different from the index ordering
- * and so we can't stop the scan on the basis of those lower-order columns.
+ * directions or just one.  Also, if the key is a row comparison header,
+ * we have to mark its first subsidiary ScanKey as required.  (Subsequent
+ * subsidiary ScanKeys are normally for lower-order columns, and thus
+ * cannot be required, since they're after the first non-equality scankey.)
  *
  * Note: when we set required-key flag bits in a subsidiary scankey, we are
  * scribbling on a data structure belonging to the index AM's caller, not on
  * our private copy.  This should be OK because the marking will not change
  * from scan to scan within a query, and so we'd just re-mark the same way
- * anyway on a rescan. Something to keep an eye on though.
+ * anyway on a rescan.  Something to keep an eye on though.
  */
 static void
 _bt_mark_scankey_required(ScanKey skey)
@@ -684,32 +1338,20 @@ _bt_mark_scankey_required(ScanKey skey)
        if (skey->sk_flags & SK_ROW_HEADER)
        {
                ScanKey         subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
-               AttrNumber      attno = skey->sk_attno;
 
-               /* First subkey should be same as the header says */
-               Assert(subkey->sk_attno == attno);
-
-               for (;;)
-               {
-                       Assert(subkey->sk_flags & SK_ROW_MEMBER);
-                       if (subkey->sk_attno != attno)
-                               break;                  /* non-adjacent key, so not required */
-                       if (subkey->sk_strategy != skey->sk_strategy)
-                               break;                  /* wrong direction, so not required */
-                       subkey->sk_flags |= addflags;
-                       if (subkey->sk_flags & SK_ROW_END)
-                               break;
-                       subkey++;
-                       attno++;
-               }
+               /* First subkey should be same column/operator as the header */
+               Assert(subkey->sk_flags & SK_ROW_MEMBER);
+               Assert(subkey->sk_attno == skey->sk_attno);
+               Assert(subkey->sk_strategy == skey->sk_strategy);
+               subkey->sk_flags |= addflags;
        }
 }
 
 /*
  * Test whether an indextuple satisfies all the scankey conditions.
  *
- * If so, copy its TID into scan->xs_ctup.t_self, and return TRUE.
- * If not, return FALSE (xs_ctup is not changed).
+ * If so, return the address of the index tuple on the index page.
+ * If not, return NULL.
  *
  * If the tuple fails to pass the qual, we also determine whether there's
  * any need to continue the scan beyond this tuple, and set *continuescan
@@ -721,14 +1363,16 @@ _bt_mark_scankey_required(ScanKey skey)
  * offnum: offset number of index tuple (must be a valid item!)
  * dir: direction we are scanning in
  * continuescan: output parameter (will be set correctly in all cases)
+ *
+ * Caller must hold pin and lock on the index page.
  */
-bool
+IndexTuple
 _bt_checkkeys(IndexScanDesc scan,
                          Page page, OffsetNumber offnum,
                          ScanDirection dir, bool *continuescan)
 {
        ItemId          iid = PageGetItemId(page, offnum);
-       bool            tuple_valid;
+       bool            tuple_alive;
        IndexTuple      tuple;
        TupleDesc       tupdesc;
        BTScanOpaque so;
@@ -746,35 +1390,33 @@ _bt_checkkeys(IndexScanDesc scan,
         * However, if this is the last tuple on the page, we should check the
         * index keys to prevent uselessly advancing to the next page.
         */
-       if (scan->ignore_killed_tuples && ItemIdDeleted(iid))
+       if (scan->ignore_killed_tuples && ItemIdIsDead(iid))
        {
                /* return immediately if there are more tuples on the page */
                if (ScanDirectionIsForward(dir))
                {
                        if (offnum < PageGetMaxOffsetNumber(page))
-                               return false;
+                               return NULL;
                }
                else
                {
                        BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
 
                        if (offnum > P_FIRSTDATAKEY(opaque))
-                               return false;
+                               return NULL;
                }
 
                /*
-                * OK, we want to check the keys, but we'll return FALSE even if the
-                * tuple passes the key tests.
+                * OK, we want to check the keys so we can set continuescan correctly,
+                * but we'll return NULL even if the tuple passes the key tests.
                 */
-               tuple_valid = false;
+               tuple_alive = false;
        }
        else
-               tuple_valid = true;
+               tuple_alive = true;
 
        tuple = (IndexTuple) PageGetItem(page, iid);
 
-       IncrIndexProcessed();
-
        tupdesc = RelationGetDescr(scan->indexRelation);
        so = (BTScanOpaque) scan->opaque;
        keysz = so->numberOfKeys;
@@ -785,12 +1427,13 @@ _bt_checkkeys(IndexScanDesc scan,
                bool            isNull;
                Datum           test;
 
+               Assert(key->sk_attno <= BTreeTupleGetNAtts(tuple, scan->indexRelation));
                /* row-comparison keys need special processing */
                if (key->sk_flags & SK_ROW_HEADER)
                {
                        if (_bt_check_rowcompare(key, tuple, tupdesc, dir, continuescan))
                                continue;
-                       return false;
+                       return NULL;
                }
 
                datum = index_getattr(tuple,
@@ -798,12 +1441,37 @@ _bt_checkkeys(IndexScanDesc scan,
                                                          tupdesc,
                                                          &isNull);
 
-               /* btree doesn't support 'A is null' clauses, yet */
                if (key->sk_flags & SK_ISNULL)
                {
-                       /* we shouldn't get here, really; see _bt_preprocess_keys() */
-                       *continuescan = false;
-                       return false;
+                       /* Handle IS NULL/NOT NULL tests */
+                       if (key->sk_flags & SK_SEARCHNULL)
+                       {
+                               if (isNull)
+                                       continue;       /* tuple satisfies this qual */
+                       }
+                       else
+                       {
+                               Assert(key->sk_flags & SK_SEARCHNOTNULL);
+                               if (!isNull)
+                                       continue;       /* tuple satisfies this qual */
+                       }
+
+                       /*
+                        * Tuple fails this qual.  If it's a required qual for the current
+                        * scan direction, then we can conclude no further tuples will
+                        * pass, either.
+                        */
+                       if ((key->sk_flags & SK_BT_REQFWD) &&
+                               ScanDirectionIsForward(dir))
+                               *continuescan = false;
+                       else if ((key->sk_flags & SK_BT_REQBKWD) &&
+                                        ScanDirectionIsBackward(dir))
+                               *continuescan = false;
+
+                       /*
+                        * In any case, this indextuple doesn't match the qual.
+                        */
+                       return NULL;
                }
 
                if (isNull)
@@ -813,11 +1481,14 @@ _bt_checkkeys(IndexScanDesc scan,
                                /*
                                 * Since NULLs are sorted before non-NULLs, we know we have
                                 * reached the lower limit of the range of values for this
-                                * index attr.  On a backward scan, we can stop if this qual is
-                                * one of the "must match" subset.  On a forward scan,
-                                * however, we should keep going.
+                                * index attr.  On a backward scan, we can stop if this qual
+                                * is one of the "must match" subset.  We can stop regardless
+                                * of whether the qual is > or <, so long as it's required,
+                                * because it's not possible for any future tuples to pass. On
+                                * a forward scan, however, we must keep going, because we may
+                                * have initially positioned to the start of the index.
                                 */
-                               if ((key->sk_flags & SK_BT_REQBKWD) &&
+                               if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
                                        ScanDirectionIsBackward(dir))
                                        *continuescan = false;
                        }
@@ -827,10 +1498,13 @@ _bt_checkkeys(IndexScanDesc scan,
                                 * Since NULLs are sorted after non-NULLs, we know we have
                                 * reached the upper limit of the range of values for this
                                 * index attr.  On a forward scan, we can stop if this qual is
-                                * one of the "must match" subset.  On a backward scan,
-                                * however, we should keep going.
+                                * one of the "must match" subset.  We can stop regardless of
+                                * whether the qual is > or <, so long as it's required,
+                                * because it's not possible for any future tuples to pass. On
+                                * a backward scan, however, we must keep going, because we
+                                * may have initially positioned to the end of the index.
                                 */
-                               if ((key->sk_flags & SK_BT_REQFWD) &&
+                               if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
                                        ScanDirectionIsForward(dir))
                                        *continuescan = false;
                        }
@@ -838,10 +1512,11 @@ _bt_checkkeys(IndexScanDesc scan,
                        /*
                         * In any case, this indextuple doesn't match the qual.
                         */
-                       return false;
+                       return NULL;
                }
 
-               test = FunctionCall2(&key->sk_func, datum, key->sk_argument);
+               test = FunctionCall2Coll(&key->sk_func, key->sk_collation,
+                                                                datum, key->sk_argument);
 
                if (!DatumGetBool(test))
                {
@@ -865,15 +1540,16 @@ _bt_checkkeys(IndexScanDesc scan,
                        /*
                         * In any case, this indextuple doesn't match the qual.
                         */
-                       return false;
+                       return NULL;
                }
        }
 
-       /* If we get here, the tuple passes all index quals. */
-       if (tuple_valid)
-               scan->xs_ctup.t_self = tuple->t_tid;
+       /* Check for failure due to it being a killed tuple. */
+       if (!tuple_alive)
+               return NULL;
 
-       return tuple_valid;
+       /* If we get here, the tuple passes all index quals. */
+       return tuple;
 }
 
 /*
@@ -916,11 +1592,14 @@ _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc,
                                /*
                                 * Since NULLs are sorted before non-NULLs, we know we have
                                 * reached the lower limit of the range of values for this
-                                * index attr. On a backward scan, we can stop if this qual is
-                                * one of the "must match" subset.  On a forward scan,
-                                * however, we should keep going.
+                                * index attr.  On a backward scan, we can stop if this qual
+                                * is one of the "must match" subset.  We can stop regardless
+                                * of whether the qual is > or <, so long as it's required,
+                                * because it's not possible for any future tuples to pass. On
+                                * a forward scan, however, we must keep going, because we may
+                                * have initially positioned to the start of the index.
                                 */
-                               if ((subkey->sk_flags & SK_BT_REQBKWD) &&
+                               if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
                                        ScanDirectionIsBackward(dir))
                                        *continuescan = false;
                        }
@@ -929,11 +1608,14 @@ _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc,
                                /*
                                 * Since NULLs are sorted after non-NULLs, we know we have
                                 * reached the upper limit of the range of values for this
-                                * index attr. On a forward scan, we can stop if this qual is
-                                * one of the "must match" subset.  On a backward scan,
-                                * however, we should keep going.
+                                * index attr.  On a forward scan, we can stop if this qual is
+                                * one of the "must match" subset.  We can stop regardless of
+                                * whether the qual is > or <, so long as it's required,
+                                * because it's not possible for any future tuples to pass. On
+                                * a backward scan, however, we must keep going, because we
+                                * may have initially positioned to the end of the index.
                                 */
-                               if ((subkey->sk_flags & SK_BT_REQFWD) &&
+                               if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
                                        ScanDirectionIsForward(dir))
                                        *continuescan = false;
                        }
@@ -948,7 +1630,7 @@ _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc,
                {
                        /*
                         * Unlike the simple-scankey case, this isn't a disallowed case.
-                        * But it can never match.      If all the earlier row comparison
+                        * But it can never match.  If all the earlier row comparison
                         * columns are required for the scan direction, we can stop the
                         * scan, because there can't be another tuple that will succeed.
                         */
@@ -964,9 +1646,10 @@ _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc,
                }
 
                /* Perform the test --- three-way comparison not bool operator */
-               cmpresult = DatumGetInt32(FunctionCall2(&subkey->sk_func,
-                                                                                               datum,
-                                                                                               subkey->sk_argument));
+               cmpresult = DatumGetInt32(FunctionCall2Coll(&subkey->sk_func,
+                                                                                                       subkey->sk_collation,
+                                                                                                       datum,
+                                                                                                       subkey->sk_argument));
 
                if (subkey->sk_flags & SK_BT_DESC)
                        cmpresult = -cmpresult;
@@ -1012,7 +1695,7 @@ _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc,
                /*
                 * Tuple fails this qual.  If it's a required qual for the current
                 * scan direction, then we can conclude no further tuples will pass,
-                * either.      Note we have to look at the deciding column, not
+                * either.  Note we have to look at the deciding column, not
                 * necessarily the first or last column of the row condition.
                 */
                if ((subkey->sk_flags & SK_BT_REQFWD) &&
@@ -1027,30 +1710,38 @@ _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, TupleDesc tupdesc,
 }
 
 /*
- * _bt_killitems - set LP_DELETE bit for items an indexscan caller has
+ * _bt_killitems - set LP_DEAD state for items an indexscan caller has
  * told us were killed
  *
- * scan->so contains information about the current page and killed tuples
- * thereon (generally, this should only be called if so->numKilled > 0).
+ * scan->opaque, referenced locally through so, contains information about the
+ * current page and killed tuples thereon (generally, this should only be
+ * called if so->numKilled > 0).
  *
- * The caller must have pin on so->currPos.buf, but may or may not have
- * read-lock, as indicated by haveLock.  Note that we assume read-lock
- * is sufficient for setting LP_DELETE hint bits.
+ * The caller does not have a lock on the page and may or may not have the
+ * page pinned in a buffer.  Note that read-lock is sufficient for setting
+ * LP_DEAD status (which is only a hint).
  *
  * We match items by heap TID before assuming they are the right ones to
- * delete.     We cope with cases where items have moved right due to insertions.
+ * delete.  We cope with cases where items have moved right due to insertions.
  * If an item has moved off the current page due to a split, we'll fail to
  * find it and do nothing (this is not an error case --- we assume the item
- * will eventually get marked in a future indexscan).  Note that because we
- * hold pin on the target page continuously from initially reading the items
- * until applying this function, VACUUM cannot have deleted any items from
- * the page, and so there is no need to search left from the recorded offset.
- * (This observation also guarantees that the item is still the right one
- * to delete, which might otherwise be questionable since heap TIDs can get
- * recycled.)
+ * will eventually get marked in a future indexscan).
+ *
+ * Note that if we hold a pin on the target page continuously from initially
+ * reading the items until applying this function, VACUUM cannot have deleted
+ * any items from the page, and so there is no need to search left from the
+ * recorded offset.  (This observation also guarantees that the item is still
+ * the right one to delete, which might otherwise be questionable since heap
+ * TIDs can get recycled.)     This holds true even if the page has been modified
+ * by inserts and page splits, so there is no need to consult the LSN.
+ *
+ * If the pin was released after reading the page, then we re-read it.  If it
+ * has been modified since we read it (as determined by the LSN), we dare not
+ * flag any entries because it is possible that the old entry was vacuumed
+ * away and the TID was re-used by a completely different heap tuple.
  */
 void
-_bt_killitems(IndexScanDesc scan, bool haveLock)
+_bt_killitems(IndexScanDesc scan)
 {
        BTScanOpaque so = (BTScanOpaque) scan->opaque;
        Page            page;
@@ -1058,19 +1749,56 @@ _bt_killitems(IndexScanDesc scan, bool haveLock)
        OffsetNumber minoff;
        OffsetNumber maxoff;
        int                     i;
+       int                     numKilled = so->numKilled;
        bool            killedsomething = false;
 
-       Assert(BufferIsValid(so->currPos.buf));
+       Assert(BTScanPosIsValid(so->currPos));
+
+       /*
+        * Always reset the scan state, so we don't look for same items on other
+        * pages.
+        */
+       so->numKilled = 0;
 
-       if (!haveLock)
+       if (BTScanPosIsPinned(so->currPos))
+       {
+               /*
+                * We have held the pin on this page since we read the index tuples,
+                * so all we need to do is lock it.  The pin will have prevented
+                * re-use of any TID on the page, so there is no need to check the
+                * LSN.
+                */
                LockBuffer(so->currPos.buf, BT_READ);
 
-       page = BufferGetPage(so->currPos.buf);
+               page = BufferGetPage(so->currPos.buf);
+       }
+       else
+       {
+               Buffer          buf;
+
+               /* Attempt to re-read the buffer, getting pin and lock. */
+               buf = _bt_getbuf(scan->indexRelation, so->currPos.currPage, BT_READ);
+
+               /* It might not exist anymore; in which case we can't hint it. */
+               if (!BufferIsValid(buf))
+                       return;
+
+               page = BufferGetPage(buf);
+               if (BufferGetLSNAtomic(buf) == so->currPos.lsn)
+                       so->currPos.buf = buf;
+               else
+               {
+                       /* Modified while not pinned means hinting is not safe. */
+                       _bt_relbuf(scan->indexRelation, buf);
+                       return;
+               }
+       }
+
        opaque = (BTPageOpaque) PageGetSpecialPointer(page);
        minoff = P_FIRSTDATAKEY(opaque);
        maxoff = PageGetMaxOffsetNumber(page);
 
-       for (i = 0; i < so->numKilled; i++)
+       for (i = 0; i < numKilled; i++)
        {
                int                     itemIndex = so->killedItems[i];
                BTScanPosItem *kitem = &so->currPos.items[itemIndex];
@@ -1088,7 +1816,7 @@ _bt_killitems(IndexScanDesc scan, bool haveLock)
                        if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
                        {
                                /* found the item */
-                               iid->lp_flags |= LP_DELETE;
+                               ItemIdMarkDead(iid);
                                killedsomething = true;
                                break;                  /* out of inner search loop */
                        }
@@ -1097,35 +1825,26 @@ _bt_killitems(IndexScanDesc scan, bool haveLock)
        }
 
        /*
-        * Since this can be redone later if needed, it's treated the same as a
-        * commit-hint-bit status update for heap tuples: we mark the buffer dirty
-        * but don't make a WAL log entry.
+        * Since this can be redone later if needed, mark as dirty hint.
         *
-        * Whenever we mark anything LP_DELETEd, we also set the page's
+        * Whenever we mark anything LP_DEAD, we also set the page's
         * BTP_HAS_GARBAGE flag, which is likewise just a hint.
         */
        if (killedsomething)
        {
                opaque->btpo_flags |= BTP_HAS_GARBAGE;
-               SetBufferCommitInfoNeedsSave(so->currPos.buf);
+               MarkBufferDirtyHint(so->currPos.buf, true);
        }
 
-       if (!haveLock)
-               LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK);
-
-       /*
-        * Always reset the scan state, so we don't look for same items on other
-        * pages.
-        */
-       so->numKilled = 0;
+       LockBuffer(so->currPos.buf, BUFFER_LOCK_UNLOCK);
 }
 
 
 /*
  * The following routines manage a shared-memory area in which we track
  * assignment of "vacuum cycle IDs" to currently-active btree vacuuming
- * operations. There is a single counter which increments each time we
- * start a vacuum to assign it a cycle ID.     Since multiple vacuums could
+ * operations.  There is a single counter which increments each time we
+ * start a vacuum to assign it a cycle ID.  Since multiple vacuums could
  * be active concurrently, we have to track the cycle ID for each active
  * vacuum; this requires at most MaxBackends entries (usually far fewer).
  * We assume at most one vacuum can be active for a given index.
@@ -1147,7 +1866,7 @@ typedef struct BTVacInfo
        BTCycleId       cycle_ctr;              /* cycle ID most recently assigned */
        int                     num_vacuums;    /* number of currently active VACUUMs */
        int                     max_vacuums;    /* allocated length of vacuums[] array */
-       BTOneVacInfo vacuums[1];        /* VARIABLE LENGTH ARRAY */
+       BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER];
 } BTVacInfo;
 
 static BTVacInfo *btvacinfo;
@@ -1190,8 +1909,11 @@ _bt_vacuum_cycleid(Relation rel)
 /*
  * _bt_start_vacuum --- assign a cycle ID to a just-starting VACUUM operation
  *
- * Note: the caller must guarantee (via PG_TRY) that it will eventually call
- * _bt_end_vacuum, else we'll permanently leak an array slot.
+ * Note: the caller must guarantee that it will eventually call
+ * _bt_end_vacuum, else we'll permanently leak an array slot.  To ensure
+ * that this happens even in elog(FATAL) scenarios, the appropriate coding
+ * is not just a PG_TRY, but
+ *             PG_ENSURE_ERROR_CLEANUP(_bt_end_vacuum_callback, PointerGetDatum(rel))
  */
 BTCycleId
 _bt_start_vacuum(Relation rel)
@@ -1202,11 +1924,13 @@ _bt_start_vacuum(Relation rel)
 
        LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
 
-       /* Assign the next cycle ID, being careful to avoid zero */
-       do
-       {
-               result = ++(btvacinfo->cycle_ctr);
-       } while (result == 0);
+       /*
+        * Assign the next cycle ID, being careful to avoid zero as well as the
+        * reserved high values.
+        */
+       result = ++(btvacinfo->cycle_ctr);
+       if (result == 0 || result > MAX_BT_CYCLE_ID)
+               result = btvacinfo->cycle_ctr = 1;
 
        /* Let's just make sure there's no entry already for this index */
        for (i = 0; i < btvacinfo->num_vacuums; i++)
@@ -1273,6 +1997,15 @@ _bt_end_vacuum(Relation rel)
        LWLockRelease(BtreeVacuumLock);
 }
 
+/*
+ * _bt_end_vacuum wrapped as an on_shmem_exit callback function
+ */
+void
+_bt_end_vacuum_callback(int code, Datum arg)
+{
+       _bt_end_vacuum((Relation) DatumGetPointer(arg));
+}
+
 /*
  * BTreeShmemSize --- report amount of shared memory space needed
  */
@@ -1281,7 +2014,7 @@ BTreeShmemSize(void)
 {
        Size            size;
 
-       size = offsetof(BTVacInfo, vacuums[0]);
+       size = offsetof(BTVacInfo, vacuums);
        size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
        return size;
 }
@@ -1317,17 +2050,166 @@ BTreeShmemInit(void)
                Assert(found);
 }
 
-Datum
-btoptions(PG_FUNCTION_ARGS)
+bytea *
+btoptions(Datum reloptions, bool validate)
+{
+       return default_reloptions(reloptions, validate, RELOPT_KIND_BTREE);
+}
+
+/*
+ *     btproperty() -- Check boolean properties of indexes.
+ *
+ * This is optional, but handling AMPROP_RETURNABLE here saves opening the rel
+ * to call btcanreturn.
+ */
+bool
+btproperty(Oid index_oid, int attno,
+                  IndexAMProperty prop, const char *propname,
+                  bool *res, bool *isnull)
 {
-       Datum           reloptions = PG_GETARG_DATUM(0);
-       bool            validate = PG_GETARG_BOOL(1);
-       bytea      *result;
-
-       result = default_reloptions(reloptions, validate,
-                                                               BTREE_MIN_FILLFACTOR,
-                                                               BTREE_DEFAULT_FILLFACTOR);
-       if (result)
-               PG_RETURN_BYTEA_P(result);
-       PG_RETURN_NULL();
+       switch (prop)
+       {
+               case AMPROP_RETURNABLE:
+                       /* answer only for columns, not AM or whole index */
+                       if (attno == 0)
+                               return false;
+                       /* otherwise, btree can always return data */
+                       *res = true;
+                       return true;
+
+               default:
+                       return false;           /* punt to generic code */
+       }
+}
+
+/*
+ *     _bt_nonkey_truncate() -- create tuple without non-key suffix attributes.
+ *
+ * Returns truncated index tuple allocated in caller's memory context, with key
+ * attributes copied from caller's itup argument.  Currently, suffix truncation
+ * is only performed to create pivot tuples in INCLUDE indexes, but some day it
+ * could be generalized to remove suffix attributes after the first
+ * distinguishing key attribute.
+ *
+ * Truncated tuple is guaranteed to be no larger than the original, which is
+ * important for staying under the 1/3 of a page restriction on tuple size.
+ *
+ * Note that returned tuple's t_tid offset will hold the number of attributes
+ * present, so the original item pointer offset is not represented.  Caller
+ * should only change truncated tuple's downlink.
+ */
+IndexTuple
+_bt_nonkey_truncate(Relation rel, IndexTuple itup)
+{
+       int                             nkeyattrs = IndexRelationGetNumberOfKeyAttributes(rel);
+       IndexTuple              truncated;
+
+       /*
+        * We should only ever truncate leaf index tuples, which must have both key
+        * and non-key attributes.  It's never okay to truncate a second time.
+        */
+       Assert(BTreeTupleGetNAtts(itup, rel) ==
+                  IndexRelationGetNumberOfAttributes(rel));
+
+       truncated = index_truncate_tuple(RelationGetDescr(rel), itup, nkeyattrs);
+       BTreeTupleSetNAtts(truncated, nkeyattrs);
+
+       return truncated;
+}
+
+/*
+ *  _bt_check_natts() -- Verify tuple has expected number of attributes.
+ *
+ * Returns value indicating if the expected number of attributes were found
+ * for a particular offset on page.  This can be used as a general purpose
+ * sanity check.
+ *
+ * Testing a tuple directly with BTreeTupleGetNAtts() should generally be
+ * preferred to calling here.  That's usually more convenient, and is always
+ * more explicit.  Call here instead when offnum's tuple may be a negative
+ * infinity tuple that uses the pre-v11 on-disk representation, or when a low
+ * context check is appropriate.
+ */
+bool
+_bt_check_natts(Relation rel, Page page, OffsetNumber offnum)
+{
+       int16                   natts = IndexRelationGetNumberOfAttributes(rel);
+       int16                   nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
+       BTPageOpaque    opaque = (BTPageOpaque) PageGetSpecialPointer(page);
+       IndexTuple              itup;
+
+       /*
+        * We cannot reliably test a deleted or half-deleted page, since they have
+        * dummy high keys
+        */
+       if (P_IGNORE(opaque))
+               return true;
+
+       Assert(offnum >= FirstOffsetNumber &&
+                  offnum <= PageGetMaxOffsetNumber(page));
+       /*
+        * Mask allocated for number of keys in index tuple must be able to fit
+        * maximum possible number of index attributes
+        */
+       StaticAssertStmt(BT_N_KEYS_OFFSET_MASK >= INDEX_MAX_KEYS,
+                                        "BT_N_KEYS_OFFSET_MASK can't fit INDEX_MAX_KEYS");
+
+       itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
+
+       if (P_ISLEAF(opaque))
+       {
+               if (offnum >= P_FIRSTDATAKEY(opaque))
+               {
+                       /*
+                        * Leaf tuples that are not the page high key (non-pivot tuples)
+                        * should never be truncated
+                        */
+                       return BTreeTupleGetNAtts(itup, rel) == natts;
+               }
+               else
+               {
+                       /*
+                        * Rightmost page doesn't contain a page high key, so tuple was
+                        * checked above as ordinary leaf tuple
+                        */
+                       Assert(!P_RIGHTMOST(opaque));
+
+                       /* Page high key tuple contains only key attributes */
+                       return BTreeTupleGetNAtts(itup, rel) == nkeyatts;
+               }
+       }
+       else  /* !P_ISLEAF(opaque) */
+       {
+               if (offnum == P_FIRSTDATAKEY(opaque))
+               {
+                       /*
+                        * The first tuple on any internal page (possibly the first after
+                        * its high key) is its negative infinity tuple.  Negative infinity
+                        * tuples are always truncated to zero attributes.  They are a
+                        * particular kind of pivot tuple.
+                        *
+                        * The number of attributes won't be explicitly represented if the
+                        * negative infinity tuple was generated during a page split that
+                        * occurred with a version of Postgres before v11.  There must be a
+                        * problem when there is an explicit representation that is
+                        * non-zero, or when there is no explicit representation and the
+                        * tuple is evidently not a pre-pg_upgrade tuple.
+                        *
+                        * Prior to v11, downlinks always had P_HIKEY as their offset.  Use
+                        * that to decide if the tuple is a pre-v11 tuple.
+                        */
+                       return BTreeTupleGetNAtts(itup, rel) == 0 ||
+                                       ((itup->t_info & INDEX_ALT_TID_MASK) == 0 &&
+                                        ItemPointerGetOffsetNumber(&(itup->t_tid)) == P_HIKEY);
+               }
+               else
+               {
+                       /*
+                        * Tuple contains only key attributes despite on is it page high
+                        * key or not
+                        */
+                       return BTreeTupleGetNAtts(itup, rel) == nkeyatts;
+               }
+
+       }
 }