LCOV - code coverage report
Current view: top level - contrib/btree_gist - btree_uuid.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 63 89 70.8 %
Date: 2026-02-02 14:17:46 Functions: 21 25 84.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * contrib/btree_gist/btree_uuid.c
       3             :  */
       4             : #include "postgres.h"
       5             : 
       6             : #include "btree_gist.h"
       7             : #include "btree_utils_num.h"
       8             : #include "port/pg_bswap.h"
       9             : #include "utils/rel.h"
      10             : #include "utils/sortsupport.h"
      11             : #include "utils/uuid.h"
      12             : 
      13             : typedef struct
      14             : {
      15             :     pg_uuid_t   lower,
      16             :                 upper;
      17             : } uuidKEY;
      18             : 
      19             : 
      20             : /* GiST support functions */
      21           8 : PG_FUNCTION_INFO_V1(gbt_uuid_compress);
      22           8 : PG_FUNCTION_INFO_V1(gbt_uuid_fetch);
      23           8 : PG_FUNCTION_INFO_V1(gbt_uuid_union);
      24           8 : PG_FUNCTION_INFO_V1(gbt_uuid_picksplit);
      25           8 : PG_FUNCTION_INFO_V1(gbt_uuid_consistent);
      26           8 : PG_FUNCTION_INFO_V1(gbt_uuid_penalty);
      27           8 : PG_FUNCTION_INFO_V1(gbt_uuid_same);
      28           8 : PG_FUNCTION_INFO_V1(gbt_uuid_sortsupport);
      29             : 
      30             : 
      31             : static int
      32       24668 : uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
      33             : {
      34       24668 :     return memcmp(arg1->data, arg2->data, UUID_LEN);
      35             : }
      36             : 
      37             : static bool
      38        4206 : gbt_uuidgt(const void *a, const void *b, FmgrInfo *flinfo)
      39             : {
      40        4206 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) > 0;
      41             : }
      42             : 
      43             : static bool
      44         618 : gbt_uuidge(const void *a, const void *b, FmgrInfo *flinfo)
      45             : {
      46         618 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) >= 0;
      47             : }
      48             : 
      49             : static bool
      50         302 : gbt_uuideq(const void *a, const void *b, FmgrInfo *flinfo)
      51             : {
      52         302 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) == 0;
      53             : }
      54             : 
      55             : static bool
      56         934 : gbt_uuidle(const void *a, const void *b, FmgrInfo *flinfo)
      57             : {
      58         934 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) <= 0;
      59             : }
      60             : 
      61             : static bool
      62        4510 : gbt_uuidlt(const void *a, const void *b, FmgrInfo *flinfo)
      63             : {
      64        4510 :     return uuid_internal_cmp((const pg_uuid_t *) a, (const pg_uuid_t *) b) < 0;
      65             : }
      66             : 
      67             : static int
      68        2406 : gbt_uuidkey_cmp(const void *a, const void *b, FmgrInfo *flinfo)
      69             : {
      70        2406 :     uuidKEY    *ia = (uuidKEY *) (((const Nsrt *) a)->t);
      71        2406 :     uuidKEY    *ib = (uuidKEY *) (((const Nsrt *) b)->t);
      72             :     int         res;
      73             : 
      74        2406 :     res = uuid_internal_cmp(&ia->lower, &ib->lower);
      75        2406 :     if (res == 0)
      76           0 :         res = uuid_internal_cmp(&ia->upper, &ib->upper);
      77        2406 :     return res;
      78             : }
      79             : 
      80             : 
      81             : static const gbtree_ninfo tinfo =
      82             : {
      83             :     gbt_t_uuid,
      84             :     UUID_LEN,
      85             :     32,                         /* sizeof(gbtreekey32) */
      86             :     gbt_uuidgt,
      87             :     gbt_uuidge,
      88             :     gbt_uuideq,
      89             :     gbt_uuidle,
      90             :     gbt_uuidlt,
      91             :     gbt_uuidkey_cmp,
      92             :     NULL
      93             : };
      94             : 
      95             : 
      96             : /**************************************************
      97             :  * GiST support functions
      98             :  **************************************************/
      99             : 
     100             : Datum
     101        1214 : gbt_uuid_compress(PG_FUNCTION_ARGS)
     102             : {
     103        1214 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     104             :     GISTENTRY  *retval;
     105             : 
     106        1214 :     if (entry->leafkey)
     107             :     {
     108        1206 :         char       *r = (char *) palloc(2 * UUID_LEN);
     109        1206 :         pg_uuid_t  *key = DatumGetUUIDP(entry->key);
     110             : 
     111        1206 :         retval = palloc_object(GISTENTRY);
     112             : 
     113        1206 :         memcpy(r, key, UUID_LEN);
     114        1206 :         memcpy(r + UUID_LEN, key, UUID_LEN);
     115        1206 :         gistentryinit(*retval, PointerGetDatum(r),
     116             :                       entry->rel, entry->page,
     117             :                       entry->offset, false);
     118             :     }
     119             :     else
     120           8 :         retval = entry;
     121             : 
     122        1214 :     PG_RETURN_POINTER(retval);
     123             : }
     124             : 
     125             : Datum
     126           0 : gbt_uuid_fetch(PG_FUNCTION_ARGS)
     127             : {
     128           0 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     129             : 
     130           0 :     PG_RETURN_POINTER(gbt_num_fetch(entry, &tinfo));
     131             : }
     132             : 
     133             : Datum
     134        3358 : gbt_uuid_consistent(PG_FUNCTION_ARGS)
     135             : {
     136        3358 :     GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
     137        3358 :     pg_uuid_t  *query = PG_GETARG_UUID_P(1);
     138        3358 :     StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
     139             : #ifdef NOT_USED
     140             :     Oid         subtype = PG_GETARG_OID(3);
     141             : #endif
     142        3358 :     bool       *recheck = (bool *) PG_GETARG_POINTER(4);
     143        3358 :     uuidKEY    *kkk = (uuidKEY *) DatumGetPointer(entry->key);
     144             :     GBT_NUMKEY_R key;
     145             : 
     146             :     /* All cases served by this function are exact */
     147        3358 :     *recheck = false;
     148             : 
     149        3358 :     key.lower = (GBT_NUMKEY *) &kkk->lower;
     150        3358 :     key.upper = (GBT_NUMKEY *) &kkk->upper;
     151             : 
     152        3358 :     PG_RETURN_BOOL(gbt_num_consistent(&key, query, &strategy,
     153             :                                       GIST_LEAF(entry), &tinfo,
     154             :                                       fcinfo->flinfo));
     155             : }
     156             : 
     157             : Datum
     158           2 : gbt_uuid_union(PG_FUNCTION_ARGS)
     159             : {
     160           2 :     GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
     161           2 :     void       *out = palloc(sizeof(uuidKEY));
     162             : 
     163           2 :     *(int *) PG_GETARG_POINTER(1) = sizeof(uuidKEY);
     164           2 :     PG_RETURN_POINTER(gbt_num_union(out, entryvec, &tinfo, fcinfo->flinfo));
     165             : }
     166             : 
     167             : /*
     168             :  * Convert a uuid to a "double" value for estimating sizes of ranges.
     169             :  */
     170             : static double
     171           0 : uuid_2_double(const pg_uuid_t *u)
     172             : {
     173             :     uint64      uu[2];
     174           0 :     const double two64 = 18446744073709551616.0;    /* 2^64 */
     175             : 
     176             :     /* Source data may not be suitably aligned, so copy */
     177           0 :     memcpy(uu, u->data, UUID_LEN);
     178             : 
     179             :     /*
     180             :      * uuid values should be considered as big-endian numbers, since that
     181             :      * corresponds to how memcmp will compare them.  On a little-endian
     182             :      * machine, byte-swap each half so we can use native uint64 arithmetic.
     183             :      */
     184             : #ifndef WORDS_BIGENDIAN
     185           0 :     uu[0] = pg_bswap64(uu[0]);
     186           0 :     uu[1] = pg_bswap64(uu[1]);
     187             : #endif
     188             : 
     189             :     /*
     190             :      * 2^128 is about 3.4e38, which in theory could exceed the range of
     191             :      * "double" (POSIX only requires 1e37).  To avoid any risk of overflow,
     192             :      * put the decimal point between the two halves rather than treating the
     193             :      * uuid value as a 128-bit integer.
     194             :      */
     195           0 :     return (double) uu[0] + (double) uu[1] / two64;
     196             : }
     197             : 
     198             : Datum
     199           0 : gbt_uuid_penalty(PG_FUNCTION_ARGS)
     200             : {
     201           0 :     uuidKEY    *origentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
     202           0 :     uuidKEY    *newentry = (uuidKEY *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(1))->key);
     203           0 :     float      *result = (float *) PG_GETARG_POINTER(2);
     204             :     double      olower,
     205             :                 oupper,
     206             :                 nlower,
     207             :                 nupper;
     208             : 
     209           0 :     olower = uuid_2_double(&origentry->lower);
     210           0 :     oupper = uuid_2_double(&origentry->upper);
     211           0 :     nlower = uuid_2_double(&newentry->lower);
     212           0 :     nupper = uuid_2_double(&newentry->upper);
     213             : 
     214           0 :     penalty_num(result, olower, oupper, nlower, nupper);
     215             : 
     216           0 :     PG_RETURN_POINTER(result);
     217             : }
     218             : 
     219             : Datum
     220           6 : gbt_uuid_picksplit(PG_FUNCTION_ARGS)
     221             : {
     222           6 :     PG_RETURN_POINTER(gbt_num_picksplit((GistEntryVector *) PG_GETARG_POINTER(0),
     223             :                                         (GIST_SPLITVEC *) PG_GETARG_POINTER(1),
     224             :                                         &tinfo, fcinfo->flinfo));
     225             : }
     226             : 
     227             : Datum
     228           0 : gbt_uuid_same(PG_FUNCTION_ARGS)
     229             : {
     230           0 :     uuidKEY    *b1 = (uuidKEY *) PG_GETARG_POINTER(0);
     231           0 :     uuidKEY    *b2 = (uuidKEY *) PG_GETARG_POINTER(1);
     232           0 :     bool       *result = (bool *) PG_GETARG_POINTER(2);
     233             : 
     234           0 :     *result = gbt_num_same((void *) b1, (void *) b2, &tinfo, fcinfo->flinfo);
     235           0 :     PG_RETURN_POINTER(result);
     236             : }
     237             : 
     238             : static int
     239       11692 : gbt_uuid_ssup_cmp(Datum x, Datum y, SortSupport ssup)
     240             : {
     241       11692 :     uuidKEY    *arg1 = (uuidKEY *) DatumGetPointer(x);
     242       11692 :     uuidKEY    *arg2 = (uuidKEY *) DatumGetPointer(y);
     243             : 
     244             :     /* for leaf items we expect lower == upper, so only compare lower */
     245       11692 :     return uuid_internal_cmp(&arg1->lower, &arg2->lower);
     246             : }
     247             : 
     248             : Datum
     249           2 : gbt_uuid_sortsupport(PG_FUNCTION_ARGS)
     250             : {
     251           2 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     252             : 
     253           2 :     ssup->comparator = gbt_uuid_ssup_cmp;
     254           2 :     ssup->ssup_extra = NULL;
     255             : 
     256           2 :     PG_RETURN_VOID();
     257             : }

Generated by: LCOV version 1.16