LCOV - differential code coverage report
Current view: top level - src/backend/access/nbtree - nbtutils.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC ECB DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 84.3 % 1390 1172 98 7 113 20 628 524 1 29 89
Current Date: 2024-04-14 14:21:10 Functions: 93.2 % 44 41 1 2 24 17 6
Baseline: 16@8cea358b128 Branches: 65.8 % 1269 835 231 6 197 15 513 307
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 86.4 % 734 634 98 2 2 615 17
(120,180] days: 100.0 % 2 2 2
(180,240] days: 100.0 % 9 9 9
(240..) days: 81.7 % 645 527 5 113 18 2 507 1
Function coverage date bins:
[..60] days: 95.0 % 20 19 1 19
(240..) days: 91.7 % 24 22 2 5 17
Branch coverage date bins:
[..60] days: 68.4 % 732 501 229 2 3 495 3
(180,240] days: 90.0 % 20 18 2 18
(240..) days: 61.1 % 517 316 6 195 12 304

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * nbtutils.c
                                  4                 :                :  *    Utility code for Postgres btree implementation.
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
                                  7                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  8                 :                :  *
                                  9                 :                :  *
                                 10                 :                :  * IDENTIFICATION
                                 11                 :                :  *    src/backend/access/nbtree/nbtutils.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : 
                                 16                 :                : #include "postgres.h"
                                 17                 :                : 
                                 18                 :                : #include <time.h>
                                 19                 :                : 
                                 20                 :                : #include "access/nbtree.h"
                                 21                 :                : #include "access/reloptions.h"
                                 22                 :                : #include "access/relscan.h"
                                 23                 :                : #include "commands/progress.h"
                                 24                 :                : #include "lib/qunique.h"
                                 25                 :                : #include "miscadmin.h"
                                 26                 :                : #include "utils/array.h"
                                 27                 :                : #include "utils/datum.h"
                                 28                 :                : #include "utils/lsyscache.h"
                                 29                 :                : #include "utils/memutils.h"
                                 30                 :                : #include "utils/rel.h"
                                 31                 :                : 
                                 32                 :                : #define LOOK_AHEAD_REQUIRED_RECHECKS    3
                                 33                 :                : #define LOOK_AHEAD_DEFAULT_DISTANCE     5
                                 34                 :                : 
                                 35                 :                : typedef struct BTSortArrayContext
                                 36                 :                : {
                                 37                 :                :     FmgrInfo   *sortproc;
                                 38                 :                :     Oid         collation;
                                 39                 :                :     bool        reverse;
                                 40                 :                : } BTSortArrayContext;
                                 41                 :                : 
                                 42                 :                : typedef struct BTScanKeyPreproc
                                 43                 :                : {
                                 44                 :                :     ScanKey     skey;
                                 45                 :                :     int         ikey;
                                 46                 :                :     int         arrayidx;
                                 47                 :                : } BTScanKeyPreproc;
                                 48                 :                : 
                                 49                 :                : static void _bt_setup_array_cmp(IndexScanDesc scan, ScanKey skey, Oid elemtype,
                                 50                 :                :                                 FmgrInfo *orderproc, FmgrInfo **sortprocp);
                                 51                 :                : static Datum _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey,
                                 52                 :                :                                       Oid elemtype, StrategyNumber strat,
                                 53                 :                :                                       Datum *elems, int nelems);
                                 54                 :                : static int  _bt_sort_array_elements(ScanKey skey, FmgrInfo *sortproc,
                                 55                 :                :                                     bool reverse, Datum *elems, int nelems);
                                 56                 :                : static bool _bt_merge_arrays(IndexScanDesc scan, ScanKey skey,
                                 57                 :                :                              FmgrInfo *sortproc, bool reverse,
                                 58                 :                :                              Oid origelemtype, Oid nextelemtype,
                                 59                 :                :                              Datum *elems_orig, int *nelems_orig,
                                 60                 :                :                              Datum *elems_next, int nelems_next);
                                 61                 :                : static bool _bt_compare_array_scankey_args(IndexScanDesc scan,
                                 62                 :                :                                            ScanKey arraysk, ScanKey skey,
                                 63                 :                :                                            FmgrInfo *orderproc, BTArrayKeyInfo *array,
                                 64                 :                :                                            bool *qual_ok);
                                 65                 :                : static ScanKey _bt_preprocess_array_keys(IndexScanDesc scan);
                                 66                 :                : static void _bt_preprocess_array_keys_final(IndexScanDesc scan, int *keyDataMap);
                                 67                 :                : static int  _bt_compare_array_elements(const void *a, const void *b, void *arg);
                                 68                 :                : static inline int32 _bt_compare_array_skey(FmgrInfo *orderproc,
                                 69                 :                :                                            Datum tupdatum, bool tupnull,
                                 70                 :                :                                            Datum arrdatum, ScanKey cur);
                                 71                 :                : static int  _bt_binsrch_array_skey(FmgrInfo *orderproc,
                                 72                 :                :                                    bool cur_elem_trig, ScanDirection dir,
                                 73                 :                :                                    Datum tupdatum, bool tupnull,
                                 74                 :                :                                    BTArrayKeyInfo *array, ScanKey cur,
                                 75                 :                :                                    int32 *set_elem_result);
                                 76                 :                : static bool _bt_advance_array_keys_increment(IndexScanDesc scan, ScanDirection dir);
                                 77                 :                : static void _bt_rewind_nonrequired_arrays(IndexScanDesc scan, ScanDirection dir);
                                 78                 :                : static bool _bt_tuple_before_array_skeys(IndexScanDesc scan, ScanDirection dir,
                                 79                 :                :                                          IndexTuple tuple, TupleDesc tupdesc, int tupnatts,
                                 80                 :                :                                          bool readpagetup, int sktrig, bool *scanBehind);
                                 81                 :                : static bool _bt_advance_array_keys(IndexScanDesc scan, BTReadPageState *pstate,
                                 82                 :                :                                    IndexTuple tuple, int tupnatts, TupleDesc tupdesc,
                                 83                 :                :                                    int sktrig, bool sktrig_required);
                                 84                 :                : #ifdef USE_ASSERT_CHECKING
                                 85                 :                : static bool _bt_verify_arrays_bt_first(IndexScanDesc scan, ScanDirection dir);
                                 86                 :                : static bool _bt_verify_keys_with_arraykeys(IndexScanDesc scan);
                                 87                 :                : #endif
                                 88                 :                : static bool _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
                                 89                 :                :                                      ScanKey leftarg, ScanKey rightarg,
                                 90                 :                :                                      BTArrayKeyInfo *array, FmgrInfo *orderproc,
                                 91                 :                :                                      bool *result);
                                 92                 :                : static bool _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption);
                                 93                 :                : static void _bt_mark_scankey_required(ScanKey skey);
                                 94                 :                : static bool _bt_check_compare(IndexScanDesc scan, ScanDirection dir,
                                 95                 :                :                               IndexTuple tuple, int tupnatts, TupleDesc tupdesc,
                                 96                 :                :                               bool advancenonrequired, bool prechecked, bool firstmatch,
                                 97                 :                :                               bool *continuescan, int *ikey);
                                 98                 :                : static bool _bt_check_rowcompare(ScanKey skey,
                                 99                 :                :                                  IndexTuple tuple, int tupnatts, TupleDesc tupdesc,
                                100                 :                :                                  ScanDirection dir, bool *continuescan);
                                101                 :                : static void _bt_checkkeys_look_ahead(IndexScanDesc scan, BTReadPageState *pstate,
                                102                 :                :                                      int tupnatts, TupleDesc tupdesc);
                                103                 :                : static int  _bt_keep_natts(Relation rel, IndexTuple lastleft,
                                104                 :                :                            IndexTuple firstright, BTScanInsert itup_key);
                                105                 :                : 
                                106                 :                : 
                                107                 :                : /*
                                108                 :                :  * _bt_mkscankey
                                109                 :                :  *      Build an insertion scan key that contains comparison data from itup
                                110                 :                :  *      as well as comparator routines appropriate to the key datatypes.
                                111                 :                :  *
                                112                 :                :  *      The result is intended for use with _bt_compare() and _bt_truncate().
                                113                 :                :  *      Callers that don't need to fill out the insertion scankey arguments
                                114                 :                :  *      (e.g. they use an ad-hoc comparison routine, or only need a scankey
                                115                 :                :  *      for _bt_truncate()) can pass a NULL index tuple.  The scankey will
                                116                 :                :  *      be initialized as if an "all truncated" pivot tuple was passed
                                117                 :                :  *      instead.
                                118                 :                :  *
                                119                 :                :  *      Note that we may occasionally have to share lock the metapage to
                                120                 :                :  *      determine whether or not the keys in the index are expected to be
                                121                 :                :  *      unique (i.e. if this is a "heapkeyspace" index).  We assume a
                                122                 :                :  *      heapkeyspace index when caller passes a NULL tuple, allowing index
                                123                 :                :  *      build callers to avoid accessing the non-existent metapage.  We
                                124                 :                :  *      also assume that the index is _not_ allequalimage when a NULL tuple
                                125                 :                :  *      is passed; CREATE INDEX callers call _bt_allequalimage() to set the
                                126                 :                :  *      field themselves.
                                127                 :                :  */
                                128                 :                : BTScanInsert
  309 pg@bowt.ie                129                 :CBC     5651737 : _bt_mkscankey(Relation rel, IndexTuple itup)
                                130                 :                : {
                                131                 :                :     BTScanInsert key;
                                132                 :                :     ScanKey     skey;
                                133                 :                :     TupleDesc   itupdesc;
                                134                 :                :     int         indnkeyatts;
                                135                 :                :     int16      *indoption;
                                136                 :                :     int         tupnatts;
                                137                 :                :     int         i;
                                138                 :                : 
 9357 bruce@momjian.us          139                 :        5651737 :     itupdesc = RelationGetDescr(rel);
 2199 teodor@sigaev.ru          140                 :        5651737 :     indnkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
 6305 tgl@sss.pgh.pa.us         141                 :        5651737 :     indoption = rel->rd_indoption;
 1852 pg@bowt.ie                142   [ +  +  +  + ]:        5651737 :     tupnatts = itup ? BTreeTupleGetNAtts(itup, rel) : 0;
                                143                 :                : 
                                144         [ -  + ]:        5651737 :     Assert(tupnatts <= IndexRelationGetNumberOfAttributes(rel));
                                145                 :                : 
                                146                 :                :     /*
                                147                 :                :      * We'll execute search using scan key constructed on key columns.
                                148                 :                :      * Truncated attributes and non-key attributes are omitted from the final
                                149                 :                :      * scan key.
                                150                 :                :      */
                                151                 :        5651737 :     key = palloc(offsetof(BTScanInsertData, scankeys) +
                                152                 :        5651737 :                  sizeof(ScanKeyData) * indnkeyatts);
 1509                           153         [ +  + ]:        5651737 :     if (itup)
  309                           154                 :        5587934 :         _bt_metaversion(rel, &key->heapkeyspace, &key->allequalimage);
                                155                 :                :     else
                                156                 :                :     {
                                157                 :                :         /* Utility statement callers can set these fields themselves */
 1509                           158                 :          63803 :         key->heapkeyspace = true;
                                159                 :          63803 :         key->allequalimage = false;
                                160                 :                :     }
 1789 tgl@sss.pgh.pa.us         161                 :        5651737 :     key->anynullkeys = false;    /* initial assumption */
  128 pg@bowt.ie                162                 :GNC     5651737 :     key->nextkey = false;        /* usual case, required by btinsert */
                                163                 :        5651737 :     key->backward = false;       /* usual case, required by btinsert */
 1852 pg@bowt.ie                164                 :CBC     5651737 :     key->keysz = Min(indnkeyatts, tupnatts);
                                165         [ +  + ]:        5651737 :     key->scantid = key->heapkeyspace && itup ?
                                166         [ +  - ]:       11303474 :         BTreeTupleGetHeapTID(itup) : NULL;
                                167                 :        5651737 :     skey = key->scankeys;
 2199 teodor@sigaev.ru          168         [ +  + ]:       15067511 :     for (i = 0; i < indnkeyatts; i++)
                                169                 :                :     {
                                170                 :                :         FmgrInfo   *procinfo;
                                171                 :                :         Datum       arg;
                                172                 :                :         bool        null;
                                173                 :                :         int         flags;
                                174                 :                : 
                                175                 :                :         /*
                                176                 :                :          * We can use the cached (default) support procs since no cross-type
                                177                 :                :          * comparison can be needed.
                                178                 :                :          */
 8226 tgl@sss.pgh.pa.us         179                 :        9415774 :         procinfo = index_getprocinfo(rel, i + 1, BTORDER_PROC);
                                180                 :                : 
                                181                 :                :         /*
                                182                 :                :          * Key arguments built from truncated attributes (or when caller
                                183                 :                :          * provides no tuple) are defensively represented as NULL values. They
                                184                 :                :          * should never be used.
                                185                 :                :          */
 1852 pg@bowt.ie                186         [ +  + ]:        9415774 :         if (i < tupnatts)
                                187                 :        9300948 :             arg = index_getattr(itup, i + 1, itupdesc, &null);
                                188                 :                :         else
                                189                 :                :         {
                                190                 :         114826 :             arg = (Datum) 0;
                                191                 :         114826 :             null = true;
                                192                 :                :         }
                                193                 :        9415774 :         flags = (null ? SK_ISNULL : 0) | (indoption[i] << SK_BT_INDOPTION_SHIFT);
 8226 tgl@sss.pgh.pa.us         194                 :        9415774 :         ScanKeyEntryInitializeWithInfo(&skey[i],
                                195                 :                :                                        flags,
                                196                 :        9415774 :                                        (AttrNumber) (i + 1),
                                197                 :                :                                        InvalidStrategy,
                                198                 :                :                                        InvalidOid,
 4751                           199                 :        9415774 :                                        rel->rd_indcollation[i],
                                200                 :                :                                        procinfo,
                                201                 :                :                                        arg);
                                202                 :                :         /* Record if any key attribute is NULL (or truncated) */
 1818 pg@bowt.ie                203         [ +  + ]:        9415774 :         if (null)
                                204                 :         125114 :             key->anynullkeys = true;
                                205                 :                :     }
                                206                 :                : 
                                207                 :                :     /*
                                208                 :                :      * In NULLS NOT DISTINCT mode, we pretend that there are no null keys, so
                                209                 :                :      * that full uniqueness check is done.
                                210                 :                :      */
  801 peter@eisentraut.org      211         [ +  + ]:        5651737 :     if (rel->rd_index->indnullsnotdistinct)
                                212                 :             84 :         key->anynullkeys = false;
                                213                 :                : 
 1852 pg@bowt.ie                214                 :        5651737 :     return key;
                                215                 :                : }
                                216                 :                : 
                                217                 :                : /*
                                218                 :                :  * free a retracement stack made by _bt_search.
                                219                 :                :  */
                                220                 :                : void
10141 scrappy@hub.org           221                 :        9537512 : _bt_freestack(BTStack stack)
                                222                 :                : {
                                223                 :                :     BTStack     ostack;
                                224                 :                : 
 7403 neilc@samurai.com         225         [ +  + ]:       17547802 :     while (stack != NULL)
                                226                 :                :     {
 9716 bruce@momjian.us          227                 :        8010290 :         ostack = stack;
                                228                 :        8010290 :         stack = stack->bts_parent;
                                229                 :        8010290 :         pfree(ostack);
                                230                 :                :     }
10141 scrappy@hub.org           231                 :        9537512 : }
                                232                 :                : 
                                233                 :                : 
                                234                 :                : /*
                                235                 :                :  *  _bt_preprocess_array_keys() -- Preprocess SK_SEARCHARRAY scan keys
                                236                 :                :  *
                                237                 :                :  * If there are any SK_SEARCHARRAY scan keys, deconstruct the array(s) and
                                238                 :                :  * set up BTArrayKeyInfo info for each one that is an equality-type key.
                                239                 :                :  * Returns modified scan keys as input for further, standard preprocessing.
                                240                 :                :  *
                                241                 :                :  * Currently we perform two kinds of preprocessing to deal with redundancies.
                                242                 :                :  * For inequality array keys, it's sufficient to find the extreme element
                                243                 :                :  * value and replace the whole array with that scalar value.  This eliminates
                                244                 :                :  * all but one array element as redundant.  Similarly, we are capable of
                                245                 :                :  * "merging together" multiple equality array keys (from two or more input
                                246                 :                :  * scan keys) into a single output scan key containing only the intersecting
                                247                 :                :  * array elements.  This can eliminate many redundant array elements, as well
                                248                 :                :  * as eliminating whole array scan keys as redundant.  It can also allow us to
                                249                 :                :  * detect contradictory quals.
                                250                 :                :  *
                                251                 :                :  * It is convenient for _bt_preprocess_keys caller to have to deal with no
                                252                 :                :  * more than one equality strategy array scan key per index attribute.  We'll
                                253                 :                :  * always be able to set things up that way when complete opfamilies are used.
                                254                 :                :  * Eliminated array scan keys can be recognized as those that have had their
                                255                 :                :  * sk_strategy field set to InvalidStrategy here by us.  Caller should avoid
                                256                 :                :  * including these in the scan's so->keyData[] output array.
                                257                 :                :  *
                                258                 :                :  * We set the scan key references from the scan's BTArrayKeyInfo info array to
                                259                 :                :  * offsets into the temp modified input array returned to caller.  Scans that
                                260                 :                :  * have array keys should call _bt_preprocess_array_keys_final when standard
                                261                 :                :  * preprocessing steps are complete.  This will convert the scan key offset
                                262                 :                :  * references into references to the scan's so->keyData[] output scan keys.
                                263                 :                :  *
                                264                 :                :  * Note: the reason we need to return a temp scan key array, rather than just
                                265                 :                :  * scribbling on scan->keyData, is that callers are permitted to call btrescan
                                266                 :                :  * without supplying a new set of scankey data.
                                267                 :                :  */
                                268                 :                : static ScanKey
 4564 tgl@sss.pgh.pa.us         269                 :        6415243 : _bt_preprocess_array_keys(IndexScanDesc scan)
                                270                 :                : {
                                271                 :        6415243 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
    8 pg@bowt.ie                272                 :GNC     6415243 :     Relation    rel = scan->indexRelation;
 4564 tgl@sss.pgh.pa.us         273                 :CBC     6415243 :     int         numberOfKeys = scan->numberOfKeys;
    8 pg@bowt.ie                274                 :GNC     6415243 :     int16      *indoption = rel->rd_indoption;
                                275                 :                :     int         numArrayKeys;
                                276                 :        6415243 :     int         origarrayatt = InvalidAttrNumber,
                                277                 :        6415243 :                 origarraykey = -1;
                                278                 :        6415243 :     Oid         origelemtype = InvalidOid;
                                279                 :                :     ScanKey     cur;
                                280                 :                :     MemoryContext oldContext;
                                281                 :                :     ScanKey     arrayKeyData;   /* modified copy of scan->keyData */
                                282                 :                : 
                                283         [ -  + ]:        6415243 :     Assert(numberOfKeys);
                                284                 :                : 
                                285                 :                :     /* Quick check to see if there are any array keys */
 4564 tgl@sss.pgh.pa.us         286                 :CBC     6415243 :     numArrayKeys = 0;
    8 pg@bowt.ie                287         [ +  + ]:GNC    16773511 :     for (int i = 0; i < numberOfKeys; i++)
                                288                 :                :     {
 4564 tgl@sss.pgh.pa.us         289                 :CBC    10358268 :         cur = &scan->keyData[i];
                                290         [ +  + ]:       10358268 :         if (cur->sk_flags & SK_SEARCHARRAY)
                                291                 :                :         {
                                292                 :            494 :             numArrayKeys++;
                                293         [ -  + ]:            494 :             Assert(!(cur->sk_flags & (SK_ROW_HEADER | SK_SEARCHNULL | SK_SEARCHNOTNULL)));
                                294                 :                :             /* If any arrays are null as a whole, we can quit right now. */
                                295         [ -  + ]:            494 :             if (cur->sk_flags & SK_ISNULL)
                                296                 :                :             {
    8 pg@bowt.ie                297                 :UNC           0 :                 so->qual_ok = false;
                                298                 :              0 :                 return NULL;
                                299                 :                :             }
                                300                 :                :         }
                                301                 :                :     }
                                302                 :                : 
                                303                 :                :     /* Quit if nothing to do. */
 4564 tgl@sss.pgh.pa.us         304         [ +  + ]:CBC     6415243 :     if (numArrayKeys == 0)
    8 pg@bowt.ie                305                 :GNC     6414824 :         return NULL;
                                306                 :                : 
                                307                 :                :     /*
                                308                 :                :      * Make a scan-lifespan context to hold array-associated data, or reset it
                                309                 :                :      * if we already have one from a previous rescan cycle.
                                310                 :                :      */
 4564 tgl@sss.pgh.pa.us         311         [ +  - ]:CBC         419 :     if (so->arrayContext == NULL)
                                312                 :            419 :         so->arrayContext = AllocSetContextCreate(CurrentMemoryContext,
                                313                 :                :                                                  "BTree array context",
                                314                 :                :                                                  ALLOCSET_SMALL_SIZES);
                                315                 :                :     else
 4564 tgl@sss.pgh.pa.us         316                 :UBC           0 :         MemoryContextReset(so->arrayContext);
                                317                 :                : 
 4564 tgl@sss.pgh.pa.us         318                 :CBC         419 :     oldContext = MemoryContextSwitchTo(so->arrayContext);
                                319                 :                : 
                                320                 :                :     /* Create modifiable copy of scan->keyData in the workspace context */
    8 pg@bowt.ie                321                 :GNC         419 :     arrayKeyData = (ScanKey) palloc(numberOfKeys * sizeof(ScanKeyData));
                                322                 :            419 :     memcpy(arrayKeyData, scan->keyData, numberOfKeys * sizeof(ScanKeyData));
                                323                 :                : 
                                324                 :                :     /* Allocate space for per-array data in the workspace context */
                                325                 :            419 :     so->arrayKeys = (BTArrayKeyInfo *) palloc(numArrayKeys * sizeof(BTArrayKeyInfo));
                                326                 :                : 
                                327                 :                :     /* Allocate space for ORDER procs used to help _bt_checkkeys */
                                328                 :            419 :     so->orderProcs = (FmgrInfo *) palloc(numberOfKeys * sizeof(FmgrInfo));
                                329                 :                : 
                                330                 :                :     /* Now process each array key */
 4564 tgl@sss.pgh.pa.us         331                 :CBC         419 :     numArrayKeys = 0;
    8 pg@bowt.ie                332         [ +  + ]:GNC        1061 :     for (int i = 0; i < numberOfKeys; i++)
                                333                 :                :     {
                                334                 :                :         FmgrInfo    sortproc;
                                335                 :            645 :         FmgrInfo   *sortprocp = &sortproc;
                                336                 :                :         Oid         elemtype;
                                337                 :                :         bool        reverse;
                                338                 :                :         ArrayType  *arrayval;
                                339                 :                :         int16       elmlen;
                                340                 :                :         bool        elmbyval;
                                341                 :                :         char        elmalign;
                                342                 :                :         int         num_elems;
                                343                 :                :         Datum      *elem_values;
                                344                 :                :         bool       *elem_nulls;
                                345                 :                :         int         num_nonnulls;
                                346                 :                :         int         j;
                                347                 :                : 
                                348                 :            645 :         cur = &arrayKeyData[i];
 4564 tgl@sss.pgh.pa.us         349         [ +  + ]:CBC         645 :         if (!(cur->sk_flags & SK_SEARCHARRAY))
                                350                 :            157 :             continue;
                                351                 :                : 
                                352                 :                :         /*
                                353                 :                :          * First, deconstruct the array into elements.  Anything allocated
                                354                 :                :          * here (including a possibly detoasted array value) is in the
                                355                 :                :          * workspace context.
                                356                 :                :          */
                                357                 :            494 :         arrayval = DatumGetArrayTypeP(cur->sk_argument);
                                358                 :                :         /* We could cache this data, but not clear it's worth it */
                                359                 :            494 :         get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
                                360                 :                :                              &elmlen, &elmbyval, &elmalign);
                                361                 :            494 :         deconstruct_array(arrayval,
                                362                 :                :                           ARR_ELEMTYPE(arrayval),
                                363                 :                :                           elmlen, elmbyval, elmalign,
                                364                 :                :                           &elem_values, &elem_nulls, &num_elems);
                                365                 :                : 
                                366                 :                :         /*
                                367                 :                :          * Compress out any null elements.  We can ignore them since we assume
                                368                 :                :          * all btree operators are strict.
                                369                 :                :          */
                                370                 :            494 :         num_nonnulls = 0;
                                371         [ +  + ]:           2673 :         for (j = 0; j < num_elems; j++)
                                372                 :                :         {
                                373         [ +  - ]:           2179 :             if (!elem_nulls[j])
                                374                 :           2179 :                 elem_values[num_nonnulls++] = elem_values[j];
                                375                 :                :         }
                                376                 :                : 
                                377                 :                :         /* We could pfree(elem_nulls) now, but not worth the cycles */
                                378                 :                : 
                                379                 :                :         /* If there's no non-nulls, the scan qual is unsatisfiable */
                                380         [ -  + ]:            494 :         if (num_nonnulls == 0)
                                381                 :                :         {
    8 pg@bowt.ie                382                 :UNC           0 :             so->qual_ok = false;
 4564 tgl@sss.pgh.pa.us         383                 :UBC           0 :             break;
                                384                 :                :         }
                                385                 :                : 
                                386                 :                :         /*
                                387                 :                :          * Determine the nominal datatype of the array elements.  We have to
                                388                 :                :          * support the convention that sk_subtype == InvalidOid means the
                                389                 :                :          * opclass input type; this is a hack to simplify life for
                                390                 :                :          * ScanKeyInit().
                                391                 :                :          */
    8 pg@bowt.ie                392                 :GNC         494 :         elemtype = cur->sk_subtype;
                                393         [ -  + ]:            494 :         if (elemtype == InvalidOid)
    8 pg@bowt.ie                394                 :UNC           0 :             elemtype = rel->rd_opcintype[cur->sk_attno - 1];
    8 pg@bowt.ie                395         [ -  + ]:GNC         494 :         Assert(elemtype == ARR_ELEMTYPE(arrayval));
                                396                 :                : 
                                397                 :                :         /*
                                398                 :                :          * If the comparison operator is not equality, then the array qual
                                399                 :                :          * degenerates to a simple comparison against the smallest or largest
                                400                 :                :          * non-null array element, as appropriate.
                                401                 :                :          */
 4564 tgl@sss.pgh.pa.us         402   [ -  +  +  - ]:CBC         494 :         switch (cur->sk_strategy)
                                403                 :                :         {
 4564 tgl@sss.pgh.pa.us         404                 :UBC           0 :             case BTLessStrategyNumber:
                                405                 :                :             case BTLessEqualStrategyNumber:
                                406                 :              0 :                 cur->sk_argument =
    8 pg@bowt.ie                407                 :UNC           0 :                     _bt_find_extreme_element(scan, cur, elemtype,
                                408                 :                :                                              BTGreaterStrategyNumber,
                                409                 :                :                                              elem_values, num_nonnulls);
 4564 tgl@sss.pgh.pa.us         410                 :UBC           0 :                 continue;
 4564 tgl@sss.pgh.pa.us         411                 :CBC         491 :             case BTEqualStrategyNumber:
                                412                 :                :                 /* proceed with rest of loop */
                                413                 :            491 :                 break;
                                414                 :              3 :             case BTGreaterEqualStrategyNumber:
                                415                 :                :             case BTGreaterStrategyNumber:
                                416                 :              3 :                 cur->sk_argument =
    8 pg@bowt.ie                417                 :GNC           3 :                     _bt_find_extreme_element(scan, cur, elemtype,
                                418                 :                :                                              BTLessStrategyNumber,
                                419                 :                :                                              elem_values, num_nonnulls);
 4564 tgl@sss.pgh.pa.us         420                 :CBC           3 :                 continue;
 4564 tgl@sss.pgh.pa.us         421                 :UBC           0 :             default:
                                422         [ #  # ]:              0 :                 elog(ERROR, "unrecognized StrategyNumber: %d",
                                423                 :                :                      (int) cur->sk_strategy);
                                424                 :                :                 break;
                                425                 :                :         }
                                426                 :                : 
                                427                 :                :         /*
                                428                 :                :          * We'll need a 3-way ORDER proc to perform binary searches for the
                                429                 :                :          * next matching array element.  Set that up now.
                                430                 :                :          *
                                431                 :                :          * Array scan keys with cross-type equality operators will require a
                                432                 :                :          * separate same-type ORDER proc for sorting their array.  Otherwise,
                                433                 :                :          * sortproc just points to the same proc used during binary searches.
                                434                 :                :          */
    8 pg@bowt.ie                435                 :GNC         491 :         _bt_setup_array_cmp(scan, cur, elemtype,
                                436                 :            491 :                             &so->orderProcs[i], &sortprocp);
                                437                 :                : 
                                438                 :                :         /*
                                439                 :                :          * Sort the non-null elements and eliminate any duplicates.  We must
                                440                 :                :          * sort in the same ordering used by the index column, so that the
                                441                 :                :          * arrays can be advanced in lockstep with the scan's progress through
                                442                 :                :          * the index's key space.
                                443                 :                :          */
                                444                 :            491 :         reverse = (indoption[cur->sk_attno - 1] & INDOPTION_DESC) != 0;
                                445                 :            491 :         num_elems = _bt_sort_array_elements(cur, sortprocp, reverse,
 4564 tgl@sss.pgh.pa.us         446                 :ECB       (344) :                                             elem_values, num_nonnulls);
                                447                 :                : 
    8 pg@bowt.ie                448         [ +  + ]:GNC         491 :         if (origarrayatt == cur->sk_attno)
                                449                 :                :         {
                                450                 :              6 :             BTArrayKeyInfo *orig = &so->arrayKeys[origarraykey];
                                451                 :                : 
                                452                 :                :             /*
                                453                 :                :              * This array scan key is redundant with a previous equality
                                454                 :                :              * operator array scan key.  Merge the two arrays together to
                                455                 :                :              * eliminate contradictory non-intersecting elements (or try to).
                                456                 :                :              *
                                457                 :                :              * We merge this next array back into attribute's original array.
                                458                 :                :              */
                                459         [ -  + ]:              6 :             Assert(arrayKeyData[orig->scan_key].sk_attno == cur->sk_attno);
                                460         [ -  + ]:              6 :             Assert(arrayKeyData[orig->scan_key].sk_collation ==
                                461                 :                :                    cur->sk_collation);
                                462         [ +  - ]:              6 :             if (_bt_merge_arrays(scan, cur, sortprocp, reverse,
                                463                 :                :                                  origelemtype, elemtype,
                                464                 :                :                                  orig->elem_values, &orig->num_elems,
                                465                 :                :                                  elem_values, num_elems))
                                466                 :                :             {
                                467                 :                :                 /* Successfully eliminated this array */
                                468                 :              6 :                 pfree(elem_values);
                                469                 :                : 
                                470                 :                :                 /*
                                471                 :                :                  * If no intersecting elements remain in the original array,
                                472                 :                :                  * the scan qual is unsatisfiable
                                473                 :                :                  */
                                474         [ +  + ]:              6 :                 if (orig->num_elems == 0)
                                475                 :                :                 {
                                476                 :              3 :                     so->qual_ok = false;
                                477                 :              3 :                     break;
                                478                 :                :                 }
                                479                 :                : 
                                480                 :                :                 /*
                                481                 :                :                  * Indicate to _bt_preprocess_keys caller that it must ignore
                                482                 :                :                  * this scan key
                                483                 :                :                  */
                                484                 :              3 :                 cur->sk_strategy = InvalidStrategy;
                                485                 :              3 :                 continue;
                                486                 :                :             }
                                487                 :                : 
                                488                 :                :             /*
                                489                 :                :              * Unable to merge this array with previous array due to a lack of
                                490                 :                :              * suitable cross-type opfamily support.  Will need to keep both
                                491                 :                :              * scan keys/arrays.
                                492                 :                :              */
                                493                 :                :         }
                                494                 :                :         else
                                495                 :                :         {
                                496                 :                :             /*
                                497                 :                :              * This array is the first for current index attribute.
                                498                 :                :              *
                                499                 :                :              * If it turns out to not be the last array (that is, if the next
                                500                 :                :              * array is redundantly applied to this same index attribute),
                                501                 :                :              * we'll then treat this array as the attribute's "original" array
                                502                 :                :              * when merging.
                                503                 :                :              */
                                504                 :            485 :             origarrayatt = cur->sk_attno;
                                505                 :            485 :             origarraykey = numArrayKeys;
                                506                 :            485 :             origelemtype = elemtype;
                                507                 :                :         }
                                508                 :                : 
                                509                 :                :         /*
                                510                 :                :          * And set up the BTArrayKeyInfo data.
                                511                 :                :          *
                                512                 :                :          * Note: _bt_preprocess_array_keys_final will fix-up each array's
                                513                 :                :          * scan_key field later on, after so->keyData[] has been finalized.
                                514                 :                :          */
 4564 tgl@sss.pgh.pa.us         515                 :CBC         485 :         so->arrayKeys[numArrayKeys].scan_key = i;
                                516                 :            485 :         so->arrayKeys[numArrayKeys].num_elems = num_elems;
                                517                 :            485 :         so->arrayKeys[numArrayKeys].elem_values = elem_values;
                                518                 :            485 :         numArrayKeys++;
                                519                 :                :     }
                                520                 :                : 
                                521                 :            419 :     so->numArrayKeys = numArrayKeys;
                                522                 :                : 
                                523                 :            419 :     MemoryContextSwitchTo(oldContext);
                                524                 :                : 
    8 pg@bowt.ie                525                 :GNC         419 :     return arrayKeyData;
                                526                 :                : }
                                527                 :                : 
                                528                 :                : /*
                                529                 :                :  *  _bt_preprocess_array_keys_final() -- fix up array scan key references
                                530                 :                :  *
                                531                 :                :  * When _bt_preprocess_array_keys performed initial array preprocessing, it
                                532                 :                :  * set each array's array->scan_key to the array's arrayKeys[] entry offset
                                533                 :                :  * (that also work as references into the original scan->keyData[] array).
                                534                 :                :  * This function handles translation of the scan key references from the
                                535                 :                :  * BTArrayKeyInfo info array, from input scan key references (to the keys in
                                536                 :                :  * scan->keyData[]), into output references (to the keys in so->keyData[]).
                                537                 :                :  * Caller's keyDataMap[] array tells us how to perform this remapping.
                                538                 :                :  *
                                539                 :                :  * Also finalizes so->orderProcs[] for the scan.  Arrays already have an ORDER
                                540                 :                :  * proc, which might need to be repositioned to its so->keyData[]-wise offset
                                541                 :                :  * (very much like the remapping that we apply to array->scan_key references).
                                542                 :                :  * Non-array equality strategy scan keys (that survived preprocessing) don't
                                543                 :                :  * yet have an so->orderProcs[] entry, so we set one for them here.
                                544                 :                :  *
                                545                 :                :  * Also converts single-element array scan keys into equivalent non-array
                                546                 :                :  * equality scan keys, which decrements so->numArrayKeys.  It's possible that
                                547                 :                :  * this will leave this new btrescan without any arrays at all.  This isn't
                                548                 :                :  * necessary for correctness; it's just an optimization.  Non-array equality
                                549                 :                :  * scan keys are slightly faster than equivalent array scan keys at runtime.
                                550                 :                :  */
                                551                 :                : static void
                                552                 :            196 : _bt_preprocess_array_keys_final(IndexScanDesc scan, int *keyDataMap)
                                553                 :                : {
                                554                 :            196 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                                555                 :            196 :     Relation    rel = scan->indexRelation;
                                556                 :            196 :     int         arrayidx = 0;
                                557                 :            196 :     int         last_equal_output_ikey PG_USED_FOR_ASSERTS_ONLY = -1;
                                558                 :                : 
                                559         [ -  + ]:            196 :     Assert(so->qual_ok);
                                560                 :                : 
                                561                 :                :     /*
                                562                 :                :      * Nothing for us to do when _bt_preprocess_array_keys only had to deal
                                563                 :                :      * with array inequalities
                                564                 :                :      */
    7                           565         [ -  + ]:            196 :     if (so->numArrayKeys == 0)
    7 pg@bowt.ie                566                 :UNC           0 :         return;
                                567                 :                : 
    8 pg@bowt.ie                568         [ +  + ]:GNC         588 :     for (int output_ikey = 0; output_ikey < so->numberOfKeys; output_ikey++)
                                569                 :                :     {
                                570                 :            401 :         ScanKey     outkey = so->keyData + output_ikey;
                                571                 :                :         int         input_ikey;
                                572                 :            401 :         bool        found PG_USED_FOR_ASSERTS_ONLY = false;
                                573                 :                : 
                                574         [ -  + ]:            401 :         Assert(outkey->sk_strategy != InvalidStrategy);
                                575                 :                : 
                                576         [ +  + ]:            401 :         if (outkey->sk_strategy != BTEqualStrategyNumber)
                                577                 :              9 :             continue;
                                578                 :                : 
                                579                 :            392 :         input_ikey = keyDataMap[output_ikey];
                                580                 :                : 
                                581         [ -  + ]:            392 :         Assert(last_equal_output_ikey < output_ikey);
                                582         [ -  + ]:            392 :         Assert(last_equal_output_ikey < input_ikey);
                                583                 :            392 :         last_equal_output_ikey = output_ikey;
                                584                 :                : 
                                585                 :                :         /*
                                586                 :                :          * We're lazy about looking up ORDER procs for non-array keys, since
                                587                 :                :          * not all input keys become output keys.  Take care of it now.
                                588                 :                :          */
                                589         [ +  + ]:            392 :         if (!(outkey->sk_flags & SK_SEARCHARRAY))
                                590                 :             75 :         {
                                591                 :                :             Oid         elemtype;
                                592                 :                : 
                                593                 :                :             /* No need for an ORDER proc given an IS NULL scan key */
                                594         [ +  + ]:            127 :             if (outkey->sk_flags & SK_SEARCHNULL)
                                595                 :              3 :                 continue;
                                596                 :                : 
                                597                 :                :             /*
                                598                 :                :              * A non-required scan key doesn't need an ORDER proc, either
                                599                 :                :              * (unless it's associated with an array, which this one isn't)
                                600                 :                :              */
                                601         [ +  + ]:            124 :             if (!(outkey->sk_flags & SK_BT_REQFWD))
                                602                 :             49 :                 continue;
                                603                 :                : 
                                604                 :             75 :             elemtype = outkey->sk_subtype;
                                605         [ -  + ]:             75 :             if (elemtype == InvalidOid)
    8 pg@bowt.ie                606                 :UNC           0 :                 elemtype = rel->rd_opcintype[outkey->sk_attno - 1];
                                607                 :                : 
    8 pg@bowt.ie                608                 :GNC          75 :             _bt_setup_array_cmp(scan, outkey, elemtype,
                                609                 :             75 :                                 &so->orderProcs[output_ikey], NULL);
                                610                 :             75 :             continue;
                                611                 :                :         }
                                612                 :                : 
                                613                 :                :         /*
                                614                 :                :          * Reorder existing array scan key so->orderProcs[] entries.
                                615                 :                :          *
                                616                 :                :          * Doing this in-place is safe because preprocessing is required to
                                617                 :                :          * output all equality strategy scan keys in original input order
                                618                 :                :          * (among each group of entries against the same index attribute).
                                619                 :                :          * This is also the order that the arrays themselves appear in.
                                620                 :                :          */
                                621                 :            265 :         so->orderProcs[output_ikey] = so->orderProcs[input_ikey];
                                622                 :                : 
                                623                 :                :         /* Fix-up array->scan_key references for arrays */
                                624         [ +  - ]:            265 :         for (; arrayidx < so->numArrayKeys; arrayidx++)
                                625                 :                :         {
                                626                 :            265 :             BTArrayKeyInfo *array = &so->arrayKeys[arrayidx];
                                627                 :                : 
                                628         [ -  + ]:            265 :             Assert(array->num_elems > 0);
                                629                 :                : 
                                630         [ +  - ]:            265 :             if (array->scan_key == input_ikey)
                                631                 :                :             {
                                632                 :                :                 /* found it */
                                633                 :            265 :                 array->scan_key = output_ikey;
                                634                 :            265 :                 found = true;
                                635                 :                : 
                                636                 :                :                 /*
                                637                 :                :                  * Transform array scan keys that have exactly 1 element
                                638                 :                :                  * remaining (following all prior preprocessing) into
                                639                 :                :                  * equivalent non-array scan keys.
                                640                 :                :                  */
                                641         [ +  + ]:            265 :                 if (array->num_elems == 1)
                                642                 :                :                 {
                                643                 :             12 :                     outkey->sk_flags &= ~SK_SEARCHARRAY;
                                644                 :             12 :                     outkey->sk_argument = array->elem_values[0];
                                645                 :             12 :                     so->numArrayKeys--;
                                646                 :                : 
                                647                 :                :                     /* If we're out of array keys, we can quit right away */
                                648         [ +  + ]:             12 :                     if (so->numArrayKeys == 0)
                                649                 :              9 :                         return;
                                650                 :                : 
                                651                 :                :                     /* Shift other arrays forward */
                                652                 :              3 :                     memmove(array, array + 1,
                                653                 :                :                             sizeof(BTArrayKeyInfo) *
                                654                 :              3 :                             (so->numArrayKeys - arrayidx));
                                655                 :                : 
                                656                 :                :                     /*
                                657                 :                :                      * Don't increment arrayidx (there was an entry that was
                                658                 :                :                      * just shifted forward to the offset at arrayidx, which
                                659                 :                :                      * will still need to be matched)
                                660                 :                :                      */
                                661                 :                :                 }
                                662                 :                :                 else
                                663                 :                :                 {
                                664                 :                :                     /* Match found, so done with this array */
                                665                 :            253 :                     arrayidx++;
                                666                 :                :                 }
                                667                 :                : 
                                668                 :            256 :                 break;
                                669                 :                :             }
                                670                 :                :         }
                                671                 :                : 
                                672         [ -  + ]:            256 :         Assert(found);
                                673                 :                :     }
                                674                 :                : 
                                675                 :                :     /*
                                676                 :                :      * Parallel index scans require space in shared memory to store the
                                677                 :                :      * current array elements (for arrays kept by preprocessing) to schedule
                                678                 :                :      * the next primitive index scan.  The underlying structure is protected
                                679                 :                :      * using a spinlock, so defensively limit its size.  In practice this can
                                680                 :                :      * only affect parallel scans that use an incomplete opfamily.
                                681                 :                :      */
                                682   [ -  +  -  - ]:            187 :     if (scan->parallel_scan && so->numArrayKeys > INDEX_MAX_KEYS)
    8 pg@bowt.ie                683         [ #  # ]:UNC           0 :         ereport(ERROR,
                                684                 :                :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                                685                 :                :                  errmsg_internal("number of array scan keys left by preprocessing (%d) exceeds the maximum allowed by parallel btree index scans (%d)",
                                686                 :                :                                  so->numArrayKeys, INDEX_MAX_KEYS)));
                                687                 :                : }
                                688                 :                : 
                                689                 :                : /*
                                690                 :                :  * _bt_setup_array_cmp() -- Set up array comparison functions
                                691                 :                :  *
                                692                 :                :  * Sets ORDER proc in caller's orderproc argument, which is used during binary
                                693                 :                :  * searches of arrays during the index scan.  Also sets a same-type ORDER proc
                                694                 :                :  * in caller's *sortprocp argument, which is used when sorting the array.
                                695                 :                :  *
                                696                 :                :  * Preprocessing calls here with all equality strategy scan keys (when scan
                                697                 :                :  * uses equality array keys), including those not associated with any array.
                                698                 :                :  * See _bt_advance_array_keys for an explanation of why it'll need to treat
                                699                 :                :  * simple scalar equality scan keys as degenerate single element arrays.
                                700                 :                :  *
                                701                 :                :  * Caller should pass an orderproc pointing to space that'll store the ORDER
                                702                 :                :  * proc for the scan, and a *sortprocp pointing to its own separate space.
                                703                 :                :  * When calling here for a non-array scan key, sortprocp arg should be NULL.
                                704                 :                :  *
                                705                 :                :  * In the common case where we don't need to deal with cross-type operators,
                                706                 :                :  * only one ORDER proc is actually required by caller.  We'll set *sortprocp
                                707                 :                :  * to point to the same memory that caller's orderproc continues to point to.
                                708                 :                :  * Otherwise, *sortprocp will continue to point to caller's own space.  Either
                                709                 :                :  * way, *sortprocp will point to a same-type ORDER proc (since that's the only
                                710                 :                :  * safe way to sort/deduplicate the array associated with caller's scan key).
                                711                 :                :  */
                                712                 :                : static void
    8 pg@bowt.ie                713                 :GNC         566 : _bt_setup_array_cmp(IndexScanDesc scan, ScanKey skey, Oid elemtype,
                                714                 :                :                     FmgrInfo *orderproc, FmgrInfo **sortprocp)
                                715                 :                : {
                                716                 :            566 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                                717                 :            566 :     Relation    rel = scan->indexRelation;
                                718                 :                :     RegProcedure cmp_proc;
                                719                 :            566 :     Oid         opcintype = rel->rd_opcintype[skey->sk_attno - 1];
                                720                 :                : 
                                721         [ -  + ]:            566 :     Assert(skey->sk_strategy == BTEqualStrategyNumber);
                                722         [ -  + ]:            566 :     Assert(OidIsValid(elemtype));
                                723                 :                : 
                                724                 :                :     /*
                                725                 :                :      * If scankey operator is not a cross-type comparison, we can use the
                                726                 :                :      * cached comparison function; otherwise gotta look it up in the catalogs
                                727                 :                :      */
                                728         [ +  + ]:            566 :     if (elemtype == opcintype)
                                729                 :                :     {
                                730                 :                :         /* Set same-type ORDER procs for caller */
                                731                 :            554 :         *orderproc = *index_getprocinfo(rel, skey->sk_attno, BTORDER_PROC);
                                732         [ +  + ]:            554 :         if (sortprocp)
                                733                 :            488 :             *sortprocp = orderproc;
                                734                 :                : 
                                735                 :            554 :         return;
                                736                 :                :     }
                                737                 :                : 
                                738                 :                :     /*
                                739                 :                :      * Look up the appropriate cross-type comparison function in the opfamily.
                                740                 :                :      *
                                741                 :                :      * Use the opclass input type as the left hand arg type, and the array
                                742                 :                :      * element type as the right hand arg type (since binary searches use an
                                743                 :                :      * index tuple's attribute value to search for a matching array element).
                                744                 :                :      *
                                745                 :                :      * Note: it's possible that this would fail, if the opfamily is
                                746                 :                :      * incomplete, but only in cases where it's quite likely that _bt_first
                                747                 :                :      * would fail in just the same way (had we not failed before it could).
                                748                 :                :      */
                                749                 :             12 :     cmp_proc = get_opfamily_proc(rel->rd_opfamily[skey->sk_attno - 1],
                                750                 :                :                                  opcintype, elemtype, BTORDER_PROC);
                                751         [ -  + ]:             12 :     if (!RegProcedureIsValid(cmp_proc))
    8 pg@bowt.ie                752         [ #  # ]:UNC           0 :         elog(ERROR, "missing support function %d(%u,%u) for attribute %d of index \"%s\"",
                                753                 :                :              BTORDER_PROC, opcintype, elemtype, skey->sk_attno,
                                754                 :                :              RelationGetRelationName(rel));
                                755                 :                : 
                                756                 :                :     /* Set cross-type ORDER proc for caller */
    8 pg@bowt.ie                757                 :GNC          12 :     fmgr_info_cxt(cmp_proc, orderproc, so->arrayContext);
                                758                 :                : 
                                759                 :                :     /* Done if caller doesn't actually have an array they'll need to sort */
                                760         [ +  + ]:             12 :     if (!sortprocp)
                                761                 :              9 :         return;
                                762                 :                : 
                                763                 :                :     /*
                                764                 :                :      * Look up the appropriate same-type comparison function in the opfamily.
                                765                 :                :      *
                                766                 :                :      * Note: it's possible that this would fail, if the opfamily is
                                767                 :                :      * incomplete, but it seems quite unlikely that an opfamily would omit
                                768                 :                :      * non-cross-type comparison procs for any datatype that it supports at
                                769                 :                :      * all.
                                770                 :                :      */
                                771                 :              3 :     cmp_proc = get_opfamily_proc(rel->rd_opfamily[skey->sk_attno - 1],
                                772                 :                :                                  elemtype, elemtype, BTORDER_PROC);
                                773         [ -  + ]:              3 :     if (!RegProcedureIsValid(cmp_proc))
    8 pg@bowt.ie                774         [ #  # ]:UNC           0 :         elog(ERROR, "missing support function %d(%u,%u) for attribute %d of index \"%s\"",
                                775                 :                :              BTORDER_PROC, elemtype, elemtype,
                                776                 :                :              skey->sk_attno, RelationGetRelationName(rel));
                                777                 :                : 
                                778                 :                :     /* Set same-type ORDER proc for caller */
    8 pg@bowt.ie                779                 :GNC           3 :     fmgr_info_cxt(cmp_proc, *sortprocp, so->arrayContext);
                                780                 :                : }
                                781                 :                : 
                                782                 :                : /*
                                783                 :                :  * _bt_find_extreme_element() -- get least or greatest array element
                                784                 :                :  *
                                785                 :                :  * scan and skey identify the index column, whose opfamily determines the
                                786                 :                :  * comparison semantics.  strat should be BTLessStrategyNumber to get the
                                787                 :                :  * least element, or BTGreaterStrategyNumber to get the greatest.
                                788                 :                :  */
                                789                 :                : static Datum
                                790                 :              3 : _bt_find_extreme_element(IndexScanDesc scan, ScanKey skey, Oid elemtype,
                                791                 :                :                          StrategyNumber strat,
                                792                 :                :                          Datum *elems, int nelems)
                                793                 :                : {
 4564 tgl@sss.pgh.pa.us         794                 :CBC           3 :     Relation    rel = scan->indexRelation;
                                795                 :                :     Oid         cmp_op;
                                796                 :                :     RegProcedure cmp_proc;
                                797                 :                :     FmgrInfo    flinfo;
                                798                 :                :     Datum       result;
                                799                 :                :     int         i;
                                800                 :                : 
                                801                 :                :     /*
                                802                 :                :      * Look up the appropriate comparison operator in the opfamily.
                                803                 :                :      *
                                804                 :                :      * Note: it's possible that this would fail, if the opfamily is
                                805                 :                :      * incomplete, but it seems quite unlikely that an opfamily would omit
                                806                 :                :      * non-cross-type comparison operators for any datatype that it supports
                                807                 :                :      * at all.
                                808                 :                :      */
    8 pg@bowt.ie                809         [ -  + ]:GNC           3 :     Assert(skey->sk_strategy != BTEqualStrategyNumber);
                                810         [ -  + ]:              3 :     Assert(OidIsValid(elemtype));
 4564 tgl@sss.pgh.pa.us         811                 :CBC           3 :     cmp_op = get_opfamily_member(rel->rd_opfamily[skey->sk_attno - 1],
                                812                 :                :                                  elemtype,
                                813                 :                :                                  elemtype,
                                814                 :                :                                  strat);
                                815         [ -  + ]:              3 :     if (!OidIsValid(cmp_op))
 4564 tgl@sss.pgh.pa.us         816         [ #  # ]:UBC           0 :         elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
                                817                 :                :              strat, elemtype, elemtype,
                                818                 :                :              rel->rd_opfamily[skey->sk_attno - 1]);
 4564 tgl@sss.pgh.pa.us         819                 :CBC           3 :     cmp_proc = get_opcode(cmp_op);
                                820         [ -  + ]:              3 :     if (!RegProcedureIsValid(cmp_proc))
 4564 tgl@sss.pgh.pa.us         821         [ #  # ]:UBC           0 :         elog(ERROR, "missing oprcode for operator %u", cmp_op);
                                822                 :                : 
 4564 tgl@sss.pgh.pa.us         823                 :CBC           3 :     fmgr_info(cmp_proc, &flinfo);
                                824                 :                : 
                                825         [ -  + ]:              3 :     Assert(nelems > 0);
                                826                 :              3 :     result = elems[0];
                                827         [ +  + ]:              6 :     for (i = 1; i < nelems; i++)
                                828                 :                :     {
                                829         [ -  + ]:              3 :         if (DatumGetBool(FunctionCall2Coll(&flinfo,
                                830                 :                :                                            skey->sk_collation,
                                831                 :              3 :                                            elems[i],
                                832                 :                :                                            result)))
 4564 tgl@sss.pgh.pa.us         833                 :UBC           0 :             result = elems[i];
                                834                 :                :     }
                                835                 :                : 
 4564 tgl@sss.pgh.pa.us         836                 :CBC           3 :     return result;
                                837                 :                : }
                                838                 :                : 
                                839                 :                : /*
                                840                 :                :  * _bt_sort_array_elements() -- sort and de-dup array elements
                                841                 :                :  *
                                842                 :                :  * The array elements are sorted in-place, and the new number of elements
                                843                 :                :  * after duplicate removal is returned.
                                844                 :                :  *
                                845                 :                :  * skey identifies the index column whose opfamily determines the comparison
                                846                 :                :  * semantics, and sortproc is a corresponding ORDER proc.  If reverse is true,
                                847                 :                :  * we sort in descending order.
                                848                 :                :  */
                                849                 :                : static int
    8 pg@bowt.ie                850                 :GNC         491 : _bt_sort_array_elements(ScanKey skey, FmgrInfo *sortproc, bool reverse,
                                851                 :                :                         Datum *elems, int nelems)
                                852                 :                : {
                                853                 :                :     BTSortArrayContext cxt;
                                854                 :                : 
 4564 tgl@sss.pgh.pa.us         855         [ +  + ]:CBC         491 :     if (nelems <= 1)
                                856                 :              6 :         return nelems;          /* no work to do */
                                857                 :                : 
                                858                 :                :     /* Sort the array elements */
    8 pg@bowt.ie                859                 :GNC         485 :     cxt.sortproc = sortproc;
 4564 tgl@sss.pgh.pa.us         860                 :CBC         485 :     cxt.collation = skey->sk_collation;
                                861                 :            485 :     cxt.reverse = reverse;
  432 peter@eisentraut.org      862                 :            485 :     qsort_arg(elems, nelems, sizeof(Datum),
                                863                 :                :               _bt_compare_array_elements, &cxt);
                                864                 :                : 
                                865                 :                :     /* Now scan the sorted elements and remove duplicates */
 1620 tmunro@postgresql.or      866                 :            485 :     return qunique_arg(elems, nelems, sizeof(Datum),
                                867                 :                :                        _bt_compare_array_elements, &cxt);
                                868                 :                : }
                                869                 :                : 
                                870                 :                : /*
                                871                 :                :  * _bt_merge_arrays() -- merge next array's elements into an original array
                                872                 :                :  *
                                873                 :                :  * Called when preprocessing encounters a pair of array equality scan keys,
                                874                 :                :  * both against the same index attribute (during initial array preprocessing).
                                875                 :                :  * Merging reorganizes caller's original array (the left hand arg) in-place,
                                876                 :                :  * without ever copying elements from one array into the other. (Mixing the
                                877                 :                :  * elements together like this would be wrong, since they don't necessarily
                                878                 :                :  * use the same underlying element type, despite all the other similarities.)
                                879                 :                :  *
                                880                 :                :  * Both arrays must have already been sorted and deduplicated by calling
                                881                 :                :  * _bt_sort_array_elements.  sortproc is the same-type ORDER proc that was
                                882                 :                :  * just used to sort and deduplicate caller's "next" array.  We'll usually be
                                883                 :                :  * able to reuse that order PROC to merge the arrays together now.  If not,
                                884                 :                :  * then we'll perform a separate ORDER proc lookup.
                                885                 :                :  *
                                886                 :                :  * If the opfamily doesn't supply a complete set of cross-type ORDER procs we
                                887                 :                :  * may not be able to determine which elements are contradictory.  If we have
                                888                 :                :  * the required ORDER proc then we return true (and validly set *nelems_orig),
                                889                 :                :  * guaranteeing that at least the next array can be considered redundant.  We
                                890                 :                :  * return false if the required comparisons cannot not be made (caller must
                                891                 :                :  * keep both arrays when this happens).
                                892                 :                :  */
                                893                 :                : static bool
    8 pg@bowt.ie                894                 :GNC           6 : _bt_merge_arrays(IndexScanDesc scan, ScanKey skey, FmgrInfo *sortproc,
                                895                 :                :                  bool reverse, Oid origelemtype, Oid nextelemtype,
                                896                 :                :                  Datum *elems_orig, int *nelems_orig,
                                897                 :                :                  Datum *elems_next, int nelems_next)
                                898                 :                : {
                                899                 :              6 :     Relation    rel = scan->indexRelation;
 4564 tgl@sss.pgh.pa.us         900                 :              6 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                                901                 :                :     BTSortArrayContext cxt;
    8 pg@bowt.ie                902                 :              6 :     int         nelems_orig_start = *nelems_orig,
                                903                 :              6 :                 nelems_orig_merged = 0;
                                904                 :              6 :     FmgrInfo   *mergeproc = sortproc;
                                905                 :                :     FmgrInfo    crosstypeproc;
                                906                 :                : 
                                907         [ -  + ]:              6 :     Assert(skey->sk_strategy == BTEqualStrategyNumber);
                                908   [ +  -  -  + ]:              6 :     Assert(OidIsValid(origelemtype) && OidIsValid(nextelemtype));
                                909                 :                : 
                                910         [ +  + ]:              6 :     if (origelemtype != nextelemtype)
                                911                 :                :     {
                                912                 :                :         RegProcedure cmp_proc;
                                913                 :                : 
                                914                 :                :         /*
                                915                 :                :          * Cross-array-element-type merging is required, so can't just reuse
                                916                 :                :          * sortproc when merging
                                917                 :                :          */
                                918                 :              3 :         cmp_proc = get_opfamily_proc(rel->rd_opfamily[skey->sk_attno - 1],
                                919                 :                :                                      origelemtype, nextelemtype, BTORDER_PROC);
                                920         [ -  + ]:              3 :         if (!RegProcedureIsValid(cmp_proc))
                                921                 :                :         {
                                922                 :                :             /* Can't make the required comparisons */
    8 pg@bowt.ie                923                 :UNC           0 :             return false;
                                924                 :                :         }
                                925                 :                : 
                                926                 :                :         /* We have all we need to determine redundancy/contradictoriness */
    8 pg@bowt.ie                927                 :GNC           3 :         mergeproc = &crosstypeproc;
                                928                 :              3 :         fmgr_info_cxt(cmp_proc, mergeproc, so->arrayContext);
                                929                 :                :     }
                                930                 :                : 
                                931                 :              6 :     cxt.sortproc = mergeproc;
                                932                 :              6 :     cxt.collation = skey->sk_collation;
                                933                 :              6 :     cxt.reverse = reverse;
                                934                 :                : 
                                935   [ +  +  +  + ]:             27 :     for (int i = 0, j = 0; i < nelems_orig_start && j < nelems_next;)
                                936                 :                :     {
                                937                 :             21 :         Datum      *oelem = elems_orig + i,
                                938                 :             21 :                    *nelem = elems_next + j;
                                939                 :             21 :         int         res = _bt_compare_array_elements(oelem, nelem, &cxt);
                                940                 :                : 
                                941         [ +  + ]:             21 :         if (res == 0)
                                942                 :                :         {
                                943                 :              3 :             elems_orig[nelems_orig_merged++] = *oelem;
                                944                 :              3 :             i++;
                                945                 :              3 :             j++;
                                946                 :                :         }
                                947         [ +  + ]:             18 :         else if (res < 0)
                                948                 :             12 :             i++;
                                949                 :                :         else                    /* res > 0 */
                                950                 :              6 :             j++;
                                951                 :                :     }
                                952                 :                : 
                                953                 :              6 :     *nelems_orig = nelems_orig_merged;
                                954                 :                : 
                                955                 :              6 :     return true;
                                956                 :                : }
                                957                 :                : 
                                958                 :                : /*
                                959                 :                :  * Compare an array scan key to a scalar scan key, eliminating contradictory
                                960                 :                :  * array elements such that the scalar scan key becomes redundant.
                                961                 :                :  *
                                962                 :                :  * Array elements can be eliminated as contradictory when excluded by some
                                963                 :                :  * other operator on the same attribute.  For example, with an index scan qual
                                964                 :                :  * "WHERE a IN (1, 2, 3) AND a < 2", all array elements except the value "1"
                                965                 :                :  * are eliminated, and the < scan key is eliminated as redundant.  Cases where
                                966                 :                :  * every array element is eliminated by a redundant scalar scan key have an
                                967                 :                :  * unsatisfiable qual, which we handle by setting *qual_ok=false for caller.
                                968                 :                :  *
                                969                 :                :  * If the opfamily doesn't supply a complete set of cross-type ORDER procs we
                                970                 :                :  * may not be able to determine which elements are contradictory.  If we have
                                971                 :                :  * the required ORDER proc then we return true (and validly set *qual_ok),
                                972                 :                :  * guaranteeing that at least the scalar scan key can be considered redundant.
                                973                 :                :  * We return false if the comparison could not be made (caller must keep both
                                974                 :                :  * scan keys when this happens).
                                975                 :                :  */
                                976                 :                : static bool
                                977                 :             15 : _bt_compare_array_scankey_args(IndexScanDesc scan, ScanKey arraysk, ScanKey skey,
                                978                 :                :                                FmgrInfo *orderproc, BTArrayKeyInfo *array,
                                979                 :                :                                bool *qual_ok)
                                980                 :                : {
                                981                 :             15 :     Relation    rel = scan->indexRelation;
                                982                 :             15 :     Oid         opcintype = rel->rd_opcintype[arraysk->sk_attno - 1];
                                983                 :             15 :     int         cmpresult = 0,
                                984                 :             15 :                 cmpexact = 0,
                                985                 :                :                 matchelem,
                                986                 :             15 :                 new_nelems = 0;
                                987                 :                :     FmgrInfo    crosstypeproc;
                                988                 :             15 :     FmgrInfo   *orderprocp = orderproc;
                                989                 :                : 
                                990         [ -  + ]:             15 :     Assert(arraysk->sk_attno == skey->sk_attno);
                                991         [ -  + ]:             15 :     Assert(array->num_elems > 0);
                                992         [ -  + ]:             15 :     Assert(!(arraysk->sk_flags & (SK_ISNULL | SK_ROW_HEADER | SK_ROW_MEMBER)));
                                993   [ +  -  -  + ]:             15 :     Assert((arraysk->sk_flags & SK_SEARCHARRAY) &&
                                994                 :                :            arraysk->sk_strategy == BTEqualStrategyNumber);
                                995         [ -  + ]:             15 :     Assert(!(skey->sk_flags & (SK_ISNULL | SK_ROW_HEADER | SK_ROW_MEMBER)));
                                996   [ -  +  -  - ]:             15 :     Assert(!(skey->sk_flags & SK_SEARCHARRAY) ||
                                997                 :                :            skey->sk_strategy != BTEqualStrategyNumber);
                                998                 :                : 
                                999                 :                :     /*
                               1000                 :                :      * _bt_binsrch_array_skey searches an array for the entry best matching a
                               1001                 :                :      * datum of opclass input type for the index's attribute (on-disk type).
                               1002                 :                :      * We can reuse the array's ORDER proc whenever the non-array scan key's
                               1003                 :                :      * type is a match for the corresponding attribute's input opclass type.
                               1004                 :                :      * Otherwise, we have to do another ORDER proc lookup so that our call to
                               1005                 :                :      * _bt_binsrch_array_skey applies the correct comparator.
                               1006                 :                :      *
                               1007                 :                :      * Note: we have to support the convention that sk_subtype == InvalidOid
                               1008                 :                :      * means the opclass input type; this is a hack to simplify life for
                               1009                 :                :      * ScanKeyInit().
                               1010                 :                :      */
                               1011   [ +  +  +  - ]:             15 :     if (skey->sk_subtype != opcintype && skey->sk_subtype != InvalidOid)
                               1012                 :                :     {
                               1013                 :                :         RegProcedure cmp_proc;
                               1014                 :                :         Oid         arraysk_elemtype;
                               1015                 :                : 
                               1016                 :                :         /*
                               1017                 :                :          * Need an ORDER proc lookup to detect redundancy/contradictoriness
                               1018                 :                :          * with this pair of scankeys.
                               1019                 :                :          *
                               1020                 :                :          * Scalar scan key's argument will be passed to _bt_compare_array_skey
                               1021                 :                :          * as its tupdatum/lefthand argument (rhs arg is for array elements).
                               1022                 :                :          */
                               1023                 :              3 :         arraysk_elemtype = arraysk->sk_subtype;
                               1024         [ -  + ]:              3 :         if (arraysk_elemtype == InvalidOid)
    8 pg@bowt.ie               1025                 :UNC           0 :             arraysk_elemtype = rel->rd_opcintype[arraysk->sk_attno - 1];
    8 pg@bowt.ie               1026                 :GNC           3 :         cmp_proc = get_opfamily_proc(rel->rd_opfamily[arraysk->sk_attno - 1],
                               1027                 :                :                                      skey->sk_subtype, arraysk_elemtype,
                               1028                 :                :                                      BTORDER_PROC);
                               1029         [ -  + ]:              3 :         if (!RegProcedureIsValid(cmp_proc))
                               1030                 :                :         {
                               1031                 :                :             /* Can't make the comparison */
    8 pg@bowt.ie               1032                 :UNC           0 :             *qual_ok = false;   /* suppress compiler warnings */
                               1033                 :              0 :             return false;
                               1034                 :                :         }
                               1035                 :                : 
                               1036                 :                :         /* We have all we need to determine redundancy/contradictoriness */
    8 pg@bowt.ie               1037                 :GNC           3 :         orderprocp = &crosstypeproc;
                               1038                 :              3 :         fmgr_info(cmp_proc, orderprocp);
                               1039                 :                :     }
                               1040                 :                : 
                               1041                 :             15 :     matchelem = _bt_binsrch_array_skey(orderprocp, false,
                               1042                 :                :                                        NoMovementScanDirection,
                               1043                 :                :                                        skey->sk_argument, false, array,
                               1044                 :                :                                        arraysk, &cmpresult);
                               1045                 :                : 
                               1046   [ +  -  +  +  :             15 :     switch (skey->sk_strategy)
                                              +  - ]
                               1047                 :                :     {
                               1048                 :              3 :         case BTLessStrategyNumber:
                               1049                 :              3 :             cmpexact = 1;       /* exclude exact match, if any */
                               1050                 :                :             /* FALL THRU */
                               1051                 :              3 :         case BTLessEqualStrategyNumber:
                               1052         [ -  + ]:              3 :             if (cmpresult >= cmpexact)
    8 pg@bowt.ie               1053                 :UNC           0 :                 matchelem++;
                               1054                 :                :             /* Resize, keeping elements from the start of the array */
    8 pg@bowt.ie               1055                 :GNC           3 :             new_nelems = matchelem;
                               1056                 :              3 :             break;
                               1057                 :              6 :         case BTEqualStrategyNumber:
                               1058         [ +  + ]:              6 :             if (cmpresult != 0)
                               1059                 :                :             {
                               1060                 :                :                 /* qual is unsatisfiable */
                               1061                 :              3 :                 new_nelems = 0;
                               1062                 :                :             }
                               1063                 :                :             else
                               1064                 :                :             {
                               1065                 :                :                 /* Shift matching element to the start of the array, resize */
                               1066                 :              3 :                 array->elem_values[0] = array->elem_values[matchelem];
                               1067                 :              3 :                 new_nelems = 1;
                               1068                 :                :             }
                               1069                 :              6 :             break;
                               1070                 :              3 :         case BTGreaterEqualStrategyNumber:
                               1071                 :              3 :             cmpexact = 1;       /* include exact match, if any */
                               1072                 :                :             /* FALL THRU */
                               1073                 :              6 :         case BTGreaterStrategyNumber:
                               1074         [ +  + ]:              6 :             if (cmpresult >= cmpexact)
                               1075                 :              3 :                 matchelem++;
                               1076                 :                :             /* Shift matching elements to the start of the array, resize */
                               1077                 :              6 :             new_nelems = array->num_elems - matchelem;
                               1078                 :              6 :             memmove(array->elem_values, array->elem_values + matchelem,
                               1079                 :                :                     sizeof(Datum) * new_nelems);
                               1080                 :              6 :             break;
    8 pg@bowt.ie               1081                 :UNC           0 :         default:
                               1082         [ #  # ]:              0 :             elog(ERROR, "unrecognized StrategyNumber: %d",
                               1083                 :                :                  (int) skey->sk_strategy);
                               1084                 :                :             break;
                               1085                 :                :     }
                               1086                 :                : 
    8 pg@bowt.ie               1087         [ -  + ]:GNC          15 :     Assert(new_nelems >= 0);
                               1088         [ -  + ]:             15 :     Assert(new_nelems <= array->num_elems);
                               1089                 :                : 
                               1090                 :             15 :     array->num_elems = new_nelems;
                               1091                 :             15 :     *qual_ok = new_nelems > 0;
                               1092                 :                : 
                               1093                 :             15 :     return true;
                               1094                 :                : }
                               1095                 :                : 
                               1096                 :                : /*
                               1097                 :                :  * qsort_arg comparator for sorting array elements
                               1098                 :                :  */
                               1099                 :                : static int
    8 pg@bowt.ie               1100                 :CBC        4538 : _bt_compare_array_elements(const void *a, const void *b, void *arg)
                               1101                 :                : {
                               1102                 :           4538 :     Datum       da = *((const Datum *) a);
                               1103                 :           4538 :     Datum       db = *((const Datum *) b);
                               1104                 :           4538 :     BTSortArrayContext *cxt = (BTSortArrayContext *) arg;
                               1105                 :                :     int32       compare;
                               1106                 :                : 
    8 pg@bowt.ie               1107                 :GNC        4538 :     compare = DatumGetInt32(FunctionCall2Coll(cxt->sortproc,
                               1108                 :                :                                               cxt->collation,
                               1109                 :                :                                               da, db));
    8 pg@bowt.ie               1110         [ +  + ]:CBC        4538 :     if (cxt->reverse)
    8 pg@bowt.ie               1111         [ -  + ]:GBC          15 :         INVERT_COMPARE_RESULT(compare);
    8 pg@bowt.ie               1112                 :CBC        4538 :     return compare;
                               1113                 :                : }
                               1114                 :                : 
                               1115                 :                : /*
                               1116                 :                :  * _bt_compare_array_skey() -- apply array comparison function
                               1117                 :                :  *
                               1118                 :                :  * Compares caller's tuple attribute value to a scan key/array element.
                               1119                 :                :  * Helper function used during binary searches of SK_SEARCHARRAY arrays.
                               1120                 :                :  *
                               1121                 :                :  *      This routine returns:
                               1122                 :                :  *          <0 if tupdatum < arrdatum;
                               1123                 :                :  *           0 if tupdatum == arrdatum;
                               1124                 :                :  *          >0 if tupdatum > arrdatum.
                               1125                 :                :  *
                               1126                 :                :  * This is essentially the same interface as _bt_compare: both functions
                               1127                 :                :  * compare the value that they're searching for to a binary search pivot.
                               1128                 :                :  * However, unlike _bt_compare, this function's "tuple argument" comes first,
                               1129                 :                :  * while its "array/scankey argument" comes second.
                               1130                 :                : */
                               1131                 :                : static inline int32
    8 pg@bowt.ie               1132                 :GNC        9033 : _bt_compare_array_skey(FmgrInfo *orderproc,
                               1133                 :                :                        Datum tupdatum, bool tupnull,
                               1134                 :                :                        Datum arrdatum, ScanKey cur)
                               1135                 :                : {
                               1136                 :           9033 :     int32       result = 0;
                               1137                 :                : 
                               1138         [ -  + ]:           9033 :     Assert(cur->sk_strategy == BTEqualStrategyNumber);
                               1139                 :                : 
                               1140         [ +  + ]:           9033 :     if (tupnull)                /* NULL tupdatum */
                               1141                 :                :     {
                               1142         [ +  - ]:              3 :         if (cur->sk_flags & SK_ISNULL)
                               1143                 :              3 :             result = 0;         /* NULL "=" NULL */
    8 pg@bowt.ie               1144         [ #  # ]:UNC           0 :         else if (cur->sk_flags & SK_BT_NULLS_FIRST)
                               1145                 :              0 :             result = -1;        /* NULL "<" NOT_NULL */
                               1146                 :                :         else
                               1147                 :              0 :             result = 1;         /* NULL ">" NOT_NULL */
                               1148                 :                :     }
    8 pg@bowt.ie               1149         [ +  + ]:GNC        9030 :     else if (cur->sk_flags & SK_ISNULL) /* NOT_NULL tupdatum, NULL arrdatum */
                               1150                 :                :     {
                               1151         [ -  + ]:              6 :         if (cur->sk_flags & SK_BT_NULLS_FIRST)
    8 pg@bowt.ie               1152                 :UNC           0 :             result = 1;         /* NOT_NULL ">" NULL */
                               1153                 :                :         else
    8 pg@bowt.ie               1154                 :GNC           6 :             result = -1;        /* NOT_NULL "<" NULL */
                               1155                 :                :     }
                               1156                 :                :     else
                               1157                 :                :     {
                               1158                 :                :         /*
                               1159                 :                :          * Like _bt_compare, we need to be careful of cross-type comparisons,
                               1160                 :                :          * so the left value has to be the value that came from an index tuple
                               1161                 :                :          */
                               1162                 :           9024 :         result = DatumGetInt32(FunctionCall2Coll(orderproc, cur->sk_collation,
                               1163                 :                :                                                  tupdatum, arrdatum));
                               1164                 :                : 
                               1165                 :                :         /*
                               1166                 :                :          * We flip the sign by following the obvious rule: flip whenever the
                               1167                 :                :          * column is a DESC column.
                               1168                 :                :          *
                               1169                 :                :          * _bt_compare does it the wrong way around (flip when *ASC*) in order
                               1170                 :                :          * to compensate for passing its orderproc arguments backwards.  We
                               1171                 :                :          * don't need to play these games because we find it natural to pass
                               1172                 :                :          * tupdatum as the left value (and arrdatum as the right value).
                               1173                 :                :          */
                               1174         [ +  + ]:           9024 :         if (cur->sk_flags & SK_BT_DESC)
                               1175         [ +  + ]:             21 :             INVERT_COMPARE_RESULT(result);
                               1176                 :                :     }
                               1177                 :                : 
                               1178                 :           9033 :     return result;
                               1179                 :                : }
                               1180                 :                : 
                               1181                 :                : /*
                               1182                 :                :  * _bt_binsrch_array_skey() -- Binary search for next matching array key
                               1183                 :                :  *
                               1184                 :                :  * Returns an index to the first array element >= caller's tupdatum argument.
                               1185                 :                :  * This convention is more natural for forwards scan callers, but that can't
                               1186                 :                :  * really matter to backwards scan callers.  Both callers require handling for
                               1187                 :                :  * the case where the match we return is < tupdatum, and symmetric handling
                               1188                 :                :  * for the case where our best match is > tupdatum.
                               1189                 :                :  *
                               1190                 :                :  * Also sets *set_elem_result to the result _bt_compare_array_skey returned
                               1191                 :                :  * when we used it to compare the matching array element to tupdatum/tupnull.
                               1192                 :                :  *
                               1193                 :                :  * cur_elem_trig indicates if array advancement was triggered by this array's
                               1194                 :                :  * scan key, and that the array is for a required scan key.  We can apply this
                               1195                 :                :  * information to find the next matching array element in the current scan
                               1196                 :                :  * direction using far fewer comparisons (fewer on average, compared to naive
                               1197                 :                :  * binary search).  This scheme takes advantage of an important property of
                               1198                 :                :  * required arrays: required arrays always advance in lockstep with the index
                               1199                 :                :  * scan's progress through the index's key space.
                               1200                 :                :  */
                               1201                 :                : static int
                               1202                 :           2033 : _bt_binsrch_array_skey(FmgrInfo *orderproc,
                               1203                 :                :                        bool cur_elem_trig, ScanDirection dir,
                               1204                 :                :                        Datum tupdatum, bool tupnull,
                               1205                 :                :                        BTArrayKeyInfo *array, ScanKey cur,
                               1206                 :                :                        int32 *set_elem_result)
                               1207                 :                : {
                               1208                 :           2033 :     int         low_elem = 0,
                               1209                 :           2033 :                 mid_elem = -1,
                               1210                 :           2033 :                 high_elem = array->num_elems - 1,
                               1211                 :           2033 :                 result = 0;
                               1212                 :                :     Datum       arrdatum;
                               1213                 :                : 
                               1214         [ -  + ]:           2033 :     Assert(cur->sk_flags & SK_SEARCHARRAY);
                               1215         [ -  + ]:           2033 :     Assert(cur->sk_strategy == BTEqualStrategyNumber);
                               1216                 :                : 
                               1217         [ +  + ]:           2033 :     if (cur_elem_trig)
                               1218                 :                :     {
                               1219         [ -  + ]:           1790 :         Assert(!ScanDirectionIsNoMovement(dir));
                               1220         [ -  + ]:           1790 :         Assert(cur->sk_flags & SK_BT_REQFWD);
                               1221                 :                : 
                               1222                 :                :         /*
                               1223                 :                :          * When the scan key that triggered array advancement is a required
                               1224                 :                :          * array scan key, it is now certain that the current array element
                               1225                 :                :          * (plus all prior elements relative to the current scan direction)
                               1226                 :                :          * cannot possibly be at or ahead of the corresponding tuple value.
                               1227                 :                :          * (_bt_checkkeys must have called _bt_tuple_before_array_skeys, which
                               1228                 :                :          * makes sure this is true as a condition of advancing the arrays.)
                               1229                 :                :          *
                               1230                 :                :          * This makes it safe to exclude array elements up to and including
                               1231                 :                :          * the former-current array element from our search.
                               1232                 :                :          *
                               1233                 :                :          * Separately, when array advancement was triggered by a required scan
                               1234                 :                :          * key, the array element immediately after the former-current element
                               1235                 :                :          * is often either an exact tupdatum match, or a "close by" near-match
                               1236                 :                :          * (a near-match tupdatum is one whose key space falls _between_ the
                               1237                 :                :          * former-current and new-current array elements).  We'll detect both
                               1238                 :                :          * cases via an optimistic comparison of the new search lower bound
                               1239                 :                :          * (or new search upper bound in the case of backwards scans).
                               1240                 :                :          */
                               1241         [ +  + ]:           1790 :         if (ScanDirectionIsForward(dir))
                               1242                 :                :         {
                               1243                 :           1778 :             low_elem = array->cur_elem + 1; /* old cur_elem exhausted */
                               1244                 :                : 
                               1245                 :                :             /* Compare prospective new cur_elem (also the new lower bound) */
                               1246         [ +  + ]:           1778 :             if (high_elem >= low_elem)
                               1247                 :                :             {
                               1248                 :           1479 :                 arrdatum = array->elem_values[low_elem];
                               1249                 :           1479 :                 result = _bt_compare_array_skey(orderproc, tupdatum, tupnull,
                               1250                 :                :                                                 arrdatum, cur);
                               1251                 :                : 
                               1252         [ +  + ]:           1479 :                 if (result <= 0)
                               1253                 :                :                 {
                               1254                 :                :                     /* Optimistic comparison optimization worked out */
                               1255                 :           1449 :                     *set_elem_result = result;
                               1256                 :           1449 :                     return low_elem;
                               1257                 :                :                 }
                               1258                 :             30 :                 mid_elem = low_elem;
                               1259                 :             30 :                 low_elem++;     /* this cur_elem exhausted, too */
                               1260                 :                :             }
                               1261                 :                : 
                               1262         [ +  + ]:            329 :             if (high_elem < low_elem)
                               1263                 :                :             {
                               1264                 :                :                 /* Caller needs to perform "beyond end" array advancement */
                               1265                 :            299 :                 *set_elem_result = 1;
                               1266                 :            299 :                 return high_elem;
                               1267                 :                :             }
                               1268                 :                :         }
                               1269                 :                :         else
                               1270                 :                :         {
                               1271                 :             12 :             high_elem = array->cur_elem - 1; /* old cur_elem exhausted */
                               1272                 :                : 
                               1273                 :                :             /* Compare prospective new cur_elem (also the new upper bound) */
                               1274         [ +  + ]:             12 :             if (high_elem >= low_elem)
                               1275                 :                :             {
                               1276                 :              9 :                 arrdatum = array->elem_values[high_elem];
                               1277                 :              9 :                 result = _bt_compare_array_skey(orderproc, tupdatum, tupnull,
                               1278                 :                :                                                 arrdatum, cur);
                               1279                 :                : 
                               1280         [ +  - ]:              9 :                 if (result >= 0)
                               1281                 :                :                 {
                               1282                 :                :                     /* Optimistic comparison optimization worked out */
                               1283                 :              9 :                     *set_elem_result = result;
                               1284                 :              9 :                     return high_elem;
                               1285                 :                :                 }
    8 pg@bowt.ie               1286                 :UNC           0 :                 mid_elem = high_elem;
                               1287                 :              0 :                 high_elem--;    /* this cur_elem exhausted, too */
                               1288                 :                :             }
                               1289                 :                : 
    8 pg@bowt.ie               1290         [ +  - ]:GNC           3 :             if (high_elem < low_elem)
                               1291                 :                :             {
                               1292                 :                :                 /* Caller needs to perform "beyond end" array advancement */
                               1293                 :              3 :                 *set_elem_result = -1;
                               1294                 :              3 :                 return low_elem;
                               1295                 :                :             }
                               1296                 :                :         }
                               1297                 :                :     }
                               1298                 :                : 
                               1299         [ +  + ]:            501 :     while (high_elem > low_elem)
                               1300                 :                :     {
                               1301                 :            299 :         mid_elem = low_elem + ((high_elem - low_elem) / 2);
                               1302                 :            299 :         arrdatum = array->elem_values[mid_elem];
                               1303                 :                : 
                               1304                 :            299 :         result = _bt_compare_array_skey(orderproc, tupdatum, tupnull,
                               1305                 :                :                                         arrdatum, cur);
                               1306                 :                : 
                               1307         [ +  + ]:            299 :         if (result == 0)
                               1308                 :                :         {
                               1309                 :                :             /*
                               1310                 :                :              * It's safe to quit as soon as we see an equal array element.
                               1311                 :                :              * This often saves an extra comparison or two...
                               1312                 :                :              */
                               1313                 :             71 :             low_elem = mid_elem;
 4564 tgl@sss.pgh.pa.us        1314                 :             71 :             break;
                               1315                 :                :         }
                               1316                 :                : 
    8 pg@bowt.ie               1317         [ +  + ]:            228 :         if (result > 0)
                               1318                 :            201 :             low_elem = mid_elem + 1;
                               1319                 :                :         else
                               1320                 :             27 :             high_elem = mid_elem;
                               1321                 :                :     }
                               1322                 :                : 
                               1323                 :                :     /*
                               1324                 :                :      * ...but our caller also cares about how its searched-for tuple datum
                               1325                 :                :      * compares to the low_elem datum.  Must always set *set_elem_result with
                               1326                 :                :      * the result of that comparison specifically.
                               1327                 :                :      */
                               1328         [ +  + ]:            273 :     if (low_elem != mid_elem)
                               1329                 :            181 :         result = _bt_compare_array_skey(orderproc, tupdatum, tupnull,
                               1330                 :            181 :                                         array->elem_values[low_elem], cur);
                               1331                 :                : 
                               1332                 :            273 :     *set_elem_result = result;
                               1333                 :                : 
                               1334                 :            273 :     return low_elem;
                               1335                 :                : }
                               1336                 :                : 
                               1337                 :                : /*
                               1338                 :                :  * _bt_start_array_keys() -- Initialize array keys at start of a scan
                               1339                 :                :  *
                               1340                 :                :  * Set up the cur_elem counters and fill in the first sk_argument value for
                               1341                 :                :  * each array scankey.
                               1342                 :                :  */
                               1343                 :                : void
    8 pg@bowt.ie               1344                 :CBC         724 : _bt_start_array_keys(IndexScanDesc scan, ScanDirection dir)
                               1345                 :                : {
 4217 tgl@sss.pgh.pa.us        1346                 :            724 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               1347                 :                :     int         i;
                               1348                 :                : 
    8 pg@bowt.ie               1349         [ -  + ]:GNC         724 :     Assert(so->numArrayKeys);
                               1350         [ -  + ]:            724 :     Assert(so->qual_ok);
                               1351                 :                : 
 4217 tgl@sss.pgh.pa.us        1352         [ +  + ]:CBC        1580 :     for (i = 0; i < so->numArrayKeys; i++)
                               1353                 :                :     {
                               1354                 :            856 :         BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
    8 pg@bowt.ie               1355                 :GNC         856 :         ScanKey     skey = &so->keyData[curArrayKey->scan_key];
                               1356                 :                : 
    8 pg@bowt.ie               1357         [ -  + ]:CBC         856 :         Assert(curArrayKey->num_elems > 0);
    8 pg@bowt.ie               1358         [ -  + ]:GNC         856 :         Assert(skey->sk_flags & SK_SEARCHARRAY);
                               1359                 :                : 
    8 pg@bowt.ie               1360         [ +  + ]:CBC         856 :         if (ScanDirectionIsBackward(dir))
    8 pg@bowt.ie               1361                 :GBC         398 :             curArrayKey->cur_elem = curArrayKey->num_elems - 1;
                               1362                 :                :         else
    8 pg@bowt.ie               1363                 :CBC         458 :             curArrayKey->cur_elem = 0;
                               1364                 :            856 :         skey->sk_argument = curArrayKey->elem_values[curArrayKey->cur_elem];
                               1365                 :                :     }
    8 pg@bowt.ie               1366                 :GNC         724 :     so->scanBehind = false;
 4217 tgl@sss.pgh.pa.us        1367                 :CBC         724 : }
                               1368                 :                : 
                               1369                 :                : /*
                               1370                 :                :  * _bt_advance_array_keys_increment() -- Advance to next set of array elements
                               1371                 :                :  *
                               1372                 :                :  * Advances the array keys by a single increment in the current scan
                               1373                 :                :  * direction.  When there are multiple array keys this can roll over from the
                               1374                 :                :  * lowest order array to higher order arrays.
                               1375                 :                :  *
                               1376                 :                :  * Returns true if there is another set of values to consider, false if not.
                               1377                 :                :  * On true result, the scankeys are initialized with the next set of values.
                               1378                 :                :  * On false result, the scankeys stay the same, and the array keys are not
                               1379                 :                :  * advanced (every array remains at its final element for scan direction).
                               1380                 :                :  */
                               1381                 :                : static bool
    8 pg@bowt.ie               1382                 :GNC         345 : _bt_advance_array_keys_increment(IndexScanDesc scan, ScanDirection dir)
                               1383                 :                : {
 4217 tgl@sss.pgh.pa.us        1384                 :CBC         345 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               1385                 :                : 
                               1386                 :                :     /*
                               1387                 :                :      * We must advance the last array key most quickly, since it will
                               1388                 :                :      * correspond to the lowest-order index column among the available
                               1389                 :                :      * qualifications
                               1390                 :                :      */
    8 pg@bowt.ie               1391         [ +  + ]:GNC         740 :     for (int i = so->numArrayKeys - 1; i >= 0; i--)
                               1392                 :                :     {
 4217 tgl@sss.pgh.pa.us        1393                 :CBC         411 :         BTArrayKeyInfo *curArrayKey = &so->arrayKeys[i];
    8 pg@bowt.ie               1394                 :GNC         411 :         ScanKey     skey = &so->keyData[curArrayKey->scan_key];
    8 pg@bowt.ie               1395                 :CBC         411 :         int         cur_elem = curArrayKey->cur_elem;
                               1396                 :            411 :         int         num_elems = curArrayKey->num_elems;
    8 pg@bowt.ie               1397                 :GNC         411 :         bool        rolled = false;
                               1398                 :                : 
                               1399   [ +  +  +  + ]:            411 :         if (ScanDirectionIsForward(dir) && ++cur_elem >= num_elems)
                               1400                 :                :         {
                               1401                 :            392 :             cur_elem = 0;
                               1402                 :            392 :             rolled = true;
                               1403                 :                :         }
                               1404   [ +  +  +  - ]:             19 :         else if (ScanDirectionIsBackward(dir) && --cur_elem < 0)
                               1405                 :                :         {
                               1406                 :              3 :             cur_elem = num_elems - 1;
                               1407                 :              3 :             rolled = true;
                               1408                 :                :         }
                               1409                 :                : 
    8 pg@bowt.ie               1410                 :CBC         411 :         curArrayKey->cur_elem = cur_elem;
                               1411                 :            411 :         skey->sk_argument = curArrayKey->elem_values[cur_elem];
    8 pg@bowt.ie               1412         [ +  + ]:GNC         411 :         if (!rolled)
                               1413                 :             16 :             return true;
                               1414                 :                : 
                               1415                 :                :         /* Need to advance next array key, if any */
                               1416                 :                :     }
                               1417                 :                : 
                               1418                 :                :     /*
                               1419                 :                :      * The array keys are now exhausted.  (There isn't actually a distinct
                               1420                 :                :      * state that represents array exhaustion, since index scans don't always
                               1421                 :                :      * end after btgettuple returns "false".)
                               1422                 :                :      *
                               1423                 :                :      * Restore the array keys to the state they were in immediately before we
                               1424                 :                :      * were called.  This ensures that the arrays only ever ratchet in the
                               1425                 :                :      * current scan direction.  Without this, scans would overlook matching
                               1426                 :                :      * tuples if and when the scan's direction was subsequently reversed.
                               1427                 :                :      */
                               1428                 :            329 :     _bt_start_array_keys(scan, -dir);
                               1429                 :                : 
                               1430                 :            329 :     return false;
                               1431                 :                : }
                               1432                 :                : 
                               1433                 :                : /*
                               1434                 :                :  * _bt_rewind_nonrequired_arrays() -- Rewind non-required arrays
                               1435                 :                :  *
                               1436                 :                :  * Called when _bt_advance_array_keys decides to start a new primitive index
                               1437                 :                :  * scan on the basis of the current scan position being before the position
                               1438                 :                :  * that _bt_first is capable of repositioning the scan to by applying an
                               1439                 :                :  * inequality operator required in the opposite-to-scan direction only.
                               1440                 :                :  *
                               1441                 :                :  * Although equality strategy scan keys (for both arrays and non-arrays alike)
                               1442                 :                :  * are either marked required in both directions or in neither direction,
                               1443                 :                :  * there is a sense in which non-required arrays behave like required arrays.
                               1444                 :                :  * With a qual such as "WHERE a IN (100, 200) AND b >= 3 AND c IN (5, 6, 7)",
                               1445                 :                :  * the scan key on "c" is non-required, but nevertheless enables positioning
                               1446                 :                :  * the scan at the first tuple >= "(100, 3, 5)" on the leaf level during the
                               1447                 :                :  * first descent of the tree by _bt_first.  Later on, there could also be a
                               1448                 :                :  * second descent, that places the scan right before tuples >= "(200, 3, 5)".
                               1449                 :                :  * _bt_first must never be allowed to build an insertion scan key whose "c"
                               1450                 :                :  * entry is set to a value other than 5, the "c" array's first element/value.
                               1451                 :                :  * (Actually, it's the first in the current scan direction.  This example uses
                               1452                 :                :  * a forward scan.)
                               1453                 :                :  *
                               1454                 :                :  * Calling here resets the array scan key elements for the scan's non-required
                               1455                 :                :  * arrays.  This is strictly necessary for correctness in a subset of cases
                               1456                 :                :  * involving "required in opposite direction"-triggered primitive index scans.
                               1457                 :                :  * Not all callers are at risk of _bt_first using a non-required array like
                               1458                 :                :  * this, but advancement always resets the arrays when another primitive scan
                               1459                 :                :  * is scheduled, just to keep things simple.  Array advancement even makes
                               1460                 :                :  * sure to reset non-required arrays during scans that have no inequalities.
                               1461                 :                :  * (Advancement still won't call here when there are no inequalities, though
                               1462                 :                :  * that's just because it's all handled indirectly instead.)
                               1463                 :                :  *
                               1464                 :                :  * Note: _bt_verify_arrays_bt_first is called by an assertion to enforce that
                               1465                 :                :  * everybody got this right.
                               1466                 :                :  */
                               1467                 :                : static void
    8 pg@bowt.ie               1468                 :UNC           0 : _bt_rewind_nonrequired_arrays(IndexScanDesc scan, ScanDirection dir)
                               1469                 :                : {
    8 pg@bowt.ie               1470                 :LBC         (3) :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
    8 pg@bowt.ie               1471                 :UNC           0 :     int         arrayidx = 0;
                               1472                 :                : 
                               1473         [ #  # ]:              0 :     for (int ikey = 0; ikey < so->numberOfKeys; ikey++)
                               1474                 :                :     {
                               1475                 :              0 :         ScanKey     cur = so->keyData + ikey;
                               1476                 :              0 :         BTArrayKeyInfo *array = NULL;
                               1477                 :                :         int         first_elem_dir;
                               1478                 :                : 
                               1479         [ #  # ]:              0 :         if (!(cur->sk_flags & SK_SEARCHARRAY) ||
                               1480         [ #  # ]:              0 :             cur->sk_strategy != BTEqualStrategyNumber)
                               1481                 :              0 :             continue;
                               1482                 :                : 
                               1483                 :              0 :         array = &so->arrayKeys[arrayidx++];
                               1484         [ #  # ]:              0 :         Assert(array->scan_key == ikey);
                               1485                 :                : 
                               1486         [ #  # ]:              0 :         if ((cur->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)))
                               1487                 :              0 :             continue;
                               1488                 :                : 
                               1489         [ #  # ]:              0 :         if (ScanDirectionIsForward(dir))
                               1490                 :              0 :             first_elem_dir = 0;
                               1491                 :                :         else
                               1492                 :              0 :             first_elem_dir = array->num_elems - 1;
                               1493                 :                : 
                               1494         [ #  # ]:              0 :         if (array->cur_elem != first_elem_dir)
                               1495                 :                :         {
                               1496                 :              0 :             array->cur_elem = first_elem_dir;
                               1497                 :              0 :             cur->sk_argument = array->elem_values[first_elem_dir];
                               1498                 :                :         }
                               1499                 :                :     }
    8 pg@bowt.ie               1500                 :LBC         (3) : }
                               1501                 :                : 
                               1502                 :                : /*
                               1503                 :                :  * _bt_tuple_before_array_skeys() -- too early to advance required arrays?
                               1504                 :                :  *
                               1505                 :                :  * We always compare the tuple using the current array keys (which we assume
                               1506                 :                :  * are already set in so->keyData[]).  readpagetup indicates if tuple is the
                               1507                 :                :  * scan's current _bt_readpage-wise tuple.
                               1508                 :                :  *
                               1509                 :                :  * readpagetup callers must only call here when _bt_check_compare already set
                               1510                 :                :  * continuescan=false.  We help these callers deal with _bt_check_compare's
                               1511                 :                :  * inability to distinguishing between the < and > cases (it uses equality
                               1512                 :                :  * operator scan keys, whereas we use 3-way ORDER procs).  These callers pass
                               1513                 :                :  * a _bt_check_compare-set sktrig value that indicates which scan key
                               1514                 :                :  * triggered the call (!readpagetup callers just pass us sktrig=0 instead).
                               1515                 :                :  * This information allows us to avoid wastefully checking earlier scan keys
                               1516                 :                :  * that were already deemed to have been satisfied inside _bt_check_compare.
                               1517                 :                :  *
                               1518                 :                :  * Returns false when caller's tuple is >= the current required equality scan
                               1519                 :                :  * keys (or <=, in the case of backwards scans).  This happens to readpagetup
                               1520                 :                :  * callers when the scan has reached the point of needing its array keys
                               1521                 :                :  * advanced; caller will need to advance required and non-required arrays at
                               1522                 :                :  * scan key offsets >= sktrig, plus scan keys < sktrig iff sktrig rolls over.
                               1523                 :                :  * (When we return false to readpagetup callers, tuple can only be == current
                               1524                 :                :  * required equality scan keys when caller's sktrig indicates that the arrays
                               1525                 :                :  * need to be advanced due to an unsatisfied required inequality key trigger.)
                               1526                 :                :  *
                               1527                 :                :  * Returns true when caller passes a tuple that is < the current set of
                               1528                 :                :  * equality keys for the most significant non-equal required scan key/column
                               1529                 :                :  * (or > the keys, during backwards scans).  This happens to readpagetup
                               1530                 :                :  * callers when tuple is still before the start of matches for the scan's
                               1531                 :                :  * required equality strategy scan keys.  (sktrig can't have indicated that an
                               1532                 :                :  * inequality strategy scan key wasn't satisfied in _bt_check_compare when we
                               1533                 :                :  * return true.  In fact, we automatically return false when passed such an
                               1534                 :                :  * inequality sktrig by readpagetup callers -- _bt_check_compare's initial
                               1535                 :                :  * continuescan=false doesn't really need to be confirmed here by us.)
                               1536                 :                :  *
                               1537                 :                :  * !readpagetup callers optionally pass us *scanBehind, which tracks whether
                               1538                 :                :  * any missing truncated attributes might have affected array advancement
                               1539                 :                :  * (compared to what would happen if it was shown the first non-pivot tuple on
                               1540                 :                :  * the page to the right of caller's finaltup/high key tuple instead).  It's
                               1541                 :                :  * only possible that we'll set *scanBehind to true when caller passes us a
                               1542                 :                :  * pivot tuple (with truncated -inf attributes) that we return false for.
                               1543                 :                :  */
                               1544                 :                : static bool
    8 pg@bowt.ie               1545                 :GNC        6675 : _bt_tuple_before_array_skeys(IndexScanDesc scan, ScanDirection dir,
                               1546                 :                :                              IndexTuple tuple, TupleDesc tupdesc, int tupnatts,
                               1547                 :                :                              bool readpagetup, int sktrig, bool *scanBehind)
                               1548                 :                : {
    8 pg@bowt.ie               1549                 :CBC        6675 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               1550                 :                : 
    8 pg@bowt.ie               1551         [ -  + ]:GNC        6675 :     Assert(so->numArrayKeys);
                               1552         [ -  + ]:           6675 :     Assert(so->numberOfKeys);
                               1553   [ +  +  -  + ]:           6675 :     Assert(sktrig == 0 || readpagetup);
                               1554   [ +  +  -  + ]:           6675 :     Assert(!readpagetup || scanBehind == NULL);
                               1555                 :                : 
                               1556         [ +  + ]:           6675 :     if (scanBehind)
                               1557                 :            543 :         *scanBehind = false;
                               1558                 :                : 
                               1559         [ +  + ]:           6906 :     for (int ikey = sktrig; ikey < so->numberOfKeys; ikey++)
                               1560                 :                :     {
                               1561                 :           6898 :         ScanKey     cur = so->keyData + ikey;
                               1562                 :                :         Datum       tupdatum;
                               1563                 :                :         bool        tupnull;
                               1564                 :                :         int32       result;
                               1565                 :                : 
                               1566                 :                :         /* readpagetup calls require one ORDER proc comparison (at most) */
                               1567   [ +  +  -  + ]:           6898 :         Assert(!readpagetup || ikey == sktrig);
                               1568                 :                : 
                               1569                 :                :         /*
                               1570                 :                :          * Once we reach a non-required scan key, we're completely done.
                               1571                 :                :          *
                               1572                 :                :          * Note: we deliberately don't consider the scan direction here.
                               1573                 :                :          * _bt_advance_array_keys caller requires that we track *scanBehind
                               1574                 :                :          * without concern for scan direction.
                               1575                 :                :          */
                               1576         [ +  + ]:           6898 :         if ((cur->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) == 0)
                               1577                 :                :         {
                               1578         [ -  + ]:              3 :             Assert(!readpagetup);
                               1579   [ -  +  -  - ]:              3 :             Assert(ikey > sktrig || ikey == 0);
                               1580                 :           6667 :             return false;
                               1581                 :                :         }
                               1582                 :                : 
                               1583         [ +  + ]:           6895 :         if (cur->sk_attno > tupnatts)
                               1584                 :                :         {
                               1585         [ -  + ]:              3 :             Assert(!readpagetup);
                               1586                 :                : 
                               1587                 :                :             /*
                               1588                 :                :              * When we reach a high key's truncated attribute, assume that the
                               1589                 :                :              * tuple attribute's value is >= the scan's equality constraint
                               1590                 :                :              * scan keys (but set *scanBehind to let interested callers know
                               1591                 :                :              * that a truncated attribute might have affected our answer).
                               1592                 :                :              */
                               1593         [ +  - ]:              3 :             if (scanBehind)
                               1594                 :              3 :                 *scanBehind = true;
                               1595                 :                : 
                               1596                 :              3 :             return false;
                               1597                 :                :         }
                               1598                 :                : 
                               1599                 :                :         /*
                               1600                 :                :          * Deal with inequality strategy scan keys that _bt_check_compare set
                               1601                 :                :          * continuescan=false for
                               1602                 :                :          */
                               1603         [ +  + ]:           6892 :         if (cur->sk_strategy != BTEqualStrategyNumber)
                               1604                 :                :         {
                               1605                 :                :             /*
                               1606                 :                :              * When _bt_check_compare indicated that a required inequality
                               1607                 :                :              * scan key wasn't satisfied, there's no need to verify anything;
                               1608                 :                :              * caller always calls _bt_advance_array_keys with this sktrig.
                               1609                 :                :              */
                               1610         [ +  + ]:              9 :             if (readpagetup)
                               1611                 :              3 :                 return false;
                               1612                 :                : 
                               1613                 :                :             /*
                               1614                 :                :              * Otherwise we can't give up, since we must check all required
                               1615                 :                :              * scan keys (required in either direction) in order to correctly
                               1616                 :                :              * track *scanBehind for caller
                               1617                 :                :              */
                               1618                 :              6 :             continue;
                               1619                 :                :         }
                               1620                 :                : 
                               1621                 :           6883 :         tupdatum = index_getattr(tuple, cur->sk_attno, tupdesc, &tupnull);
                               1622                 :                : 
                               1623                 :           6883 :         result = _bt_compare_array_skey(&so->orderProcs[ikey],
                               1624                 :                :                                         tupdatum, tupnull,
                               1625                 :                :                                         cur->sk_argument, cur);
                               1626                 :                : 
                               1627                 :                :         /*
                               1628                 :                :          * Does this comparison indicate that caller must _not_ advance the
                               1629                 :                :          * scan's arrays just yet?
                               1630                 :                :          */
                               1631   [ +  +  +  +  :           6883 :         if ((ScanDirectionIsForward(dir) && result < 0) ||
                                              +  + ]
                               1632         [ +  + ]:             54 :             (ScanDirectionIsBackward(dir) && result > 0))
                               1633                 :           2495 :             return true;
                               1634                 :                : 
                               1635                 :                :         /*
                               1636                 :                :          * Does this comparison indicate that caller should now advance the
                               1637                 :                :          * scan's arrays?  (Must be if we get here during a readpagetup call.)
                               1638                 :                :          */
                               1639   [ +  +  +  + ]:           4388 :         if (readpagetup || result != 0)
                               1640                 :                :         {
                               1641         [ -  + ]:           4163 :             Assert(result != 0);
                               1642                 :           4163 :             return false;
                               1643                 :                :         }
                               1644                 :                : 
                               1645                 :                :         /*
                               1646                 :                :          * Inconclusive -- need to check later scan keys, too.
                               1647                 :                :          *
                               1648                 :                :          * This must be a finaltup precheck, or a call made from an assertion.
                               1649                 :                :          */
                               1650         [ -  + ]:            225 :         Assert(result == 0);
                               1651                 :                :     }
                               1652                 :                : 
                               1653         [ -  + ]:              8 :     Assert(!readpagetup);
                               1654                 :                : 
                               1655                 :              8 :     return false;
                               1656                 :                : }
                               1657                 :                : 
                               1658                 :                : /*
                               1659                 :                :  * _bt_start_prim_scan() -- start scheduled primitive index scan?
                               1660                 :                :  *
                               1661                 :                :  * Returns true if _bt_checkkeys scheduled another primitive index scan, just
                               1662                 :                :  * as the last one ended.  Otherwise returns false, indicating that the array
                               1663                 :                :  * keys are now fully exhausted.
                               1664                 :                :  *
                               1665                 :                :  * Only call here during scans with one or more equality type array scan keys,
                               1666                 :                :  * after _bt_first or _bt_next return false.
                               1667                 :                :  */
                               1668                 :                : bool
                               1669                 :            689 : _bt_start_prim_scan(IndexScanDesc scan, ScanDirection dir)
                               1670                 :                : {
                               1671                 :            689 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               1672                 :                : 
                               1673         [ -  + ]:            689 :     Assert(so->numArrayKeys);
                               1674                 :                : 
                               1675                 :                :     /* scanBehind flag doesn't persist across primitive index scans - reset */
                               1676                 :            689 :     so->scanBehind = false;
                               1677                 :                : 
                               1678                 :                :     /*
                               1679                 :                :      * Array keys are advanced within _bt_checkkeys when the scan reaches the
                               1680                 :                :      * leaf level (more precisely, they're advanced when the scan reaches the
                               1681                 :                :      * end of each distinct set of array elements).  This process avoids
                               1682                 :                :      * repeat access to leaf pages (across multiple primitive index scans) by
                               1683                 :                :      * advancing the scan's array keys when it allows the primitive index scan
                               1684                 :                :      * to find nearby matching tuples (or when it eliminates ranges of array
                               1685                 :                :      * key space that can't possibly be satisfied by any index tuple).
                               1686                 :                :      *
                               1687                 :                :      * _bt_checkkeys sets a simple flag variable to schedule another primitive
                               1688                 :                :      * index scan.  The flag tells us what to do.
                               1689                 :                :      *
                               1690                 :                :      * We cannot rely on _bt_first always reaching _bt_checkkeys.  There are
                               1691                 :                :      * various cases where that won't happen.  For example, if the index is
                               1692                 :                :      * completely empty, then _bt_first won't call _bt_readpage/_bt_checkkeys.
                               1693                 :                :      * We also don't expect a call to _bt_checkkeys during searches for a
                               1694                 :                :      * non-existent value that happens to be lower/higher than any existing
                               1695                 :                :      * value in the index.
                               1696                 :                :      *
                               1697                 :                :      * We don't require special handling for these cases -- we don't need to
                               1698                 :                :      * be explicitly instructed to _not_ perform another primitive index scan.
                               1699                 :                :      * It's up to code under the control of _bt_first to always set the flag
                               1700                 :                :      * when another primitive index scan will be required.
                               1701                 :                :      *
                               1702                 :                :      * This works correctly, even with the tricky cases listed above, which
                               1703                 :                :      * all involve access to leaf pages "near the boundaries of the key space"
                               1704                 :                :      * (whether it's from a leftmost/rightmost page, or an imaginary empty
                               1705                 :                :      * leaf root page).  If _bt_checkkeys cannot be reached by a primitive
                               1706                 :                :      * index scan for one set of array keys, then it also won't be reached for
                               1707                 :                :      * any later set ("later" in terms of the direction that we scan the index
                               1708                 :                :      * and advance the arrays).  The array keys won't have advanced in these
                               1709                 :                :      * cases, but that's the correct behavior (even _bt_advance_array_keys
                               1710                 :                :      * won't always advance the arrays at the point they become "exhausted").
                               1711                 :                :      */
                               1712         [ +  + ]:            689 :     if (so->needPrimScan)
                               1713                 :                :     {
                               1714         [ -  + ]:            285 :         Assert(_bt_verify_arrays_bt_first(scan, dir));
                               1715                 :                : 
                               1716                 :                :         /*
                               1717                 :                :          * Flag was set -- must call _bt_first again, which will reset the
                               1718                 :                :          * scan's needPrimScan flag
                               1719                 :                :          */
                               1720                 :            285 :         return true;
                               1721                 :                :     }
                               1722                 :                : 
                               1723                 :                :     /* The top-level index scan ran out of tuples in this scan direction */
                               1724         [ +  + ]:            404 :     if (scan->parallel_scan != NULL)
                               1725                 :             15 :         _bt_parallel_done(scan);
                               1726                 :                : 
                               1727                 :            404 :     return false;
                               1728                 :                : }
                               1729                 :                : 
                               1730                 :                : /*
                               1731                 :                :  * _bt_advance_array_keys() -- Advance array elements using a tuple
                               1732                 :                :  *
                               1733                 :                :  * The scan always gets a new qual as a consequence of calling here (except
                               1734                 :                :  * when we determine that the top-level scan has run out of matching tuples).
                               1735                 :                :  * All later _bt_check_compare calls also use the same new qual that was first
                               1736                 :                :  * used here (at least until the next call here advances the keys once again).
                               1737                 :                :  * It's convenient to structure _bt_check_compare rechecks of caller's tuple
                               1738                 :                :  * (using the new qual) as one the steps of advancing the scan's array keys,
                               1739                 :                :  * so this function works as a wrapper around _bt_check_compare.
                               1740                 :                :  *
                               1741                 :                :  * Like _bt_check_compare, we'll set pstate.continuescan on behalf of the
                               1742                 :                :  * caller, and return a boolean indicating if caller's tuple satisfies the
                               1743                 :                :  * scan's new qual.  But unlike _bt_check_compare, we set so->needPrimScan
                               1744                 :                :  * when we set continuescan=false, indicating if a new primitive index scan
                               1745                 :                :  * has been scheduled (otherwise, the top-level scan has run out of tuples in
                               1746                 :                :  * the current scan direction).
                               1747                 :                :  *
                               1748                 :                :  * Caller must use _bt_tuple_before_array_skeys to determine if the current
                               1749                 :                :  * place in the scan is >= the current array keys _before_ calling here.
                               1750                 :                :  * We're responsible for ensuring that caller's tuple is <= the newly advanced
                               1751                 :                :  * required array keys once we return.  We try to find an exact match, but
                               1752                 :                :  * failing that we'll advance the array keys to whatever set of array elements
                               1753                 :                :  * comes next in the key space for the current scan direction.  Required array
                               1754                 :                :  * keys "ratchet forwards" (or backwards).  They can only advance as the scan
                               1755                 :                :  * itself advances through the index/key space.
                               1756                 :                :  *
                               1757                 :                :  * (The rules are the same for backwards scans, except that the operators are
                               1758                 :                :  * flipped: just replace the precondition's >= operator with a <=, and the
                               1759                 :                :  * postcondition's <= operator with with a >=.  In other words, just swap the
                               1760                 :                :  * precondition with the postcondition.)
                               1761                 :                :  *
                               1762                 :                :  * We also deal with "advancing" non-required arrays here.  Callers whose
                               1763                 :                :  * sktrig scan key is non-required specify sktrig_required=false.  These calls
                               1764                 :                :  * are the only exception to the general rule about always advancing the
                               1765                 :                :  * required array keys (the scan may not even have a required array).  These
                               1766                 :                :  * callers should just pass a NULL pstate (since there is never any question
                               1767                 :                :  * of stopping the scan).  No call to _bt_tuple_before_array_skeys is required
                               1768                 :                :  * ahead of these calls (it's already clear that any required scan keys must
                               1769                 :                :  * be satisfied by caller's tuple).
                               1770                 :                :  *
                               1771                 :                :  * Note that we deal with non-array required equality strategy scan keys as
                               1772                 :                :  * degenerate single element arrays here.  Obviously, they can never really
                               1773                 :                :  * advance in the way that real arrays can, but they must still affect how we
                               1774                 :                :  * advance real array scan keys (exactly like true array equality scan keys).
                               1775                 :                :  * We have to keep around a 3-way ORDER proc for these (using the "=" operator
                               1776                 :                :  * won't do), since in general whether the tuple is < or > _any_ unsatisfied
                               1777                 :                :  * required equality key influences how the scan's real arrays must advance.
                               1778                 :                :  *
                               1779                 :                :  * Note also that we may sometimes need to advance the array keys when the
                               1780                 :                :  * existing required array keys (and other required equality keys) are already
                               1781                 :                :  * an exact match for every corresponding value from caller's tuple.  We must
                               1782                 :                :  * do this for inequalities that _bt_check_compare set continuescan=false for.
                               1783                 :                :  * They'll advance the array keys here, just like any other scan key that
                               1784                 :                :  * _bt_check_compare stops on.  (This can even happen _after_ we advance the
                               1785                 :                :  * array keys, in which case we'll advance the array keys a second time.  That
                               1786                 :                :  * way _bt_checkkeys caller always has its required arrays advance to the
                               1787                 :                :  * maximum possible extent that its tuple will allow.)
                               1788                 :                :  */
                               1789                 :                : static bool
                               1790                 :           1941 : _bt_advance_array_keys(IndexScanDesc scan, BTReadPageState *pstate,
                               1791                 :                :                        IndexTuple tuple, int tupnatts, TupleDesc tupdesc,
                               1792                 :                :                        int sktrig, bool sktrig_required)
                               1793                 :                : {
                               1794                 :           1941 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               1795                 :           1941 :     Relation    rel = scan->indexRelation;
                               1796         [ +  + ]:           1941 :     ScanDirection dir = pstate ? pstate->dir : ForwardScanDirection;
                               1797                 :           1941 :     int         arrayidx = 0;
                               1798                 :           1941 :     bool        beyond_end_advance = false,
                               1799                 :           1941 :                 has_required_opposite_direction_only = false,
                               1800                 :           1941 :                 oppodir_inequality_sktrig = false,
                               1801                 :           1941 :                 all_required_satisfied = true,
                               1802                 :           1941 :                 all_satisfied = true;
                               1803                 :                : 
                               1804         [ +  + ]:           1941 :     if (sktrig_required)
                               1805                 :                :     {
                               1806                 :                :         /*
                               1807                 :                :          * Precondition array state assertion
                               1808                 :                :          */
                               1809         [ -  + ]:           1821 :         Assert(!_bt_tuple_before_array_skeys(scan, dir, tuple, tupdesc,
                               1810                 :                :                                              tupnatts, false, 0, NULL));
                               1811                 :                : 
                               1812                 :           1821 :         so->scanBehind = false; /* reset */
                               1813                 :                : 
                               1814                 :                :         /*
                               1815                 :                :          * Required scan key wasn't satisfied, so required arrays will have to
                               1816                 :                :          * advance.  Invalidate page-level state that tracks whether the
                               1817                 :                :          * scan's required-in-opposite-direction-only keys are known to be
                               1818                 :                :          * satisfied by page's remaining tuples.
                               1819                 :                :          */
                               1820                 :           1821 :         pstate->firstmatch = false;
                               1821                 :                : 
                               1822                 :                :         /* Shouldn't have to invalidate 'prechecked', though */
                               1823         [ -  + ]:           1821 :         Assert(!pstate->prechecked);
                               1824                 :                : 
                               1825                 :                :         /*
                               1826                 :                :          * Once we return we'll have a new set of required array keys, so
                               1827                 :                :          * reset state used by "look ahead" optimization
                               1828                 :                :          */
                               1829                 :           1821 :         pstate->rechecks = 0;
                               1830                 :           1821 :         pstate->targetdistance = 0;
                               1831                 :                :     }
                               1832                 :                : 
                               1833         [ -  + ]:           1941 :     Assert(_bt_verify_keys_with_arraykeys(scan));
                               1834                 :                : 
                               1835         [ +  + ]:           5330 :     for (int ikey = 0; ikey < so->numberOfKeys; ikey++)
                               1836                 :                :     {
                               1837                 :           3497 :         ScanKey     cur = so->keyData + ikey;
                               1838                 :           3497 :         BTArrayKeyInfo *array = NULL;
                               1839                 :                :         Datum       tupdatum;
                               1840                 :           3497 :         bool        required = false,
                               1841                 :           3497 :                     required_opposite_direction_only = false,
                               1842                 :                :                     tupnull;
                               1843                 :                :         int32       result;
                               1844                 :           3497 :         int         set_elem = 0;
                               1845                 :                : 
                               1846         [ +  + ]:           3497 :         if (cur->sk_strategy == BTEqualStrategyNumber)
                               1847                 :                :         {
                               1848                 :                :             /* Manage array state */
                               1849         [ +  + ]:           3365 :             if (cur->sk_flags & SK_SEARCHARRAY)
                               1850                 :                :             {
                               1851                 :           2337 :                 array = &so->arrayKeys[arrayidx++];
                               1852         [ -  + ]:           2337 :                 Assert(array->scan_key == ikey);
                               1853                 :                :             }
                               1854                 :                :         }
                               1855                 :                :         else
                               1856                 :                :         {
                               1857                 :                :             /*
                               1858                 :                :              * Are any inequalities required in the opposite direction only
                               1859                 :                :              * present here?
                               1860                 :                :              */
                               1861         [ +  - ]:            132 :             if (((ScanDirectionIsForward(dir) &&
                               1862   [ +  +  -  + ]:            132 :                   (cur->sk_flags & (SK_BT_REQBKWD))) ||
    8 pg@bowt.ie               1863                 :UNC           0 :                  (ScanDirectionIsBackward(dir) &&
                               1864         [ #  # ]:              0 :                   (cur->sk_flags & (SK_BT_REQFWD)))))
    8 pg@bowt.ie               1865                 :GNC           9 :                 has_required_opposite_direction_only =
                               1866                 :              9 :                     required_opposite_direction_only = true;
                               1867                 :                :         }
                               1868                 :                : 
                               1869                 :                :         /* Optimization: skip over known-satisfied scan keys */
                               1870         [ +  + ]:           3497 :         if (ikey < sktrig)
                               1871                 :           1297 :             continue;
                               1872                 :                : 
                               1873         [ +  + ]:           3214 :         if (cur->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD))
                               1874                 :                :         {
                               1875         [ -  + ]:           2454 :             Assert(sktrig_required);
                               1876                 :                : 
                               1877                 :           2454 :             required = true;
                               1878                 :                : 
                               1879         [ -  + ]:           2454 :             if (cur->sk_attno > tupnatts)
                               1880                 :                :             {
                               1881                 :                :                 /* Set this just like _bt_tuple_before_array_skeys */
    8 pg@bowt.ie               1882         [ #  # ]:UNC           0 :                 Assert(sktrig < ikey);
                               1883                 :              0 :                 so->scanBehind = true;
                               1884                 :                :             }
                               1885                 :                :         }
                               1886                 :                : 
                               1887                 :                :         /*
                               1888                 :                :          * Handle a required non-array scan key that the initial call to
                               1889                 :                :          * _bt_check_compare indicated triggered array advancement, if any.
                               1890                 :                :          *
                               1891                 :                :          * The non-array scan key's strategy will be <, <=, or = during a
                               1892                 :                :          * forwards scan (or any one of =, >=, or > during a backwards scan).
                               1893                 :                :          * It follows that the corresponding tuple attribute's value must now
                               1894                 :                :          * be either > or >= the scan key value (for backwards scans it must
                               1895                 :                :          * be either < or <= that value).
                               1896                 :                :          *
                               1897                 :                :          * If this is a required equality strategy scan key, this is just an
                               1898                 :                :          * optimization; _bt_tuple_before_array_skeys already confirmed that
                               1899                 :                :          * this scan key places us ahead of caller's tuple.  There's no need
                               1900                 :                :          * to repeat that work now.  (The same underlying principle also gets
                               1901                 :                :          * applied by the cur_elem_trig optimization used to speed up searches
                               1902                 :                :          * for the next array element.)
                               1903                 :                :          *
                               1904                 :                :          * If this is a required inequality strategy scan key, we _must_ rely
                               1905                 :                :          * on _bt_check_compare like this; we aren't capable of directly
                               1906                 :                :          * evaluating required inequality strategy scan keys here, on our own.
                               1907                 :                :          */
    8 pg@bowt.ie               1908   [ +  +  +  + ]:GNC        3214 :         if (ikey == sktrig && !array)
                               1909                 :                :         {
                               1910   [ +  -  +  -  :             31 :             Assert(sktrig_required && required && all_required_satisfied);
                                              -  + ]
                               1911                 :                : 
                               1912                 :                :             /* Use "beyond end" advancement.  See below for an explanation. */
                               1913                 :             31 :             beyond_end_advance = true;
                               1914                 :             31 :             all_satisfied = all_required_satisfied = false;
                               1915                 :                : 
                               1916                 :                :             /*
                               1917                 :                :              * Set a flag that remembers that this was an inequality required
                               1918                 :                :              * in the opposite scan direction only, that nevertheless
                               1919                 :                :              * triggered the call here.
                               1920                 :                :              *
                               1921                 :                :              * This only happens when an inequality operator (which must be
                               1922                 :                :              * strict) encounters a group of NULLs that indicate the end of
                               1923                 :                :              * non-NULL values for tuples in the current scan direction.
                               1924                 :                :              */
                               1925         [ -  + ]:             31 :             if (unlikely(required_opposite_direction_only))
    8 pg@bowt.ie               1926                 :UNC           0 :                 oppodir_inequality_sktrig = true;
                               1927                 :                : 
    8 pg@bowt.ie               1928                 :GNC          31 :             continue;
                               1929                 :                :         }
                               1930                 :                : 
                               1931                 :                :         /*
                               1932                 :                :          * Nothing more for us to do with an inequality strategy scan key that
                               1933                 :                :          * wasn't the one that _bt_check_compare stopped on, though.
                               1934                 :                :          *
                               1935                 :                :          * Note: if our later call to _bt_check_compare (to recheck caller's
                               1936                 :                :          * tuple) sets continuescan=false due to finding this same inequality
                               1937                 :                :          * unsatisfied (possible when it's required in the scan direction),
                               1938                 :                :          * we'll deal with it via a recursive "second pass" call.
                               1939                 :                :          */
                               1940         [ +  + ]:           3183 :         else if (cur->sk_strategy != BTEqualStrategyNumber)
                               1941                 :              9 :             continue;
                               1942                 :                : 
                               1943                 :                :         /*
                               1944                 :                :          * Nothing for us to do with an equality strategy scan key that isn't
                               1945                 :                :          * marked required, either -- unless it's a non-required array
                               1946                 :                :          */
                               1947   [ +  +  +  + ]:           3174 :         else if (!required && !array)
                               1948                 :            637 :             continue;
                               1949                 :                : 
                               1950                 :                :         /*
                               1951                 :                :          * Here we perform steps for all array scan keys after a required
                               1952                 :                :          * array scan key whose binary search triggered "beyond end of array
                               1953                 :                :          * element" array advancement due to encountering a tuple attribute
                               1954                 :                :          * value > the closest matching array key (or < for backwards scans).
                               1955                 :                :          */
                               1956         [ +  + ]:           2537 :         if (beyond_end_advance)
                               1957                 :            132 :         {
                               1958                 :                :             int         final_elem_dir;
                               1959                 :                : 
                               1960   [ +  -  +  + ]:            132 :             if (ScanDirectionIsBackward(dir) || !array)
                               1961                 :             48 :                 final_elem_dir = 0;
                               1962                 :                :             else
                               1963                 :             84 :                 final_elem_dir = array->num_elems - 1;
                               1964                 :                : 
                               1965   [ +  +  +  + ]:            132 :             if (array && array->cur_elem != final_elem_dir)
                               1966                 :                :             {
                               1967                 :             24 :                 array->cur_elem = final_elem_dir;
                               1968                 :             24 :                 cur->sk_argument = array->elem_values[final_elem_dir];
                               1969                 :                :             }
                               1970                 :                : 
                               1971                 :            132 :             continue;
                               1972                 :                :         }
                               1973                 :                : 
                               1974                 :                :         /*
                               1975                 :                :          * Here we perform steps for all array scan keys after a required
                               1976                 :                :          * array scan key whose tuple attribute was < the closest matching
                               1977                 :                :          * array key when we dealt with it (or > for backwards scans).
                               1978                 :                :          *
                               1979                 :                :          * This earlier required array key already puts us ahead of caller's
                               1980                 :                :          * tuple in the key space (for the current scan direction).  We must
                               1981                 :                :          * make sure that subsequent lower-order array keys do not put us too
                               1982                 :                :          * far ahead (ahead of tuples that have yet to be seen by our caller).
                               1983                 :                :          * For example, when a tuple "(a, b) = (42, 5)" advances the array
                               1984                 :                :          * keys on "a" from 40 to 45, we must also set "b" to whatever the
                               1985                 :                :          * first array element for "b" is.  It would be wrong to allow "b" to
                               1986                 :                :          * be set based on the tuple value.
                               1987                 :                :          *
                               1988                 :                :          * Perform the same steps with truncated high key attributes.  You can
                               1989                 :                :          * think of this as a "binary search" for the element closest to the
                               1990                 :                :          * value -inf.  Again, the arrays must never get ahead of the scan.
                               1991                 :                :          */
                               1992   [ +  +  -  + ]:           2405 :         if (!all_required_satisfied || cur->sk_attno > tupnatts)
                               1993                 :            205 :         {
                               1994                 :                :             int         first_elem_dir;
                               1995                 :                : 
                               1996   [ -  +  -  - ]:            205 :             if (ScanDirectionIsForward(dir) || !array)
                               1997                 :            205 :                 first_elem_dir = 0;
                               1998                 :                :             else
    8 pg@bowt.ie               1999                 :UNC           0 :                 first_elem_dir = array->num_elems - 1;
                               2000                 :                : 
    8 pg@bowt.ie               2001   [ +  +  +  + ]:GNC         205 :             if (array && array->cur_elem != first_elem_dir)
                               2002                 :                :             {
                               2003                 :             90 :                 array->cur_elem = first_elem_dir;
                               2004                 :             90 :                 cur->sk_argument = array->elem_values[first_elem_dir];
                               2005                 :                :             }
                               2006                 :                : 
                               2007                 :            205 :             continue;
                               2008                 :                :         }
                               2009                 :                : 
                               2010                 :                :         /*
                               2011                 :                :          * Search in scankey's array for the corresponding tuple attribute
                               2012                 :                :          * value from caller's tuple
                               2013                 :                :          */
                               2014                 :           2200 :         tupdatum = index_getattr(tuple, cur->sk_attno, tupdesc, &tupnull);
                               2015                 :                : 
                               2016         [ +  + ]:           2200 :         if (array)
                               2017                 :                :         {
                               2018   [ +  +  +  + ]:           2018 :             bool        cur_elem_trig = (sktrig_required && ikey == sktrig);
                               2019                 :                : 
                               2020                 :                :             /*
                               2021                 :                :              * Binary search for closest match that's available from the array
                               2022                 :                :              */
                               2023                 :           2018 :             set_elem = _bt_binsrch_array_skey(&so->orderProcs[ikey],
                               2024                 :                :                                               cur_elem_trig, dir,
                               2025                 :                :                                               tupdatum, tupnull, array, cur,
                               2026                 :                :                                               &result);
                               2027                 :                : 
                               2028   [ +  -  -  + ]:           2018 :             Assert(set_elem >= 0 && set_elem < array->num_elems);
                               2029                 :                :         }
                               2030                 :                :         else
                               2031                 :                :         {
                               2032   [ +  -  -  + ]:            182 :             Assert(sktrig_required && required);
                               2033                 :                : 
                               2034                 :                :             /*
                               2035                 :                :              * This is a required non-array equality strategy scan key, which
                               2036                 :                :              * we'll treat as a degenerate single element array.
                               2037                 :                :              *
                               2038                 :                :              * This scan key's imaginary "array" can't really advance, but it
                               2039                 :                :              * can still roll over like any other array.  (Actually, this is
                               2040                 :                :              * no different to real single value arrays, which never advance
                               2041                 :                :              * without rolling over -- they can never truly advance, either.)
                               2042                 :                :              */
                               2043                 :            182 :             result = _bt_compare_array_skey(&so->orderProcs[ikey],
                               2044                 :                :                                             tupdatum, tupnull,
                               2045                 :                :                                             cur->sk_argument, cur);
                               2046                 :                :         }
                               2047                 :                : 
                               2048                 :                :         /*
                               2049                 :                :          * Consider "beyond end of array element" array advancement.
                               2050                 :                :          *
                               2051                 :                :          * When the tuple attribute value is > the closest matching array key
                               2052                 :                :          * (or < in the backwards scan case), we need to ratchet this array
                               2053                 :                :          * forward (backward) by one increment, so that caller's tuple ends up
                               2054                 :                :          * being < final array value instead (or > final array value instead).
                               2055                 :                :          * This process has to work for all of the arrays, not just this one:
                               2056                 :                :          * it must "carry" to higher-order arrays when the set_elem that we
                               2057                 :                :          * just found happens to be the final one for the scan's direction.
                               2058                 :                :          * Incrementing (decrementing) set_elem itself isn't good enough.
                               2059                 :                :          *
                               2060                 :                :          * Our approach is to provisionally use set_elem as if it was an exact
                               2061                 :                :          * match now, then set each later/less significant array to whatever
                               2062                 :                :          * its final element is.  Once outside the loop we'll then "increment
                               2063                 :                :          * this array's set_elem" by calling _bt_advance_array_keys_increment.
                               2064                 :                :          * That way the process rolls over to higher order arrays as needed.
                               2065                 :                :          *
                               2066                 :                :          * Under this scheme any required arrays only ever ratchet forwards
                               2067                 :                :          * (or backwards), and always do so to the maximum possible extent
                               2068                 :                :          * that we can know will be safe without seeing the scan's next tuple.
                               2069                 :                :          * We don't need any special handling for required scan keys that lack
                               2070                 :                :          * a real array to advance, nor for redundant scan keys that couldn't
                               2071                 :                :          * be eliminated by _bt_preprocess_keys.  It won't matter if some of
                               2072                 :                :          * our "true" array scan keys (or even all of them) are non-required.
                               2073                 :                :          */
                               2074   [ +  +  +  + ]:           2200 :         if (required &&
                               2075   [ +  +  +  + ]:           2080 :             ((ScanDirectionIsForward(dir) && result > 0) ||
                               2076         [ +  + ]:             12 :              (ScanDirectionIsBackward(dir) && result < 0)))
                               2077                 :            314 :             beyond_end_advance = true;
                               2078                 :                : 
                               2079   [ +  -  -  + ]:           2200 :         Assert(all_required_satisfied && all_satisfied);
                               2080         [ +  + ]:           2200 :         if (result != 0)
                               2081                 :                :         {
                               2082                 :                :             /*
                               2083                 :                :              * Track whether caller's tuple satisfies our new post-advancement
                               2084                 :                :              * qual, for required scan keys, as well as for the entire set of
                               2085                 :                :              * interesting scan keys (all required scan keys plus non-required
                               2086                 :                :              * array scan keys are considered interesting.)
                               2087                 :                :              */
                               2088                 :           1001 :             all_satisfied = false;
                               2089         [ +  + ]:           1001 :             if (required)
                               2090                 :            893 :                 all_required_satisfied = false;
                               2091                 :                :             else
                               2092                 :                :             {
                               2093                 :                :                 /*
                               2094                 :                :                  * There's no need to advance the arrays using the best
                               2095                 :                :                  * available match for a non-required array.  Give up now.
                               2096                 :                :                  * (Though note that sktrig_required calls still have to do
                               2097                 :                :                  * all the usual post-advancement steps, including the recheck
                               2098                 :                :                  * call to _bt_check_compare.)
                               2099                 :                :                  */
                               2100                 :            108 :                 break;
                               2101                 :                :             }
                               2102                 :                :         }
                               2103                 :                : 
                               2104                 :                :         /* Advance array keys, even when set_elem isn't an exact match */
                               2105   [ +  +  +  + ]:           2092 :         if (array && array->cur_elem != set_elem)
                               2106                 :                :         {
                               2107                 :           1596 :             array->cur_elem = set_elem;
                               2108                 :           1596 :             cur->sk_argument = array->elem_values[set_elem];
                               2109                 :                :         }
                               2110                 :                :     }
                               2111                 :                : 
                               2112                 :                :     /*
                               2113                 :                :      * Advance the array keys incrementally whenever "beyond end of array
                               2114                 :                :      * element" array advancement happens, so that advancement will carry to
                               2115                 :                :      * higher-order arrays (might exhaust all the scan's arrays instead, which
                               2116                 :                :      * ends the top-level scan).
                               2117                 :                :      */
                               2118   [ +  +  +  + ]:           1941 :     if (beyond_end_advance && !_bt_advance_array_keys_increment(scan, dir))
                               2119                 :            329 :         goto end_toplevel_scan;
                               2120                 :                : 
                               2121         [ -  + ]:           1612 :     Assert(_bt_verify_keys_with_arraykeys(scan));
                               2122                 :                : 
                               2123                 :                :     /*
                               2124                 :                :      * Does tuple now satisfy our new qual?  Recheck with _bt_check_compare.
                               2125                 :                :      *
                               2126                 :                :      * Calls triggered by an unsatisfied required scan key, whose tuple now
                               2127                 :                :      * satisfies all required scan keys, but not all nonrequired array keys,
                               2128                 :                :      * will still require a recheck call to _bt_check_compare.  They'll still
                               2129                 :                :      * need its "second pass" handling of required inequality scan keys.
                               2130                 :                :      * (Might have missed a still-unsatisfied required inequality scan key
                               2131                 :                :      * that caller didn't detect as the sktrig scan key during its initial
                               2132                 :                :      * _bt_check_compare call that used the old/original qual.)
                               2133                 :                :      *
                               2134                 :                :      * Calls triggered by an unsatisfied nonrequired array scan key never need
                               2135                 :                :      * "second pass" handling of required inequalities (nor any other handling
                               2136                 :                :      * of any required scan key).  All that matters is whether caller's tuple
                               2137                 :                :      * satisfies the new qual, so it's safe to just skip the _bt_check_compare
                               2138                 :                :      * recheck when we've already determined that it can only return 'false'.
                               2139                 :                :      */
                               2140   [ +  +  +  + ]:           1612 :     if ((sktrig_required && all_required_satisfied) ||
                               2141   [ +  +  +  + ]:            715 :         (!sktrig_required && all_satisfied))
                               2142                 :                :     {
                               2143                 :            909 :         int         nsktrig = sktrig + 1;
                               2144                 :                :         bool        continuescan;
                               2145                 :                : 
                               2146         [ -  + ]:            909 :         Assert(all_required_satisfied);
                               2147                 :                : 
                               2148                 :                :         /* Recheck _bt_check_compare on behalf of caller */
                               2149         [ +  + ]:            909 :         if (_bt_check_compare(scan, dir, tuple, tupnatts, tupdesc,
                               2150                 :                :                               false, false, false,
                               2151                 :            906 :                               &continuescan, &nsktrig) &&
                               2152         [ +  - ]:            906 :             !so->scanBehind)
                               2153                 :                :         {
                               2154                 :                :             /* This tuple satisfies the new qual */
                               2155   [ +  -  -  + ]:            906 :             Assert(all_satisfied && continuescan);
                               2156                 :                : 
                               2157         [ +  + ]:            906 :             if (pstate)
                               2158                 :            894 :                 pstate->continuescan = true;
                               2159                 :                : 
                               2160                 :            906 :             return true;
                               2161                 :                :         }
                               2162                 :                : 
                               2163                 :                :         /*
                               2164                 :                :          * Consider "second pass" handling of required inequalities.
                               2165                 :                :          *
                               2166                 :                :          * It's possible that our _bt_check_compare call indicated that the
                               2167                 :                :          * scan should end due to some unsatisfied inequality that wasn't
                               2168                 :                :          * initially recognized as such by us.  Handle this by calling
                               2169                 :                :          * ourselves recursively, this time indicating that the trigger is the
                               2170                 :                :          * inequality that we missed first time around (and using a set of
                               2171                 :                :          * required array/equality keys that are now exact matches for tuple).
                               2172                 :                :          *
                               2173                 :                :          * We make a strong, general guarantee that every _bt_checkkeys call
                               2174                 :                :          * here will advance the array keys to the maximum possible extent
                               2175                 :                :          * that we can know to be safe based on caller's tuple alone.  If we
                               2176                 :                :          * didn't perform this step, then that guarantee wouldn't quite hold.
                               2177                 :                :          */
                               2178         [ -  + ]:              3 :         if (unlikely(!continuescan))
                               2179                 :                :         {
                               2180                 :                :             bool        satisfied PG_USED_FOR_ASSERTS_ONLY;
                               2181                 :                : 
    8 pg@bowt.ie               2182         [ #  # ]:UNC           0 :             Assert(sktrig_required);
                               2183         [ #  # ]:              0 :             Assert(so->keyData[nsktrig].sk_strategy != BTEqualStrategyNumber);
                               2184                 :                : 
                               2185                 :                :             /*
                               2186                 :                :              * The tuple must use "beyond end" advancement during the
                               2187                 :                :              * recursive call, so we cannot possibly end up back here when
                               2188                 :                :              * recursing.  We'll consume a small, fixed amount of stack space.
                               2189                 :                :              */
                               2190         [ #  # ]:              0 :             Assert(!beyond_end_advance);
                               2191                 :                : 
                               2192                 :                :             /* Advance the array keys a second time using same tuple */
                               2193                 :              0 :             satisfied = _bt_advance_array_keys(scan, pstate, tuple, tupnatts,
                               2194                 :                :                                                tupdesc, nsktrig, true);
                               2195                 :                : 
                               2196                 :                :             /* This tuple doesn't satisfy the inequality */
                               2197         [ #  # ]:              0 :             Assert(!satisfied);
                               2198                 :              0 :             return false;
                               2199                 :                :         }
                               2200                 :                : 
                               2201                 :                :         /*
                               2202                 :                :          * Some non-required scan key (from new qual) still not satisfied.
                               2203                 :                :          *
                               2204                 :                :          * All scan keys required in the current scan direction must still be
                               2205                 :                :          * satisfied, though, so we can trust all_required_satisfied below.
                               2206                 :                :          */
                               2207                 :                :     }
                               2208                 :                : 
                               2209                 :                :     /*
                               2210                 :                :      * When we were called just to deal with "advancing" non-required arrays,
                               2211                 :                :      * this is as far as we can go (cannot stop the scan for these callers)
                               2212                 :                :      */
    8 pg@bowt.ie               2213         [ +  + ]:GNC         706 :     if (!sktrig_required)
                               2214                 :                :     {
                               2215                 :                :         /* Caller's tuple doesn't match any qual */
                               2216                 :            108 :         return false;
                               2217                 :                :     }
                               2218                 :                : 
                               2219                 :                :     /*
                               2220                 :                :      * Postcondition array state assertion (for still-unsatisfied tuples).
                               2221                 :                :      *
                               2222                 :                :      * By here we have established that the scan's required arrays (scan must
                               2223                 :                :      * have at least one required array) advanced, without becoming exhausted.
                               2224                 :                :      *
                               2225                 :                :      * Caller's tuple is now < the newly advanced array keys (or > when this
                               2226                 :                :      * is a backwards scan), except in the case where we only got this far due
                               2227                 :                :      * to an unsatisfied non-required scan key.  Verify that with an assert.
                               2228                 :                :      *
                               2229                 :                :      * Note: we don't just quit at this point when all required scan keys were
                               2230                 :                :      * found to be satisfied because we need to consider edge-cases involving
                               2231                 :                :      * scan keys required in the opposite direction only; those aren't tracked
                               2232                 :                :      * by all_required_satisfied. (Actually, oppodir_inequality_sktrig trigger
                               2233                 :                :      * scan keys are tracked by all_required_satisfied, since it's convenient
                               2234                 :                :      * for _bt_check_compare to behave as if they are required in the current
                               2235                 :                :      * scan direction to deal with NULLs.  We'll account for that separately.)
                               2236                 :                :      */
                               2237         [ -  + ]:            598 :     Assert(_bt_tuple_before_array_skeys(scan, dir, tuple, tupdesc, tupnatts,
                               2238                 :                :                                         false, 0, NULL) ==
                               2239                 :                :            !all_required_satisfied);
                               2240                 :                : 
                               2241                 :                :     /*
                               2242                 :                :      * We generally permit primitive index scans to continue onto the next
                               2243                 :                :      * sibling page when the page's finaltup satisfies all required scan keys
                               2244                 :                :      * at the point where we're between pages.
                               2245                 :                :      *
                               2246                 :                :      * If caller's tuple is also the page's finaltup, and we see that required
                               2247                 :                :      * scan keys still aren't satisfied, start a new primitive index scan.
                               2248                 :                :      */
                               2249   [ +  +  -  + ]:            598 :     if (!all_required_satisfied && pstate->finaltup == tuple)
    8 pg@bowt.ie               2250                 :UNC           0 :         goto new_prim_scan;
                               2251                 :                : 
                               2252                 :                :     /*
                               2253                 :                :      * Proactively check finaltup (don't wait until finaltup is reached by the
                               2254                 :                :      * scan) when it might well turn out to not be satisfied later on.
                               2255                 :                :      *
                               2256                 :                :      * Note: if so->scanBehind hasn't already been set for finaltup by us,
                               2257                 :                :      * it'll be set during this call to _bt_tuple_before_array_skeys.  Either
                               2258                 :                :      * way, it'll be set correctly (for the whole page) after this point.
                               2259                 :                :      */
    8 pg@bowt.ie               2260   [ +  +  +  +  :GNC        1141 :     if (!all_required_satisfied && pstate->finaltup &&
                                              +  + ]
                               2261         [ +  + ]:           1086 :         _bt_tuple_before_array_skeys(scan, dir, pstate->finaltup, tupdesc,
                               2262                 :           1086 :                                      BTreeTupleGetNAtts(pstate->finaltup, rel),
                               2263                 :                :                                      false, 0, &so->scanBehind))
                               2264                 :            285 :         goto new_prim_scan;
                               2265                 :                : 
                               2266                 :                :     /*
                               2267                 :                :      * When we encounter a truncated finaltup high key attribute, we're
                               2268                 :                :      * optimistic about the chances of its corresponding required scan key
                               2269                 :                :      * being satisfied when we go on to check it against tuples from this
                               2270                 :                :      * page's right sibling leaf page.  We consider truncated attributes to be
                               2271                 :                :      * satisfied by required scan keys, which allows the primitive index scan
                               2272                 :                :      * to continue to the next leaf page.  We must set so->scanBehind to true
                               2273                 :                :      * to remember that the last page's finaltup had "satisfied" required scan
                               2274                 :                :      * keys for one or more truncated attribute values (scan keys required in
                               2275                 :                :      * _either_ scan direction).
                               2276                 :                :      *
                               2277                 :                :      * There is a chance that _bt_checkkeys (which checks so->scanBehind) will
                               2278                 :                :      * find that even the sibling leaf page's finaltup is < the new array
                               2279                 :                :      * keys.  When that happens, our optimistic policy will have incurred a
                               2280                 :                :      * single extra leaf page access that could have been avoided.
                               2281                 :                :      *
                               2282                 :                :      * A pessimistic policy would give backward scans a gratuitous advantage
                               2283                 :                :      * over forward scans.  We'd punish forward scans for applying more
                               2284                 :                :      * accurate information from the high key, rather than just using the
                               2285                 :                :      * final non-pivot tuple as finaltup, in the style of backward scans.
                               2286                 :                :      * Being pessimistic would also give some scans with non-required arrays a
                               2287                 :                :      * perverse advantage over similar scans that use required arrays instead.
                               2288                 :                :      *
                               2289                 :                :      * You can think of this as a speculative bet on what the scan is likely
                               2290                 :                :      * to find on the next page.  It's not much of a gamble, though, since the
                               2291                 :                :      * untruncated prefix of attributes must strictly satisfy the new qual
                               2292                 :                :      * (though it's okay if any non-required scan keys fail to be satisfied).
                               2293                 :                :      */
                               2294   [ +  +  -  + ]:            313 :     if (so->scanBehind && has_required_opposite_direction_only)
                               2295                 :                :     {
                               2296                 :                :         /*
                               2297                 :                :          * However, we avoid this behavior whenever the scan involves a scan
                               2298                 :                :          * key required in the opposite direction to the scan only, along with
                               2299                 :                :          * a finaltup with at least one truncated attribute that's associated
                               2300                 :                :          * with a scan key marked required (required in either direction).
                               2301                 :                :          *
                               2302                 :                :          * _bt_check_compare simply won't stop the scan for a scan key that's
                               2303                 :                :          * marked required in the opposite scan direction only.  That leaves
                               2304                 :                :          * us without any reliable way of reconsidering any opposite-direction
                               2305                 :                :          * inequalities if it turns out that starting a new primitive index
                               2306                 :                :          * scan will allow _bt_first to skip ahead by a great many leaf pages
                               2307                 :                :          * (see next section for details of how that works).
                               2308                 :                :          */
    8 pg@bowt.ie               2309                 :UNC           0 :         goto new_prim_scan;
                               2310                 :                :     }
                               2311                 :                : 
                               2312                 :                :     /*
                               2313                 :                :      * Handle inequalities marked required in the opposite scan direction.
                               2314                 :                :      * They can also signal that we should start a new primitive index scan.
                               2315                 :                :      *
                               2316                 :                :      * It's possible that the scan is now positioned where "matching" tuples
                               2317                 :                :      * begin, and that caller's tuple satisfies all scan keys required in the
                               2318                 :                :      * current scan direction.  But if caller's tuple still doesn't satisfy
                               2319                 :                :      * other scan keys that are required in the opposite scan direction only
                               2320                 :                :      * (e.g., a required >= strategy scan key when scan direction is forward),
                               2321                 :                :      * it's still possible that there are many leaf pages before the page that
                               2322                 :                :      * _bt_first could skip straight to.  Groveling through all those pages
                               2323                 :                :      * will always give correct answers, but it can be very inefficient.  We
                               2324                 :                :      * must avoid needlessly scanning extra pages.
                               2325                 :                :      *
                               2326                 :                :      * Separately, it's possible that _bt_check_compare set continuescan=false
                               2327                 :                :      * for a scan key that's required in the opposite direction only.  This is
                               2328                 :                :      * a special case, that happens only when _bt_check_compare sees that the
                               2329                 :                :      * inequality encountered a NULL value.  This signals the end of non-NULL
                               2330                 :                :      * values in the current scan direction, which is reason enough to end the
                               2331                 :                :      * (primitive) scan.  If this happens at the start of a large group of
                               2332                 :                :      * NULL values, then we shouldn't expect to be called again until after
                               2333                 :                :      * the scan has already read indefinitely-many leaf pages full of tuples
                               2334                 :                :      * with NULL suffix values.  We need a separate test for this case so that
                               2335                 :                :      * we don't miss our only opportunity to skip over such a group of pages.
                               2336                 :                :      * (_bt_first is expected to skip over the group of NULLs by applying a
                               2337                 :                :      * similar "deduce NOT NULL" rule, where it finishes its insertion scan
                               2338                 :                :      * key by consing up an explicit SK_SEARCHNOTNULL key.)
                               2339                 :                :      *
                               2340                 :                :      * Apply a test against finaltup to detect and recover from these problem:
                               2341                 :                :      * if even finaltup doesn't satisfy such an inequality, we just skip by
                               2342                 :                :      * starting a new primitive index scan.  When we skip, we know for sure
                               2343                 :                :      * that all of the tuples on the current page following caller's tuple are
                               2344                 :                :      * also before the _bt_first-wise start of tuples for our new qual.  That
                               2345                 :                :      * at least suggests many more skippable pages beyond the current page.
                               2346                 :                :      */
    8 pg@bowt.ie               2347   [ +  +  -  +  :GNC         313 :     if (has_required_opposite_direction_only && pstate->finaltup &&
                                              -  - ]
    8 pg@bowt.ie               2348         [ #  # ]:UNC           0 :         (all_required_satisfied || oppodir_inequality_sktrig))
                               2349                 :                :     {
                               2350         [ #  # ]:              0 :         int         nfinaltupatts = BTreeTupleGetNAtts(pstate->finaltup, rel);
                               2351                 :                :         ScanDirection flipped;
                               2352                 :                :         bool        continuescanflip;
                               2353                 :                :         int         opsktrig;
                               2354                 :                : 
                               2355                 :                :         /*
                               2356                 :                :          * We're checking finaltup (which is usually not caller's tuple), so
                               2357                 :                :          * cannot reuse work from caller's earlier _bt_check_compare call.
                               2358                 :                :          *
                               2359                 :                :          * Flip the scan direction when calling _bt_check_compare this time,
                               2360                 :                :          * so that it will set continuescanflip=false when it encounters an
                               2361                 :                :          * inequality required in the opposite scan direction.
                               2362                 :                :          */
                               2363         [ #  # ]:              0 :         Assert(!so->scanBehind);
                               2364                 :              0 :         opsktrig = 0;
                               2365                 :              0 :         flipped = -dir;
                               2366                 :              0 :         _bt_check_compare(scan, flipped,
                               2367                 :                :                           pstate->finaltup, nfinaltupatts, tupdesc,
                               2368                 :                :                           false, false, false,
                               2369                 :                :                           &continuescanflip, &opsktrig);
                               2370                 :                : 
                               2371                 :                :         /*
                               2372                 :                :          * If we ended up here due to the all_required_satisfied criteria,
                               2373                 :                :          * test opsktrig in a way that ensures that finaltup contains the same
                               2374                 :                :          * prefix of key columns as caller's tuple (a prefix that satisfies
                               2375                 :                :          * earlier required-in-current-direction scan keys).
                               2376                 :                :          *
                               2377                 :                :          * If we ended up here due to the oppodir_inequality_sktrig criteria,
                               2378                 :                :          * test opsktrig in a way that ensures that the same scan key that our
                               2379                 :                :          * caller found to be unsatisfied (by the scan's tuple) was also the
                               2380                 :                :          * one unsatisfied just now (by finaltup).  That way we'll only start
                               2381                 :                :          * a new primitive scan when we're sure that both tuples _don't_ share
                               2382                 :                :          * the same prefix of satisfied equality-constrained attribute values,
                               2383                 :                :          * and that finaltup has a non-NULL attribute value indicated by the
                               2384                 :                :          * unsatisfied scan key at offset opsktrig/sktrig.  (This depends on
                               2385                 :                :          * _bt_check_compare not caring about the direction that inequalities
                               2386                 :                :          * are required in whenever NULL attribute values are unsatisfied.  It
                               2387                 :                :          * only cares about the scan direction, and its relationship to
                               2388                 :                :          * whether NULLs are stored first or last relative to non-NULLs.)
                               2389                 :                :          */
                               2390         [ #  # ]:              0 :         Assert(all_required_satisfied != oppodir_inequality_sktrig);
                               2391   [ #  #  #  #  :              0 :         if (unlikely(!continuescanflip &&
                                     #  #  #  #  #  
                                           #  #  # ]
                               2392                 :                :                      ((all_required_satisfied && opsktrig > sktrig) ||
                               2393                 :                :                       (oppodir_inequality_sktrig && opsktrig >= sktrig))))
                               2394                 :                :         {
                               2395         [ #  # ]:              0 :             Assert(so->keyData[opsktrig].sk_strategy != BTEqualStrategyNumber);
                               2396                 :                : 
                               2397                 :                :             /*
                               2398                 :                :              * Make sure that any non-required arrays are set to the first
                               2399                 :                :              * array element for the current scan direction
                               2400                 :                :              */
                               2401                 :              0 :             _bt_rewind_nonrequired_arrays(scan, dir);
                               2402                 :                : 
                               2403                 :              0 :             goto new_prim_scan;
                               2404                 :                :         }
                               2405                 :                :     }
                               2406                 :                : 
                               2407                 :                :     /*
                               2408                 :                :      * Stick with the ongoing primitive index scan for now.
                               2409                 :                :      *
                               2410                 :                :      * It's possible that later tuples will also turn out to have values that
                               2411                 :                :      * are still < the now-current array keys (or > the current array keys).
                               2412                 :                :      * Our caller will handle this by performing what amounts to a linear
                               2413                 :                :      * search of the page, implemented by calling _bt_check_compare and then
                               2414                 :                :      * _bt_tuple_before_array_skeys for each tuple.
                               2415                 :                :      *
                               2416                 :                :      * This approach has various advantages over a binary search of the page.
                               2417                 :                :      * Repeated binary searches of the page (one binary search for every array
                               2418                 :                :      * advancement) won't outperform a continuous linear search.  While there
                               2419                 :                :      * are workloads that a naive linear search won't handle well, our caller
                               2420                 :                :      * has a "look ahead" fallback mechanism to deal with that problem.
                               2421                 :                :      */
    8 pg@bowt.ie               2422                 :GNC         313 :     pstate->continuescan = true; /* Override _bt_check_compare */
                               2423                 :            313 :     so->needPrimScan = false;    /* _bt_readpage has more tuples to check */
                               2424                 :                : 
                               2425         [ +  + ]:            313 :     if (so->scanBehind)
                               2426                 :                :     {
                               2427                 :                :         /* Optimization: skip by setting "look ahead" mechanism's offnum */
                               2428         [ -  + ]:              3 :         Assert(ScanDirectionIsForward(dir));
                               2429                 :              3 :         pstate->skip = pstate->maxoff + 1;
                               2430                 :                :     }
                               2431                 :                : 
                               2432                 :                :     /* Caller's tuple doesn't match the new qual */
                               2433                 :            313 :     return false;
                               2434                 :                : 
                               2435                 :            285 : new_prim_scan:
                               2436                 :                : 
                               2437                 :                :     /*
                               2438                 :                :      * End this primitive index scan, but schedule another.
                               2439                 :                :      *
                               2440                 :                :      * Note: If the scan direction happens to change, this scheduled primitive
                               2441                 :                :      * index scan won't go ahead after all.
                               2442                 :                :      */
                               2443                 :            285 :     pstate->continuescan = false;    /* Tell _bt_readpage we're done... */
                               2444                 :            285 :     so->needPrimScan = true; /* ...but call _bt_first again */
                               2445                 :                : 
                               2446         [ +  + ]:            285 :     if (scan->parallel_scan)
                               2447                 :             18 :         _bt_parallel_primscan_schedule(scan, pstate->prev_scan_page);
                               2448                 :                : 
                               2449                 :                :     /* Caller's tuple doesn't match the new qual */
                               2450                 :            285 :     return false;
                               2451                 :                : 
                               2452                 :            329 : end_toplevel_scan:
                               2453                 :                : 
                               2454                 :                :     /*
                               2455                 :                :      * End the current primitive index scan, but don't schedule another.
                               2456                 :                :      *
                               2457                 :                :      * This ends the entire top-level scan in the current scan direction.
                               2458                 :                :      *
                               2459                 :                :      * Note: The scan's arrays (including any non-required arrays) are now in
                               2460                 :                :      * their final positions for the current scan direction.  If the scan
                               2461                 :                :      * direction happens to change, then the arrays will already be in their
                               2462                 :                :      * first positions for what will then be the current scan direction.
                               2463                 :                :      */
                               2464                 :            329 :     pstate->continuescan = false;    /* Tell _bt_readpage we're done... */
                               2465                 :            329 :     so->needPrimScan = false;    /* ...don't call _bt_first again, though */
                               2466                 :                : 
                               2467                 :                :     /* Caller's tuple doesn't match any qual */
                               2468                 :            329 :     return false;
                               2469                 :                : }
                               2470                 :                : 
                               2471                 :                : /*
                               2472                 :                :  *  _bt_preprocess_keys() -- Preprocess scan keys
                               2473                 :                :  *
                               2474                 :                :  * The given search-type keys (taken from scan->keyData[])
                               2475                 :                :  * are copied to so->keyData[] with possible transformation.
                               2476                 :                :  * scan->numberOfKeys is the number of input keys, so->numberOfKeys gets
                               2477                 :                :  * the number of output keys (possibly less, never greater).
                               2478                 :                :  *
                               2479                 :                :  * The output keys are marked with additional sk_flags bits beyond the
                               2480                 :                :  * system-standard bits supplied by the caller.  The DESC and NULLS_FIRST
                               2481                 :                :  * indoption bits for the relevant index attribute are copied into the flags.
                               2482                 :                :  * Also, for a DESC column, we commute (flip) all the sk_strategy numbers
                               2483                 :                :  * so that the index sorts in the desired direction.
                               2484                 :                :  *
                               2485                 :                :  * One key purpose of this routine is to discover which scan keys must be
                               2486                 :                :  * satisfied to continue the scan.  It also attempts to eliminate redundant
                               2487                 :                :  * keys and detect contradictory keys.  (If the index opfamily provides
                               2488                 :                :  * incomplete sets of cross-type operators, we may fail to detect redundant
                               2489                 :                :  * or contradictory keys, but we can survive that.)
                               2490                 :                :  *
                               2491                 :                :  * The output keys must be sorted by index attribute.  Presently we expect
                               2492                 :                :  * (but verify) that the input keys are already so sorted --- this is done
                               2493                 :                :  * by match_clauses_to_index() in indxpath.c.  Some reordering of the keys
                               2494                 :                :  * within each attribute may be done as a byproduct of the processing here.
                               2495                 :                :  * That process must leave array scan keys (within an attribute) in the same
                               2496                 :                :  * order as corresponding entries from the scan's BTArrayKeyInfo array info.
                               2497                 :                :  *
                               2498                 :                :  * The output keys are marked with flags SK_BT_REQFWD and/or SK_BT_REQBKWD
                               2499                 :                :  * if they must be satisfied in order to continue the scan forward or backward
                               2500                 :                :  * respectively.  _bt_checkkeys uses these flags.  For example, if the quals
                               2501                 :                :  * are "x = 1 AND y < 4 AND z < 5", then _bt_checkkeys will reject a tuple
                               2502                 :                :  * (1,2,7), but we must continue the scan in case there are tuples (1,3,z).
                               2503                 :                :  * But once we reach tuples like (1,4,z) we can stop scanning because no
                               2504                 :                :  * later tuples could match.  This is reflected by marking the x and y keys,
                               2505                 :                :  * but not the z key, with SK_BT_REQFWD.  In general, the keys for leading
                               2506                 :                :  * attributes with "=" keys are marked both SK_BT_REQFWD and SK_BT_REQBKWD.
                               2507                 :                :  * For the first attribute without an "=" key, any "<" and "<=" keys are
                               2508                 :                :  * marked SK_BT_REQFWD while any ">" and ">=" keys are marked SK_BT_REQBKWD.
                               2509                 :                :  * This can be seen to be correct by considering the above example.  Note
                               2510                 :                :  * in particular that if there are no keys for a given attribute, the keys for
                               2511                 :                :  * subsequent attributes can never be required; for instance "WHERE y = 4"
                               2512                 :                :  * requires a full-index scan.
                               2513                 :                :  *
                               2514                 :                :  * If possible, redundant keys are eliminated: we keep only the tightest
                               2515                 :                :  * >/>= bound and the tightest </<= bound, and if there's an = key then
                               2516                 :                :  * that's the only one returned.  (So, we return either a single = key,
                               2517                 :                :  * or one or two boundary-condition keys for each attr.)  However, if we
                               2518                 :                :  * cannot compare two keys for lack of a suitable cross-type operator,
                               2519                 :                :  * we cannot eliminate either.  If there are two such keys of the same
                               2520                 :                :  * operator strategy, the second one is just pushed into the output array
                               2521                 :                :  * without further processing here.  We may also emit both >/>= or both
                               2522                 :                :  * </<= keys if we can't compare them.  The logic about required keys still
                               2523                 :                :  * works if we don't eliminate redundant keys.
                               2524                 :                :  *
                               2525                 :                :  * Note that one reason we need direction-sensitive required-key flags is
                               2526                 :                :  * precisely that we may not be able to eliminate redundant keys.  Suppose
                               2527                 :                :  * we have "x > 4::int AND x > 10::bigint", and we are unable to determine
                               2528                 :                :  * which key is more restrictive for lack of a suitable cross-type operator.
                               2529                 :                :  * _bt_first will arbitrarily pick one of the keys to do the initial
                               2530                 :                :  * positioning with.  If it picks x > 4, then the x > 10 condition will fail
                               2531                 :                :  * until we reach index entries > 10; but we can't stop the scan just because
                               2532                 :                :  * x > 10 is failing.  On the other hand, if we are scanning backwards, then
                               2533                 :                :  * failure of either key is indeed enough to stop the scan.  (In general, when
                               2534                 :                :  * inequality keys are present, the initial-positioning code only promises to
                               2535                 :                :  * position before the first possible match, not exactly at the first match,
                               2536                 :                :  * for a forward scan; or after the last match for a backward scan.)
                               2537                 :                :  *
                               2538                 :                :  * As a byproduct of this work, we can detect contradictory quals such
                               2539                 :                :  * as "x = 1 AND x > 2".  If we see that, we return so->qual_ok = false,
                               2540                 :                :  * indicating the scan need not be run at all since no tuples can match.
                               2541                 :                :  * (In this case we do not bother completing the output key array!)
                               2542                 :                :  * Again, missing cross-type operators might cause us to fail to prove the
                               2543                 :                :  * quals contradictory when they really are, but the scan will work correctly.
                               2544                 :                :  *
                               2545                 :                :  * Row comparison keys are currently also treated without any smarts:
                               2546                 :                :  * we just transfer them into the preprocessed array without any
                               2547                 :                :  * editorialization.  We can treat them the same as an ordinary inequality
                               2548                 :                :  * comparison on the row's first index column, for the purposes of the logic
                               2549                 :                :  * about required keys.
                               2550                 :                :  *
                               2551                 :                :  * Note: the reason we have to copy the preprocessed scan keys into private
                               2552                 :                :  * storage is that we are modifying the array based on comparisons of the
                               2553                 :                :  * key argument values, which could change on a rescan.  Therefore we can't
                               2554                 :                :  * overwrite the source data.
                               2555                 :                :  */
                               2556                 :                : void
 7459 tgl@sss.pgh.pa.us        2557                 :CBC     6420890 : _bt_preprocess_keys(IndexScanDesc scan)
                               2558                 :                : {
 7996                          2559                 :        6420890 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
 7459                          2560                 :        6420890 :     int         numberOfKeys = scan->numberOfKeys;
 6305                          2561                 :        6420890 :     int16      *indoption = scan->indexRelation->rd_indoption;
                               2562                 :                :     int         new_numberOfKeys;
                               2563                 :                :     int         numberOfEqualCols;
                               2564                 :                :     ScanKey     inkeys;
                               2565                 :                :     ScanKey     outkeys;
                               2566                 :                :     ScanKey     cur;
                               2567                 :                :     BTScanKeyPreproc xform[BTMaxStrategyNumber];
                               2568                 :                :     bool        test_result;
                               2569                 :                :     int         i,
                               2570                 :                :                 j;
                               2571                 :                :     AttrNumber  attno;
                               2572                 :                :     ScanKey     arrayKeyData;
    8 pg@bowt.ie               2573                 :GNC     6420890 :     int        *keyDataMap = NULL;
                               2574                 :        6420890 :     int         arrayidx = 0;
                               2575                 :                : 
                               2576                 :                :     /*
                               2577                 :                :      * We're called at the start of each primitive index scan during scans
                               2578                 :                :      * that use equality array keys.  We can just reuse the scan keys that
                               2579                 :                :      * were output at the start of the scan's first primitive index scan.
                               2580                 :                :      */
                               2581         [ +  + ]:        6420890 :     if (so->numberOfKeys > 0)
                               2582                 :                :     {
                               2583                 :                :         /*
                               2584                 :                :          * An earlier call to _bt_advance_array_keys already set everything up
                               2585                 :                :          * already.  Just assert that the scan's existing output scan keys are
                               2586                 :                :          * consistent with its current array elements.
                               2587                 :                :          */
                               2588         [ -  + ]:            285 :         Assert(so->numArrayKeys);
                               2589         [ -  + ]:            285 :         Assert(_bt_verify_keys_with_arraykeys(scan));
                               2590                 :        3242235 :         return;
                               2591                 :                :     }
                               2592                 :                : 
                               2593                 :                :     /* initialize result variables */
 8664 tgl@sss.pgh.pa.us        2594                 :CBC     6420605 :     so->qual_ok = true;
 7459                          2595                 :        6420605 :     so->numberOfKeys = 0;
                               2596                 :                : 
 9716 bruce@momjian.us         2597         [ +  + ]:        6420605 :     if (numberOfKeys < 1)
 8664 tgl@sss.pgh.pa.us        2598                 :           5362 :         return;                 /* done if qual-less scan */
                               2599                 :                : 
                               2600                 :                :     /* If any keys are SK_SEARCHARRAY type, set up array-key info */
    8 pg@bowt.ie               2601                 :GNC     6415243 :     arrayKeyData = _bt_preprocess_array_keys(scan);
                               2602         [ +  + ]:        6415243 :     if (!so->qual_ok)
                               2603                 :                :     {
                               2604                 :                :         /* unmatchable array, so give up */
                               2605                 :              3 :         return;
                               2606                 :                :     }
                               2607                 :                : 
                               2608                 :                :     /*
                               2609                 :                :      * Treat arrayKeyData[] (a partially preprocessed copy of scan->keyData[])
                               2610                 :                :      * as our input if _bt_preprocess_array_keys just allocated it, else just
                               2611                 :                :      * use scan->keyData[]
                               2612                 :                :      */
                               2613         [ +  + ]:        6415240 :     if (arrayKeyData)
                               2614                 :                :     {
                               2615                 :            416 :         inkeys = arrayKeyData;
                               2616                 :                : 
                               2617                 :                :         /* Also maintain keyDataMap for remapping so->orderProc[] later */
                               2618                 :            416 :         keyDataMap = MemoryContextAlloc(so->arrayContext,
                               2619                 :                :                                         numberOfKeys * sizeof(int));
                               2620                 :                :     }
                               2621                 :                :     else
 4564 tgl@sss.pgh.pa.us        2622                 :CBC     6414824 :         inkeys = scan->keyData;
                               2623                 :                : 
 7459                          2624                 :        6415240 :     outkeys = so->keyData;
                               2625                 :        6415240 :     cur = &inkeys[0];
                               2626                 :                :     /* we check that input keys are correctly ordered */
 6880                          2627         [ -  + ]:        6415240 :     if (cur->sk_attno < 1)
 6880 tgl@sss.pgh.pa.us        2628         [ #  # ]:UBC           0 :         elog(ERROR, "btree index keys must be ordered by attribute");
                               2629                 :                : 
                               2630                 :                :     /* We can short-circuit most of the work if there's just one key */
 9716 bruce@momjian.us         2631         [ +  + ]:CBC     6415240 :     if (numberOfKeys == 1)
                               2632                 :                :     {
                               2633                 :                :         /* Apply indoption to scankey (might change sk_strategy!) */
 5215 tgl@sss.pgh.pa.us        2634         [ +  + ]:        3236555 :         if (!_bt_fix_scankey_strategy(cur, indoption))
                               2635                 :           1354 :             so->qual_ok = false;
 6305                          2636                 :        3236555 :         memcpy(outkeys, cur, sizeof(ScanKeyData));
 7459                          2637                 :        3236555 :         so->numberOfKeys = 1;
                               2638                 :                :         /* We can mark the qual as required if it's for first index col */
 6654                          2639         [ +  + ]:        3236555 :         if (cur->sk_attno == 1)
                               2640                 :        3235232 :             _bt_mark_scankey_required(outkeys);
    8 pg@bowt.ie               2641         [ +  + ]:GNC     3236555 :         if (arrayKeyData)
                               2642                 :                :         {
                               2643                 :                :             /*
                               2644                 :                :              * Don't call _bt_preprocess_array_keys_final in this fast path
                               2645                 :                :              * (we'll miss out on the single value array transformation, but
                               2646                 :                :              * that's not nearly as important when there's only one scan key)
                               2647                 :                :              */
                               2648         [ -  + ]:            211 :             Assert(cur->sk_flags & SK_SEARCHARRAY);
                               2649   [ +  +  +  -  :            211 :             Assert(cur->sk_strategy != BTEqualStrategyNumber ||
                                              -  + ]
                               2650                 :                :                    (so->arrayKeys[0].scan_key == 0 &&
                               2651                 :                :                     OidIsValid(so->orderProcs[0].fn_oid)));
                               2652                 :                :         }
                               2653                 :                : 
 9716 bruce@momjian.us         2654                 :CBC     3236555 :         return;
                               2655                 :                :     }
                               2656                 :                : 
                               2657                 :                :     /*
                               2658                 :                :      * Otherwise, do the full set of pushups.
                               2659                 :                :      */
 8664 tgl@sss.pgh.pa.us        2660                 :        3178685 :     new_numberOfKeys = 0;
 7060                          2661                 :        3178685 :     numberOfEqualCols = 0;
                               2662                 :                : 
                               2663                 :                :     /*
                               2664                 :                :      * Initialize for processing of keys for attr 1.
                               2665                 :                :      *
                               2666                 :                :      * xform[i] points to the currently best scan key of strategy type i+1; it
                               2667                 :                :      * is NULL if we haven't yet found such a key for this attr.
                               2668                 :                :      */
 8664                          2669                 :        3178685 :     attno = 1;
 7459                          2670                 :        3178685 :     memset(xform, 0, sizeof(xform));
                               2671                 :                : 
                               2672                 :                :     /*
                               2673                 :                :      * Loop iterates from 0 to numberOfKeys inclusive; we use the last pass to
                               2674                 :                :      * handle after-last-key processing.  Actual exit from the loop is at the
                               2675                 :                :      * "break" statement below.
                               2676                 :                :      */
 8424 bruce@momjian.us         2677                 :       10300380 :     for (i = 0;; cur++, i++)
                               2678                 :                :     {
 9716                          2679         [ +  + ]:       10300380 :         if (i < numberOfKeys)
                               2680                 :                :         {
                               2681                 :                :             /* Apply indoption to scankey (might change sk_strategy!) */
 5215 tgl@sss.pgh.pa.us        2682         [ +  + ]:        7121707 :             if (!_bt_fix_scankey_strategy(cur, indoption))
                               2683                 :                :             {
                               2684                 :                :                 /* NULL can't be matched, so give up */
 5215 tgl@sss.pgh.pa.us        2685                 :GBC           9 :                 so->qual_ok = false;
                               2686                 :              9 :                 return;
                               2687                 :                :             }
                               2688                 :                :         }
                               2689                 :                : 
                               2690                 :                :         /*
                               2691                 :                :          * If we are at the end of the keys for a particular attr, finish up
                               2692                 :                :          * processing and emit the cleaned-up keys.
                               2693                 :                :          */
 9716 bruce@momjian.us         2694   [ +  +  +  + ]:CBC    10300371 :         if (i == numberOfKeys || cur->sk_attno != attno)
                               2695                 :                :         {
 7060 tgl@sss.pgh.pa.us        2696                 :        7120701 :             int         priorNumberOfEqualCols = numberOfEqualCols;
                               2697                 :                : 
                               2698                 :                :             /* check input keys are correctly ordered */
 6880                          2699   [ +  +  -  + ]:        7120701 :             if (i < numberOfKeys && cur->sk_attno < attno)
 6880 tgl@sss.pgh.pa.us        2700         [ #  # ]:UBC           0 :                 elog(ERROR, "btree index keys must be ordered by attribute");
                               2701                 :                : 
                               2702                 :                :             /*
                               2703                 :                :              * If = has been specified, all other keys can be eliminated as
                               2704                 :                :              * redundant.  If we have a case like key = 1 AND key > 2, we can
                               2705                 :                :              * set qual_ok to false and abandon further processing.
                               2706                 :                :              *
                               2707                 :                :              * We also have to deal with the case of "key IS NULL", which is
                               2708                 :                :              * unsatisfiable in combination with any other index condition. By
                               2709                 :                :              * the time we get here, that's been classified as an equality
                               2710                 :                :              * check, and we've rejected any combination of it with a regular
                               2711                 :                :              * equality condition; but not with other types of conditions.
                               2712                 :                :              */
    8 pg@bowt.ie               2713         [ +  + ]:GNC     7120701 :             if (xform[BTEqualStrategyNumber - 1].skey)
                               2714                 :                :             {
                               2715                 :        6474113 :                 ScanKey     eq = xform[BTEqualStrategyNumber - 1].skey;
                               2716                 :        6474113 :                 BTArrayKeyInfo *array = NULL;
                               2717                 :        6474113 :                 FmgrInfo   *orderproc = NULL;
                               2718                 :                : 
                               2719   [ +  +  +  + ]:        6474113 :                 if (arrayKeyData && (eq->sk_flags & SK_SEARCHARRAY))
                               2720                 :                :                 {
                               2721                 :                :                     int         eq_in_ikey,
                               2722                 :                :                                 eq_arrayidx;
                               2723                 :                : 
                               2724                 :            271 :                     eq_in_ikey = xform[BTEqualStrategyNumber - 1].ikey;
                               2725                 :            271 :                     eq_arrayidx = xform[BTEqualStrategyNumber - 1].arrayidx;
                               2726                 :            271 :                     array = &so->arrayKeys[eq_arrayidx - 1];
                               2727                 :            271 :                     orderproc = so->orderProcs + eq_in_ikey;
                               2728                 :                : 
                               2729         [ -  + ]:            271 :                     Assert(array->scan_key == eq_in_ikey);
                               2730         [ -  + ]:            271 :                     Assert(OidIsValid(orderproc->fn_oid));
                               2731                 :                :                 }
                               2732                 :                : 
 9716 bruce@momjian.us         2733         [ +  + ]:CBC    38844600 :                 for (j = BTMaxStrategyNumber; --j >= 0;)
                               2734                 :                :                 {
    8 pg@bowt.ie               2735                 :GNC    32370505 :                     ScanKey     chk = xform[j].skey;
                               2736                 :                : 
 7459 tgl@sss.pgh.pa.us        2737   [ +  +  +  + ]:CBC    32370505 :                     if (!chk || j == (BTEqualStrategyNumber - 1))
 9716 bruce@momjian.us         2738                 :       32370450 :                         continue;
                               2739                 :                : 
 4673 tgl@sss.pgh.pa.us        2740         [ +  + ]:             55 :                     if (eq->sk_flags & SK_SEARCHNULL)
                               2741                 :                :                     {
                               2742                 :                :                         /* IS NULL is contradictory to anything else */
                               2743                 :             12 :                         so->qual_ok = false;
                               2744                 :             12 :                         return;
                               2745                 :                :                     }
                               2746                 :                : 
 6317                          2747         [ +  - ]:             43 :                     if (_bt_compare_scankey_args(scan, chk, eq, chk,
                               2748                 :                :                                                  array, orderproc,
                               2749                 :                :                                                  &test_result))
                               2750                 :                :                     {
                               2751         [ +  + ]:             43 :                         if (!test_result)
                               2752                 :                :                         {
                               2753                 :                :                             /* keys proven mutually contradictory */
 6317 tgl@sss.pgh.pa.us        2754                 :GBC           6 :                             so->qual_ok = false;
                               2755                 :              6 :                             return;
                               2756                 :                :                         }
                               2757                 :                :                         /* else discard the redundant non-equality key */
    8 pg@bowt.ie               2758   [ +  +  -  + ]:GNC          37 :                         Assert(!array || array->num_elems > 0);
                               2759                 :             37 :                         xform[j].skey = NULL;
                               2760                 :             37 :                         xform[j].ikey = -1;
                               2761                 :                :                     }
                               2762                 :                :                     /* else, cannot determine redundancy, keep both keys */
                               2763                 :                :                 }
                               2764                 :                :                 /* track number of attrs for which we have "=" keys */
 7060 tgl@sss.pgh.pa.us        2765                 :CBC     6474095 :                 numberOfEqualCols++;
                               2766                 :                :             }
                               2767                 :                : 
                               2768                 :                :             /* try to keep only one of <, <= */
    8 pg@bowt.ie               2769         [ +  + ]:GNC     7120683 :             if (xform[BTLessStrategyNumber - 1].skey
                               2770         [ +  + ]:            981 :                 && xform[BTLessEqualStrategyNumber - 1].skey)
                               2771                 :                :             {
                               2772                 :              3 :                 ScanKey     lt = xform[BTLessStrategyNumber - 1].skey;
                               2773                 :              3 :                 ScanKey     le = xform[BTLessEqualStrategyNumber - 1].skey;
                               2774                 :                : 
                               2775         [ +  - ]:              3 :                 if (_bt_compare_scankey_args(scan, le, lt, le, NULL, NULL,
                               2776                 :                :                                              &test_result))
                               2777                 :                :                 {
 6317 tgl@sss.pgh.pa.us        2778         [ +  - ]:GBC           3 :                     if (test_result)
    8 pg@bowt.ie               2779                 :GNC           3 :                         xform[BTLessEqualStrategyNumber - 1].skey = NULL;
                               2780                 :                :                     else
    8 pg@bowt.ie               2781                 :UNC           0 :                         xform[BTLessStrategyNumber - 1].skey = NULL;
                               2782                 :                :                 }
                               2783                 :                :             }
                               2784                 :                : 
                               2785                 :                :             /* try to keep only one of >, >= */
    8 pg@bowt.ie               2786         [ +  + ]:GNC     7120683 :             if (xform[BTGreaterStrategyNumber - 1].skey
                               2787         [ +  + ]:         644409 :                 && xform[BTGreaterEqualStrategyNumber - 1].skey)
                               2788                 :                :             {
                               2789                 :              3 :                 ScanKey     gt = xform[BTGreaterStrategyNumber - 1].skey;
                               2790                 :              3 :                 ScanKey     ge = xform[BTGreaterEqualStrategyNumber - 1].skey;
                               2791                 :                : 
                               2792         [ +  - ]:              3 :                 if (_bt_compare_scankey_args(scan, ge, gt, ge, NULL, NULL,
                               2793                 :                :                                              &test_result))
                               2794                 :                :                 {
 6317 tgl@sss.pgh.pa.us        2795         [ -  + ]:GBC           3 :                     if (test_result)
    8 pg@bowt.ie               2796                 :UNC           0 :                         xform[BTGreaterEqualStrategyNumber - 1].skey = NULL;
                               2797                 :                :                     else
    8 pg@bowt.ie               2798                 :GNC           3 :                         xform[BTGreaterStrategyNumber - 1].skey = NULL;
                               2799                 :                :                 }
                               2800                 :                :             }
                               2801                 :                : 
                               2802                 :                :             /*
                               2803                 :                :              * Emit the cleaned-up keys into the outkeys[] array, and then
                               2804                 :                :              * mark them if they are required.  They are required (possibly
                               2805                 :                :              * only in one direction) if all attrs before this one had "=".
                               2806                 :                :              */
 9716 bruce@momjian.us         2807         [ +  + ]:CBC    42724098 :             for (j = BTMaxStrategyNumber; --j >= 0;)
                               2808                 :                :             {
    8 pg@bowt.ie               2809         [ +  + ]:GNC    35603415 :                 if (xform[j].skey)
                               2810                 :                :                 {
 6656 tgl@sss.pgh.pa.us        2811                 :CBC     7121580 :                     ScanKey     outkey = &outkeys[new_numberOfKeys++];
                               2812                 :                : 
    8 pg@bowt.ie               2813                 :GNC     7121580 :                     memcpy(outkey, xform[j].skey, sizeof(ScanKeyData));
                               2814         [ +  + ]:        7121580 :                     if (arrayKeyData)
                               2815                 :            401 :                         keyDataMap[new_numberOfKeys - 1] = xform[j].ikey;
 6656 tgl@sss.pgh.pa.us        2816         [ +  + ]:CBC     7121580 :                     if (priorNumberOfEqualCols == attno - 1)
 6654                          2817                 :        7121264 :                         _bt_mark_scankey_required(outkey);
                               2818                 :                :                 }
                               2819                 :                :             }
                               2820                 :                : 
                               2821                 :                :             /*
                               2822                 :                :              * Exit loop here if done.
                               2823                 :                :              */
 9716 bruce@momjian.us         2824         [ +  + ]:        7120683 :             if (i == numberOfKeys)
                               2825                 :        3178655 :                 break;
                               2826                 :                : 
                               2827                 :                :             /* Re-initialize for new attno */
                               2828                 :        3942028 :             attno = cur->sk_attno;
 7459 tgl@sss.pgh.pa.us        2829                 :        3942028 :             memset(xform, 0, sizeof(xform));
                               2830                 :                :         }
                               2831                 :                : 
                               2832                 :                :         /* check strategy this key's operator corresponds to */
 7462                          2833                 :        7121698 :         j = cur->sk_strategy - 1;
                               2834                 :                : 
                               2835                 :                :         /* if row comparison, push it directly to the output array */
 6317                          2836         [ -  + ]:        7121698 :         if (cur->sk_flags & SK_ROW_HEADER)
 7459 tgl@sss.pgh.pa.us        2837                 :UBC           0 :         {
 6654                          2838                 :              0 :             ScanKey     outkey = &outkeys[new_numberOfKeys++];
                               2839                 :                : 
                               2840                 :              0 :             memcpy(outkey, cur, sizeof(ScanKeyData));
    8 pg@bowt.ie               2841         [ #  # ]:UNC           0 :             if (arrayKeyData)
                               2842                 :              0 :                 keyDataMap[new_numberOfKeys - 1] = i;
 6654 tgl@sss.pgh.pa.us        2843         [ #  # ]:UBC           0 :             if (numberOfEqualCols == attno - 1)
                               2844                 :              0 :                 _bt_mark_scankey_required(outkey);
                               2845                 :                : 
                               2846                 :                :             /*
                               2847                 :                :              * We don't support RowCompare using equality; such a qual would
                               2848                 :                :              * mess up the numberOfEqualCols tracking.
                               2849                 :                :              */
 6317                          2850         [ #  # ]:              0 :             Assert(j != (BTEqualStrategyNumber - 1));
 7459                          2851                 :              0 :             continue;
                               2852                 :                :         }
                               2853                 :                : 
                               2854                 :                :         /*
                               2855                 :                :          * Does this input scan key require further processing as an array?
                               2856                 :                :          */
    8 pg@bowt.ie               2857         [ +  + ]:GNC     7121698 :         if (cur->sk_strategy == InvalidStrategy)
                               2858                 :                :         {
                               2859                 :                :             /* _bt_preprocess_array_keys marked this array key redundant */
                               2860         [ -  + ]:              3 :             Assert(arrayKeyData);
                               2861         [ -  + ]:              3 :             Assert(cur->sk_flags & SK_SEARCHARRAY);
                               2862                 :              3 :             continue;
                               2863                 :                :         }
                               2864                 :                : 
                               2865         [ +  + ]:        7121695 :         if (cur->sk_strategy == BTEqualStrategyNumber &&
                               2866         [ +  + ]:        6474134 :             (cur->sk_flags & SK_SEARCHARRAY))
                               2867                 :                :         {
                               2868                 :                :             /* _bt_preprocess_array_keys kept this array key */
                               2869         [ -  + ]:            274 :             Assert(arrayKeyData);
                               2870                 :            274 :             arrayidx++;
                               2871                 :                :         }
                               2872                 :                : 
                               2873                 :                :         /*
                               2874                 :                :          * have we seen a scan key for this same attribute and using this same
                               2875                 :                :          * operator strategy before now?
                               2876                 :                :          */
                               2877         [ +  + ]:        7121695 :         if (xform[j].skey == NULL)
                               2878                 :                :         {
                               2879                 :                :             /* nope, so this scan key wins by default (at least for now) */
                               2880                 :        7121671 :             xform[j].skey = cur;
                               2881                 :        7121671 :             xform[j].ikey = i;
                               2882                 :        7121671 :             xform[j].arrayidx = arrayidx;
                               2883                 :                :         }
                               2884                 :                :         else
                               2885                 :                :         {
                               2886                 :             24 :             FmgrInfo   *orderproc = NULL;
                               2887                 :             24 :             BTArrayKeyInfo *array = NULL;
                               2888                 :                : 
                               2889                 :                :             /*
                               2890                 :                :              * Seen one of these before, so keep only the more restrictive key
                               2891                 :                :              * if possible
                               2892                 :                :              */
                               2893   [ +  +  +  + ]:             24 :             if (j == (BTEqualStrategyNumber - 1) && arrayKeyData)
                               2894                 :                :             {
                               2895                 :                :                 /*
                               2896                 :                :                  * Have to set up array keys
                               2897                 :                :                  */
                               2898         [ -  + ]:              6 :                 if ((cur->sk_flags & SK_SEARCHARRAY))
                               2899                 :                :                 {
    8 pg@bowt.ie               2900                 :UNC           0 :                     array = &so->arrayKeys[arrayidx - 1];
                               2901                 :              0 :                     orderproc = so->orderProcs + i;
                               2902                 :                : 
                               2903         [ #  # ]:              0 :                     Assert(array->scan_key == i);
                               2904         [ #  # ]:              0 :                     Assert(OidIsValid(orderproc->fn_oid));
                               2905                 :                :                 }
    8 pg@bowt.ie               2906         [ +  - ]:GNC           6 :                 else if ((xform[j].skey->sk_flags & SK_SEARCHARRAY))
                               2907                 :                :                 {
                               2908                 :              6 :                     array = &so->arrayKeys[xform[j].arrayidx - 1];
                               2909                 :              6 :                     orderproc = so->orderProcs + xform[j].ikey;
                               2910                 :                : 
                               2911         [ -  + ]:              6 :                     Assert(array->scan_key == xform[j].ikey);
                               2912         [ -  + ]:              6 :                     Assert(OidIsValid(orderproc->fn_oid));
                               2913                 :                :                 }
                               2914                 :                : 
                               2915                 :                :                 /*
                               2916                 :                :                  * Both scan keys might have arrays, in which case we'll
                               2917                 :                :                  * arbitrarily pass only one of the arrays.  That won't
                               2918                 :                :                  * matter, since _bt_compare_scankey_args is aware that two
                               2919                 :                :                  * SEARCHARRAY scan keys mean that _bt_preprocess_array_keys
                               2920                 :                :                  * failed to eliminate redundant arrays through array merging.
                               2921                 :                :                  * _bt_compare_scankey_args just returns false when it sees
                               2922                 :                :                  * this; it won't even try to examine either array.
                               2923                 :                :                  */
                               2924                 :                :             }
                               2925                 :                : 
                               2926         [ +  - ]:             24 :             if (_bt_compare_scankey_args(scan, cur, cur, xform[j].skey,
                               2927                 :                :                                          array, orderproc, &test_result))
                               2928                 :                :             {
                               2929                 :                :                 /* Have all we need to determine redundancy */
 6317 tgl@sss.pgh.pa.us        2930         [ +  + ]:CBC          24 :                 if (test_result)
                               2931                 :                :                 {
    8 pg@bowt.ie               2932   [ +  +  -  + ]:GNC          21 :                     Assert(!array || array->num_elems > 0);
                               2933                 :                : 
                               2934                 :                :                     /*
                               2935                 :                :                      * New key is more restrictive, and so replaces old key...
                               2936                 :                :                      */
                               2937         [ +  + ]:             21 :                     if (j != (BTEqualStrategyNumber - 1) ||
                               2938         [ +  + ]:              6 :                         !(xform[j].skey->sk_flags & SK_SEARCHARRAY))
                               2939                 :                :                     {
                               2940                 :             18 :                         xform[j].skey = cur;
                               2941                 :             18 :                         xform[j].ikey = i;
                               2942                 :             18 :                         xform[j].arrayidx = arrayidx;
                               2943                 :                :                     }
                               2944                 :                :                     else
                               2945                 :                :                     {
                               2946                 :                :                         /*
                               2947                 :                :                          * ...unless we have to keep the old key because it's
                               2948                 :                :                          * an array that rendered the new key redundant.  We
                               2949                 :                :                          * need to make sure that we don't throw away an array
                               2950                 :                :                          * scan key.  _bt_compare_scankey_args expects us to
                               2951                 :                :                          * always keep arrays (and discard non-arrays).
                               2952                 :                :                          */
                               2953         [ -  + ]:              3 :                         Assert(!(cur->sk_flags & SK_SEARCHARRAY));
                               2954                 :                :                     }
                               2955                 :                :                 }
 6317 tgl@sss.pgh.pa.us        2956         [ +  - ]:CBC           3 :                 else if (j == (BTEqualStrategyNumber - 1))
                               2957                 :                :                 {
                               2958                 :                :                     /* key == a && key == b, but a != b */
 6317 tgl@sss.pgh.pa.us        2959                 :GBC           3 :                     so->qual_ok = false;
                               2960                 :              3 :                     return;
                               2961                 :                :                 }
                               2962                 :                :                 /* else old key is more restrictive, keep it */
                               2963                 :                :             }
                               2964                 :                :             else
                               2965                 :                :             {
                               2966                 :                :                 /*
                               2967                 :                :                  * We can't determine which key is more restrictive.  Push
                               2968                 :                :                  * xform[j] directly to the output array, then set xform[j] to
                               2969                 :                :                  * the new scan key.
                               2970                 :                :                  *
                               2971                 :                :                  * Note: We do things this way around so that our arrays are
                               2972                 :                :                  * always in the same order as their corresponding scan keys,
                               2973                 :                :                  * even with incomplete opfamilies.  _bt_advance_array_keys
                               2974                 :                :                  * depends on this.
                               2975                 :                :                  */
 6317 tgl@sss.pgh.pa.us        2976                 :UBC           0 :                 ScanKey     outkey = &outkeys[new_numberOfKeys++];
                               2977                 :                : 
    8 pg@bowt.ie               2978                 :UNC           0 :                 memcpy(outkey, xform[j].skey, sizeof(ScanKeyData));
                               2979         [ #  # ]:              0 :                 if (arrayKeyData)
                               2980                 :              0 :                     keyDataMap[new_numberOfKeys - 1] = xform[j].ikey;
 6317 tgl@sss.pgh.pa.us        2981         [ #  # ]:UBC           0 :                 if (numberOfEqualCols == attno - 1)
                               2982                 :              0 :                     _bt_mark_scankey_required(outkey);
    8 pg@bowt.ie               2983                 :UNC           0 :                 xform[j].skey = cur;
                               2984                 :              0 :                 xform[j].ikey = i;
                               2985                 :              0 :                 xform[j].arrayidx = arrayidx;
                               2986                 :                :             }
                               2987                 :                :         }
                               2988                 :                :     }
                               2989                 :                : 
 9716 bruce@momjian.us         2990                 :CBC     3178655 :     so->numberOfKeys = new_numberOfKeys;
                               2991                 :                : 
                               2992                 :                :     /*
                               2993                 :                :      * Now that we've built a temporary mapping from so->keyData[] (output
                               2994                 :                :      * scan keys) to scan->keyData[] (input scan keys), fix array->scan_key
                               2995                 :                :      * references.  Also consolidate the so->orderProc[] array such that it
                               2996                 :                :      * can be subscripted using so->keyData[]-wise offsets.
                               2997                 :                :      */
    8 pg@bowt.ie               2998         [ +  + ]:GNC     3178655 :     if (arrayKeyData)
                               2999                 :            196 :         _bt_preprocess_array_keys_final(scan, keyDataMap);
                               3000                 :                : 
                               3001                 :                :     /* Could pfree arrayKeyData/keyDataMap now, but not worth the cycles */
                               3002                 :                : }
                               3003                 :                : 
                               3004                 :                : #ifdef USE_ASSERT_CHECKING
                               3005                 :                : /*
                               3006                 :                :  * Verify that the scan's qual state matches what we expect at the point that
                               3007                 :                :  * _bt_start_prim_scan is about to start a just-scheduled new primitive scan.
                               3008                 :                :  *
                               3009                 :                :  * We enforce a rule against non-required array scan keys: they must start out
                               3010                 :                :  * with whatever element is the first for the scan's current scan direction.
                               3011                 :                :  * See _bt_rewind_nonrequired_arrays comments for an explanation.
                               3012                 :                :  */
                               3013                 :                : static bool
                               3014                 :            285 : _bt_verify_arrays_bt_first(IndexScanDesc scan, ScanDirection dir)
                               3015                 :                : {
                               3016                 :            285 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               3017                 :            285 :     int         arrayidx = 0;
                               3018                 :                : 
                               3019         [ +  + ]:            830 :     for (int ikey = 0; ikey < so->numberOfKeys; ikey++)
                               3020                 :                :     {
                               3021                 :            545 :         ScanKey     cur = so->keyData + ikey;
                               3022                 :            545 :         BTArrayKeyInfo *array = NULL;
                               3023                 :                :         int         first_elem_dir;
                               3024                 :                : 
                               3025         [ +  + ]:            545 :         if (!(cur->sk_flags & SK_SEARCHARRAY) ||
                               3026         [ -  + ]:            435 :             cur->sk_strategy != BTEqualStrategyNumber)
                               3027                 :            110 :             continue;
                               3028                 :                : 
                               3029                 :            435 :         array = &so->arrayKeys[arrayidx++];
                               3030                 :                : 
                               3031   [ +  -  +  + ]:            435 :         if (((cur->sk_flags & SK_BT_REQFWD) && ScanDirectionIsForward(dir)) ||
                               3032   [ +  -  +  - ]:              3 :             ((cur->sk_flags & SK_BT_REQBKWD) && ScanDirectionIsBackward(dir)))
                               3033                 :            435 :             continue;
                               3034                 :                : 
    8 pg@bowt.ie               3035         [ #  # ]:UNC           0 :         if (ScanDirectionIsForward(dir))
                               3036                 :              0 :             first_elem_dir = 0;
                               3037                 :                :         else
                               3038                 :              0 :             first_elem_dir = array->num_elems - 1;
                               3039                 :                : 
                               3040         [ #  # ]:              0 :         if (array->cur_elem != first_elem_dir)
                               3041                 :              0 :             return false;
                               3042                 :                :     }
                               3043                 :                : 
    8 pg@bowt.ie               3044                 :GNC         285 :     return _bt_verify_keys_with_arraykeys(scan);
                               3045                 :                : }
                               3046                 :                : 
                               3047                 :                : /*
                               3048                 :                :  * Verify that the scan's "so->keyData[]" scan keys are in agreement with
                               3049                 :                :  * its array key state
                               3050                 :                :  */
                               3051                 :                : static bool
                               3052                 :           4123 : _bt_verify_keys_with_arraykeys(IndexScanDesc scan)
                               3053                 :                : {
                               3054                 :           4123 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               3055                 :           4123 :     int         last_sk_attno = InvalidAttrNumber,
                               3056                 :           4123 :                 arrayidx = 0;
                               3057                 :                : 
                               3058         [ -  + ]:           4123 :     if (!so->qual_ok)
    8 pg@bowt.ie               3059                 :UNC           0 :         return false;
                               3060                 :                : 
    8 pg@bowt.ie               3061         [ +  + ]:GNC       11679 :     for (int ikey = 0; ikey < so->numberOfKeys; ikey++)
                               3062                 :                :     {
                               3063                 :           7556 :         ScanKey     cur = so->keyData + ikey;
                               3064                 :                :         BTArrayKeyInfo *array;
                               3065                 :                : 
                               3066         [ +  + ]:           7556 :         if (cur->sk_strategy != BTEqualStrategyNumber ||
                               3067         [ +  + ]:           7298 :             !(cur->sk_flags & SK_SEARCHARRAY))
                               3068                 :           2407 :             continue;
                               3069                 :                : 
                               3070                 :           5149 :         array = &so->arrayKeys[arrayidx++];
                               3071         [ -  + ]:           5149 :         if (array->scan_key != ikey)
    8 pg@bowt.ie               3072                 :UNC           0 :             return false;
                               3073                 :                : 
    8 pg@bowt.ie               3074         [ -  + ]:GNC        5149 :         if (array->num_elems <= 0)
    8 pg@bowt.ie               3075                 :UNC           0 :             return false;
                               3076                 :                : 
    8 pg@bowt.ie               3077         [ -  + ]:GNC        5149 :         if (cur->sk_argument != array->elem_values[array->cur_elem])
    8 pg@bowt.ie               3078                 :UNC           0 :             return false;
    8 pg@bowt.ie               3079         [ -  + ]:GNC        5149 :         if (last_sk_attno > cur->sk_attno)
    8 pg@bowt.ie               3080                 :UNC           0 :             return false;
    8 pg@bowt.ie               3081                 :GNC        5149 :         last_sk_attno = cur->sk_attno;
                               3082                 :                :     }
                               3083                 :                : 
                               3084         [ -  + ]:           4123 :     if (arrayidx != so->numArrayKeys)
    8 pg@bowt.ie               3085                 :UNC           0 :         return false;
                               3086                 :                : 
    8 pg@bowt.ie               3087                 :GNC        4123 :     return true;
                               3088                 :                : }
                               3089                 :                : #endif
                               3090                 :                : 
                               3091                 :                : /*
                               3092                 :                :  * Compare two scankey values using a specified operator.
                               3093                 :                :  *
                               3094                 :                :  * The test we want to perform is logically "leftarg op rightarg", where
                               3095                 :                :  * leftarg and rightarg are the sk_argument values in those ScanKeys, and
                               3096                 :                :  * the comparison operator is the one in the op ScanKey.  However, in
                               3097                 :                :  * cross-data-type situations we may need to look up the correct operator in
                               3098                 :                :  * the index's opfamily: it is the one having amopstrategy = op->sk_strategy
                               3099                 :                :  * and amoplefttype/amoprighttype equal to the two argument datatypes.
                               3100                 :                :  *
                               3101                 :                :  * If the opfamily doesn't supply a complete set of cross-type operators we
                               3102                 :                :  * may not be able to make the comparison.  If we can make the comparison
                               3103                 :                :  * we store the operator result in *result and return true.  We return false
                               3104                 :                :  * if the comparison could not be made.
                               3105                 :                :  *
                               3106                 :                :  * If either leftarg or rightarg are an array, we'll apply array-specific
                               3107                 :                :  * rules to determine which array elements are redundant on behalf of caller.
                               3108                 :                :  * It is up to our caller to save whichever of the two scan keys is the array,
                               3109                 :                :  * and discard the non-array scan key (the non-array scan key is guaranteed to
                               3110                 :                :  * be redundant with any complete opfamily).  Caller isn't expected to call
                               3111                 :                :  * here with a pair of array scan keys provided we're dealing with a complete
                               3112                 :                :  * opfamily (_bt_preprocess_array_keys will merge array keys together to make
                               3113                 :                :  * sure of that).
                               3114                 :                :  *
                               3115                 :                :  * Note: we'll also shrink caller's array as needed to eliminate redundant
                               3116                 :                :  * array elements.  One reason why caller should prefer to discard non-array
                               3117                 :                :  * scan keys is so that we'll have the opportunity to shrink the array
                               3118                 :                :  * multiple times, in multiple calls (for each of several other scan keys on
                               3119                 :                :  * the same index attribute).
                               3120                 :                :  *
                               3121                 :                :  * Note: op always points at the same ScanKey as either leftarg or rightarg.
                               3122                 :                :  * Since we don't scribble on the scankeys themselves, this aliasing should
                               3123                 :                :  * cause no trouble.
                               3124                 :                :  *
                               3125                 :                :  * Note: this routine needs to be insensitive to any DESC option applied
                               3126                 :                :  * to the index column.  For example, "x < 4" is a tighter constraint than
                               3127                 :                :  * "x < 5" regardless of which way the index is sorted.
                               3128                 :                :  */
                               3129                 :                : static bool
 6317 tgl@sss.pgh.pa.us        3130                 :CBC          73 : _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op,
                               3131                 :                :                          ScanKey leftarg, ScanKey rightarg,
                               3132                 :                :                          BTArrayKeyInfo *array, FmgrInfo *orderproc,
                               3133                 :                :                          bool *result)
                               3134                 :                : {
                               3135                 :             73 :     Relation    rel = scan->indexRelation;
                               3136                 :                :     Oid         lefttype,
                               3137                 :                :                 righttype,
                               3138                 :                :                 optype,
                               3139                 :                :                 opcintype,
                               3140                 :                :                 cmp_op;
                               3141                 :                :     StrategyNumber strat;
                               3142                 :                : 
                               3143                 :                :     /*
                               3144                 :                :      * First, deal with cases where one or both args are NULL.  This should
                               3145                 :                :      * only happen when the scankeys represent IS NULL/NOT NULL conditions.
                               3146                 :                :      */
 5217                          3147         [ +  + ]:             73 :     if ((leftarg->sk_flags | rightarg->sk_flags) & SK_ISNULL)
                               3148                 :                :     {
                               3149                 :                :         bool        leftnull,
                               3150                 :                :                     rightnull;
                               3151                 :                : 
                               3152         [ -  + ]:              6 :         if (leftarg->sk_flags & SK_ISNULL)
                               3153                 :                :         {
 5217 tgl@sss.pgh.pa.us        3154         [ #  # ]:LBC         (3) :             Assert(leftarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
                               3155                 :            (3) :             leftnull = true;
                               3156                 :                :         }
                               3157                 :                :         else
 5217 tgl@sss.pgh.pa.us        3158                 :CBC           6 :             leftnull = false;
                               3159         [ +  - ]:              6 :         if (rightarg->sk_flags & SK_ISNULL)
                               3160                 :                :         {
                               3161         [ -  + ]:              6 :             Assert(rightarg->sk_flags & (SK_SEARCHNULL | SK_SEARCHNOTNULL));
                               3162                 :              6 :             rightnull = true;
                               3163                 :                :         }
                               3164                 :                :         else
 5217 tgl@sss.pgh.pa.us        3165                 :LBC         (3) :             rightnull = false;
                               3166                 :                : 
                               3167                 :                :         /*
                               3168                 :                :          * We treat NULL as either greater than or less than all other values.
                               3169                 :                :          * Since true > false, the tests below work correctly for NULLS LAST
                               3170                 :                :          * logic.  If the index is NULLS FIRST, we need to flip the strategy.
                               3171                 :                :          */
 5217 tgl@sss.pgh.pa.us        3172                 :CBC           6 :         strat = op->sk_strategy;
                               3173         [ -  + ]:              6 :         if (op->sk_flags & SK_BT_NULLS_FIRST)
 5217 tgl@sss.pgh.pa.us        3174                 :UBC           0 :             strat = BTCommuteStrategyNumber(strat);
                               3175                 :                : 
 5217 tgl@sss.pgh.pa.us        3176   [ +  -  -  -  :CBC           6 :         switch (strat)
                                              -  - ]
                               3177                 :                :         {
                               3178                 :              6 :             case BTLessStrategyNumber:
                               3179                 :              6 :                 *result = (leftnull < rightnull);
                               3180                 :              6 :                 break;
 5217 tgl@sss.pgh.pa.us        3181                 :UBC           0 :             case BTLessEqualStrategyNumber:
                               3182                 :              0 :                 *result = (leftnull <= rightnull);
                               3183                 :              0 :                 break;
                               3184                 :              0 :             case BTEqualStrategyNumber:
                               3185                 :              0 :                 *result = (leftnull == rightnull);
                               3186                 :              0 :                 break;
                               3187                 :              0 :             case BTGreaterEqualStrategyNumber:
                               3188                 :              0 :                 *result = (leftnull >= rightnull);
                               3189                 :              0 :                 break;
                               3190                 :              0 :             case BTGreaterStrategyNumber:
                               3191                 :              0 :                 *result = (leftnull > rightnull);
                               3192                 :              0 :                 break;
                               3193                 :              0 :             default:
                               3194         [ #  # ]:              0 :                 elog(ERROR, "unrecognized StrategyNumber: %d", (int) strat);
                               3195                 :                :                 *result = false;    /* keep compiler quiet */
                               3196                 :                :                 break;
                               3197                 :                :         }
 5217 tgl@sss.pgh.pa.us        3198                 :CBC           6 :         return true;
                               3199                 :                :     }
                               3200                 :                : 
                               3201                 :                :     /*
                               3202                 :                :      * If either leftarg or rightarg are equality-type array scankeys, we need
                               3203                 :                :      * specialized handling (since by now we know that IS NULL wasn't used)
                               3204                 :                :      */
    8 pg@bowt.ie               3205         [ +  + ]:GNC          67 :     if (array)
                               3206                 :                :     {
                               3207                 :                :         bool        leftarray,
                               3208                 :                :                     rightarray;
                               3209                 :                : 
                               3210         [ +  + ]:             24 :         leftarray = ((leftarg->sk_flags & SK_SEARCHARRAY) &&
                               3211         [ +  - ]:              9 :                      leftarg->sk_strategy == BTEqualStrategyNumber);
                               3212         [ +  + ]:             21 :         rightarray = ((rightarg->sk_flags & SK_SEARCHARRAY) &&
                               3213         [ +  - ]:              6 :                       rightarg->sk_strategy == BTEqualStrategyNumber);
                               3214                 :                : 
                               3215                 :                :         /*
                               3216                 :                :          * _bt_preprocess_array_keys is responsible for merging together array
                               3217                 :                :          * scan keys, and will do so whenever the opfamily has the required
                               3218                 :                :          * cross-type support.  If it failed to do that, we handle it just
                               3219                 :                :          * like the case where we can't make the comparison ourselves.
                               3220                 :                :          */
                               3221   [ +  +  -  + ]:             15 :         if (leftarray && rightarray)
                               3222                 :                :         {
                               3223                 :                :             /* Can't make the comparison */
    8 pg@bowt.ie               3224                 :UNC           0 :             *result = false;    /* suppress compiler warnings */
                               3225                 :              0 :             return false;
                               3226                 :                :         }
                               3227                 :                : 
                               3228                 :                :         /*
                               3229                 :                :          * Otherwise we need to determine if either one of leftarg or rightarg
                               3230                 :                :          * uses an array, then pass this through to a dedicated helper
                               3231                 :                :          * function.
                               3232                 :                :          */
    8 pg@bowt.ie               3233         [ +  + ]:GNC          15 :         if (leftarray)
                               3234                 :              9 :             return _bt_compare_array_scankey_args(scan, leftarg, rightarg,
                               3235                 :                :                                                   orderproc, array, result);
                               3236         [ +  - ]:              6 :         else if (rightarray)
                               3237                 :              6 :             return _bt_compare_array_scankey_args(scan, rightarg, leftarg,
                               3238                 :                :                                                   orderproc, array, result);
                               3239                 :                : 
                               3240                 :                :         /* FALL THRU */
                               3241                 :                :     }
                               3242                 :                : 
                               3243                 :                :     /*
                               3244                 :                :      * The opfamily we need to worry about is identified by the index column.
                               3245                 :                :      */
 6317 tgl@sss.pgh.pa.us        3246         [ -  + ]:CBC          52 :     Assert(leftarg->sk_attno == rightarg->sk_attno);
                               3247                 :                : 
                               3248                 :             52 :     opcintype = rel->rd_opcintype[leftarg->sk_attno - 1];
                               3249                 :                : 
                               3250                 :                :     /*
                               3251                 :                :      * Determine the actual datatypes of the ScanKey arguments.  We have to
                               3252                 :                :      * support the convention that sk_subtype == InvalidOid means the opclass
                               3253                 :                :      * input type; this is a hack to simplify life for ScanKeyInit().
                               3254                 :                :      */
                               3255                 :             52 :     lefttype = leftarg->sk_subtype;
                               3256         [ -  + ]:             52 :     if (lefttype == InvalidOid)
 6317 tgl@sss.pgh.pa.us        3257                 :UBC           0 :         lefttype = opcintype;
 6317 tgl@sss.pgh.pa.us        3258                 :CBC          52 :     righttype = rightarg->sk_subtype;
                               3259         [ -  + ]:             52 :     if (righttype == InvalidOid)
 6317 tgl@sss.pgh.pa.us        3260                 :UBC           0 :         righttype = opcintype;
 6317 tgl@sss.pgh.pa.us        3261                 :CBC          52 :     optype = op->sk_subtype;
                               3262         [ -  + ]:             52 :     if (optype == InvalidOid)
 6317 tgl@sss.pgh.pa.us        3263                 :UBC           0 :         optype = opcintype;
                               3264                 :                : 
                               3265                 :                :     /*
                               3266                 :                :      * If leftarg and rightarg match the types expected for the "op" scankey,
                               3267                 :                :      * we can use its already-looked-up comparison function.
                               3268                 :                :      */
 6317 tgl@sss.pgh.pa.us        3269   [ +  +  +  - ]:CBC          52 :     if (lefttype == opcintype && righttype == optype)
                               3270                 :                :     {
 4751                          3271                 :             49 :         *result = DatumGetBool(FunctionCall2Coll(&op->sk_func,
                               3272                 :                :                                                  op->sk_collation,
                               3273                 :                :                                                  leftarg->sk_argument,
                               3274                 :                :                                                  rightarg->sk_argument));
 6317                          3275                 :             49 :         return true;
                               3276                 :                :     }
                               3277                 :                : 
                               3278                 :                :     /*
                               3279                 :                :      * Otherwise, we need to go to the syscache to find the appropriate
                               3280                 :                :      * operator.  (This cannot result in infinite recursion, since no
                               3281                 :                :      * indexscan initiated by syscache lookup will use cross-data-type
                               3282                 :                :      * operators.)
                               3283                 :                :      *
                               3284                 :                :      * If the sk_strategy was flipped by _bt_fix_scankey_strategy, we have to
                               3285                 :                :      * un-flip it to get the correct opfamily member.
                               3286                 :                :      */
 6305 tgl@sss.pgh.pa.us        3287                 :GBC           3 :     strat = op->sk_strategy;
                               3288         [ -  + ]:              3 :     if (op->sk_flags & SK_BT_DESC)
 6305 tgl@sss.pgh.pa.us        3289                 :UBC           0 :         strat = BTCommuteStrategyNumber(strat);
                               3290                 :                : 
 6317 tgl@sss.pgh.pa.us        3291                 :GBC           3 :     cmp_op = get_opfamily_member(rel->rd_opfamily[leftarg->sk_attno - 1],
                               3292                 :                :                                  lefttype,
                               3293                 :                :                                  righttype,
                               3294                 :                :                                  strat);
                               3295         [ +  - ]:              3 :     if (OidIsValid(cmp_op))
                               3296                 :                :     {
                               3297                 :              3 :         RegProcedure cmp_proc = get_opcode(cmp_op);
                               3298                 :                : 
                               3299         [ +  - ]:              3 :         if (RegProcedureIsValid(cmp_proc))
                               3300                 :                :         {
 4751                          3301                 :              3 :             *result = DatumGetBool(OidFunctionCall2Coll(cmp_proc,
                               3302                 :                :                                                         op->sk_collation,
                               3303                 :                :                                                         leftarg->sk_argument,
                               3304                 :                :                                                         rightarg->sk_argument));
 6317                          3305                 :              3 :             return true;
                               3306                 :                :         }
                               3307                 :                :     }
                               3308                 :                : 
                               3309                 :                :     /* Can't make the comparison */
 6317 tgl@sss.pgh.pa.us        3310                 :UBC           0 :     *result = false;            /* suppress compiler warnings */
                               3311                 :              0 :     return false;
                               3312                 :                : }
                               3313                 :                : 
                               3314                 :                : /*
                               3315                 :                :  * Adjust a scankey's strategy and flags setting as needed for indoptions.
                               3316                 :                :  *
                               3317                 :                :  * We copy the appropriate indoption value into the scankey sk_flags
                               3318                 :                :  * (shifting to avoid clobbering system-defined flag bits).  Also, if
                               3319                 :                :  * the DESC option is set, commute (flip) the operator strategy number.
                               3320                 :                :  *
                               3321                 :                :  * A secondary purpose is to check for IS NULL/NOT NULL scankeys and set up
                               3322                 :                :  * the strategy field correctly for them.
                               3323                 :                :  *
                               3324                 :                :  * Lastly, for ordinary scankeys (not IS NULL/NOT NULL), we check for a
                               3325                 :                :  * NULL comparison value.  Since all btree operators are assumed strict,
                               3326                 :                :  * a NULL means that the qual cannot be satisfied.  We return true if the
                               3327                 :                :  * comparison value isn't NULL, or false if the scan should be abandoned.
                               3328                 :                :  *
                               3329                 :                :  * This function is applied to the *input* scankey structure; therefore
                               3330                 :                :  * on a rescan we will be looking at already-processed scankeys.  Hence
                               3331                 :                :  * we have to be careful not to re-commute the strategy if we already did it.
                               3332                 :                :  * It's a bit ugly to modify the caller's copy of the scankey but in practice
                               3333                 :                :  * there shouldn't be any problem, since the index's indoptions are certainly
                               3334                 :                :  * not going to change while the scankey survives.
                               3335                 :                :  */
                               3336                 :                : static bool
 5215 tgl@sss.pgh.pa.us        3337                 :CBC    10358262 : _bt_fix_scankey_strategy(ScanKey skey, int16 *indoption)
                               3338                 :                : {
                               3339                 :                :     int         addflags;
                               3340                 :                : 
 6305                          3341                 :       10358262 :     addflags = indoption[skey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
                               3342                 :                : 
                               3343                 :                :     /*
                               3344                 :                :      * We treat all btree operators as strict (even if they're not so marked
                               3345                 :                :      * in pg_proc). This means that it is impossible for an operator condition
                               3346                 :                :      * with a NULL comparison constant to succeed, and we can reject it right
                               3347                 :                :      * away.
                               3348                 :                :      *
                               3349                 :                :      * However, we now also support "x IS NULL" clauses as search conditions,
                               3350                 :                :      * so in that case keep going. The planner has not filled in any
                               3351                 :                :      * particular strategy in this case, so set it to BTEqualStrategyNumber
                               3352                 :                :      * --- we can treat IS NULL as an equality operator for purposes of search
                               3353                 :                :      * strategy.
                               3354                 :                :      *
                               3355                 :                :      * Likewise, "x IS NOT NULL" is supported.  We treat that as either "less
                               3356                 :                :      * than NULL" in a NULLS LAST index, or "greater than NULL" in a NULLS
                               3357                 :                :      * FIRST index.
                               3358                 :                :      *
                               3359                 :                :      * Note: someday we might have to fill in sk_collation from the index
                               3360                 :                :      * column's collation.  At the moment this is a non-issue because we'll
                               3361                 :                :      * never actually call the comparison operator on a NULL.
                               3362                 :                :      */
 5215                          3363         [ +  + ]:       10358262 :     if (skey->sk_flags & SK_ISNULL)
                               3364                 :                :     {
                               3365                 :                :         /* SK_ISNULL shouldn't be set in a row header scankey */
                               3366         [ -  + ]:          56875 :         Assert(!(skey->sk_flags & SK_ROW_HEADER));
                               3367                 :                : 
                               3368                 :                :         /* Set indoption flags in scankey (might be done already) */
                               3369                 :          56875 :         skey->sk_flags |= addflags;
                               3370                 :                : 
                               3371                 :                :         /* Set correct strategy for IS NULL or NOT NULL search */
                               3372         [ +  + ]:          56875 :         if (skey->sk_flags & SK_SEARCHNULL)
                               3373                 :                :         {
                               3374                 :             70 :             skey->sk_strategy = BTEqualStrategyNumber;
                               3375                 :             70 :             skey->sk_subtype = InvalidOid;
 4751                          3376                 :             70 :             skey->sk_collation = InvalidOid;
                               3377                 :                :         }
 5215                          3378         [ +  + ]:          56805 :         else if (skey->sk_flags & SK_SEARCHNOTNULL)
                               3379                 :                :         {
                               3380         [ +  + ]:          55442 :             if (skey->sk_flags & SK_BT_NULLS_FIRST)
                               3381                 :             18 :                 skey->sk_strategy = BTGreaterStrategyNumber;
                               3382                 :                :             else
                               3383                 :          55424 :                 skey->sk_strategy = BTLessStrategyNumber;
                               3384                 :          55442 :             skey->sk_subtype = InvalidOid;
 4751                          3385                 :          55442 :             skey->sk_collation = InvalidOid;
                               3386                 :                :         }
                               3387                 :                :         else
                               3388                 :                :         {
                               3389                 :                :             /* regular qual, so it cannot be satisfied */
 5215                          3390                 :           1363 :             return false;
                               3391                 :                :         }
                               3392                 :                : 
                               3393                 :                :         /* Needn't do the rest */
                               3394                 :          55512 :         return true;
                               3395                 :                :     }
                               3396                 :                : 
                               3397                 :                :     /* Adjust strategy for DESC, if we didn't already */
 6305                          3398   [ +  +  +  - ]:       10301387 :     if ((addflags & SK_BT_DESC) && !(skey->sk_flags & SK_BT_DESC))
 6305 tgl@sss.pgh.pa.us        3399                 :GBC           3 :         skey->sk_strategy = BTCommuteStrategyNumber(skey->sk_strategy);
 6305 tgl@sss.pgh.pa.us        3400                 :CBC    10301387 :     skey->sk_flags |= addflags;
                               3401                 :                : 
                               3402                 :                :     /* If it's a row header, fix row member flags and strategies similarly */
                               3403         [ +  + ]:       10301387 :     if (skey->sk_flags & SK_ROW_HEADER)
                               3404                 :                :     {
                               3405                 :             18 :         ScanKey     subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
                               3406                 :                : 
                               3407                 :                :         for (;;)
                               3408                 :                :         {
                               3409         [ -  + ]:             36 :             Assert(subkey->sk_flags & SK_ROW_MEMBER);
                               3410                 :             36 :             addflags = indoption[subkey->sk_attno - 1] << SK_BT_INDOPTION_SHIFT;
                               3411   [ -  +  -  - ]:             36 :             if ((addflags & SK_BT_DESC) && !(subkey->sk_flags & SK_BT_DESC))
 6305 tgl@sss.pgh.pa.us        3412                 :UBC           0 :                 subkey->sk_strategy = BTCommuteStrategyNumber(subkey->sk_strategy);
 6305 tgl@sss.pgh.pa.us        3413                 :CBC          36 :             subkey->sk_flags |= addflags;
                               3414         [ +  + ]:             36 :             if (subkey->sk_flags & SK_ROW_END)
                               3415                 :             18 :                 break;
                               3416                 :             18 :             subkey++;
                               3417                 :                :         }
                               3418                 :                :     }
                               3419                 :                : 
 5215                          3420                 :       10301387 :     return true;
                               3421                 :                : }
                               3422                 :                : 
                               3423                 :                : /*
                               3424                 :                :  * Mark a scankey as "required to continue the scan".
                               3425                 :                :  *
                               3426                 :                :  * Depending on the operator type, the key may be required for both scan
                               3427                 :                :  * directions or just one.  Also, if the key is a row comparison header,
                               3428                 :                :  * we have to mark its first subsidiary ScanKey as required.  (Subsequent
                               3429                 :                :  * subsidiary ScanKeys are normally for lower-order columns, and thus
                               3430                 :                :  * cannot be required, since they're after the first non-equality scankey.)
                               3431                 :                :  *
                               3432                 :                :  * Note: when we set required-key flag bits in a subsidiary scankey, we are
                               3433                 :                :  * scribbling on a data structure belonging to the index AM's caller, not on
                               3434                 :                :  * our private copy.  This should be OK because the marking will not change
                               3435                 :                :  * from scan to scan within a query, and so we'd just re-mark the same way
                               3436                 :                :  * anyway on a rescan.  Something to keep an eye on though.
                               3437                 :                :  */
                               3438                 :                : static void
 6654                          3439                 :       10356496 : _bt_mark_scankey_required(ScanKey skey)
                               3440                 :                : {
                               3441                 :                :     int         addflags;
                               3442                 :                : 
                               3443   [ +  +  +  - ]:       10356496 :     switch (skey->sk_strategy)
                               3444                 :                :     {
                               3445                 :          56916 :         case BTLessStrategyNumber:
                               3446                 :                :         case BTLessEqualStrategyNumber:
                               3447                 :          56916 :             addflags = SK_BT_REQFWD;
                               3448                 :          56916 :             break;
                               3449                 :        9652566 :         case BTEqualStrategyNumber:
                               3450                 :        9652566 :             addflags = SK_BT_REQFWD | SK_BT_REQBKWD;
                               3451                 :        9652566 :             break;
                               3452                 :         647014 :         case BTGreaterEqualStrategyNumber:
                               3453                 :                :         case BTGreaterStrategyNumber:
                               3454                 :         647014 :             addflags = SK_BT_REQBKWD;
                               3455                 :         647014 :             break;
 6654 tgl@sss.pgh.pa.us        3456                 :UBC           0 :         default:
                               3457         [ #  # ]:              0 :             elog(ERROR, "unrecognized StrategyNumber: %d",
                               3458                 :                :                  (int) skey->sk_strategy);
                               3459                 :                :             addflags = 0;       /* keep compiler quiet */
                               3460                 :                :             break;
                               3461                 :                :     }
                               3462                 :                : 
 6654 tgl@sss.pgh.pa.us        3463                 :CBC    10356496 :     skey->sk_flags |= addflags;
                               3464                 :                : 
                               3465         [ +  + ]:       10356496 :     if (skey->sk_flags & SK_ROW_HEADER)
                               3466                 :                :     {
 6402 bruce@momjian.us         3467                 :             18 :         ScanKey     subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
                               3468                 :                : 
                               3469                 :                :         /* First subkey should be same column/operator as the header */
 2958 tgl@sss.pgh.pa.us        3470         [ -  + ]:             18 :         Assert(subkey->sk_flags & SK_ROW_MEMBER);
                               3471         [ -  + ]:             18 :         Assert(subkey->sk_attno == skey->sk_attno);
                               3472         [ -  + ]:             18 :         Assert(subkey->sk_strategy == skey->sk_strategy);
                               3473                 :             18 :         subkey->sk_flags |= addflags;
                               3474                 :                :     }
 6654                          3475                 :       10356496 : }
                               3476                 :                : 
                               3477                 :                : /*
                               3478                 :                :  * Test whether an indextuple satisfies all the scankey conditions.
                               3479                 :                :  *
                               3480                 :                :  * Return true if so, false if not.  If the tuple fails to pass the qual,
                               3481                 :                :  * we also determine whether there's any need to continue the scan beyond
                               3482                 :                :  * this tuple, and set pstate.continuescan accordingly.  See comments for
                               3483                 :                :  * _bt_preprocess_keys(), above, about how this is done.
                               3484                 :                :  *
                               3485                 :                :  * Forward scan callers can pass a high key tuple in the hopes of having
                               3486                 :                :  * us set *continuescan to false, and avoiding an unnecessary visit to
                               3487                 :                :  * the page to the right.
                               3488                 :                :  *
                               3489                 :                :  * Advances the scan's array keys when necessary for arrayKeys=true callers.
                               3490                 :                :  * Caller can avoid all array related side-effects when calling just to do a
                               3491                 :                :  * page continuescan precheck -- pass arrayKeys=false for that.  Scans without
                               3492                 :                :  * any arrays keys must always pass arrayKeys=false.
                               3493                 :                :  *
                               3494                 :                :  * Also stops and starts primitive index scans for arrayKeys=true callers.
                               3495                 :                :  * Scans with array keys are required to set up page state that helps us with
                               3496                 :                :  * this.  The page's finaltup tuple (the page high key for a forward scan, or
                               3497                 :                :  * the page's first non-pivot tuple for a backward scan) must be set in
                               3498                 :                :  * pstate.finaltup ahead of the first call here for the page (or possibly the
                               3499                 :                :  * first call after an initial continuescan-setting page precheck call).  Set
                               3500                 :                :  * this to NULL for rightmost page (or the leftmost page for backwards scans).
                               3501                 :                :  *
                               3502                 :                :  * scan: index scan descriptor (containing a search-type scankey)
                               3503                 :                :  * pstate: page level input and output parameters
                               3504                 :                :  * arrayKeys: should we advance the scan's array keys if necessary?
                               3505                 :                :  * tuple: index tuple to test
                               3506                 :                :  * tupnatts: number of attributes in tupnatts (high key may be truncated)
                               3507                 :                :  */
                               3508                 :                : bool
    8 pg@bowt.ie               3509                 :GNC    26089089 : _bt_checkkeys(IndexScanDesc scan, BTReadPageState *pstate, bool arrayKeys,
                               3510                 :                :               IndexTuple tuple, int tupnatts)
                               3511                 :                : {
                               3512                 :       26089089 :     TupleDesc   tupdesc = RelationGetDescr(scan->indexRelation);
                               3513                 :       26089089 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               3514                 :       26089089 :     ScanDirection dir = pstate->dir;
                               3515                 :       26089089 :     int         ikey = 0;
                               3516                 :                :     bool        res;
                               3517                 :                : 
 1849 pg@bowt.ie               3518   [ +  +  -  + ]:CBC    26089089 :     Assert(BTreeTupleGetNAtts(tuple, scan->indexRelation) == tupnatts);
                               3519                 :                : 
    8 pg@bowt.ie               3520                 :GNC    26089089 :     res = _bt_check_compare(scan, dir, tuple, tupnatts, tupdesc,
                               3521                 :       26089089 :                             arrayKeys, pstate->prechecked, pstate->firstmatch,
                               3522                 :                :                             &pstate->continuescan, &ikey);
                               3523                 :                : 
                               3524                 :                : #ifdef USE_ASSERT_CHECKING
                               3525   [ +  +  +  + ]:       26089089 :     if (!arrayKeys && so->numArrayKeys)
                               3526                 :                :     {
                               3527                 :                :         /*
                               3528                 :                :          * This is a continuescan precheck call for a scan with array keys.
                               3529                 :                :          *
                               3530                 :                :          * Assert that the scan isn't in danger of becoming confused.
                               3531                 :                :          */
                               3532   [ +  -  +  -  :              9 :         Assert(!so->scanBehind && !pstate->prechecked && !pstate->firstmatch);
                                              -  + ]
                               3533         [ -  + ]:              9 :         Assert(!_bt_tuple_before_array_skeys(scan, dir, tuple, tupdesc,
                               3534                 :                :                                              tupnatts, false, 0, NULL));
                               3535                 :                :     }
                               3536   [ +  +  +  + ]:       26089089 :     if (pstate->prechecked || pstate->firstmatch)
                               3537                 :                :     {
                               3538                 :                :         bool        dcontinuescan;
                               3539                 :       20716788 :         int         dikey = 0;
                               3540                 :                : 
                               3541                 :                :         /*
                               3542                 :                :          * Call relied on continuescan/firstmatch prechecks -- assert that we
                               3543                 :                :          * get the same answer without those optimizations
                               3544                 :                :          */
                               3545         [ -  + ]:       20716788 :         Assert(res == _bt_check_compare(scan, dir, tuple, tupnatts, tupdesc,
                               3546                 :                :                                         false, false, false,
                               3547                 :                :                                         &dcontinuescan, &dikey));
                               3548         [ -  + ]:       20716788 :         Assert(pstate->continuescan == dcontinuescan);
                               3549                 :                :     }
                               3550                 :                : #endif
                               3551                 :                : 
                               3552                 :                :     /*
                               3553                 :                :      * Only one _bt_check_compare call is required in the common case where
                               3554                 :                :      * there are no equality strategy array scan keys.  Otherwise we can only
                               3555                 :                :      * accept _bt_check_compare's answer unreservedly when it didn't set
                               3556                 :                :      * pstate.continuescan=false.
                               3557                 :                :      */
                               3558   [ +  +  +  + ]:       26089089 :     if (!arrayKeys || pstate->continuescan)
                               3559                 :       26085928 :         return res;
                               3560                 :                : 
                               3561                 :                :     /*
                               3562                 :                :      * _bt_check_compare call set continuescan=false in the presence of
                               3563                 :                :      * equality type array keys.  This could mean that the tuple is just past
                               3564                 :                :      * the end of matches for the current array keys.
                               3565                 :                :      *
                               3566                 :                :      * It's also possible that the scan is still _before_ the _start_ of
                               3567                 :                :      * tuples matching the current set of array keys.  Check for that first.
                               3568                 :                :      */
                               3569         [ +  + ]:           3161 :     if (_bt_tuple_before_array_skeys(scan, dir, tuple, tupdesc, tupnatts, true,
                               3570                 :                :                                      ikey, NULL))
                               3571                 :                :     {
                               3572                 :                :         /*
                               3573                 :                :          * Tuple is still before the start of matches according to the scan's
                               3574                 :                :          * required array keys (according to _all_ of its required equality
                               3575                 :                :          * strategy keys, actually).
                               3576                 :                :          *
                               3577                 :                :          * _bt_advance_array_keys occasionally sets so->scanBehind to signal
                               3578                 :                :          * that the scan's current position/tuples might be significantly
                               3579                 :                :          * behind (multiple pages behind) its current array keys.  When this
                               3580                 :                :          * happens, we need to be prepared to recover by starting a new
                               3581                 :                :          * primitive index scan here, on our own.
                               3582                 :                :          */
                               3583   [ +  +  -  + ]:           1340 :         Assert(!so->scanBehind ||
                               3584                 :                :                so->keyData[ikey].sk_strategy == BTEqualStrategyNumber);
                               3585   [ +  +  +  -  :           1355 :         if (unlikely(so->scanBehind) && pstate->finaltup &&
                                              -  + ]
                               3586         [ +  - ]:             30 :             _bt_tuple_before_array_skeys(scan, dir, pstate->finaltup, tupdesc,
    8 pg@bowt.ie               3587                 :UNC           0 :                                          BTreeTupleGetNAtts(pstate->finaltup,
                               3588                 :                :                                                             scan->indexRelation),
                               3589                 :                :                                          false, 0, NULL))
                               3590                 :                :         {
                               3591                 :                :             /* Cut our losses -- start a new primitive index scan now */
                               3592                 :              0 :             pstate->continuescan = false;
                               3593                 :              0 :             so->needPrimScan = true;
                               3594                 :                :         }
                               3595                 :                :         else
                               3596                 :                :         {
                               3597                 :                :             /* Override _bt_check_compare, continue primitive scan */
    8 pg@bowt.ie               3598                 :GNC        1340 :             pstate->continuescan = true;
                               3599                 :                : 
                               3600                 :                :             /*
                               3601                 :                :              * We will end up here repeatedly given a group of tuples > the
                               3602                 :                :              * previous array keys and < the now-current keys (for a backwards
                               3603                 :                :              * scan it's just the same, though the operators swap positions).
                               3604                 :                :              *
                               3605                 :                :              * We must avoid allowing this linear search process to scan very
                               3606                 :                :              * many tuples from well before the start of tuples matching the
                               3607                 :                :              * current array keys (or from well before the point where we'll
                               3608                 :                :              * once again have to advance the scan's array keys).
                               3609                 :                :              *
                               3610                 :                :              * We keep the overhead under control by speculatively "looking
                               3611                 :                :              * ahead" to later still-unscanned items from this same leaf page.
                               3612                 :                :              * We'll only attempt this once the number of tuples that the
                               3613                 :                :              * linear search process has examined starts to get out of hand.
                               3614                 :                :              */
                               3615                 :           1340 :             pstate->rechecks++;
                               3616         [ +  + ]:           1340 :             if (pstate->rechecks >= LOOK_AHEAD_REQUIRED_RECHECKS)
                               3617                 :                :             {
                               3618                 :                :                 /* See if we should skip ahead within the current leaf page */
                               3619                 :            529 :                 _bt_checkkeys_look_ahead(scan, pstate, tupnatts, tupdesc);
                               3620                 :                : 
                               3621                 :                :                 /*
                               3622                 :                :                  * Might have set pstate.skip to a later page offset.  When
                               3623                 :                :                  * that happens then _bt_readpage caller will inexpensively
                               3624                 :                :                  * skip ahead to a later tuple from the same page (the one
                               3625                 :                :                  * just after the tuple we successfully "looked ahead" to).
                               3626                 :                :                  */
                               3627                 :                :             }
                               3628                 :                :         }
                               3629                 :                : 
                               3630                 :                :         /* This indextuple doesn't match the current qual, in any case */
                               3631                 :           1340 :         return false;
                               3632                 :                :     }
                               3633                 :                : 
                               3634                 :                :     /*
                               3635                 :                :      * Caller's tuple is >= the current set of array keys and other equality
                               3636                 :                :      * constraint scan keys (or <= if this is a backwards scan).  It's now
                               3637                 :                :      * clear that we _must_ advance any required array keys in lockstep with
                               3638                 :                :      * the scan.
                               3639                 :                :      */
                               3640                 :           1821 :     return _bt_advance_array_keys(scan, pstate, tuple, tupnatts, tupdesc,
                               3641                 :                :                                   ikey, true);
                               3642                 :                : }
                               3643                 :                : 
                               3644                 :                : /*
                               3645                 :                :  * Test whether an indextuple satisfies current scan condition.
                               3646                 :                :  *
                               3647                 :                :  * Return true if so, false if not.  If not, also sets *continuescan to false
                               3648                 :                :  * when it's also not possible for any later tuples to pass the current qual
                               3649                 :                :  * (with the scan's current set of array keys, in the current scan direction),
                               3650                 :                :  * in addition to setting *ikey to the so->keyData[] subscript/offset for the
                               3651                 :                :  * unsatisfied scan key (needed when caller must consider advancing the scan's
                               3652                 :                :  * array keys).
                               3653                 :                :  *
                               3654                 :                :  * This is a subroutine for _bt_checkkeys.  We provisionally assume that
                               3655                 :                :  * reaching the end of the current set of required keys (in particular the
                               3656                 :                :  * current required array keys) ends the ongoing (primitive) index scan.
                               3657                 :                :  * Callers without array keys should just end the scan right away when they
                               3658                 :                :  * find that continuescan has been set to false here by us.  Things are more
                               3659                 :                :  * complicated for callers with array keys.
                               3660                 :                :  *
                               3661                 :                :  * Callers with array keys must first consider advancing the arrays when
                               3662                 :                :  * continuescan has been set to false here by us.  They must then consider if
                               3663                 :                :  * it really does make sense to end the current (primitive) index scan, in
                               3664                 :                :  * light of everything that is known at that point.  (In general when we set
                               3665                 :                :  * continuescan=false for these callers it must be treated as provisional.)
                               3666                 :                :  *
                               3667                 :                :  * We deal with advancing unsatisfied non-required arrays directly, though.
                               3668                 :                :  * This is safe, since by definition non-required keys can't end the scan.
                               3669                 :                :  * This is just how we determine if non-required arrays are just unsatisfied
                               3670                 :                :  * by the current array key, or if they're truly unsatisfied (that is, if
                               3671                 :                :  * they're unsatisfied by every possible array key).
                               3672                 :                :  *
                               3673                 :                :  * Though we advance non-required array keys on our own, that shouldn't have
                               3674                 :                :  * any lasting consequences for the scan.  By definition, non-required arrays
                               3675                 :                :  * have no fixed relationship with the scan's progress.  (There are delicate
                               3676                 :                :  * considerations for non-required arrays when the arrays need to be advanced
                               3677                 :                :  * following our setting continuescan to false, but that doesn't concern us.)
                               3678                 :                :  *
                               3679                 :                :  * Pass advancenonrequired=false to avoid all array related side effects.
                               3680                 :                :  * This allows _bt_advance_array_keys caller to avoid infinite recursion.
                               3681                 :                :  */
                               3682                 :                : static bool
                               3683                 :       46806786 : _bt_check_compare(IndexScanDesc scan, ScanDirection dir,
                               3684                 :                :                   IndexTuple tuple, int tupnatts, TupleDesc tupdesc,
                               3685                 :                :                   bool advancenonrequired, bool prechecked, bool firstmatch,
                               3686                 :                :                   bool *continuescan, int *ikey)
                               3687                 :                : {
                               3688                 :       46806786 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               3689                 :                : 
    8 pg@bowt.ie               3690                 :CBC    46806786 :     *continuescan = true;       /* default assumption */
                               3691                 :                : 
    8 pg@bowt.ie               3692         [ +  + ]:GNC    88619010 :     for (; *ikey < so->numberOfKeys; (*ikey)++)
                               3693                 :                :     {
                               3694                 :       51094027 :         ScanKey     key = so->keyData + *ikey;
                               3695                 :                :         Datum       datum;
                               3696                 :                :         bool        isNull;
  191 akorotkov@postgresql     3697                 :       51094027 :         bool        requiredSameDir = false,
    8 pg@bowt.ie               3698                 :       51094027 :                     requiredOppositeDirOnly = false;
                               3699                 :                : 
                               3700                 :                :         /*
                               3701                 :                :          * Check if the key is required in the current scan direction, in the
                               3702                 :                :          * opposite scan direction _only_, or in neither direction
                               3703                 :                :          */
  191 akorotkov@postgresql     3704   [ +  +  +  + ]:       51094027 :         if (((key->sk_flags & SK_BT_REQFWD) && ScanDirectionIsForward(dir)) ||
                               3705   [ +  +  +  + ]:       12705636 :             ((key->sk_flags & SK_BT_REQBKWD) && ScanDirectionIsBackward(dir)))
                               3706                 :       38405577 :             requiredSameDir = true;
                               3707   [ +  +  -  + ]:       12688450 :         else if (((key->sk_flags & SK_BT_REQFWD) && ScanDirectionIsBackward(dir)) ||
                               3708   [ +  +  +  - ]:        5414417 :                  ((key->sk_flags & SK_BT_REQBKWD) && ScanDirectionIsForward(dir)))
    8 pg@bowt.ie               3709                 :       12231411 :             requiredOppositeDirOnly = true;
                               3710                 :                : 
                               3711                 :                :         /*
                               3712                 :                :          * If the caller told us the *continuescan flag is known to be true
                               3713                 :                :          * for the last item on the page, then we know the keys required for
                               3714                 :                :          * the current direction scan should be matched.  Otherwise, the
                               3715                 :                :          * *continuescan flag would be set for the current item and
                               3716                 :                :          * subsequently the last item on the page accordingly.
                               3717                 :                :          *
                               3718                 :                :          * If the key is required for the opposite direction scan, we can skip
                               3719                 :                :          * the check if the caller tells us there was already at least one
                               3720                 :                :          * matching item on the page. Also, we require the *continuescan flag
                               3721                 :                :          * to be true for the last item on the page to know there are no
                               3722                 :                :          * NULLs.
                               3723                 :                :          *
                               3724                 :                :          * Both cases above work except for the row keys, where NULLs could be
                               3725                 :                :          * found in the middle of matching values.
                               3726                 :                :          */
                               3727   [ +  +  +  + ]:       51094027 :         if (prechecked &&
                               3728   [ +  +  +  + ]:        1082501 :             (requiredSameDir || (requiredOppositeDirOnly && firstmatch)) &&
                               3729         [ +  - ]:        1063109 :             !(key->sk_flags & SK_ROW_HEADER))
  191 akorotkov@postgresql     3730                 :       19095760 :             continue;
                               3731                 :                : 
 1849 pg@bowt.ie               3732         [ +  + ]:CBC    50030918 :         if (key->sk_attno > tupnatts)
                               3733                 :                :         {
                               3734                 :                :             /*
                               3735                 :                :              * This attribute is truncated (must be high key).  The value for
                               3736                 :                :              * this attribute in the first non-pivot tuple on the page to the
                               3737                 :                :              * right could be any possible value.  Assume that truncated
                               3738                 :                :              * attribute passes the qual.
                               3739                 :                :              */
 1509                          3740         [ -  + ]:           1535 :             Assert(BTreeTupleIsPivot(tuple));
 1849                          3741                 :           1535 :             continue;
                               3742                 :                :         }
                               3743                 :                : 
                               3744                 :                :         /* row-comparison keys need special processing */
 6654 tgl@sss.pgh.pa.us        3745         [ +  + ]:       50029383 :         if (key->sk_flags & SK_ROW_HEADER)
                               3746                 :                :         {
 1849 pg@bowt.ie               3747         [ +  + ]:           1962 :             if (_bt_check_rowcompare(key, tuple, tupnatts, tupdesc, dir,
                               3748                 :                :                                      continuescan))
 6654 tgl@sss.pgh.pa.us        3749                 :           1908 :                 continue;
 1849 pg@bowt.ie               3750                 :        9281803 :             return false;
                               3751                 :                :         }
                               3752                 :                : 
 9716 bruce@momjian.us         3753                 :       50027421 :         datum = index_getattr(tuple,
 8664 tgl@sss.pgh.pa.us        3754                 :       50027421 :                               key->sk_attno,
                               3755                 :                :                               tupdesc,
                               3756                 :                :                               &isNull);
                               3757                 :                : 
                               3758         [ +  + ]:       50027421 :         if (key->sk_flags & SK_ISNULL)
                               3759                 :                :         {
                               3760                 :                :             /* Handle IS NULL/NOT NULL tests */
 5217                          3761         [ +  + ]:       18072405 :             if (key->sk_flags & SK_SEARCHNULL)
                               3762                 :                :             {
                               3763         [ +  + ]:          43237 :                 if (isNull)
 5161 bruce@momjian.us         3764                 :            100 :                     continue;   /* tuple satisfies this qual */
                               3765                 :                :             }
                               3766                 :                :             else
                               3767                 :                :             {
 5217 tgl@sss.pgh.pa.us        3768         [ -  + ]:       18029168 :                 Assert(key->sk_flags & SK_SEARCHNOTNULL);
                               3769         [ +  + ]:       18029168 :                 if (!isNull)
 5161 bruce@momjian.us         3770                 :       18029108 :                     continue;   /* tuple satisfies this qual */
                               3771                 :                :             }
                               3772                 :                : 
                               3773                 :                :             /*
                               3774                 :                :              * Tuple fails this qual.  If it's a required qual for the current
                               3775                 :                :              * scan direction, then we can conclude no further tuples will
                               3776                 :                :              * pass, either.
                               3777                 :                :              */
  191 akorotkov@postgresql     3778         [ +  + ]:GNC       43197 :             if (requiredSameDir)
 6218 tgl@sss.pgh.pa.us        3779                 :GBC          27 :                 *continuescan = false;
                               3780                 :                : 
                               3781                 :                :             /*
                               3782                 :                :              * In any case, this indextuple doesn't match the qual.
                               3783                 :                :              */
 1849 pg@bowt.ie               3784                 :CBC       43197 :             return false;
                               3785                 :                :         }
                               3786                 :                : 
 8664 tgl@sss.pgh.pa.us        3787         [ +  + ]:       31955016 :         if (isNull)
                               3788                 :                :         {
 4547                          3789         [ -  + ]:            123 :             if (key->sk_flags & SK_BT_NULLS_FIRST)
                               3790                 :                :             {
                               3791                 :                :                 /*
                               3792                 :                :                  * Since NULLs are sorted before non-NULLs, we know we have
                               3793                 :                :                  * reached the lower limit of the range of values for this
                               3794                 :                :                  * index attr.  On a backward scan, we can stop if this qual
                               3795                 :                :                  * is one of the "must match" subset.  We can stop regardless
                               3796                 :                :                  * of whether the qual is > or <, so long as it's required,
                               3797                 :                :                  * because it's not possible for any future tuples to pass. On
                               3798                 :                :                  * a forward scan, however, we must keep going, because we may
                               3799                 :                :                  * have initially positioned to the start of the index.
                               3800                 :                :                  * (_bt_advance_array_keys also relies on this behavior during
                               3801                 :                :                  * forward scans.)
                               3802                 :                :                  */
 4547 tgl@sss.pgh.pa.us        3803   [ #  #  #  # ]:UBC           0 :                 if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
                               3804                 :                :                     ScanDirectionIsBackward(dir))
                               3805                 :              0 :                     *continuescan = false;
                               3806                 :                :             }
                               3807                 :                :             else
                               3808                 :                :             {
                               3809                 :                :                 /*
                               3810                 :                :                  * Since NULLs are sorted after non-NULLs, we know we have
                               3811                 :                :                  * reached the upper limit of the range of values for this
                               3812                 :                :                  * index attr.  On a forward scan, we can stop if this qual is
                               3813                 :                :                  * one of the "must match" subset.  We can stop regardless of
                               3814                 :                :                  * whether the qual is > or <, so long as it's required,
                               3815                 :                :                  * because it's not possible for any future tuples to pass. On
                               3816                 :                :                  * a backward scan, however, we must keep going, because we
                               3817                 :                :                  * may have initially positioned to the end of the index.
                               3818                 :                :                  * (_bt_advance_array_keys also relies on this behavior during
                               3819                 :                :                  * backward scans.)
                               3820                 :                :                  */
 4547 tgl@sss.pgh.pa.us        3821   [ +  +  +  - ]:CBC         123 :                 if ((key->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
                               3822                 :                :                     ScanDirectionIsForward(dir))
                               3823                 :             78 :                     *continuescan = false;
                               3824                 :                :             }
                               3825                 :                : 
                               3826                 :                :             /*
                               3827                 :                :              * In any case, this indextuple doesn't match the qual.
                               3828                 :                :              */
 1849 pg@bowt.ie               3829                 :            123 :             return false;
                               3830                 :                :         }
                               3831                 :                : 
                               3832                 :                :         /*
                               3833                 :                :          * Apply the key-checking function, though only if we must.
                               3834                 :                :          *
                               3835                 :                :          * When a key is required in the opposite-of-scan direction _only_,
                               3836                 :                :          * then it must already be satisfied if firstmatch=true indicates that
                               3837                 :                :          * an earlier tuple from this same page satisfied it earlier on.
                               3838                 :                :          */
    8 pg@bowt.ie               3839   [ +  +  +  + ]:GNC    31954893 :         if (!(requiredOppositeDirOnly && firstmatch) &&
                               3840         [ +  + ]:       30344834 :             !DatumGetBool(FunctionCall2Coll(&key->sk_func, key->sk_collation,
                               3841                 :                :                                             datum, key->sk_argument)))
                               3842                 :                :         {
                               3843                 :                :             /*
                               3844                 :                :              * Tuple fails this qual.  If it's a required qual for the current
                               3845                 :                :              * scan direction, then we can conclude no further tuples will
                               3846                 :                :              * pass, either.
                               3847                 :                :              *
                               3848                 :                :              * Note: because we stop the scan as soon as any required equality
                               3849                 :                :              * qual fails, it is critical that equality quals be used for the
                               3850                 :                :              * initial positioning in _bt_first() when they are available. See
                               3851                 :                :              * comments in _bt_first().
                               3852                 :                :              */
  191 akorotkov@postgresql     3853         [ +  + ]:        9238429 :             if (requiredSameDir)
 6656 tgl@sss.pgh.pa.us        3854                 :CBC     8908231 :                 *continuescan = false;
                               3855                 :                : 
                               3856                 :                :             /*
                               3857                 :                :              * If this is a non-required equality-type array key, the tuple
                               3858                 :                :              * needs to be checked against every possible array key.  Handle
                               3859                 :                :              * this by "advancing" the scan key's array to a matching value
                               3860                 :                :              * (if we're successful then the tuple might match the qual).
                               3861                 :                :              */
    8 pg@bowt.ie               3862         [ +  + ]:GNC      330198 :             else if (advancenonrequired &&
                               3863         [ +  + ]:            153 :                      key->sk_strategy == BTEqualStrategyNumber &&
                               3864         [ +  - ]:            120 :                      (key->sk_flags & SK_SEARCHARRAY))
                               3865                 :            120 :                 return _bt_advance_array_keys(scan, NULL, tuple, tupnatts,
                               3866                 :                :                                               tupdesc, *ikey, false);
                               3867                 :                : 
                               3868                 :                :             /*
                               3869                 :                :              * This indextuple doesn't match the qual.
                               3870                 :                :              */
 1849 pg@bowt.ie               3871                 :CBC     9238309 :             return false;
                               3872                 :                :         }
                               3873                 :                :     }
                               3874                 :                : 
                               3875                 :                :     /* If we get here, the tuple passes all index quals. */
                               3876                 :       37524983 :     return true;
                               3877                 :                : }
                               3878                 :                : 
                               3879                 :                : /*
                               3880                 :                :  * Test whether an indextuple satisfies a row-comparison scan condition.
                               3881                 :                :  *
                               3882                 :                :  * Return true if so, false if not.  If not, also clear *continuescan if
                               3883                 :                :  * it's not possible for any future tuples in the current scan direction
                               3884                 :                :  * to pass the qual.
                               3885                 :                :  *
                               3886                 :                :  * This is a subroutine for _bt_checkkeys/_bt_check_compare.
                               3887                 :                :  */
                               3888                 :                : static bool
                               3889                 :           1962 : _bt_check_rowcompare(ScanKey skey, IndexTuple tuple, int tupnatts,
                               3890                 :                :                      TupleDesc tupdesc, ScanDirection dir, bool *continuescan)
                               3891                 :                : {
 6654 tgl@sss.pgh.pa.us        3892                 :           1962 :     ScanKey     subkey = (ScanKey) DatumGetPointer(skey->sk_argument);
                               3893                 :           1962 :     int32       cmpresult = 0;
                               3894                 :                :     bool        result;
                               3895                 :                : 
                               3896                 :                :     /* First subkey should be same as the header says */
                               3897         [ +  - ]:           1962 :     Assert(subkey->sk_attno == skey->sk_attno);
                               3898                 :                : 
                               3899                 :                :     /* Loop over columns of the row condition */
                               3900                 :                :     for (;;)
                               3901                 :            144 :     {
                               3902                 :                :         Datum       datum;
                               3903                 :                :         bool        isNull;
                               3904                 :                : 
                               3905         [ -  + ]:           2106 :         Assert(subkey->sk_flags & SK_ROW_MEMBER);
                               3906                 :                : 
 1849 pg@bowt.ie               3907         [ +  + ]:           2106 :         if (subkey->sk_attno > tupnatts)
                               3908                 :                :         {
                               3909                 :                :             /*
                               3910                 :                :              * This attribute is truncated (must be high key).  The value for
                               3911                 :                :              * this attribute in the first non-pivot tuple on the page to the
                               3912                 :                :              * right could be any possible value.  Assume that truncated
                               3913                 :                :              * attribute passes the qual.
                               3914                 :                :              */
 1509                          3915         [ -  + ]:              6 :             Assert(BTreeTupleIsPivot(tuple));
 1849                          3916                 :              6 :             cmpresult = 0;
 1841                          3917         [ +  - ]:              6 :             if (subkey->sk_flags & SK_ROW_END)
                               3918                 :              6 :                 break;
 1841 pg@bowt.ie               3919                 :UBC           0 :             subkey++;
 1849                          3920                 :              0 :             continue;
                               3921                 :                :         }
                               3922                 :                : 
 6654 tgl@sss.pgh.pa.us        3923                 :CBC        2100 :         datum = index_getattr(tuple,
                               3924                 :           2100 :                               subkey->sk_attno,
                               3925                 :                :                               tupdesc,
                               3926                 :                :                               &isNull);
                               3927                 :                : 
                               3928         [ +  + ]:           2100 :         if (isNull)
                               3929                 :                :         {
 4547                          3930         [ -  + ]:             48 :             if (subkey->sk_flags & SK_BT_NULLS_FIRST)
                               3931                 :                :             {
                               3932                 :                :                 /*
                               3933                 :                :                  * Since NULLs are sorted before non-NULLs, we know we have
                               3934                 :                :                  * reached the lower limit of the range of values for this
                               3935                 :                :                  * index attr.  On a backward scan, we can stop if this qual
                               3936                 :                :                  * is one of the "must match" subset.  We can stop regardless
                               3937                 :                :                  * of whether the qual is > or <, so long as it's required,
                               3938                 :                :                  * because it's not possible for any future tuples to pass. On
                               3939                 :                :                  * a forward scan, however, we must keep going, because we may
                               3940                 :                :                  * have initially positioned to the start of the index.
                               3941                 :                :                  * (_bt_advance_array_keys also relies on this behavior during
                               3942                 :                :                  * forward scans.)
                               3943                 :                :                  */
 4547 tgl@sss.pgh.pa.us        3944   [ #  #  #  # ]:UBC           0 :                 if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
                               3945                 :                :                     ScanDirectionIsBackward(dir))
                               3946                 :              0 :                     *continuescan = false;
                               3947                 :                :             }
                               3948                 :                :             else
                               3949                 :                :             {
                               3950                 :                :                 /*
                               3951                 :                :                  * Since NULLs are sorted after non-NULLs, we know we have
                               3952                 :                :                  * reached the upper limit of the range of values for this
                               3953                 :                :                  * index attr.  On a forward scan, we can stop if this qual is
                               3954                 :                :                  * one of the "must match" subset.  We can stop regardless of
                               3955                 :                :                  * whether the qual is > or <, so long as it's required,
                               3956                 :                :                  * because it's not possible for any future tuples to pass. On
                               3957                 :                :                  * a backward scan, however, we must keep going, because we
                               3958                 :                :                  * may have initially positioned to the end of the index.
                               3959                 :                :                  * (_bt_advance_array_keys also relies on this behavior during
                               3960                 :                :                  * backward scans.)
                               3961                 :                :                  */
 4547 tgl@sss.pgh.pa.us        3962   [ -  +  -  - ]:CBC          48 :                 if ((subkey->sk_flags & (SK_BT_REQFWD | SK_BT_REQBKWD)) &&
                               3963                 :                :                     ScanDirectionIsForward(dir))
 4547 tgl@sss.pgh.pa.us        3964                 :UBC           0 :                     *continuescan = false;
                               3965                 :                :             }
                               3966                 :                : 
                               3967                 :                :             /*
                               3968                 :                :              * In any case, this indextuple doesn't match the qual.
                               3969                 :                :              */
 6654 tgl@sss.pgh.pa.us        3970                 :CBC          48 :             return false;
                               3971                 :                :         }
                               3972                 :                : 
                               3973         [ -  + ]:           2052 :         if (subkey->sk_flags & SK_ISNULL)
                               3974                 :                :         {
                               3975                 :                :             /*
                               3976                 :                :              * Unlike the simple-scankey case, this isn't a disallowed case.
                               3977                 :                :              * But it can never match.  If all the earlier row comparison
                               3978                 :                :              * columns are required for the scan direction, we can stop the
                               3979                 :                :              * scan, because there can't be another tuple that will succeed.
                               3980                 :                :              */
 6654 tgl@sss.pgh.pa.us        3981         [ #  # ]:UBC           0 :             if (subkey != (ScanKey) DatumGetPointer(skey->sk_argument))
                               3982                 :              0 :                 subkey--;
                               3983   [ #  #  #  # ]:              0 :             if ((subkey->sk_flags & SK_BT_REQFWD) &&
                               3984                 :                :                 ScanDirectionIsForward(dir))
                               3985                 :              0 :                 *continuescan = false;
                               3986   [ #  #  #  # ]:              0 :             else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
                               3987                 :                :                      ScanDirectionIsBackward(dir))
                               3988                 :              0 :                 *continuescan = false;
                               3989                 :              0 :             return false;
                               3990                 :                :         }
                               3991                 :                : 
                               3992                 :                :         /* Perform the test --- three-way comparison not bool operator */
 4751 tgl@sss.pgh.pa.us        3993                 :CBC        2052 :         cmpresult = DatumGetInt32(FunctionCall2Coll(&subkey->sk_func,
                               3994                 :                :                                                     subkey->sk_collation,
                               3995                 :                :                                                     datum,
                               3996                 :                :                                                     subkey->sk_argument));
                               3997                 :                : 
 6305                          3998         [ -  + ]:           2052 :         if (subkey->sk_flags & SK_BT_DESC)
 2018 tgl@sss.pgh.pa.us        3999         [ #  # ]:UBC           0 :             INVERT_COMPARE_RESULT(cmpresult);
                               4000                 :                : 
                               4001                 :                :         /* Done comparing if unequal, else advance to next column */
 6654 tgl@sss.pgh.pa.us        4002         [ +  + ]:CBC        2052 :         if (cmpresult != 0)
                               4003                 :           1908 :             break;
                               4004                 :                : 
                               4005         [ -  + ]:            144 :         if (subkey->sk_flags & SK_ROW_END)
 6654 tgl@sss.pgh.pa.us        4006                 :UBC           0 :             break;
 6654 tgl@sss.pgh.pa.us        4007                 :CBC         144 :         subkey++;
                               4008                 :                :     }
                               4009                 :                : 
                               4010                 :                :     /*
                               4011                 :                :      * At this point cmpresult indicates the overall result of the row
                               4012                 :                :      * comparison, and subkey points to the deciding column (or the last
                               4013                 :                :      * column if the result is "=").
                               4014                 :                :      */
                               4015   [ -  +  +  +  :           1914 :     switch (subkey->sk_strategy)
                                                 - ]
                               4016                 :                :     {
                               4017                 :                :             /* EQ and NE cases aren't allowed here */
 6654 tgl@sss.pgh.pa.us        4018                 :UBC           0 :         case BTLessStrategyNumber:
                               4019                 :              0 :             result = (cmpresult < 0);
                               4020                 :              0 :             break;
 6654 tgl@sss.pgh.pa.us        4021                 :CBC        1584 :         case BTLessEqualStrategyNumber:
                               4022                 :           1584 :             result = (cmpresult <= 0);
                               4023                 :           1584 :             break;
                               4024                 :            234 :         case BTGreaterEqualStrategyNumber:
                               4025                 :            234 :             result = (cmpresult >= 0);
                               4026                 :            234 :             break;
                               4027                 :             96 :         case BTGreaterStrategyNumber:
                               4028                 :             96 :             result = (cmpresult > 0);
                               4029                 :             96 :             break;
 6654 tgl@sss.pgh.pa.us        4030                 :UBC           0 :         default:
                               4031         [ #  # ]:              0 :             elog(ERROR, "unrecognized RowCompareType: %d",
                               4032                 :                :                  (int) subkey->sk_strategy);
                               4033                 :                :             result = 0;         /* keep compiler quiet */
                               4034                 :                :             break;
                               4035                 :                :     }
                               4036                 :                : 
 6654 tgl@sss.pgh.pa.us        4037         [ +  + ]:CBC        1914 :     if (!result)
                               4038                 :                :     {
                               4039                 :                :         /*
                               4040                 :                :          * Tuple fails this qual.  If it's a required qual for the current
                               4041                 :                :          * scan direction, then we can conclude no further tuples will pass,
                               4042                 :                :          * either.  Note we have to look at the deciding column, not
                               4043                 :                :          * necessarily the first or last column of the row condition.
                               4044                 :                :          */
                               4045   [ +  -  +  - ]:              6 :         if ((subkey->sk_flags & SK_BT_REQFWD) &&
                               4046                 :                :             ScanDirectionIsForward(dir))
                               4047                 :              6 :             *continuescan = false;
 6654 tgl@sss.pgh.pa.us        4048   [ #  #  #  # ]:UBC           0 :         else if ((subkey->sk_flags & SK_BT_REQBKWD) &&
                               4049                 :                :                  ScanDirectionIsBackward(dir))
                               4050                 :              0 :             *continuescan = false;
                               4051                 :                :     }
                               4052                 :                : 
 6654 tgl@sss.pgh.pa.us        4053                 :CBC        1914 :     return result;
                               4054                 :                : }
                               4055                 :                : 
                               4056                 :                : /*
                               4057                 :                :  * Determine if a scan with array keys should skip over uninteresting tuples.
                               4058                 :                :  *
                               4059                 :                :  * This is a subroutine for _bt_checkkeys.  Called when _bt_readpage's linear
                               4060                 :                :  * search process (started after it finishes reading an initial group of
                               4061                 :                :  * matching tuples, used to locate the start of the next group of tuples
                               4062                 :                :  * matching the next set of required array keys) has already scanned an
                               4063                 :                :  * excessive number of tuples whose key space is "between arrays".
                               4064                 :                :  *
                               4065                 :                :  * When we perform look ahead successfully, we'll sets pstate.skip, which
                               4066                 :                :  * instructs _bt_readpage to skip ahead to that tuple next (could be past the
                               4067                 :                :  * end of the scan's leaf page).  Pages where the optimization is effective
                               4068                 :                :  * will generally still need to skip several times.  Each call here performs
                               4069                 :                :  * only a single "look ahead" comparison of a later tuple, whose distance from
                               4070                 :                :  * the current tuple's offset number is determined by applying heuristics.
                               4071                 :                :  */
                               4072                 :                : static void
    8 pg@bowt.ie               4073                 :GNC         529 : _bt_checkkeys_look_ahead(IndexScanDesc scan, BTReadPageState *pstate,
                               4074                 :                :                          int tupnatts, TupleDesc tupdesc)
                               4075                 :                : {
                               4076                 :            529 :     ScanDirection dir = pstate->dir;
                               4077                 :                :     OffsetNumber aheadoffnum;
                               4078                 :                :     IndexTuple  ahead;
                               4079                 :                : 
                               4080                 :                :     /* Avoid looking ahead when comparing the page high key */
                               4081         [ -  + ]:            529 :     if (pstate->offnum < pstate->minoff)
    8 pg@bowt.ie               4082                 :UNC           0 :         return;
                               4083                 :                : 
                               4084                 :                :     /*
                               4085                 :                :      * Don't look ahead when there aren't enough tuples remaining on the page
                               4086                 :                :      * (in the current scan direction) for it to be worth our while
                               4087                 :                :      */
    8 pg@bowt.ie               4088         [ +  + ]:GNC         529 :     if (ScanDirectionIsForward(dir) &&
                               4089         [ +  + ]:            526 :         pstate->offnum >= pstate->maxoff - LOOK_AHEAD_DEFAULT_DISTANCE)
                               4090                 :              1 :         return;
                               4091         [ +  + ]:            528 :     else if (ScanDirectionIsBackward(dir) &&
                               4092         [ -  + ]:              3 :              pstate->offnum <= pstate->minoff + LOOK_AHEAD_DEFAULT_DISTANCE)
    8 pg@bowt.ie               4093                 :UNC           0 :         return;
                               4094                 :                : 
                               4095                 :                :     /*
                               4096                 :                :      * The look ahead distance starts small, and ramps up as each call here
                               4097                 :                :      * allows _bt_readpage to skip over more tuples
                               4098                 :                :      */
    8 pg@bowt.ie               4099         [ +  + ]:GNC         528 :     if (!pstate->targetdistance)
                               4100                 :            201 :         pstate->targetdistance = LOOK_AHEAD_DEFAULT_DISTANCE;
                               4101                 :                :     else
                               4102                 :            327 :         pstate->targetdistance *= 2;
                               4103                 :                : 
                               4104                 :                :     /* Don't read past the end (or before the start) of the page, though */
                               4105         [ +  + ]:            528 :     if (ScanDirectionIsForward(dir))
                               4106                 :            525 :         aheadoffnum = Min((int) pstate->maxoff,
                               4107                 :                :                           (int) pstate->offnum + pstate->targetdistance);
                               4108                 :                :     else
                               4109                 :              3 :         aheadoffnum = Max((int) pstate->minoff,
                               4110                 :                :                           (int) pstate->offnum - pstate->targetdistance);
                               4111                 :                : 
                               4112                 :            528 :     ahead = (IndexTuple) PageGetItem(pstate->page,
                               4113                 :                :                                      PageGetItemId(pstate->page, aheadoffnum));
                               4114         [ +  + ]:            528 :     if (_bt_tuple_before_array_skeys(scan, dir, ahead, tupdesc, tupnatts,
                               4115                 :                :                                      false, 0, NULL))
                               4116                 :                :     {
                               4117                 :                :         /*
                               4118                 :                :          * Success -- instruct _bt_readpage to skip ahead to very next tuple
                               4119                 :                :          * after the one we determined was still before the current array keys
                               4120                 :                :          */
                               4121         [ +  + ]:            275 :         if (ScanDirectionIsForward(dir))
                               4122                 :            272 :             pstate->skip = aheadoffnum + 1;
                               4123                 :                :         else
                               4124                 :              3 :             pstate->skip = aheadoffnum - 1;
                               4125                 :                :     }
                               4126                 :                :     else
                               4127                 :                :     {
                               4128                 :                :         /*
                               4129                 :                :          * Failure -- "ahead" tuple is too far ahead (we were too aggresive).
                               4130                 :                :          *
                               4131                 :                :          * Reset the number of rechecks, and aggressively reduce the target
                               4132                 :                :          * distance (we're much more aggressive here than we were when the
                               4133                 :                :          * distance was initially ramped up).
                               4134                 :                :          */
                               4135                 :            253 :         pstate->rechecks = 0;
                               4136         [ +  + ]:            253 :         pstate->targetdistance = Max(pstate->targetdistance / 8, 1);
                               4137                 :                :     }
                               4138                 :                : }
                               4139                 :                : 
                               4140                 :                : /*
                               4141                 :                :  * _bt_killitems - set LP_DEAD state for items an indexscan caller has
                               4142                 :                :  * told us were killed
                               4143                 :                :  *
                               4144                 :                :  * scan->opaque, referenced locally through so, contains information about the
                               4145                 :                :  * current page and killed tuples thereon (generally, this should only be
                               4146                 :                :  * called if so->numKilled > 0).
                               4147                 :                :  *
                               4148                 :                :  * The caller does not have a lock on the page and may or may not have the
                               4149                 :                :  * page pinned in a buffer.  Note that read-lock is sufficient for setting
                               4150                 :                :  * LP_DEAD status (which is only a hint).
                               4151                 :                :  *
                               4152                 :                :  * We match items by heap TID before assuming they are the right ones to
                               4153                 :                :  * delete.  We cope with cases where items have moved right due to insertions.
                               4154                 :                :  * If an item has moved off the current page due to a split, we'll fail to
                               4155                 :                :  * find it and do nothing (this is not an error case --- we assume the item
                               4156                 :                :  * will eventually get marked in a future indexscan).
                               4157                 :                :  *
                               4158                 :                :  * Note that if we hold a pin on the target page continuously from initially
                               4159                 :                :  * reading the items until applying this function, VACUUM cannot have deleted
                               4160                 :                :  * any items from the page, and so there is no need to search left from the
                               4161                 :                :  * recorded offset.  (This observation also guarantees that the item is still
                               4162                 :                :  * the right one to delete, which might otherwise be questionable since heap
                               4163                 :                :  * TIDs can get recycled.)  This holds true even if the page has been modified
                               4164                 :                :  * by inserts and page splits, so there is no need to consult the LSN.
                               4165                 :                :  *
                               4166                 :                :  * If the pin was released after reading the page, then we re-read it.  If it
                               4167                 :                :  * has been modified since we read it (as determined by the LSN), we dare not
                               4168                 :                :  * flag any entries because it is possible that the old entry was vacuumed
                               4169                 :                :  * away and the TID was re-used by a completely different heap tuple.
                               4170                 :                :  */
                               4171                 :                : void
 3308 kgrittn@postgresql.o     4172                 :CBC       70253 : _bt_killitems(IndexScanDesc scan)
                               4173                 :                : {
 6552 tgl@sss.pgh.pa.us        4174                 :          70253 :     BTScanOpaque so = (BTScanOpaque) scan->opaque;
                               4175                 :                :     Page        page;
                               4176                 :                :     BTPageOpaque opaque;
                               4177                 :                :     OffsetNumber minoff;
                               4178                 :                :     OffsetNumber maxoff;
                               4179                 :                :     int         i;
 3308 kgrittn@postgresql.o     4180                 :          70253 :     int         numKilled = so->numKilled;
 6552 tgl@sss.pgh.pa.us        4181                 :          70253 :     bool        killedsomething = false;
                               4182                 :                :     bool        droppedpin PG_USED_FOR_ASSERTS_ONLY;
                               4183                 :                : 
 3308 kgrittn@postgresql.o     4184   [ -  +  -  -  :          70253 :     Assert(BTScanPosIsValid(so->currPos));
                                              -  + ]
                               4185                 :                : 
                               4186                 :                :     /*
                               4187                 :                :      * Always reset the scan state, so we don't look for same items on other
                               4188                 :                :      * pages.
                               4189                 :                :      */
                               4190                 :          70253 :     so->numKilled = 0;
                               4191                 :                : 
                               4192   [ -  +  -  -  :          70253 :     if (BTScanPosIsPinned(so->currPos))
                                              +  + ]
                               4193                 :                :     {
                               4194                 :                :         /*
                               4195                 :                :          * We have held the pin on this page since we read the index tuples,
                               4196                 :                :          * so all we need to do is lock it.  The pin will have prevented
                               4197                 :                :          * re-use of any TID on the page, so there is no need to check the
                               4198                 :                :          * LSN.
                               4199                 :                :          */
 1469 pg@bowt.ie               4200                 :          17307 :         droppedpin = false;
 1363                          4201                 :          17307 :         _bt_lockbuf(scan->indexRelation, so->currPos.buf, BT_READ);
                               4202                 :                : 
 2916 kgrittn@postgresql.o     4203                 :          17307 :         page = BufferGetPage(so->currPos.buf);
                               4204                 :                :     }
                               4205                 :                :     else
                               4206                 :                :     {
                               4207                 :                :         Buffer      buf;
                               4208                 :                : 
 1469 pg@bowt.ie               4209                 :          52946 :         droppedpin = true;
                               4210                 :                :         /* Attempt to re-read the buffer, getting pin and lock. */
  309                          4211                 :          52946 :         buf = _bt_getbuf(scan->indexRelation, so->currPos.currPage, BT_READ);
                               4212                 :                : 
 2916 kgrittn@postgresql.o     4213                 :          52946 :         page = BufferGetPage(buf);
 2287 alvherre@alvh.no-ip.     4214         [ +  + ]:          52946 :         if (BufferGetLSNAtomic(buf) == so->currPos.lsn)
 3308 kgrittn@postgresql.o     4215                 :          52861 :             so->currPos.buf = buf;
                               4216                 :                :         else
                               4217                 :                :         {
                               4218                 :                :             /* Modified while not pinned means hinting is not safe. */
                               4219                 :             85 :             _bt_relbuf(scan->indexRelation, buf);
                               4220                 :             85 :             return;
                               4221                 :                :         }
                               4222                 :                :     }
                               4223                 :                : 
  744 michael@paquier.xyz      4224                 :          70168 :     opaque = BTPageGetOpaque(page);
 6552 tgl@sss.pgh.pa.us        4225         [ +  + ]:          70168 :     minoff = P_FIRSTDATAKEY(opaque);
                               4226                 :          70168 :     maxoff = PageGetMaxOffsetNumber(page);
                               4227                 :                : 
 3308 kgrittn@postgresql.o     4228         [ +  + ]:         261200 :     for (i = 0; i < numKilled; i++)
                               4229                 :                :     {
 6402 bruce@momjian.us         4230                 :         191032 :         int         itemIndex = so->killedItems[i];
                               4231                 :         191032 :         BTScanPosItem *kitem = &so->currPos.items[itemIndex];
                               4232                 :         191032 :         OffsetNumber offnum = kitem->indexOffset;
                               4233                 :                : 
 6552 tgl@sss.pgh.pa.us        4234   [ +  -  -  + ]:         191032 :         Assert(itemIndex >= so->currPos.firstItem &&
                               4235                 :                :                itemIndex <= so->currPos.lastItem);
                               4236         [ -  + ]:         191032 :         if (offnum < minoff)
 6552 tgl@sss.pgh.pa.us        4237                 :UBC           0 :             continue;           /* pure paranoia */
 6552 tgl@sss.pgh.pa.us        4238         [ +  + ]:CBC     2436936 :         while (offnum <= maxoff)
                               4239                 :                :         {
                               4240                 :        2417128 :             ItemId      iid = PageGetItemId(page, offnum);
                               4241                 :        2417128 :             IndexTuple  ituple = (IndexTuple) PageGetItem(page, iid);
 1509 pg@bowt.ie               4242                 :        2417128 :             bool        killtuple = false;
                               4243                 :                : 
                               4244         [ +  + ]:        2417128 :             if (BTreeTupleIsPosting(ituple))
                               4245                 :                :             {
                               4246                 :         594306 :                 int         pi = i + 1;
                               4247                 :         594306 :                 int         nposting = BTreeTupleGetNPosting(ituple);
                               4248                 :                :                 int         j;
                               4249                 :                : 
                               4250                 :                :                 /*
                               4251                 :                :                  * We rely on the convention that heap TIDs in the scanpos
                               4252                 :                :                  * items array are stored in ascending heap TID order for a
                               4253                 :                :                  * group of TIDs that originally came from a posting list
                               4254                 :                :                  * tuple.  This convention even applies during backwards
                               4255                 :                :                  * scans, where returning the TIDs in descending order might
                               4256                 :                :                  * seem more natural.  This is about effectiveness, not
                               4257                 :                :                  * correctness.
                               4258                 :                :                  *
                               4259                 :                :                  * Note that the page may have been modified in almost any way
                               4260                 :                :                  * since we first read it (in the !droppedpin case), so it's
                               4261                 :                :                  * possible that this posting list tuple wasn't a posting list
                               4262                 :                :                  * tuple when we first encountered its heap TIDs.
                               4263                 :                :                  */
                               4264         [ +  + ]:         612641 :                 for (j = 0; j < nposting; j++)
                               4265                 :                :                 {
                               4266                 :         611989 :                     ItemPointer item = BTreeTupleGetPostingN(ituple, j);
                               4267                 :                : 
                               4268         [ +  + ]:         611989 :                     if (!ItemPointerEquals(item, &kitem->heapTid))
                               4269                 :         593654 :                         break;  /* out of posting list loop */
                               4270                 :                : 
                               4271                 :                :                     /*
                               4272                 :                :                      * kitem must have matching offnum when heap TIDs match,
                               4273                 :                :                      * though only in the common case where the page can't
                               4274                 :                :                      * have been concurrently modified
                               4275                 :                :                      */
 1469                          4276   [ -  +  -  - ]:          18335 :                     Assert(kitem->indexOffset == offnum || !droppedpin);
                               4277                 :                : 
                               4278                 :                :                     /*
                               4279                 :                :                      * Read-ahead to later kitems here.
                               4280                 :                :                      *
                               4281                 :                :                      * We rely on the assumption that not advancing kitem here
                               4282                 :                :                      * will prevent us from considering the posting list tuple
                               4283                 :                :                      * fully dead by not matching its next heap TID in next
                               4284                 :                :                      * loop iteration.
                               4285                 :                :                      *
                               4286                 :                :                      * If, on the other hand, this is the final heap TID in
                               4287                 :                :                      * the posting list tuple, then tuple gets killed
                               4288                 :                :                      * regardless (i.e. we handle the case where the last
                               4289                 :                :                      * kitem is also the last heap TID in the last index tuple
                               4290                 :                :                      * correctly -- posting tuple still gets killed).
                               4291                 :                :                      */
 1509                          4292         [ +  + ]:          18335 :                     if (pi < numKilled)
                               4293                 :           8393 :                         kitem = &so->currPos.items[so->killedItems[pi++]];
                               4294                 :                :                 }
                               4295                 :                : 
                               4296                 :                :                 /*
                               4297                 :                :                  * Don't bother advancing the outermost loop's int iterator to
                               4298                 :                :                  * avoid processing killed items that relate to the same
                               4299                 :                :                  * offnum/posting list tuple.  This micro-optimization hardly
                               4300                 :                :                  * seems worth it.  (Further iterations of the outermost loop
                               4301                 :                :                  * will fail to match on this same posting list's first heap
                               4302                 :                :                  * TID instead, so we'll advance to the next offnum/index
                               4303                 :                :                  * tuple pretty quickly.)
                               4304                 :                :                  */
                               4305         [ +  + ]:         594306 :                 if (j == nposting)
                               4306                 :            652 :                     killtuple = true;
                               4307                 :                :             }
                               4308         [ +  + ]:        1822822 :             else if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
                               4309                 :         170828 :                 killtuple = true;
                               4310                 :                : 
                               4311                 :                :             /*
                               4312                 :                :              * Mark index item as dead, if it isn't already.  Since this
                               4313                 :                :              * happens while holding a buffer lock possibly in shared mode,
                               4314                 :                :              * it's possible that multiple processes attempt to do this
                               4315                 :                :              * simultaneously, leading to multiple full-page images being sent
                               4316                 :                :              * to WAL (if wal_log_hints or data checksums are enabled), which
                               4317                 :                :              * is undesirable.
                               4318                 :                :              */
 1430 alvherre@alvh.no-ip.     4319   [ +  +  +  + ]:        2417128 :             if (killtuple && !ItemIdIsDead(iid))
                               4320                 :                :             {
                               4321                 :                :                 /* found the item/all posting list items */
 6059 tgl@sss.pgh.pa.us        4322                 :         171224 :                 ItemIdMarkDead(iid);
 6552                          4323                 :         171224 :                 killedsomething = true;
                               4324                 :         171224 :                 break;          /* out of inner search loop */
                               4325                 :                :             }
                               4326                 :        2245904 :             offnum = OffsetNumberNext(offnum);
                               4327                 :                :         }
                               4328                 :                :     }
                               4329                 :                : 
                               4330                 :                :     /*
                               4331                 :                :      * Since this can be redone later if needed, mark as dirty hint.
                               4332                 :                :      *
                               4333                 :                :      * Whenever we mark anything LP_DEAD, we also set the page's
                               4334                 :                :      * BTP_HAS_GARBAGE flag, which is likewise just a hint.  (Note that we
                               4335                 :                :      * only rely on the page-level flag in !heapkeyspace indexes.)
                               4336                 :                :      */
                               4337         [ +  + ]:          70168 :     if (killedsomething)
                               4338                 :                :     {
 6473                          4339                 :          59720 :         opaque->btpo_flags |= BTP_HAS_GARBAGE;
 3954 jdavis@postgresql.or     4340                 :          59720 :         MarkBufferDirtyHint(so->currPos.buf, true);
                               4341                 :                :     }
                               4342                 :                : 
 1363 pg@bowt.ie               4343                 :          70168 :     _bt_unlockbuf(scan->indexRelation, so->currPos.buf);
                               4344                 :                : }
                               4345                 :                : 
                               4346                 :                : 
                               4347                 :                : /*
                               4348                 :                :  * The following routines manage a shared-memory area in which we track
                               4349                 :                :  * assignment of "vacuum cycle IDs" to currently-active btree vacuuming
                               4350                 :                :  * operations.  There is a single counter which increments each time we
                               4351                 :                :  * start a vacuum to assign it a cycle ID.  Since multiple vacuums could
                               4352                 :                :  * be active concurrently, we have to track the cycle ID for each active
                               4353                 :                :  * vacuum; this requires at most MaxBackends entries (usually far fewer).
                               4354                 :                :  * We assume at most one vacuum can be active for a given index.
                               4355                 :                :  *
                               4356                 :                :  * Access to the shared memory area is controlled by BtreeVacuumLock.
                               4357                 :                :  * In principle we could use a separate lmgr locktag for each index,
                               4358                 :                :  * but a single LWLock is much cheaper, and given the short time that
                               4359                 :                :  * the lock is ever held, the concurrency hit should be minimal.
                               4360                 :                :  */
                               4361                 :                : 
                               4362                 :                : typedef struct BTOneVacInfo
                               4363                 :                : {
                               4364                 :                :     LockRelId   relid;          /* global identifier of an index */
                               4365                 :                :     BTCycleId   cycleid;        /* cycle ID for its active VACUUM */
                               4366                 :                : } BTOneVacInfo;
                               4367                 :                : 
                               4368                 :                : typedef struct BTVacInfo
                               4369                 :                : {
                               4370                 :                :     BTCycleId   cycle_ctr;      /* cycle ID most recently assigned */
                               4371                 :                :     int         num_vacuums;    /* number of currently active VACUUMs */
                               4372                 :                :     int         max_vacuums;    /* allocated length of vacuums[] array */
                               4373                 :                :     BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER];
                               4374                 :                : } BTVacInfo;
                               4375                 :                : 
                               4376                 :                : static BTVacInfo *btvacinfo;
                               4377                 :                : 
                               4378                 :                : 
                               4379                 :                : /*
                               4380                 :                :  * _bt_vacuum_cycleid --- get the active vacuum cycle ID for an index,
                               4381                 :                :  *      or zero if there is no active VACUUM
                               4382                 :                :  *
                               4383                 :                :  * Note: for correct interlocking, the caller must already hold pin and
                               4384                 :                :  * exclusive lock on each buffer it will store the cycle ID into.  This
                               4385                 :                :  * ensures that even if a VACUUM starts immediately afterwards, it cannot
                               4386                 :                :  * process those pages until the page split is complete.
                               4387                 :                :  */
                               4388                 :                : BTCycleId
 6551 tgl@sss.pgh.pa.us        4389                 :          10590 : _bt_vacuum_cycleid(Relation rel)
                               4390                 :                : {
                               4391                 :          10590 :     BTCycleId   result = 0;
                               4392                 :                :     int         i;
                               4393                 :                : 
                               4394                 :                :     /* Share lock is enough since this is a read-only operation */
                               4395                 :          10590 :     LWLockAcquire(BtreeVacuumLock, LW_SHARED);
                               4396                 :                : 
                               4397         [ +  + ]:          10601 :     for (i = 0; i < btvacinfo->num_vacuums; i++)
                               4398                 :                :     {
                               4399                 :             12 :         BTOneVacInfo *vac = &btvacinfo->vacuums[i];
                               4400                 :                : 
                               4401         [ +  + ]:             12 :         if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
                               4402         [ +  - ]:              1 :             vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
                               4403                 :                :         {
                               4404                 :              1 :             result = vac->cycleid;
                               4405                 :              1 :             break;
                               4406                 :                :         }
                               4407                 :                :     }
                               4408                 :                : 
                               4409                 :          10590 :     LWLockRelease(BtreeVacuumLock);
                               4410                 :          10590 :     return result;
                               4411                 :                : }
                               4412                 :                : 
                               4413                 :                : /*
                               4414                 :                :  * _bt_start_vacuum --- assign a cycle ID to a just-starting VACUUM operation
                               4415                 :                :  *
                               4416                 :                :  * Note: the caller must guarantee that it will eventually call
                               4417                 :                :  * _bt_end_vacuum, else we'll permanently leak an array slot.  To ensure
                               4418                 :                :  * that this happens even in elog(FATAL) scenarios, the appropriate coding
                               4419                 :                :  * is not just a PG_TRY, but
                               4420                 :                :  *      PG_ENSURE_ERROR_CLEANUP(_bt_end_vacuum_callback, PointerGetDatum(rel))
                               4421                 :                :  */
                               4422                 :                : BTCycleId
                               4423                 :           1256 : _bt_start_vacuum(Relation rel)
                               4424                 :                : {
                               4425                 :                :     BTCycleId   result;
                               4426                 :                :     int         i;
                               4427                 :                :     BTOneVacInfo *vac;
                               4428                 :                : 
                               4429                 :           1256 :     LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
                               4430                 :                : 
                               4431                 :                :     /*
                               4432                 :                :      * Assign the next cycle ID, being careful to avoid zero as well as the
                               4433                 :                :      * reserved high values.
                               4434                 :                :      */
 6215                          4435                 :           1256 :     result = ++(btvacinfo->cycle_ctr);
                               4436   [ +  -  -  + ]:           1256 :     if (result == 0 || result > MAX_BT_CYCLE_ID)
 6215 tgl@sss.pgh.pa.us        4437                 :UBC           0 :         result = btvacinfo->cycle_ctr = 1;
                               4438                 :                : 
                               4439                 :                :     /* Let's just make sure there's no entry already for this index */
 6551 tgl@sss.pgh.pa.us        4440         [ -  + ]:CBC        1256 :     for (i = 0; i < btvacinfo->num_vacuums; i++)
                               4441                 :                :     {
 6551 tgl@sss.pgh.pa.us        4442                 :LBC         (6) :         vac = &btvacinfo->vacuums[i];
                               4443         [ #  # ]:            (6) :         if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
 6551 tgl@sss.pgh.pa.us        4444         [ #  # ]:UBC           0 :             vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
                               4445                 :                :         {
                               4446                 :                :             /*
                               4447                 :                :              * Unlike most places in the backend, we have to explicitly
                               4448                 :                :              * release our LWLock before throwing an error.  This is because
                               4449                 :                :              * we expect _bt_end_vacuum() to be called before transaction
                               4450                 :                :              * abort cleanup can run to release LWLocks.
                               4451                 :                :              */
 6225                          4452                 :              0 :             LWLockRelease(BtreeVacuumLock);
 6551                          4453         [ #  # ]:              0 :             elog(ERROR, "multiple active vacuums for index \"%s\"",
                               4454                 :                :                  RelationGetRelationName(rel));
                               4455                 :                :         }
                               4456                 :                :     }
                               4457                 :                : 
                               4458                 :                :     /* OK, add an entry */
 6551 tgl@sss.pgh.pa.us        4459         [ -  + ]:CBC        1256 :     if (btvacinfo->num_vacuums >= btvacinfo->max_vacuums)
                               4460                 :                :     {
 6225 tgl@sss.pgh.pa.us        4461                 :UBC           0 :         LWLockRelease(BtreeVacuumLock);
 6551                          4462         [ #  # ]:              0 :         elog(ERROR, "out of btvacinfo slots");
                               4463                 :                :     }
 6551 tgl@sss.pgh.pa.us        4464                 :CBC        1256 :     vac = &btvacinfo->vacuums[btvacinfo->num_vacuums];
                               4465                 :           1256 :     vac->relid = rel->rd_lockInfo.lockRelId;
                               4466                 :           1256 :     vac->cycleid = result;
                               4467                 :           1256 :     btvacinfo->num_vacuums++;
                               4468                 :                : 
                               4469                 :           1256 :     LWLockRelease(BtreeVacuumLock);
                               4470                 :           1256 :     return result;
                               4471                 :                : }
                               4472                 :                : 
                               4473                 :                : /*
                               4474                 :                :  * _bt_end_vacuum --- mark a btree VACUUM operation as done
                               4475                 :                :  *
                               4476                 :                :  * Note: this is deliberately coded not to complain if no entry is found;
                               4477                 :                :  * this allows the caller to put PG_TRY around the start_vacuum operation.
                               4478                 :                :  */
                               4479                 :                : void
                               4480                 :           1256 : _bt_end_vacuum(Relation rel)
                               4481                 :                : {
                               4482                 :                :     int         i;
                               4483                 :                : 
                               4484                 :           1256 :     LWLockAcquire(BtreeVacuumLock, LW_EXCLUSIVE);
                               4485                 :                : 
                               4486                 :                :     /* Find the array entry */
                               4487         [ +  - ]:           1256 :     for (i = 0; i < btvacinfo->num_vacuums; i++)
                               4488                 :                :     {
                               4489                 :           1256 :         BTOneVacInfo *vac = &btvacinfo->vacuums[i];
                               4490                 :                : 
                               4491         [ +  - ]:           1256 :         if (vac->relid.relId == rel->rd_lockInfo.lockRelId.relId &&
                               4492         [ +  - ]:           1256 :             vac->relid.dbId == rel->rd_lockInfo.lockRelId.dbId)
                               4493                 :                :         {
                               4494                 :                :             /* Remove it by shifting down the last entry */
                               4495                 :           1256 :             *vac = btvacinfo->vacuums[btvacinfo->num_vacuums - 1];
                               4496                 :           1256 :             btvacinfo->num_vacuums--;
                               4497                 :           1256 :             break;
                               4498                 :                :         }
                               4499                 :                :     }
                               4500                 :                : 
                               4501                 :           1256 :     LWLockRelease(BtreeVacuumLock);
                               4502                 :           1256 : }
                               4503                 :                : 
                               4504                 :                : /*
                               4505                 :                :  * _bt_end_vacuum wrapped as an on_shmem_exit callback function
                               4506                 :                :  */
                               4507                 :                : void
 5842 tgl@sss.pgh.pa.us        4508                 :UBC           0 : _bt_end_vacuum_callback(int code, Datum arg)
                               4509                 :                : {
                               4510                 :              0 :     _bt_end_vacuum((Relation) DatumGetPointer(arg));
                               4511                 :              0 : }
                               4512                 :                : 
                               4513                 :                : /*
                               4514                 :                :  * BTreeShmemSize --- report amount of shared memory space needed
                               4515                 :                :  */
                               4516                 :                : Size
 6551 tgl@sss.pgh.pa.us        4517                 :CBC        2577 : BTreeShmemSize(void)
                               4518                 :                : {
                               4519                 :                :     Size        size;
                               4520                 :                : 
 3341                          4521                 :           2577 :     size = offsetof(BTVacInfo, vacuums);
  733 rhaas@postgresql.org     4522                 :           2577 :     size = add_size(size, mul_size(MaxBackends, sizeof(BTOneVacInfo)));
 6551 tgl@sss.pgh.pa.us        4523                 :           2577 :     return size;
                               4524                 :                : }
                               4525                 :                : 
                               4526                 :                : /*
                               4527                 :                :  * BTreeShmemInit --- initialize this module's shared memory
                               4528                 :                :  */
                               4529                 :                : void
                               4530                 :            898 : BTreeShmemInit(void)
                               4531                 :                : {
                               4532                 :                :     bool        found;
                               4533                 :                : 
                               4534                 :            898 :     btvacinfo = (BTVacInfo *) ShmemInitStruct("BTree Vacuum State",
                               4535                 :                :                                               BTreeShmemSize(),
                               4536                 :                :                                               &found);
                               4537                 :                : 
                               4538         [ +  - ]:            898 :     if (!IsUnderPostmaster)
                               4539                 :                :     {
                               4540                 :                :         /* Initialize shared memory area */
                               4541         [ -  + ]:            898 :         Assert(!found);
                               4542                 :                : 
                               4543                 :                :         /*
                               4544                 :                :          * It doesn't really matter what the cycle counter starts at, but
                               4545                 :                :          * having it always start the same doesn't seem good.  Seed with
                               4546                 :                :          * low-order bits of time() instead.
                               4547                 :                :          */
                               4548                 :            898 :         btvacinfo->cycle_ctr = (BTCycleId) time(NULL);
                               4549                 :                : 
                               4550                 :            898 :         btvacinfo->num_vacuums = 0;
  733 rhaas@postgresql.org     4551                 :            898 :         btvacinfo->max_vacuums = MaxBackends;
                               4552                 :                :     }
                               4553                 :                :     else
 6551 tgl@sss.pgh.pa.us        4554         [ #  # ]:UBC           0 :         Assert(found);
 6551 tgl@sss.pgh.pa.us        4555                 :CBC         898 : }
                               4556                 :                : 
                               4557                 :                : bytea *
 3010                          4558                 :            138 : btoptions(Datum reloptions, bool validate)
                               4559                 :                : {
                               4560                 :                :     static const relopt_parse_elt tab[] = {
                               4561                 :                :         {"fillfactor", RELOPT_TYPE_INT, offsetof(BTOptions, fillfactor)},
                               4562                 :                :         {"vacuum_cleanup_index_scale_factor", RELOPT_TYPE_REAL,
                               4563                 :                :         offsetof(BTOptions, vacuum_cleanup_index_scale_factor)},
                               4564                 :                :         {"deduplicate_items", RELOPT_TYPE_BOOL,
                               4565                 :                :         offsetof(BTOptions, deduplicate_items)}
                               4566                 :                :     };
                               4567                 :                : 
 1602 michael@paquier.xyz      4568                 :            138 :     return (bytea *) build_reloptions(reloptions, validate,
                               4569                 :                :                                       RELOPT_KIND_BTREE,
                               4570                 :                :                                       sizeof(BTOptions),
                               4571                 :                :                                       tab, lengthof(tab));
                               4572                 :                : }
                               4573                 :                : 
                               4574                 :                : /*
                               4575                 :                :  *  btproperty() -- Check boolean properties of indexes.
                               4576                 :                :  *
                               4577                 :                :  * This is optional, but handling AMPROP_RETURNABLE here saves opening the rel
                               4578                 :                :  * to call btcanreturn.
                               4579                 :                :  */
                               4580                 :                : bool
 2801 tgl@sss.pgh.pa.us        4581                 :            378 : btproperty(Oid index_oid, int attno,
                               4582                 :                :            IndexAMProperty prop, const char *propname,
                               4583                 :                :            bool *res, bool *isnull)
                               4584                 :                : {
                               4585         [ +  + ]:            378 :     switch (prop)
                               4586                 :                :     {
                               4587                 :             21 :         case AMPROP_RETURNABLE:
                               4588                 :                :             /* answer only for columns, not AM or whole index */
                               4589         [ +  + ]:             21 :             if (attno == 0)
                               4590                 :              6 :                 return false;
                               4591                 :                :             /* otherwise, btree can always return data */
                               4592                 :             15 :             *res = true;
                               4593                 :             15 :             return true;
                               4594                 :                : 
                               4595                 :            357 :         default:
                               4596                 :            357 :             return false;       /* punt to generic code */
                               4597                 :                :     }
                               4598                 :                : }
                               4599                 :                : 
                               4600                 :                : /*
                               4601                 :                :  *  btbuildphasename() -- Return name of index build phase.
                               4602                 :                :  */
                               4603                 :                : char *
 1839 alvherre@alvh.no-ip.     4604                 :UBC           0 : btbuildphasename(int64 phasenum)
                               4605                 :                : {
                               4606   [ #  #  #  #  :              0 :     switch (phasenum)
                                              #  # ]
                               4607                 :                :     {
                               4608                 :              0 :         case PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE:
                               4609                 :              0 :             return "initializing";
                               4610                 :              0 :         case PROGRESS_BTREE_PHASE_INDEXBUILD_TABLESCAN:
                               4611                 :              0 :             return "scanning table";
                               4612                 :              0 :         case PROGRESS_BTREE_PHASE_PERFORMSORT_1:
                               4613                 :              0 :             return "sorting live tuples";
                               4614                 :              0 :         case PROGRESS_BTREE_PHASE_PERFORMSORT_2:
                               4615                 :              0 :             return "sorting dead tuples";
                               4616                 :              0 :         case PROGRESS_BTREE_PHASE_LEAF_LOAD:
                               4617                 :              0 :             return "loading tuples in tree";
                               4618                 :              0 :         default:
                               4619                 :              0 :             return NULL;
                               4620                 :                :     }
                               4621                 :                : }
                               4622                 :                : 
                               4623                 :                : /*
                               4624                 :                :  *  _bt_truncate() -- create tuple without unneeded suffix attributes.
                               4625                 :                :  *
                               4626                 :                :  * Returns truncated pivot index tuple allocated in caller's memory context,
                               4627                 :                :  * with key attributes copied from caller's firstright argument.  If rel is
                               4628                 :                :  * an INCLUDE index, non-key attributes will definitely be truncated away,
                               4629                 :                :  * since they're not part of the key space.  More aggressive suffix
                               4630                 :                :  * truncation can take place when it's clear that the returned tuple does not
                               4631                 :                :  * need one or more suffix key attributes.  We only need to keep firstright
                               4632                 :                :  * attributes up to and including the first non-lastleft-equal attribute.
                               4633                 :                :  * Caller's insertion scankey is used to compare the tuples; the scankey's
                               4634                 :                :  * argument values are not considered here.
                               4635                 :                :  *
                               4636                 :                :  * Note that returned tuple's t_tid offset will hold the number of attributes
                               4637                 :                :  * present, so the original item pointer offset is not represented.  Caller
                               4638                 :                :  * should only change truncated tuple's downlink.  Note also that truncated
                               4639                 :                :  * key attributes are treated as containing "minus infinity" values by
                               4640                 :                :  * _bt_compare().
                               4641                 :                :  *
                               4642                 :                :  * In the worst case (when a heap TID must be appended to distinguish lastleft
                               4643                 :                :  * from firstright), the size of the returned tuple is the size of firstright
                               4644                 :                :  * plus the size of an additional MAXALIGN()'d item pointer.  This guarantee
                               4645                 :                :  * is important, since callers need to stay under the 1/3 of a page
                               4646                 :                :  * restriction on tuple size.  If this routine is ever taught to truncate
                               4647                 :                :  * within an attribute/datum, it will need to avoid returning an enlarged
                               4648                 :                :  * tuple to caller when truncation + TOAST compression ends up enlarging the
                               4649                 :                :  * final datum.
                               4650                 :                :  */
                               4651                 :                : IndexTuple
 1852 pg@bowt.ie               4652                 :CBC       29103 : _bt_truncate(Relation rel, IndexTuple lastleft, IndexTuple firstright,
                               4653                 :                :              BTScanInsert itup_key)
                               4654                 :                : {
                               4655                 :          29103 :     TupleDesc   itupdesc = RelationGetDescr(rel);
                               4656                 :          29103 :     int16       nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
                               4657                 :                :     int         keepnatts;
                               4658                 :                :     IndexTuple  pivot;
                               4659                 :                :     IndexTuple  tidpivot;
                               4660                 :                :     ItemPointer pivotheaptid;
                               4661                 :                :     Size        newsize;
                               4662                 :                : 
                               4663                 :                :     /*
                               4664                 :                :      * We should only ever truncate non-pivot tuples from leaf pages.  It's
                               4665                 :                :      * never okay to truncate when splitting an internal page.
                               4666                 :                :      */
 1509                          4667   [ +  -  -  + ]:          29103 :     Assert(!BTreeTupleIsPivot(lastleft) && !BTreeTupleIsPivot(firstright));
                               4668                 :                : 
                               4669                 :                :     /* Determine how many attributes must be kept in truncated tuple */
 1852                          4670                 :          29103 :     keepnatts = _bt_keep_natts(rel, lastleft, firstright, itup_key);
                               4671                 :                : 
                               4672                 :                : #ifdef DEBUG_NO_TRUNCATE
                               4673                 :                :     /* Force truncation to be ineffective for testing purposes */
                               4674                 :                :     keepnatts = nkeyatts + 1;
                               4675                 :                : #endif
                               4676                 :                : 
 1476                          4677                 :          29103 :     pivot = index_truncate_tuple(itupdesc, firstright,
                               4678                 :                :                                  Min(keepnatts, nkeyatts));
                               4679                 :                : 
                               4680         [ +  + ]:          29103 :     if (BTreeTupleIsPosting(pivot))
                               4681                 :                :     {
                               4682                 :                :         /*
                               4683                 :                :          * index_truncate_tuple() just returns a straight copy of firstright
                               4684                 :                :          * when it has no attributes to truncate.  When that happens, we may
                               4685                 :                :          * need to truncate away a posting list here instead.
                               4686                 :                :          */
                               4687   [ +  +  -  + ]:            624 :         Assert(keepnatts == nkeyatts || keepnatts == nkeyatts + 1);
                               4688         [ -  + ]:            624 :         Assert(IndexRelationGetNumberOfAttributes(rel) == nkeyatts);
                               4689                 :            624 :         pivot->t_info &= ~INDEX_SIZE_MASK;
                               4690                 :            624 :         pivot->t_info |= MAXALIGN(BTreeTupleGetPostingOffset(firstright));
                               4691                 :                :     }
                               4692                 :                : 
                               4693                 :                :     /*
                               4694                 :                :      * If there is a distinguishing key attribute within pivot tuple, we're
                               4695                 :                :      * done
                               4696                 :                :      */
                               4697         [ +  + ]:          29103 :     if (keepnatts <= nkeyatts)
                               4698                 :                :     {
 1468                          4699                 :          28577 :         BTreeTupleSetNAtts(pivot, keepnatts, false);
 1476                          4700                 :          28577 :         return pivot;
                               4701                 :                :     }
                               4702                 :                : 
                               4703                 :                :     /*
                               4704                 :                :      * We have to store a heap TID in the new pivot tuple, since no non-TID
                               4705                 :                :      * key attribute value in firstright distinguishes the right side of the
                               4706                 :                :      * split from the left side.  nbtree conceptualizes this case as an
                               4707                 :                :      * inability to truncate away any key attributes, since heap TID is
                               4708                 :                :      * treated as just another key attribute (despite lacking a pg_attribute
                               4709                 :                :      * entry).
                               4710                 :                :      *
                               4711                 :                :      * Use enlarged space that holds a copy of pivot.  We need the extra space
                               4712                 :                :      * to store a heap TID at the end (using the special pivot tuple
                               4713                 :                :      * representation).  Note that the original pivot already has firstright's
                               4714                 :                :      * possible posting list/non-key attribute values removed at this point.
                               4715                 :                :      */
                               4716                 :            526 :     newsize = MAXALIGN(IndexTupleSize(pivot)) + MAXALIGN(sizeof(ItemPointerData));
                               4717                 :            526 :     tidpivot = palloc0(newsize);
                               4718                 :            526 :     memcpy(tidpivot, pivot, MAXALIGN(IndexTupleSize(pivot)));
                               4719                 :                :     /* Cannot leak memory here */
                               4720                 :            526 :     pfree(pivot);
                               4721                 :                : 
                               4722                 :                :     /*
                               4723                 :                :      * Store all of firstright's key attribute values plus a tiebreaker heap
                               4724                 :                :      * TID value in enlarged pivot tuple
                               4725                 :                :      */
                               4726                 :            526 :     tidpivot->t_info &= ~INDEX_SIZE_MASK;
                               4727                 :            526 :     tidpivot->t_info |= newsize;
 1468                          4728                 :            526 :     BTreeTupleSetNAtts(tidpivot, nkeyatts, true);
 1476                          4729                 :            526 :     pivotheaptid = BTreeTupleGetHeapTID(tidpivot);
                               4730                 :                : 
                               4731                 :                :     /*
                               4732                 :                :      * Lehman & Yao use lastleft as the leaf high key in all cases, but don't
                               4733                 :                :      * consider suffix truncation.  It seems like a good idea to follow that
                               4734                 :                :      * example in cases where no truncation takes place -- use lastleft's heap
                               4735                 :                :      * TID.  (This is also the closest value to negative infinity that's
                               4736                 :                :      * legally usable.)
                               4737                 :                :      */
 1509                          4738                 :            526 :     ItemPointerCopy(BTreeTupleGetMaxHeapTID(lastleft), pivotheaptid);
                               4739                 :                : 
                               4740                 :                :     /*
                               4741                 :                :      * We're done.  Assert() that heap TID invariants hold before returning.
                               4742                 :                :      *
                               4743                 :                :      * Lehman and Yao require that the downlink to the right page, which is to
                               4744                 :                :      * be inserted into the parent page in the second phase of a page split be
                               4745                 :                :      * a strict lower bound on items on the right page, and a non-strict upper
                               4746                 :                :      * bound for items on the left page.  Assert that heap TIDs follow these
                               4747                 :                :      * invariants, since a heap TID value is apparently needed as a
                               4748                 :                :      * tiebreaker.
                               4749                 :                :      */
                               4750                 :                : #ifndef DEBUG_NO_TRUNCATE
                               4751         [ -  + ]:            526 :     Assert(ItemPointerCompare(BTreeTupleGetMaxHeapTID(lastleft),
                               4752                 :                :                               BTreeTupleGetHeapTID(firstright)) < 0);
                               4753         [ -  + ]:            526 :     Assert(ItemPointerCompare(pivotheaptid,
                               4754                 :                :                               BTreeTupleGetHeapTID(lastleft)) >= 0);
                               4755         [ -  + ]:            526 :     Assert(ItemPointerCompare(pivotheaptid,
                               4756                 :                :                               BTreeTupleGetHeapTID(firstright)) < 0);
                               4757                 :                : #else
                               4758                 :                : 
                               4759                 :                :     /*
                               4760                 :                :      * Those invariants aren't guaranteed to hold for lastleft + firstright
                               4761                 :                :      * heap TID attribute values when they're considered here only because
                               4762                 :                :      * DEBUG_NO_TRUNCATE is defined (a heap TID is probably not actually
                               4763                 :                :      * needed as a tiebreaker).  DEBUG_NO_TRUNCATE must therefore use a heap
                               4764                 :                :      * TID value that always works as a strict lower bound for items to the
                               4765                 :                :      * right.  In particular, it must avoid using firstright's leading key
                               4766                 :                :      * attribute values along with lastleft's heap TID value when lastleft's
                               4767                 :                :      * TID happens to be greater than firstright's TID.
                               4768                 :                :      */
                               4769                 :                :     ItemPointerCopy(BTreeTupleGetHeapTID(firstright), pivotheaptid);
                               4770                 :                : 
                               4771                 :                :     /*
                               4772                 :                :      * Pivot heap TID should never be fully equal to firstright.  Note that
                               4773                 :                :      * the pivot heap TID will still end up equal to lastleft's heap TID when
                               4774                 :                :      * that's the only usable value.
                               4775                 :                :      */
                               4776                 :                :     ItemPointerSetOffsetNumber(pivotheaptid,
                               4777                 :                :                                OffsetNumberPrev(ItemPointerGetOffsetNumber(pivotheaptid)));
                               4778                 :                :     Assert(ItemPointerCompare(pivotheaptid,
                               4779                 :                :                               BTreeTupleGetHeapTID(firstright)) < 0);
                               4780                 :                : #endif
                               4781                 :                : 
 1476                          4782                 :            526 :     return tidpivot;
                               4783                 :                : }
                               4784                 :                : 
                               4785                 :                : /*
                               4786                 :                :  * _bt_keep_natts - how many key attributes to keep when truncating.
                               4787                 :                :  *
                               4788                 :                :  * Caller provides two tuples that enclose a split point.  Caller's insertion
                               4789                 :                :  * scankey is used to compare the tuples; the scankey's argument values are
                               4790                 :                :  * not considered here.
                               4791                 :                :  *
                               4792                 :                :  * This can return a number of attributes that is one greater than the
                               4793                 :                :  * number of key attributes for the index relation.  This indicates that the
                               4794                 :                :  * caller must use a heap TID as a unique-ifier in new pivot tuple.
                               4795                 :                :  */
                               4796                 :                : static int
 1852                          4797                 :          29103 : _bt_keep_natts(Relation rel, IndexTuple lastleft, IndexTuple firstright,
                               4798                 :                :                BTScanInsert itup_key)
                               4799                 :                : {
                               4800                 :          29103 :     int         nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
                               4801                 :          29103 :     TupleDesc   itupdesc = RelationGetDescr(rel);
                               4802                 :                :     int         keepnatts;
                               4803                 :                :     ScanKey     scankey;
                               4804                 :                : 
                               4805                 :                :     /*
                               4806                 :                :      * _bt_compare() treats truncated key attributes as having the value minus
                               4807                 :                :      * infinity, which would break searches within !heapkeyspace indexes.  We
                               4808                 :                :      * must still truncate away non-key attribute values, though.
                               4809                 :                :      */
                               4810         [ -  + ]:          29103 :     if (!itup_key->heapkeyspace)
 1852 pg@bowt.ie               4811                 :UBC           0 :         return nkeyatts;
                               4812                 :                : 
 1852 pg@bowt.ie               4813                 :CBC       29103 :     scankey = itup_key->scankeys;
                               4814                 :          29103 :     keepnatts = 1;
                               4815         [ +  + ]:          35059 :     for (int attnum = 1; attnum <= nkeyatts; attnum++, scankey++)
                               4816                 :                :     {
                               4817                 :                :         Datum       datum1,
                               4818                 :                :                     datum2;
                               4819                 :                :         bool        isNull1,
                               4820                 :                :                     isNull2;
                               4821                 :                : 
                               4822                 :          34533 :         datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
                               4823                 :          34533 :         datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
                               4824                 :                : 
                               4825         [ -  + ]:          34533 :         if (isNull1 != isNull2)
 1852 pg@bowt.ie               4826                 :UBC           0 :             break;
                               4827                 :                : 
 1852 pg@bowt.ie               4828   [ +  +  +  + ]:CBC       69051 :         if (!isNull1 &&
                               4829                 :          34518 :             DatumGetInt32(FunctionCall2Coll(&scankey->sk_func,
                               4830                 :                :                                             scankey->sk_collation,
                               4831                 :                :                                             datum1,
                               4832                 :                :                                             datum2)) != 0)
                               4833                 :          28577 :             break;
                               4834                 :                : 
                               4835                 :           5956 :         keepnatts++;
                               4836                 :                :     }
                               4837                 :                : 
                               4838                 :                :     /*
                               4839                 :                :      * Assert that _bt_keep_natts_fast() agrees with us in passing.  This is
                               4840                 :                :      * expected in an allequalimage index.
                               4841                 :                :      */
 1509                          4842   [ +  +  -  + ]:          29103 :     Assert(!itup_key->allequalimage ||
                               4843                 :                :            keepnatts == _bt_keep_natts_fast(rel, lastleft, firstright));
                               4844                 :                : 
 1852                          4845                 :          29103 :     return keepnatts;
                               4846                 :                : }
                               4847                 :                : 
                               4848                 :                : /*
                               4849                 :                :  * _bt_keep_natts_fast - fast bitwise variant of _bt_keep_natts.
                               4850                 :                :  *
                               4851                 :                :  * This is exported so that a candidate split point can have its effect on
                               4852                 :                :  * suffix truncation inexpensively evaluated ahead of time when finding a
                               4853                 :                :  * split location.  A naive bitwise approach to datum comparisons is used to
                               4854                 :                :  * save cycles.
                               4855                 :                :  *
                               4856                 :                :  * The approach taken here usually provides the same answer as _bt_keep_natts
                               4857                 :                :  * will (for the same pair of tuples from a heapkeyspace index), since the
                               4858                 :                :  * majority of btree opclasses can never indicate that two datums are equal
                               4859                 :                :  * unless they're bitwise equal after detoasting.  When an index only has
                               4860                 :                :  * "equal image" columns, routine is guaranteed to give the same result as
                               4861                 :                :  * _bt_keep_natts would.
                               4862                 :                :  *
                               4863                 :                :  * Callers can rely on the fact that attributes considered equal here are
                               4864                 :                :  * definitely also equal according to _bt_keep_natts, even when the index uses
                               4865                 :                :  * an opclass or collation that is not "allequalimage"/deduplication-safe.
                               4866                 :                :  * This weaker guarantee is good enough for nbtsplitloc.c caller, since false
                               4867                 :                :  * negatives generally only have the effect of making leaf page splits use a
                               4868                 :                :  * more balanced split point.
                               4869                 :                :  */
                               4870                 :                : int
                               4871                 :        6148929 : _bt_keep_natts_fast(Relation rel, IndexTuple lastleft, IndexTuple firstright)
                               4872                 :                : {
                               4873                 :        6148929 :     TupleDesc   itupdesc = RelationGetDescr(rel);
                               4874                 :        6148929 :     int         keysz = IndexRelationGetNumberOfKeyAttributes(rel);
                               4875                 :                :     int         keepnatts;
                               4876                 :                : 
                               4877                 :        6148929 :     keepnatts = 1;
                               4878         [ +  + ]:       10073174 :     for (int attnum = 1; attnum <= keysz; attnum++)
                               4879                 :                :     {
                               4880                 :                :         Datum       datum1,
                               4881                 :                :                     datum2;
                               4882                 :                :         bool        isNull1,
                               4883                 :                :                     isNull2;
                               4884                 :                :         Form_pg_attribute att;
                               4885                 :                : 
                               4886                 :        8986052 :         datum1 = index_getattr(lastleft, attnum, itupdesc, &isNull1);
                               4887                 :        8986052 :         datum2 = index_getattr(firstright, attnum, itupdesc, &isNull2);
                               4888                 :        8986052 :         att = TupleDescAttr(itupdesc, attnum - 1);
                               4889                 :                : 
                               4890         [ +  + ]:        8986052 :         if (isNull1 != isNull2)
                               4891                 :        5061807 :             break;
                               4892                 :                : 
                               4893         [ +  + ]:        8985980 :         if (!isNull1 &&
 1615                          4894         [ +  + ]:        8962437 :             !datum_image_eq(datum1, datum2, att->attbyval, att->attlen))
 1852                          4895                 :        5061735 :             break;
                               4896                 :                : 
                               4897                 :        3924245 :         keepnatts++;
                               4898                 :                :     }
                               4899                 :                : 
                               4900                 :        6148929 :     return keepnatts;
                               4901                 :                : }
                               4902                 :                : 
                               4903                 :                : /*
                               4904                 :                :  *  _bt_check_natts() -- Verify tuple has expected number of attributes.
                               4905                 :                :  *
                               4906                 :                :  * Returns value indicating if the expected number of attributes were found
                               4907                 :                :  * for a particular offset on page.  This can be used as a general purpose
                               4908                 :                :  * sanity check.
                               4909                 :                :  *
                               4910                 :                :  * Testing a tuple directly with BTreeTupleGetNAtts() should generally be
                               4911                 :                :  * preferred to calling here.  That's usually more convenient, and is always
                               4912                 :                :  * more explicit.  Call here instead when offnum's tuple may be a negative
                               4913                 :                :  * infinity tuple that uses the pre-v11 on-disk representation, or when a low
                               4914                 :                :  * context check is appropriate.  This routine is as strict as possible about
                               4915                 :                :  * what is expected on each version of btree.
                               4916                 :                :  */
                               4917                 :                : bool
                               4918                 :      121516554 : _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
                               4919                 :                : {
 2180 tgl@sss.pgh.pa.us        4920                 :      121516554 :     int16       natts = IndexRelationGetNumberOfAttributes(rel);
                               4921                 :      121516554 :     int16       nkeyatts = IndexRelationGetNumberOfKeyAttributes(rel);
  744 michael@paquier.xyz      4922                 :      121516554 :     BTPageOpaque opaque = BTPageGetOpaque(page);
                               4923                 :                :     IndexTuple  itup;
                               4924                 :                :     int         tupnatts;
                               4925                 :                : 
                               4926                 :                :     /*
                               4927                 :                :      * We cannot reliably test a deleted or half-dead page, since they have
                               4928                 :                :      * dummy high keys
                               4929                 :                :      */
 2187 teodor@sigaev.ru         4930         [ -  + ]:      121516554 :     if (P_IGNORE(opaque))
 2187 teodor@sigaev.ru         4931                 :UBC           0 :         return true;
                               4932                 :                : 
 2187 teodor@sigaev.ru         4933   [ +  -  -  + ]:CBC   121516554 :     Assert(offnum >= FirstOffsetNumber &&
                               4934                 :                :            offnum <= PageGetMaxOffsetNumber(page));
                               4935                 :                : 
                               4936                 :      121516554 :     itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
 1852 pg@bowt.ie               4937         [ +  + ]:      121516554 :     tupnatts = BTreeTupleGetNAtts(itup, rel);
                               4938                 :                : 
                               4939                 :                :     /* !heapkeyspace indexes do not support deduplication */
 1509                          4940   [ -  +  -  - ]:      121516554 :     if (!heapkeyspace && BTreeTupleIsPosting(itup))
 1509 pg@bowt.ie               4941                 :UBC           0 :         return false;
                               4942                 :                : 
                               4943                 :                :     /* Posting list tuples should never have "pivot heap TID" bit set */
 1509 pg@bowt.ie               4944         [ +  + ]:CBC   121516554 :     if (BTreeTupleIsPosting(itup) &&
                               4945         [ -  + ]:        1581521 :         (ItemPointerGetOffsetNumberNoCheck(&itup->t_tid) &
                               4946                 :                :          BT_PIVOT_HEAP_TID_ATTR) != 0)
 1509 pg@bowt.ie               4947                 :UBC           0 :         return false;
                               4948                 :                : 
                               4949                 :                :     /* INCLUDE indexes do not support deduplication */
 1509 pg@bowt.ie               4950   [ +  +  -  + ]:CBC   121516554 :     if (natts != nkeyatts && BTreeTupleIsPosting(itup))
 1509 pg@bowt.ie               4951                 :UBC           0 :         return false;
                               4952                 :                : 
 2187 teodor@sigaev.ru         4953         [ +  + ]:CBC   121516554 :     if (P_ISLEAF(opaque))
                               4954                 :                :     {
                               4955   [ +  +  +  + ]:       89499096 :         if (offnum >= P_FIRSTDATAKEY(opaque))
                               4956                 :                :         {
                               4957                 :                :             /*
                               4958                 :                :              * Non-pivot tuple should never be explicitly marked as a pivot
                               4959                 :                :              * tuple
                               4960                 :                :              */
 1509 pg@bowt.ie               4961         [ -  + ]:       82191287 :             if (BTreeTupleIsPivot(itup))
 1852 pg@bowt.ie               4962                 :UBC           0 :                 return false;
                               4963                 :                : 
                               4964                 :                :             /*
                               4965                 :                :              * Leaf tuples that are not the page high key (non-pivot tuples)
                               4966                 :                :              * should never be truncated.  (Note that tupnatts must have been
                               4967                 :                :              * inferred, even with a posting list tuple, because only pivot
                               4968                 :                :              * tuples store tupnatts directly.)
                               4969                 :                :              */
 1852 pg@bowt.ie               4970                 :CBC    82191287 :             return tupnatts == natts;
                               4971                 :                :         }
                               4972                 :                :         else
                               4973                 :                :         {
                               4974                 :                :             /*
                               4975                 :                :              * Rightmost page doesn't contain a page high key, so tuple was
                               4976                 :                :              * checked above as ordinary leaf tuple
                               4977                 :                :              */
 2187 teodor@sigaev.ru         4978         [ -  + ]:        7307809 :             Assert(!P_RIGHTMOST(opaque));
                               4979                 :                : 
                               4980                 :                :             /*
                               4981                 :                :              * !heapkeyspace high key tuple contains only key attributes. Note
                               4982                 :                :              * that tupnatts will only have been explicitly represented in
                               4983                 :                :              * !heapkeyspace indexes that happen to have non-key attributes.
                               4984                 :                :              */
 1852 pg@bowt.ie               4985         [ -  + ]:        7307809 :             if (!heapkeyspace)
 1852 pg@bowt.ie               4986                 :UBC           0 :                 return tupnatts == nkeyatts;
                               4987                 :                : 
                               4988                 :                :             /* Use generic heapkeyspace pivot tuple handling */
                               4989                 :                :         }
                               4990                 :                :     }
                               4991                 :                :     else                        /* !P_ISLEAF(opaque) */
                               4992                 :                :     {
 2187 teodor@sigaev.ru         4993   [ +  +  +  + ]:CBC    32017458 :         if (offnum == P_FIRSTDATAKEY(opaque))
                               4994                 :                :         {
                               4995                 :                :             /*
                               4996                 :                :              * The first tuple on any internal page (possibly the first after
                               4997                 :                :              * its high key) is its negative infinity tuple.  Negative
                               4998                 :                :              * infinity tuples are always truncated to zero attributes.  They
                               4999                 :                :              * are a particular kind of pivot tuple.
                               5000                 :                :              */
 1852 pg@bowt.ie               5001         [ +  - ]:        1399389 :             if (heapkeyspace)
                               5002                 :        1399389 :                 return tupnatts == 0;
                               5003                 :                : 
                               5004                 :                :             /*
                               5005                 :                :              * The number of attributes won't be explicitly represented if the
                               5006                 :                :              * negative infinity tuple was generated during a page split that
                               5007                 :                :              * occurred with a version of Postgres before v11.  There must be
                               5008                 :                :              * a problem when there is an explicit representation that is
                               5009                 :                :              * non-zero, or when there is no explicit representation and the
                               5010                 :                :              * tuple is evidently not a pre-pg_upgrade tuple.
                               5011                 :                :              *
                               5012                 :                :              * Prior to v11, downlinks always had P_HIKEY as their offset.
                               5013                 :                :              * Accept that as an alternative indication of a valid
                               5014                 :                :              * !heapkeyspace negative infinity tuple.
                               5015                 :                :              */
 1852 pg@bowt.ie               5016   [ #  #  #  # ]:UBC           0 :             return tupnatts == 0 ||
 1509                          5017                 :              0 :                 ItemPointerGetOffsetNumber(&(itup->t_tid)) == P_HIKEY;
                               5018                 :                :         }
                               5019                 :                :         else
                               5020                 :                :         {
                               5021                 :                :             /*
                               5022                 :                :              * !heapkeyspace downlink tuple with separator key contains only
                               5023                 :                :              * key attributes.  Note that tupnatts will only have been
                               5024                 :                :              * explicitly represented in !heapkeyspace indexes that happen to
                               5025                 :                :              * have non-key attributes.
                               5026                 :                :              */
 1852 pg@bowt.ie               5027         [ -  + ]:CBC    30618069 :             if (!heapkeyspace)
 1852 pg@bowt.ie               5028                 :UBC           0 :                 return tupnatts == nkeyatts;
                               5029                 :                : 
                               5030                 :                :             /* Use generic heapkeyspace pivot tuple handling */
                               5031                 :                :         }
                               5032                 :                :     }
                               5033                 :                : 
                               5034                 :                :     /* Handle heapkeyspace pivot tuples (excluding minus infinity items) */
 1852 pg@bowt.ie               5035         [ -  + ]:CBC    37925878 :     Assert(heapkeyspace);
                               5036                 :                : 
                               5037                 :                :     /*
                               5038                 :                :      * Explicit representation of the number of attributes is mandatory with
                               5039                 :                :      * heapkeyspace index pivot tuples, regardless of whether or not there are
                               5040                 :                :      * non-key attributes.
                               5041                 :                :      */
 1509                          5042         [ -  + ]:       37925878 :     if (!BTreeTupleIsPivot(itup))
 1509 pg@bowt.ie               5043                 :UBC           0 :         return false;
                               5044                 :                : 
                               5045                 :                :     /* Pivot tuple should not use posting list representation (redundant) */
 1509 pg@bowt.ie               5046         [ -  + ]:CBC    37925878 :     if (BTreeTupleIsPosting(itup))
 1852 pg@bowt.ie               5047                 :UBC           0 :         return false;
                               5048                 :                : 
                               5049                 :                :     /*
                               5050                 :                :      * Heap TID is a tiebreaker key attribute, so it cannot be untruncated
                               5051                 :                :      * when any other key attribute is truncated
                               5052                 :                :      */
 1852 pg@bowt.ie               5053   [ +  +  -  + ]:CBC    37925878 :     if (BTreeTupleGetHeapTID(itup) != NULL && tupnatts != nkeyatts)
 1852 pg@bowt.ie               5054                 :UBC           0 :         return false;
                               5055                 :                : 
                               5056                 :                :     /*
                               5057                 :                :      * Pivot tuple must have at least one untruncated key attribute (minus
                               5058                 :                :      * infinity pivot tuples are the only exception).  Pivot tuples can never
                               5059                 :                :      * represent that there is a value present for a key attribute that
                               5060                 :                :      * exceeds pg_index.indnkeyatts for the index.
                               5061                 :                :      */
 1852 pg@bowt.ie               5062   [ +  -  +  - ]:CBC    37925878 :     return tupnatts > 0 && tupnatts <= nkeyatts;
                               5063                 :                : }
                               5064                 :                : 
                               5065                 :                : /*
                               5066                 :                :  *
                               5067                 :                :  *  _bt_check_third_page() -- check whether tuple fits on a btree page at all.
                               5068                 :                :  *
                               5069                 :                :  * We actually need to be able to fit three items on every page, so restrict
                               5070                 :                :  * any one item to 1/3 the per-page available space.  Note that itemsz should
                               5071                 :                :  * not include the ItemId overhead.
                               5072                 :                :  *
                               5073                 :                :  * It might be useful to apply TOAST methods rather than throw an error here.
                               5074                 :                :  * Using out of line storage would break assumptions made by suffix truncation
                               5075                 :                :  * and by contrib/amcheck, though.
                               5076                 :                :  */
                               5077                 :                : void
                               5078                 :            132 : _bt_check_third_page(Relation rel, Relation heap, bool needheaptidspace,
                               5079                 :                :                      Page page, IndexTuple newtup)
                               5080                 :                : {
                               5081                 :                :     Size        itemsz;
                               5082                 :                :     BTPageOpaque opaque;
                               5083                 :                : 
                               5084                 :            132 :     itemsz = MAXALIGN(IndexTupleSize(newtup));
                               5085                 :                : 
                               5086                 :                :     /* Double check item size against limit */
                               5087         [ -  + ]:            132 :     if (itemsz <= BTMaxItemSize(page))
 1852 pg@bowt.ie               5088                 :UBC           0 :         return;
                               5089                 :                : 
                               5090                 :                :     /*
                               5091                 :                :      * Tuple is probably too large to fit on page, but it's possible that the
                               5092                 :                :      * index uses version 2 or version 3, or that page is an internal page, in
                               5093                 :                :      * which case a slightly higher limit applies.
                               5094                 :                :      */
 1852 pg@bowt.ie               5095   [ +  -  +  - ]:CBC         132 :     if (!needheaptidspace && itemsz <= BTMaxItemSizeNoHeapTid(page))
                               5096                 :            132 :         return;
                               5097                 :                : 
                               5098                 :                :     /*
                               5099                 :                :      * Internal page insertions cannot fail here, because that would mean that
                               5100                 :                :      * an earlier leaf level insertion that should have failed didn't
                               5101                 :                :      */
  744 michael@paquier.xyz      5102                 :UBC           0 :     opaque = BTPageGetOpaque(page);
 1852 pg@bowt.ie               5103         [ #  # ]:              0 :     if (!P_ISLEAF(opaque))
                               5104         [ #  # ]:              0 :         elog(ERROR, "cannot insert oversized tuple of size %zu on internal page of index \"%s\"",
                               5105                 :                :              itemsz, RelationGetRelationName(rel));
                               5106                 :                : 
                               5107   [ #  #  #  #  :              0 :     ereport(ERROR,
                                              #  # ]
                               5108                 :                :             (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                               5109                 :                :              errmsg("index row size %zu exceeds btree version %u maximum %zu for index \"%s\"",
                               5110                 :                :                     itemsz,
                               5111                 :                :                     needheaptidspace ? BTREE_VERSION : BTREE_NOVAC_VERSION,
                               5112                 :                :                     needheaptidspace ? BTMaxItemSize(page) :
                               5113                 :                :                     BTMaxItemSizeNoHeapTid(page),
                               5114                 :                :                     RelationGetRelationName(rel)),
                               5115                 :                :              errdetail("Index row references tuple (%u,%u) in relation \"%s\".",
                               5116                 :                :                        ItemPointerGetBlockNumber(BTreeTupleGetHeapTID(newtup)),
                               5117                 :                :                        ItemPointerGetOffsetNumber(BTreeTupleGetHeapTID(newtup)),
                               5118                 :                :                        RelationGetRelationName(heap)),
                               5119                 :                :              errhint("Values larger than 1/3 of a buffer page cannot be indexed.\n"
                               5120                 :                :                      "Consider a function index of an MD5 hash of the value, "
                               5121                 :                :                      "or use full text indexing."),
                               5122                 :                :              errtableconstraint(heap, RelationGetRelationName(rel))));
                               5123                 :                : }
                               5124                 :                : 
                               5125                 :                : /*
                               5126                 :                :  * Are all attributes in rel "equality is image equality" attributes?
                               5127                 :                :  *
                               5128                 :                :  * We use each attribute's BTEQUALIMAGE_PROC opclass procedure.  If any
                               5129                 :                :  * opclass either lacks a BTEQUALIMAGE_PROC procedure or returns false, we
                               5130                 :                :  * return false; otherwise we return true.
                               5131                 :                :  *
                               5132                 :                :  * Returned boolean value is stored in index metapage during index builds.
                               5133                 :                :  * Deduplication can only be used when we return true.
                               5134                 :                :  */
                               5135                 :                : bool
 1509 pg@bowt.ie               5136                 :CBC       26628 : _bt_allequalimage(Relation rel, bool debugmessage)
                               5137                 :                : {
                               5138                 :          26628 :     bool        allequalimage = true;
                               5139                 :                : 
                               5140                 :                :     /* INCLUDE indexes can never support deduplication */
                               5141                 :          26628 :     if (IndexRelationGetNumberOfAttributes(rel) !=
                               5142         [ +  + ]:          26628 :         IndexRelationGetNumberOfKeyAttributes(rel))
                               5143                 :            133 :         return false;
                               5144                 :                : 
                               5145         [ +  + ]:          69686 :     for (int i = 0; i < IndexRelationGetNumberOfKeyAttributes(rel); i++)
                               5146                 :                :     {
                               5147                 :          43418 :         Oid         opfamily = rel->rd_opfamily[i];
                               5148                 :          43418 :         Oid         opcintype = rel->rd_opcintype[i];
                               5149                 :          43418 :         Oid         collation = rel->rd_indcollation[i];
                               5150                 :                :         Oid         equalimageproc;
                               5151                 :                : 
                               5152                 :          43418 :         equalimageproc = get_opfamily_proc(opfamily, opcintype, opcintype,
                               5153                 :                :                                            BTEQUALIMAGE_PROC);
                               5154                 :                : 
                               5155                 :                :         /*
                               5156                 :                :          * If there is no BTEQUALIMAGE_PROC then deduplication is assumed to
                               5157                 :                :          * be unsafe.  Otherwise, actually call proc and see what it says.
                               5158                 :                :          */
                               5159         [ +  + ]:          43418 :         if (!OidIsValid(equalimageproc) ||
                               5160         [ +  + ]:          43201 :             !DatumGetBool(OidFunctionCall1Coll(equalimageproc, collation,
                               5161                 :                :                                                ObjectIdGetDatum(opcintype))))
                               5162                 :                :         {
                               5163                 :            227 :             allequalimage = false;
                               5164                 :            227 :             break;
                               5165                 :                :         }
                               5166                 :                :     }
                               5167                 :                : 
                               5168         [ +  + ]:          26495 :     if (debugmessage)
                               5169                 :                :     {
                               5170         [ +  + ]:          22447 :         if (allequalimage)
                               5171         [ +  + ]:          22220 :             elog(DEBUG1, "index \"%s\" can safely use deduplication",
                               5172                 :                :                  RelationGetRelationName(rel));
                               5173                 :                :         else
                               5174         [ -  + ]:            227 :             elog(DEBUG1, "index \"%s\" cannot use deduplication",
                               5175                 :                :                  RelationGetRelationName(rel));
                               5176                 :                :     }
                               5177                 :                : 
                               5178                 :          26495 :     return allequalimage;
                               5179                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622