LCOV - differential code coverage report
Current view: top level - contrib/btree_gist - btree_bit.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: 77aeca80249c9e640c811e80633a2e334a9320de vs 38afc3dcb25c45b744d4025029ce0a6c90b7059f Lines: 87.9 % 91 80 11 4 76 6
Current Date: 2026-07-25 19:08:27 +0900 Functions: 92.0 % 25 23 2 3 20 1
Baseline: lcov-20260725-baseline Branches: 75.0 % 8 6 2 6
Baseline Date: 2026-07-25 19:09:19 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 5 5 4 1
(360..) days: 87.2 % 86 75 11 75
Function coverage date bins:
(7,30] days: 100.0 % 1 1 1
(360..) days: 91.7 % 24 22 2 2 20
Branch coverage date bins:
(360..) days: 75.0 % 8 6 2 6

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  * contrib/btree_gist/btree_bit.c
                                  3                 :                :  *
                                  4                 :                :  * Support for bit and varbit types (which act the same for our purposes).
                                  5                 :                :  *
                                  6                 :                :  * Leaf-page keys are bit/varbit values, but internal-page keys are just
                                  7                 :                :  * bytea values containing the first N bytes of the represented bitstring.
                                  8                 :                :  * (In particular, they lack the bit_len field of a bit/varbit Datum.)
                                  9                 :                :  */
                                 10                 :                : #include "postgres.h"
                                 11                 :                : 
                                 12                 :                : #include "btree_gist.h"
                                 13                 :                : #include "btree_utils_var.h"
                                 14                 :                : #include "utils/fmgrprotos.h"
                                 15                 :                : #include "utils/sortsupport.h"
                                 16                 :                : #include "utils/varbit.h"
                                 17                 :                : #include "varatt.h"
                                 18                 :                : 
                                 19                 :                : /* GiST support functions */
 8093 teodor@sigaev.ru           20                 :CBC           5 : PG_FUNCTION_INFO_V1(gbt_bit_compress);
                                 21                 :              5 : PG_FUNCTION_INFO_V1(gbt_bit_union);
                                 22                 :              5 : PG_FUNCTION_INFO_V1(gbt_bit_picksplit);
                                 23                 :              5 : PG_FUNCTION_INFO_V1(gbt_bit_consistent);
                                 24                 :              5 : PG_FUNCTION_INFO_V1(gbt_bit_penalty);
                                 25                 :              5 : PG_FUNCTION_INFO_V1(gbt_bit_same);
  478 heikki.linnakangas@i       26                 :              4 : PG_FUNCTION_INFO_V1(gbt_bit_sortsupport);
                                 27                 :              4 : PG_FUNCTION_INFO_V1(gbt_varbit_sortsupport);
                                 28                 :                : 
                                 29                 :                : 
                                 30                 :                : /* define for comparison */
                                 31                 :                : 
                                 32                 :                : static bool
 3413 andrew@dunslane.net        33                 :            600 : gbt_bitgt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 34                 :                : {
 5573 tgl@sss.pgh.pa.us          35                 :            600 :     return DatumGetBool(DirectFunctionCall2(bitgt,
                                 36                 :                :                                             PointerGetDatum(a),
                                 37                 :                :                                             PointerGetDatum(b)));
                                 38                 :                : }
                                 39                 :                : 
                                 40                 :                : static bool
 3413 andrew@dunslane.net        41                 :            600 : gbt_bitge(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 42                 :                : {
 5573 tgl@sss.pgh.pa.us          43                 :            600 :     return DatumGetBool(DirectFunctionCall2(bitge,
                                 44                 :                :                                             PointerGetDatum(a),
                                 45                 :                :                                             PointerGetDatum(b)));
                                 46                 :                : }
                                 47                 :                : 
                                 48                 :                : static bool
 3413 andrew@dunslane.net        49                 :            300 : gbt_biteq(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 50                 :                : {
 5573 tgl@sss.pgh.pa.us          51                 :            300 :     return DatumGetBool(DirectFunctionCall2(biteq,
                                 52                 :                :                                             PointerGetDatum(a),
                                 53                 :                :                                             PointerGetDatum(b)));
                                 54                 :                : }
                                 55                 :                : 
                                 56                 :                : static bool
 3413 andrew@dunslane.net        57                 :            900 : gbt_bitle(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 58                 :                : {
 5573 tgl@sss.pgh.pa.us          59                 :            900 :     return DatumGetBool(DirectFunctionCall2(bitle,
                                 60                 :                :                                             PointerGetDatum(a),
                                 61                 :                :                                             PointerGetDatum(b)));
                                 62                 :                : }
                                 63                 :                : 
                                 64                 :                : static bool
 3413 andrew@dunslane.net        65                 :            900 : gbt_bitlt(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 66                 :                : {
 5573 tgl@sss.pgh.pa.us          67                 :            900 :     return DatumGetBool(DirectFunctionCall2(bitlt,
                                 68                 :                :                                             PointerGetDatum(a),
                                 69                 :                :                                             PointerGetDatum(b)));
                                 70                 :                : }
                                 71                 :                : 
                                 72                 :                : /*
                                 73                 :                :  * Notice we use byteacmp here, not bitcmp as you might expect.
                                 74                 :                :  * That's because internal-page keys are bytea.
                                 75                 :                :  */
                                 76                 :                : static int32
 3413 andrew@dunslane.net        77                 :           9674 : gbt_bitcmp(const void *a, const void *b, Oid collation, FmgrInfo *flinfo)
                                 78                 :                : {
 5573 tgl@sss.pgh.pa.us          79                 :           9674 :     return DatumGetInt32(DirectFunctionCall2(byteacmp,
                                 80                 :                :                                              PointerGetDatum(a),
                                 81                 :                :                                              PointerGetDatum(b)));
                                 82                 :                : }
                                 83                 :                : 
                                 84                 :                : 
                                 85                 :                : /*
                                 86                 :                :  * Convert a leaf-page bit/varbit value to internal-page form.
                                 87                 :                :  *
                                 88                 :                :  * The important change here is to remove the bit_len field so that
                                 89                 :                :  * what we have left looks like a bytea.
                                 90                 :                :  *
                                 91                 :                :  * The business with padding to INTALIGN length appears entirely historical,
                                 92                 :                :  * but we can't remove that without breaking on-disk compatibility.
                                 93                 :                :  * For example, if the lower bound of some leaf page is the 20-bit string
                                 94                 :                :  * of all ones, with data contents FFFFF0, this code pads it to bytea FFFFF000
                                 95                 :                :  * in the internal key representing that page.  If, when we search the index
                                 96                 :                :  * for that value, we did not again pad to FFFFF000, then byteacmp would
                                 97                 :                :  * say that the query is strictly less than the lower bound so we would not
                                 98                 :                :  * descend to that leaf page.
                                 99                 :                :  */
                                100                 :                : static bytea *
   22 tgl@sss.pgh.pa.us         101                 :GNC        3638 : gbt_bit_xfrm(VarBit *leaf)
                                102                 :                : {
                                103                 :                :     bytea      *out;
 4456 heikki.linnakangas@i      104                 :CBC        3638 :     int         sz = VARBITBYTES(leaf) + VARHDRSZ;
                                105                 :           3638 :     int         padded_sz = INTALIGN(sz);
                                106                 :                : 
                                107                 :           3638 :     out = (bytea *) palloc(padded_sz);
                                108                 :                :     /* initialize the padding bytes to zero */
                                109         [ +  + ]:          11799 :     while (sz < padded_sz)
                                110                 :           8161 :         ((char *) out)[sz++] = 0;
                                111                 :           3638 :     SET_VARSIZE(out, padded_sz);
 1264 peter@eisentraut.org      112                 :           3638 :     memcpy(VARDATA(out), VARBITS(leaf), VARBITBYTES(leaf));
 8000 bruce@momjian.us          113                 :           3638 :     return out;
                                114                 :                : }
                                115                 :                : 
                                116                 :                : /*
                                117                 :                :  * Convert a GBT_VARKEY representation of a leaf key to a palloc'd
                                118                 :                :  * GBT_VARKEY representation of an internal key.
                                119                 :                :  * We assume lower == upper since it's a leaf key.
                                120                 :                :  */
                                121                 :                : static GBT_VARKEY *
 3413 andrew@dunslane.net       122                 :           3598 : gbt_bit_l2n(GBT_VARKEY *leaf, FmgrInfo *flinfo)
                                123                 :                : {
                                124                 :                :     GBT_VARKEY *out;
 8000 bruce@momjian.us          125                 :           3598 :     GBT_VARKEY_R r = gbt_var_key_readable(leaf);
                                126                 :                :     bytea      *o;
                                127                 :                : 
   22 tgl@sss.pgh.pa.us         128                 :GNC        3598 :     o = gbt_bit_xfrm((VarBit *) r.lower);
 8000 bruce@momjian.us          129                 :CBC        3598 :     r.upper = r.lower = o;
 4139 heikki.linnakangas@i      130                 :           3598 :     out = gbt_var_key_copy(&r);
 8000 bruce@momjian.us          131                 :           3598 :     pfree(o);
                                132                 :                : 
                                133                 :           3598 :     return out;
                                134                 :                : }
                                135                 :                : 
                                136                 :                : static const gbtree_vinfo tinfo =
                                137                 :                : {
                                138                 :                :     gbt_t_bit,
                                139                 :                :     true,                       /* internal keys can be truncated */
                                140                 :                :     gbt_bitgt,
                                141                 :                :     gbt_bitge,
                                142                 :                :     gbt_biteq,
                                143                 :                :     gbt_bitle,
                                144                 :                :     gbt_bitlt,
                                145                 :                :     gbt_bitcmp,
                                146                 :                :     gbt_bit_l2n                 /* leaf to internal transformation */
                                147                 :                : };
                                148                 :                : 
                                149                 :                : 
                                150                 :                : /**************************************************
                                151                 :                :  * GiST support functions
                                152                 :                :  **************************************************/
                                153                 :                : 
                                154                 :                : Datum
                                155                 :           1208 : gbt_bit_compress(PG_FUNCTION_ARGS)
                                156                 :                : {
                                157                 :           1208 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
                                158                 :                : 
                                159                 :           1208 :     PG_RETURN_POINTER(gbt_var_compress(entry, &tinfo));
                                160                 :                : }
                                161                 :                : 
                                162                 :                : Datum
 8093 teodor@sigaev.ru          163                 :           3340 : gbt_bit_consistent(PG_FUNCTION_ARGS)
                                164                 :                : {
 8000 bruce@momjian.us          165                 :           3340 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
   22 tgl@sss.pgh.pa.us         166                 :GNC        3340 :     VarBit     *query = PG_GETARG_VARBIT_P(1);
 8000 bruce@momjian.us          167                 :CBC        3340 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
                                168                 :                : #ifdef NOT_USED
                                169                 :                :     Oid         subtype = PG_GETARG_OID(3);
                                170                 :                : #endif
 6676 tgl@sss.pgh.pa.us         171                 :           3340 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
                                172                 :                :     bool        retval;
                                173                 :           3340 :     GBT_VARKEY *key = (GBT_VARKEY *) DatumGetPointer(entry->key);
 8000 bruce@momjian.us          174                 :           3340 :     GBT_VARKEY_R r = gbt_var_key_readable(key);
                                175                 :                : 
                                176                 :                :     /* All cases served by this function are exact */
 6676 tgl@sss.pgh.pa.us         177                 :           3340 :     *recheck = false;
                                178                 :                : 
 8000 bruce@momjian.us          179         [ +  + ]:           3340 :     if (GIST_LEAF(entry))
 5573 tgl@sss.pgh.pa.us         180                 :           3300 :         retval = gbt_var_consistent(&r, query, strategy, PG_GET_COLLATION(),
                                181                 :                :                                     true, &tinfo, fcinfo->flinfo);
                                182                 :                :     else
                                183                 :                :     {
                                184                 :                :         /* Must convert to internal form to compare to internal-page entries */
   22 tgl@sss.pgh.pa.us         185                 :GNC          40 :         bytea      *q = gbt_bit_xfrm(query);
                                186                 :                : 
 5573 tgl@sss.pgh.pa.us         187                 :CBC          40 :         retval = gbt_var_consistent(&r, q, strategy, PG_GET_COLLATION(),
                                188                 :                :                                     false, &tinfo, fcinfo->flinfo);
                                189                 :                :     }
 8000 bruce@momjian.us          190                 :           3340 :     PG_RETURN_BOOL(retval);
                                191                 :                : }
                                192                 :                : 
                                193                 :                : Datum
 8093 teodor@sigaev.ru          194                 :              2 : gbt_bit_union(PG_FUNCTION_ARGS)
                                195                 :                : {
 8000 bruce@momjian.us          196                 :              2 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
                                197                 :              2 :     int32      *size = (int *) PG_GETARG_POINTER(1);
                                198                 :                : 
 5573 tgl@sss.pgh.pa.us         199                 :              2 :     PG_RETURN_POINTER(gbt_var_union(entryvec, size, PG_GET_COLLATION(),
                                200                 :                :                                     &tinfo, fcinfo->flinfo));
                                201                 :                : }
                                202                 :                : 
                                203                 :                : Datum
 8093 teodor@sigaev.ru          204                 :              6 : gbt_bit_picksplit(PG_FUNCTION_ARGS)
                                205                 :                : {
 8000 bruce@momjian.us          206                 :              6 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
                                207                 :              6 :     GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
                                208                 :                : 
 5573 tgl@sss.pgh.pa.us         209                 :              6 :     gbt_var_picksplit(entryvec, v, PG_GET_COLLATION(),
                                210                 :                :                       &tinfo, fcinfo->flinfo);
 8000 bruce@momjian.us          211                 :              6 :     PG_RETURN_POINTER(v);
                                212                 :                : }
                                213                 :                : 
                                214                 :                : Datum
 8093 teodor@sigaev.ru          215                 :UBC           0 : gbt_bit_same(PG_FUNCTION_ARGS)
                                216                 :                : {
 8000 bruce@momjian.us          217                 :              0 :     Datum       d1 = PG_GETARG_DATUM(0);
                                218                 :              0 :     Datum       d2 = PG_GETARG_DATUM(1);
                                219                 :              0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
                                220                 :                : 
 3413 andrew@dunslane.net       221                 :              0 :     *result = gbt_var_same(d1, d2, PG_GET_COLLATION(), &tinfo, fcinfo->flinfo);
 5573 tgl@sss.pgh.pa.us         222                 :              0 :     PG_RETURN_POINTER(result);
                                223                 :                : }
                                224                 :                : 
                                225                 :                : Datum
 8093 teodor@sigaev.ru          226                 :              0 : gbt_bit_penalty(PG_FUNCTION_ARGS)
                                227                 :                : {
 8000 bruce@momjian.us          228                 :              0 :     GISTENTRY  *o = (GISTENTRY *) PG_GETARG_POINTER(0);
                                229                 :              0 :     GISTENTRY  *n = (GISTENTRY *) PG_GETARG_POINTER(1);
 7744 neilc@samurai.com         230                 :              0 :     float      *result = (float *) PG_GETARG_POINTER(2);
                                231                 :                : 
 5573 tgl@sss.pgh.pa.us         232                 :              0 :     PG_RETURN_POINTER(gbt_var_penalty(result, o, n, PG_GET_COLLATION(),
                                233                 :                :                                       &tinfo, fcinfo->flinfo));
                                234                 :                : }
                                235                 :                : 
                                236                 :                : static int
  478 heikki.linnakangas@i      237                 :CBC       11343 : gbt_bit_ssup_cmp(Datum x, Datum y, SortSupport ssup)
                                238                 :                : {
                                239                 :          11343 :     GBT_VARKEY *key1 = PG_DETOAST_DATUM(x);
                                240                 :          11343 :     GBT_VARKEY *key2 = PG_DETOAST_DATUM(y);
                                241                 :                : 
                                242                 :          11343 :     GBT_VARKEY_R arg1 = gbt_var_key_readable(key1);
                                243                 :          11343 :     GBT_VARKEY_R arg2 = gbt_var_key_readable(key2);
                                244                 :                :     Datum       result;
                                245                 :                : 
                                246                 :                :     /* for leaf items we expect lower == upper, so only compare lower */
   22 tgl@sss.pgh.pa.us         247                 :          11343 :     result = DirectFunctionCall2(bitcmp,
                                248                 :                :                                  PointerGetDatum(arg1.lower),
                                249                 :                :                                  PointerGetDatum(arg2.lower));
                                250                 :                : 
  478 heikki.linnakangas@i      251         [ +  - ]:          11343 :     GBT_FREE_IF_COPY(key1, x);
                                252         [ +  - ]:          11343 :     GBT_FREE_IF_COPY(key2, y);
                                253                 :                : 
                                254                 :          11343 :     return DatumGetInt32(result);
                                255                 :                : }
                                256                 :                : 
                                257                 :                : Datum
                                258                 :              1 : gbt_bit_sortsupport(PG_FUNCTION_ARGS)
                                259                 :                : {
                                260                 :              1 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
                                261                 :                : 
                                262                 :              1 :     ssup->comparator = gbt_bit_ssup_cmp;
                                263                 :              1 :     ssup->ssup_extra = NULL;
                                264                 :                : 
                                265                 :              1 :     PG_RETURN_VOID();
                                266                 :                : }
                                267                 :                : 
                                268                 :                : Datum
                                269                 :              1 : gbt_varbit_sortsupport(PG_FUNCTION_ARGS)
                                270                 :                : {
                                271                 :              1 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
                                272                 :                : 
                                273                 :              1 :     ssup->comparator = gbt_bit_ssup_cmp;
                                274                 :              1 :     ssup->ssup_extra = NULL;
                                275                 :                : 
                                276                 :              1 :     PG_RETURN_VOID();
                                277                 :                : }
        

Generated by: LCOV version 2.0-1