LCOV - code coverage report
Current view: top level - src/backend/utils/adt - uuid.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 197 216 91.2 %
Date: 2025-01-18 05:15:39 Functions: 25 27 92.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * uuid.c
       4             :  *    Functions for the built-in type "uuid".
       5             :  *
       6             :  * Copyright (c) 2007-2025, PostgreSQL Global Development Group
       7             :  *
       8             :  * IDENTIFICATION
       9             :  *    src/backend/utils/adt/uuid.c
      10             :  *
      11             :  *-------------------------------------------------------------------------
      12             :  */
      13             : 
      14             : #include "postgres.h"
      15             : 
      16             : #include <time.h>             /* for clock_gettime() */
      17             : 
      18             : #include "common/hashfn.h"
      19             : #include "lib/hyperloglog.h"
      20             : #include "libpq/pqformat.h"
      21             : #include "port/pg_bswap.h"
      22             : #include "utils/fmgrprotos.h"
      23             : #include "utils/guc.h"
      24             : #include "utils/sortsupport.h"
      25             : #include "utils/timestamp.h"
      26             : #include "utils/uuid.h"
      27             : 
      28             : /* helper macros */
      29             : #define NS_PER_S    INT64CONST(1000000000)
      30             : #define NS_PER_MS   INT64CONST(1000000)
      31             : #define NS_PER_US   INT64CONST(1000)
      32             : 
      33             : /*
      34             :  * UUID version 7 uses 12 bits in "rand_a" to store  1/4096 (or 2^12) fractions of
      35             :  * sub-millisecond. While most Unix-like platforms provide nanosecond-precision
      36             :  * timestamps, some systems only offer microsecond precision, limiting us to 10
      37             :  * bits of sub-millisecond information. For example, on macOS, real time is
      38             :  * truncated to microseconds. Additionally, MSVC uses the ported version of
      39             :  * gettimeofday() that returns microsecond precision.
      40             :  *
      41             :  * On systems with only 10 bits of sub-millisecond precision, we still use
      42             :  * 1/4096 parts of a millisecond, but fill lower 2 bits with random numbers
      43             :  * (see generate_uuidv7() for details).
      44             :  *
      45             :  * SUBMS_MINIMAL_STEP_NS defines the minimum number of nanoseconds that guarantees
      46             :  * an increase in the UUID's clock precision.
      47             :  */
      48             : #if defined(__darwin__) || defined(_MSC_VER)
      49             : #define SUBMS_MINIMAL_STEP_BITS 10
      50             : #else
      51             : #define SUBMS_MINIMAL_STEP_BITS 12
      52             : #endif
      53             : #define SUBMS_BITS  12
      54             : #define SUBMS_MINIMAL_STEP_NS ((NS_PER_MS / (1 << SUBMS_MINIMAL_STEP_BITS)) + 1)
      55             : 
      56             : /* sortsupport for uuid */
      57             : typedef struct
      58             : {
      59             :     int64       input_count;    /* number of non-null values seen */
      60             :     bool        estimating;     /* true if estimating cardinality */
      61             : 
      62             :     hyperLogLogState abbr_card; /* cardinality estimator */
      63             : } uuid_sortsupport_state;
      64             : 
      65             : static void string_to_uuid(const char *source, pg_uuid_t *uuid, Node *escontext);
      66             : static int  uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2);
      67             : static int  uuid_fast_cmp(Datum x, Datum y, SortSupport ssup);
      68             : static bool uuid_abbrev_abort(int memtupcount, SortSupport ssup);
      69             : static Datum uuid_abbrev_convert(Datum original, SortSupport ssup);
      70             : static inline void uuid_set_version(pg_uuid_t *uuid, unsigned char version);
      71             : static inline int64 get_real_time_ns_ascending();
      72             : 
      73             : Datum
      74      585920 : uuid_in(PG_FUNCTION_ARGS)
      75             : {
      76      585920 :     char       *uuid_str = PG_GETARG_CSTRING(0);
      77             :     pg_uuid_t  *uuid;
      78             : 
      79      585920 :     uuid = (pg_uuid_t *) palloc(sizeof(*uuid));
      80      585920 :     string_to_uuid(uuid_str, uuid, fcinfo->context);
      81      585884 :     PG_RETURN_UUID_P(uuid);
      82             : }
      83             : 
      84             : Datum
      85        4704 : uuid_out(PG_FUNCTION_ARGS)
      86             : {
      87        4704 :     pg_uuid_t  *uuid = PG_GETARG_UUID_P(0);
      88             :     static const char hex_chars[] = "0123456789abcdef";
      89             :     char       *buf,
      90             :                *p;
      91             :     int         i;
      92             : 
      93             :     /* counts for the four hyphens and the zero-terminator */
      94        4704 :     buf = palloc(2 * UUID_LEN + 5);
      95        4704 :     p = buf;
      96       79968 :     for (i = 0; i < UUID_LEN; i++)
      97             :     {
      98             :         int         hi;
      99             :         int         lo;
     100             : 
     101             :         /*
     102             :          * We print uuid values as a string of 8, 4, 4, 4, and then 12
     103             :          * hexadecimal characters, with each group is separated by a hyphen
     104             :          * ("-"). Therefore, add the hyphens at the appropriate places here.
     105             :          */
     106       75264 :         if (i == 4 || i == 6 || i == 8 || i == 10)
     107       18816 :             *p++ = '-';
     108             : 
     109       75264 :         hi = uuid->data[i] >> 4;
     110       75264 :         lo = uuid->data[i] & 0x0F;
     111             : 
     112       75264 :         *p++ = hex_chars[hi];
     113       75264 :         *p++ = hex_chars[lo];
     114             :     }
     115        4704 :     *p = '\0';
     116             : 
     117        4704 :     PG_RETURN_CSTRING(buf);
     118             : }
     119             : 
     120             : /*
     121             :  * We allow UUIDs as a series of 32 hexadecimal digits with an optional dash
     122             :  * after each group of 4 hexadecimal digits, and optionally surrounded by {}.
     123             :  * (The canonical format 8x-4x-4x-4x-12x, where "nx" means n hexadecimal
     124             :  * digits, is the only one used for output.)
     125             :  */
     126             : static void
     127      585920 : string_to_uuid(const char *source, pg_uuid_t *uuid, Node *escontext)
     128             : {
     129      585920 :     const char *src = source;
     130      585920 :     bool        braces = false;
     131             :     int         i;
     132             : 
     133      585920 :     if (src[0] == '{')
     134             :     {
     135          24 :         src++;
     136          24 :         braces = true;
     137             :     }
     138             : 
     139     9960226 :     for (i = 0; i < UUID_LEN; i++)
     140             :     {
     141             :         char        str_buf[3];
     142             : 
     143     9374342 :         if (src[0] == '\0' || src[1] == '\0')
     144          36 :             goto syntax_error;
     145     9374330 :         memcpy(str_buf, src, 2);
     146     9374330 :         if (!isxdigit((unsigned char) str_buf[0]) ||
     147     9374318 :             !isxdigit((unsigned char) str_buf[1]))
     148          24 :             goto syntax_error;
     149             : 
     150     9374306 :         str_buf[2] = '\0';
     151     9374306 :         uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
     152     9374306 :         src += 2;
     153     9374306 :         if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1)
     154     1935434 :             src++;
     155             :     }
     156             : 
     157      585884 :     if (braces)
     158             :     {
     159          18 :         if (*src != '}')
     160           6 :             goto syntax_error;
     161          12 :         src++;
     162             :     }
     163             : 
     164      585878 :     if (*src != '\0')
     165           6 :         goto syntax_error;
     166             : 
     167      585872 :     return;
     168             : 
     169          48 : syntax_error:
     170          48 :     ereturn(escontext,,
     171             :             (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
     172             :              errmsg("invalid input syntax for type %s: \"%s\"",
     173             :                     "uuid", source)));
     174             : }
     175             : 
     176             : Datum
     177           0 : uuid_recv(PG_FUNCTION_ARGS)
     178             : {
     179           0 :     StringInfo  buffer = (StringInfo) PG_GETARG_POINTER(0);
     180             :     pg_uuid_t  *uuid;
     181             : 
     182           0 :     uuid = (pg_uuid_t *) palloc(UUID_LEN);
     183           0 :     memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN);
     184           0 :     PG_RETURN_POINTER(uuid);
     185             : }
     186             : 
     187             : Datum
     188           0 : uuid_send(PG_FUNCTION_ARGS)
     189             : {
     190           0 :     pg_uuid_t  *uuid = PG_GETARG_UUID_P(0);
     191             :     StringInfoData buffer;
     192             : 
     193           0 :     pq_begintypsend(&buffer);
     194           0 :     pq_sendbytes(&buffer, uuid->data, UUID_LEN);
     195           0 :     PG_RETURN_BYTEA_P(pq_endtypsend(&buffer));
     196             : }
     197             : 
     198             : /* internal uuid compare function */
     199             : static int
     200    41777838 : uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
     201             : {
     202    41777838 :     return memcmp(arg1->data, arg2->data, UUID_LEN);
     203             : }
     204             : 
     205             : Datum
     206       83806 : uuid_lt(PG_FUNCTION_ARGS)
     207             : {
     208       83806 :     pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
     209       83806 :     pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
     210             : 
     211       83806 :     PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) < 0);
     212             : }
     213             : 
     214             : Datum
     215       17046 : uuid_le(PG_FUNCTION_ARGS)
     216             : {
     217       17046 :     pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
     218       17046 :     pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
     219             : 
     220       17046 :     PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) <= 0);
     221             : }
     222             : 
     223             : Datum
     224      154444 : uuid_eq(PG_FUNCTION_ARGS)
     225             : {
     226      154444 :     pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
     227      154444 :     pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
     228             : 
     229      154444 :     PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) == 0);
     230             : }
     231             : 
     232             : Datum
     233       12554 : uuid_ge(PG_FUNCTION_ARGS)
     234             : {
     235       12554 :     pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
     236       12554 :     pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
     237             : 
     238       12554 :     PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) >= 0);
     239             : }
     240             : 
     241             : Datum
     242       16286 : uuid_gt(PG_FUNCTION_ARGS)
     243             : {
     244       16286 :     pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
     245       16286 :     pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
     246             : 
     247       16286 :     PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) > 0);
     248             : }
     249             : 
     250             : Datum
     251          18 : uuid_ne(PG_FUNCTION_ARGS)
     252             : {
     253          18 :     pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
     254          18 :     pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
     255             : 
     256          18 :     PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) != 0);
     257             : }
     258             : 
     259             : /* handler for btree index operator */
     260             : Datum
     261        9308 : uuid_cmp(PG_FUNCTION_ARGS)
     262             : {
     263        9308 :     pg_uuid_t  *arg1 = PG_GETARG_UUID_P(0);
     264        9308 :     pg_uuid_t  *arg2 = PG_GETARG_UUID_P(1);
     265             : 
     266        9308 :     PG_RETURN_INT32(uuid_internal_cmp(arg1, arg2));
     267             : }
     268             : 
     269             : /*
     270             :  * Sort support strategy routine
     271             :  */
     272             : Datum
     273         388 : uuid_sortsupport(PG_FUNCTION_ARGS)
     274             : {
     275         388 :     SortSupport ssup = (SortSupport) PG_GETARG_POINTER(0);
     276             : 
     277         388 :     ssup->comparator = uuid_fast_cmp;
     278         388 :     ssup->ssup_extra = NULL;
     279             : 
     280         388 :     if (ssup->abbreviate)
     281             :     {
     282             :         uuid_sortsupport_state *uss;
     283             :         MemoryContext oldcontext;
     284             : 
     285         308 :         oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
     286             : 
     287         308 :         uss = palloc(sizeof(uuid_sortsupport_state));
     288         308 :         uss->input_count = 0;
     289         308 :         uss->estimating = true;
     290         308 :         initHyperLogLog(&uss->abbr_card, 10);
     291             : 
     292         308 :         ssup->ssup_extra = uss;
     293             : 
     294         308 :         ssup->comparator = ssup_datum_unsigned_cmp;
     295         308 :         ssup->abbrev_converter = uuid_abbrev_convert;
     296         308 :         ssup->abbrev_abort = uuid_abbrev_abort;
     297         308 :         ssup->abbrev_full_comparator = uuid_fast_cmp;
     298             : 
     299         308 :         MemoryContextSwitchTo(oldcontext);
     300             :     }
     301             : 
     302         388 :     PG_RETURN_VOID();
     303             : }
     304             : 
     305             : /*
     306             :  * SortSupport comparison func
     307             :  */
     308             : static int
     309    41484376 : uuid_fast_cmp(Datum x, Datum y, SortSupport ssup)
     310             : {
     311    41484376 :     pg_uuid_t  *arg1 = DatumGetUUIDP(x);
     312    41484376 :     pg_uuid_t  *arg2 = DatumGetUUIDP(y);
     313             : 
     314    41484376 :     return uuid_internal_cmp(arg1, arg2);
     315             : }
     316             : 
     317             : /*
     318             :  * Callback for estimating effectiveness of abbreviated key optimization.
     319             :  *
     320             :  * We pay no attention to the cardinality of the non-abbreviated data, because
     321             :  * there is no equality fast-path within authoritative uuid comparator.
     322             :  */
     323             : static bool
     324        2322 : uuid_abbrev_abort(int memtupcount, SortSupport ssup)
     325             : {
     326        2322 :     uuid_sortsupport_state *uss = ssup->ssup_extra;
     327             :     double      abbr_card;
     328             : 
     329        2322 :     if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
     330        2130 :         return false;
     331             : 
     332         192 :     abbr_card = estimateHyperLogLog(&uss->abbr_card);
     333             : 
     334             :     /*
     335             :      * If we have >100k distinct values, then even if we were sorting many
     336             :      * billion rows we'd likely still break even, and the penalty of undoing
     337             :      * that many rows of abbrevs would probably not be worth it.  Stop even
     338             :      * counting at that point.
     339             :      */
     340         192 :     if (abbr_card > 100000.0)
     341             :     {
     342           0 :         if (trace_sort)
     343           0 :             elog(LOG,
     344             :                  "uuid_abbrev: estimation ends at cardinality %f"
     345             :                  " after " INT64_FORMAT " values (%d rows)",
     346             :                  abbr_card, uss->input_count, memtupcount);
     347           0 :         uss->estimating = false;
     348           0 :         return false;
     349             :     }
     350             : 
     351             :     /*
     352             :      * Target minimum cardinality is 1 per ~2k of non-null inputs.  0.5 row
     353             :      * fudge factor allows us to abort earlier on genuinely pathological data
     354             :      * where we've had exactly one abbreviated value in the first 2k
     355             :      * (non-null) rows.
     356             :      */
     357         192 :     if (abbr_card < uss->input_count / 2000.0 + 0.5)
     358             :     {
     359          96 :         if (trace_sort)
     360           0 :             elog(LOG,
     361             :                  "uuid_abbrev: aborting abbreviation at cardinality %f"
     362             :                  " below threshold %f after " INT64_FORMAT " values (%d rows)",
     363             :                  abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
     364             :                  memtupcount);
     365          96 :         return true;
     366             :     }
     367             : 
     368          96 :     if (trace_sort)
     369           0 :         elog(LOG,
     370             :              "uuid_abbrev: cardinality %f after " INT64_FORMAT
     371             :              " values (%d rows)", abbr_card, uss->input_count, memtupcount);
     372             : 
     373          96 :     return false;
     374             : }
     375             : 
     376             : /*
     377             :  * Conversion routine for sortsupport.  Converts original uuid representation
     378             :  * to abbreviated key representation.  Our encoding strategy is simple -- pack
     379             :  * the first `sizeof(Datum)` bytes of uuid data into a Datum (on little-endian
     380             :  * machines, the bytes are stored in reverse order), and treat it as an
     381             :  * unsigned integer.
     382             :  */
     383             : static Datum
     384     3384150 : uuid_abbrev_convert(Datum original, SortSupport ssup)
     385             : {
     386     3384150 :     uuid_sortsupport_state *uss = ssup->ssup_extra;
     387     3384150 :     pg_uuid_t  *authoritative = DatumGetUUIDP(original);
     388             :     Datum       res;
     389             : 
     390     3384150 :     memcpy(&res, authoritative->data, sizeof(Datum));
     391     3384150 :     uss->input_count += 1;
     392             : 
     393     3384150 :     if (uss->estimating)
     394             :     {
     395             :         uint32      tmp;
     396             : 
     397             : #if SIZEOF_DATUM == 8
     398     3384150 :         tmp = (uint32) res ^ (uint32) ((uint64) res >> 32);
     399             : #else                           /* SIZEOF_DATUM != 8 */
     400             :         tmp = (uint32) res;
     401             : #endif
     402             : 
     403     3384150 :         addHyperLogLog(&uss->abbr_card, DatumGetUInt32(hash_uint32(tmp)));
     404             :     }
     405             : 
     406             :     /*
     407             :      * Byteswap on little-endian machines.
     408             :      *
     409             :      * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
     410             :      * 3-way comparator) works correctly on all platforms.  If we didn't do
     411             :      * this, the comparator would have to call memcmp() with a pair of
     412             :      * pointers to the first byte of each abbreviated key, which is slower.
     413             :      */
     414     3384150 :     res = DatumBigEndianToNative(res);
     415             : 
     416     3384150 :     return res;
     417             : }
     418             : 
     419             : /* hash index support */
     420             : Datum
     421        2426 : uuid_hash(PG_FUNCTION_ARGS)
     422             : {
     423        2426 :     pg_uuid_t  *key = PG_GETARG_UUID_P(0);
     424             : 
     425        2426 :     return hash_any(key->data, UUID_LEN);
     426             : }
     427             : 
     428             : Datum
     429          60 : uuid_hash_extended(PG_FUNCTION_ARGS)
     430             : {
     431          60 :     pg_uuid_t  *key = PG_GETARG_UUID_P(0);
     432             : 
     433          60 :     return hash_any_extended(key->data, UUID_LEN, PG_GETARG_INT64(1));
     434             : }
     435             : 
     436             : /*
     437             :  * Set the given UUID version and the variant bits
     438             :  */
     439             : static inline void
     440         126 : uuid_set_version(pg_uuid_t *uuid, unsigned char version)
     441             : {
     442             :     /* set version field, top four bits */
     443         126 :     uuid->data[6] = (uuid->data[6] & 0x0f) | (version << 4);
     444             : 
     445             :     /* set variant field, top two bits are 1, 0 */
     446         126 :     uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80;
     447         126 : }
     448             : 
     449             : /*
     450             :  * Generate UUID version 4.
     451             :  *
     452             :  * All UUID bytes are filled with strong random numbers except version and
     453             :  * variant bits.
     454             :  */
     455             : Datum
     456          42 : gen_random_uuid(PG_FUNCTION_ARGS)
     457             : {
     458          42 :     pg_uuid_t  *uuid = palloc(UUID_LEN);
     459             : 
     460          42 :     if (!pg_strong_random(uuid, UUID_LEN))
     461           0 :         ereport(ERROR,
     462             :                 (errcode(ERRCODE_INTERNAL_ERROR),
     463             :                  errmsg("could not generate random values")));
     464             : 
     465             :     /*
     466             :      * Set magic numbers for a "version 4" (pseudorandom) UUID and variant,
     467             :      * see https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4
     468             :      */
     469          42 :     uuid_set_version(uuid, 4);
     470             : 
     471          42 :     PG_RETURN_UUID_P(uuid);
     472             : }
     473             : 
     474             : /*
     475             :  * Get the current timestamp with nanosecond precision for UUID generation.
     476             :  * The returned timestamp is ensured to be at least SUBMS_MINIMAL_STEP greater
     477             :  * than the previous returned timestamp (on this backend).
     478             :  */
     479             : static inline int64
     480          84 : get_real_time_ns_ascending()
     481             : {
     482             :     static int64 previous_ns = 0;
     483             :     int64       ns;
     484             : 
     485             :     /* Get the current real timestamp */
     486             : 
     487             : #ifdef  _MSC_VER
     488             :     struct timeval tmp;
     489             : 
     490             :     gettimeofday(&tmp, NULL);
     491             :     ns = tmp.tv_sec * NS_PER_S + tmp.tv_usec * NS_PER_US;
     492             : #else
     493             :     struct timespec tmp;
     494             : 
     495             :     /*
     496             :      * We don't use gettimeofday(), instead use clock_gettime() with
     497             :      * CLOCK_REALTIME where available in order to get a high-precision
     498             :      * (nanoseconds) real timestamp.
     499             :      *
     500             :      * Note while a timestamp returned by clock_gettime() with CLOCK_REALTIME
     501             :      * is nanosecond-precision on most Unix-like platforms, on some platforms
     502             :      * such as macOS it's restricted to microsecond-precision.
     503             :      */
     504          84 :     clock_gettime(CLOCK_REALTIME, &tmp);
     505          84 :     ns = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;
     506             : #endif
     507             : 
     508             :     /* Guarantee the minimal step advancement of the timestamp */
     509          84 :     if (previous_ns + SUBMS_MINIMAL_STEP_NS >= ns)
     510           0 :         ns = previous_ns + SUBMS_MINIMAL_STEP_NS;
     511          84 :     previous_ns = ns;
     512             : 
     513          84 :     return ns;
     514             : }
     515             : 
     516             : /*
     517             :  * Generate UUID version 7 per RFC 9562, with the given timestamp.
     518             :  *
     519             :  * UUID version 7 consists of a Unix timestamp in milliseconds (48 bits) and
     520             :  * 74 random bits, excluding the required version and variant bits. To ensure
     521             :  * monotonicity in scenarios of high-frequency UUID generation, we employ the
     522             :  * method "Replace Leftmost Random Bits with Increased Clock Precision (Method 3)",
     523             :  * described in the RFC. This method utilizes 12 bits from the "rand_a" bits
     524             :  * to store a 1/4096 (or 2^12) fraction of sub-millisecond precision.
     525             :  *
     526             :  * ns is a number of nanoseconds since start of the UNIX epoch. This value is
     527             :  * used for time-dependent bits of UUID.
     528             :  */
     529             : static pg_uuid_t *
     530          84 : generate_uuidv7(int64 ns)
     531             : {
     532          84 :     pg_uuid_t  *uuid = palloc(UUID_LEN);
     533             :     int64       unix_ts_ms;
     534             :     int32       increased_clock_precision;
     535             : 
     536          84 :     unix_ts_ms = ns / NS_PER_MS;
     537             : 
     538             :     /* Fill in time part */
     539          84 :     uuid->data[0] = (unsigned char) (unix_ts_ms >> 40);
     540          84 :     uuid->data[1] = (unsigned char) (unix_ts_ms >> 32);
     541          84 :     uuid->data[2] = (unsigned char) (unix_ts_ms >> 24);
     542          84 :     uuid->data[3] = (unsigned char) (unix_ts_ms >> 16);
     543          84 :     uuid->data[4] = (unsigned char) (unix_ts_ms >> 8);
     544          84 :     uuid->data[5] = (unsigned char) unix_ts_ms;
     545             : 
     546             :     /*
     547             :      * sub-millisecond timestamp fraction (SUBMS_BITS bits, not
     548             :      * SUBMS_MINIMAL_STEP_BITS)
     549             :      */
     550          84 :     increased_clock_precision = ((ns % NS_PER_MS) * (1 << SUBMS_BITS)) / NS_PER_MS;
     551             : 
     552             :     /* Fill the increased clock precision to "rand_a" bits */
     553          84 :     uuid->data[6] = (unsigned char) (increased_clock_precision >> 8);
     554          84 :     uuid->data[7] = (unsigned char) (increased_clock_precision);
     555             : 
     556             :     /* fill everything after the increased clock precision with random bytes */
     557          84 :     if (!pg_strong_random(&uuid->data[8], UUID_LEN - 8))
     558           0 :         ereport(ERROR,
     559             :                 (errcode(ERRCODE_INTERNAL_ERROR),
     560             :                  errmsg("could not generate random values")));
     561             : 
     562             : #if SUBMS_MINIMAL_STEP_BITS == 10
     563             : 
     564             :     /*
     565             :      * On systems that have only 10 bits of sub-ms precision,  2 least
     566             :      * significant are dependent on other time-specific bits, and they do not
     567             :      * contribute to uniqueness. To make these bit random we mix in two bits
     568             :      * from CSPRNG. SUBMS_MINIMAL_STEP is chosen so that we still guarantee
     569             :      * monotonicity despite altering these bits.
     570             :      */
     571             :     uuid->data[7] = uuid->data[7] ^ (uuid->data[8] >> 6);
     572             : #endif
     573             : 
     574             :     /*
     575             :      * Set magic numbers for a "version 7" (pseudorandom) UUID and variant,
     576             :      * see https://www.rfc-editor.org/rfc/rfc9562#name-version-field
     577             :      */
     578          84 :     uuid_set_version(uuid, 7);
     579             : 
     580          84 :     return uuid;
     581             : }
     582             : 
     583             : /*
     584             :  * Generate UUID version 7 with the current timestamp.
     585             :  */
     586             : Datum
     587          78 : uuidv7(PG_FUNCTION_ARGS)
     588             : {
     589          78 :     pg_uuid_t  *uuid = generate_uuidv7(get_real_time_ns_ascending());
     590             : 
     591          78 :     PG_RETURN_UUID_P(uuid);
     592             : }
     593             : 
     594             : /*
     595             :  * Similar to uuidv7() but with the timestamp adjusted by the given interval.
     596             :  */
     597             : Datum
     598           6 : uuidv7_interval(PG_FUNCTION_ARGS)
     599             : {
     600           6 :     Interval   *shift = PG_GETARG_INTERVAL_P(0);
     601             :     TimestampTz ts;
     602             :     pg_uuid_t  *uuid;
     603           6 :     int64       ns = get_real_time_ns_ascending();
     604             : 
     605             :     /*
     606             :      * Shift the current timestamp by the given interval. To calculate time
     607             :      * shift correctly, we convert the UNIX epoch to TimestampTz and use
     608             :      * timestamptz_pl_interval(). Since this calculation is done with
     609             :      * microsecond precision, we carry nanoseconds from original ns value to
     610             :      * shifted ns value.
     611             :      */
     612             : 
     613           6 :     ts = (TimestampTz) (ns / NS_PER_US) -
     614             :         (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY * USECS_PER_SEC;
     615             : 
     616             :     /* Compute time shift */
     617           6 :     ts = DatumGetTimestampTz(DirectFunctionCall2(timestamptz_pl_interval,
     618             :                                                  TimestampTzGetDatum(ts),
     619             :                                                  IntervalPGetDatum(shift)));
     620             : 
     621             :     /*
     622             :      * Convert a TimestampTz value back to an UNIX epoch and back nanoseconds.
     623             :      */
     624           6 :     ns = (ts + (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY * USECS_PER_SEC)
     625           6 :         * NS_PER_US + ns % NS_PER_US;
     626             : 
     627             :     /* Generate an UUIDv7 */
     628           6 :     uuid = generate_uuidv7(ns);
     629             : 
     630           6 :     PG_RETURN_UUID_P(uuid);
     631             : }
     632             : 
     633             : /*
     634             :  * Start of a Gregorian epoch == date2j(1582,10,15)
     635             :  * We cast it to 64-bit because it's used in overflow-prone computations
     636             :  */
     637             : #define GREGORIAN_EPOCH_JDATE  INT64CONST(2299161)
     638             : 
     639             : /*
     640             :  * Extract timestamp from UUID.
     641             :  *
     642             :  * Returns null if not RFC 9562 variant or not a version that has a timestamp.
     643             :  */
     644             : Datum
     645          24 : uuid_extract_timestamp(PG_FUNCTION_ARGS)
     646             : {
     647          24 :     pg_uuid_t  *uuid = PG_GETARG_UUID_P(0);
     648             :     int         version;
     649             :     uint64      tms;
     650             :     TimestampTz ts;
     651             : 
     652             :     /* check if RFC 9562 variant */
     653          24 :     if ((uuid->data[8] & 0xc0) != 0x80)
     654           6 :         PG_RETURN_NULL();
     655             : 
     656          18 :     version = uuid->data[6] >> 4;
     657             : 
     658          18 :     if (version == 1)
     659             :     {
     660           6 :         tms = ((uint64) uuid->data[0] << 24)
     661           6 :             + ((uint64) uuid->data[1] << 16)
     662           6 :             + ((uint64) uuid->data[2] << 8)
     663           6 :             + ((uint64) uuid->data[3])
     664           6 :             + ((uint64) uuid->data[4] << 40)
     665           6 :             + ((uint64) uuid->data[5] << 32)
     666           6 :             + (((uint64) uuid->data[6] & 0xf) << 56)
     667           6 :             + ((uint64) uuid->data[7] << 48);
     668             : 
     669             :         /* convert 100-ns intervals to us, then adjust */
     670           6 :         ts = (TimestampTz) (tms / 10) -
     671             :             ((uint64) POSTGRES_EPOCH_JDATE - GREGORIAN_EPOCH_JDATE) * SECS_PER_DAY * USECS_PER_SEC;
     672           6 :         PG_RETURN_TIMESTAMPTZ(ts);
     673             :     }
     674             : 
     675          12 :     if (version == 7)
     676             :     {
     677           6 :         tms = (uuid->data[5])
     678           6 :             + (((uint64) uuid->data[4]) << 8)
     679           6 :             + (((uint64) uuid->data[3]) << 16)
     680           6 :             + (((uint64) uuid->data[2]) << 24)
     681           6 :             + (((uint64) uuid->data[1]) << 32)
     682           6 :             + (((uint64) uuid->data[0]) << 40);
     683             : 
     684             :         /* convert ms to us, then adjust */
     685           6 :         ts = (TimestampTz) (tms * NS_PER_US) -
     686             :             (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY * USECS_PER_SEC;
     687             : 
     688           6 :         PG_RETURN_TIMESTAMPTZ(ts);
     689             :     }
     690             : 
     691             :     /* not a timestamp-containing UUID version */
     692           6 :     PG_RETURN_NULL();
     693             : }
     694             : 
     695             : /*
     696             :  * Extract UUID version.
     697             :  *
     698             :  * Returns null if not RFC 9562 variant.
     699             :  */
     700             : Datum
     701          30 : uuid_extract_version(PG_FUNCTION_ARGS)
     702             : {
     703          30 :     pg_uuid_t  *uuid = PG_GETARG_UUID_P(0);
     704             :     uint16      version;
     705             : 
     706             :     /* check if RFC 9562 variant */
     707          30 :     if ((uuid->data[8] & 0xc0) != 0x80)
     708           6 :         PG_RETURN_NULL();
     709             : 
     710          24 :     version = uuid->data[6] >> 4;
     711             : 
     712          24 :     PG_RETURN_UINT16(version);
     713             : }

Generated by: LCOV version 1.14