PostgreSQL Source Code git master
Loading...
Searching...
No Matches
uuid.c File Reference
#include "postgres.h"
#include <limits.h>
#include <time.h>
#include "common/hashfn.h"
#include "lib/hyperloglog.h"
#include "libpq/pqformat.h"
#include "port/pg_bswap.h"
#include "utils/fmgrprotos.h"
#include "utils/guc.h"
#include "utils/skipsupport.h"
#include "utils/sortsupport.h"
#include "utils/timestamp.h"
#include "utils/uuid.h"
Include dependency graph for uuid.c:

Go to the source code of this file.

Data Structures

struct  uuid_sortsupport_state
 

Macros

#define NS_PER_S   INT64CONST(1000000000)
 
#define NS_PER_MS   INT64CONST(1000000)
 
#define NS_PER_US   INT64CONST(1000)
 
#define US_PER_MS   INT64CONST(1000)
 
#define SUBMS_MINIMAL_STEP_BITS   12
 
#define SUBMS_BITS   12
 
#define SUBMS_MINIMAL_STEP_NS   ((NS_PER_MS / (1 << SUBMS_MINIMAL_STEP_BITS)) + 1)
 
#define GREGORIAN_EPOCH_JDATE   INT64CONST(2299161)
 

Functions

static void string_to_uuid (const char *source, pg_uuid_t *uuid, Node *escontext)
 
static int uuid_internal_cmp (const pg_uuid_t *arg1, const pg_uuid_t *arg2)
 
static int uuid_fast_cmp (Datum x, Datum y, SortSupport ssup)
 
static bool uuid_abbrev_abort (int memtupcount, SortSupport ssup)
 
static Datum uuid_abbrev_convert (Datum original, SortSupport ssup)
 
static void uuid_set_version (pg_uuid_t *uuid, unsigned char version)
 
static int64 get_real_time_ns_ascending (void)
 
static pg_uuid_tgenerate_uuidv7 (uint64 unix_ts_ms, uint32 sub_ms)
 
Datum uuid_in (PG_FUNCTION_ARGS)
 
Datum uuid_out (PG_FUNCTION_ARGS)
 
Datum uuid_recv (PG_FUNCTION_ARGS)
 
Datum uuid_send (PG_FUNCTION_ARGS)
 
Datum uuid_lt (PG_FUNCTION_ARGS)
 
Datum uuid_le (PG_FUNCTION_ARGS)
 
Datum uuid_eq (PG_FUNCTION_ARGS)
 
Datum uuid_ge (PG_FUNCTION_ARGS)
 
Datum uuid_gt (PG_FUNCTION_ARGS)
 
Datum uuid_ne (PG_FUNCTION_ARGS)
 
Datum uuid_cmp (PG_FUNCTION_ARGS)
 
Datum uuid_larger (PG_FUNCTION_ARGS)
 
Datum uuid_smaller (PG_FUNCTION_ARGS)
 
Datum uuid_sortsupport (PG_FUNCTION_ARGS)
 
static Datum uuid_decrement (Relation rel, Datum existing, bool *underflow)
 
static Datum uuid_increment (Relation rel, Datum existing, bool *overflow)
 
Datum uuid_skipsupport (PG_FUNCTION_ARGS)
 
Datum uuid_hash (PG_FUNCTION_ARGS)
 
Datum uuid_hash_extended (PG_FUNCTION_ARGS)
 
Datum gen_random_uuid (PG_FUNCTION_ARGS)
 
Datum uuidv7 (PG_FUNCTION_ARGS)
 
Datum uuidv7_interval (PG_FUNCTION_ARGS)
 
Datum uuid_extract_timestamp (PG_FUNCTION_ARGS)
 
Datum uuid_extract_version (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ GREGORIAN_EPOCH_JDATE

#define GREGORIAN_EPOCH_JDATE   INT64CONST(2299161)

Definition at line 721 of file uuid.c.

◆ NS_PER_MS

#define NS_PER_MS   INT64CONST(1000000)

Definition at line 32 of file uuid.c.

◆ NS_PER_S

#define NS_PER_S   INT64CONST(1000000000)

Definition at line 31 of file uuid.c.

◆ NS_PER_US

#define NS_PER_US   INT64CONST(1000)

Definition at line 33 of file uuid.c.

◆ SUBMS_BITS

#define SUBMS_BITS   12

Definition at line 56 of file uuid.c.

◆ SUBMS_MINIMAL_STEP_BITS

#define SUBMS_MINIMAL_STEP_BITS   12

Definition at line 54 of file uuid.c.

◆ SUBMS_MINIMAL_STEP_NS

#define SUBMS_MINIMAL_STEP_NS   ((NS_PER_MS / (1 << SUBMS_MINIMAL_STEP_BITS)) + 1)

Definition at line 57 of file uuid.c.

◆ US_PER_MS

#define US_PER_MS   INT64CONST(1000)

Definition at line 34 of file uuid.c.

Function Documentation

◆ gen_random_uuid()

Datum gen_random_uuid ( PG_FUNCTION_ARGS  )

Definition at line 542 of file uuid.c.

543{
545
549 errmsg("could not generate random values")));
550
551 /*
552 * Set magic numbers for a "version 4" (pseudorandom) UUID and variant,
553 * see https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4
554 */
556
558}
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
void * palloc(Size size)
Definition mcxt.c:1390
static char * errmsg
bool pg_strong_random(void *buf, size_t len)
static int fb(int x)
static void uuid_set_version(pg_uuid_t *uuid, unsigned char version)
Definition uuid.c:526
#define PG_RETURN_UUID_P(X)
Definition uuid.h:32
#define UUID_LEN
Definition uuid.h:18

References ereport, errcode(), errmsg, ERROR, fb(), palloc(), PG_RETURN_UUID_P, pg_strong_random(), UUID_LEN, and uuid_set_version().

Referenced by pg_random_uuid().

◆ generate_uuidv7()

static pg_uuid_t * generate_uuidv7 ( uint64  unix_ts_ms,
uint32  sub_ms 
)
static

Definition at line 619 of file uuid.c.

620{
623
624 /* Fill in time part */
625 uuid->data[0] = (unsigned char) (unix_ts_ms >> 40);
626 uuid->data[1] = (unsigned char) (unix_ts_ms >> 32);
627 uuid->data[2] = (unsigned char) (unix_ts_ms >> 24);
628 uuid->data[3] = (unsigned char) (unix_ts_ms >> 16);
629 uuid->data[4] = (unsigned char) (unix_ts_ms >> 8);
630 uuid->data[5] = (unsigned char) unix_ts_ms;
631
632 /*
633 * sub-millisecond timestamp fraction (SUBMS_BITS bits, not
634 * SUBMS_MINIMAL_STEP_BITS)
635 */
637
638 /* Fill the increased clock precision to "rand_a" bits */
639 uuid->data[6] = (unsigned char) (increased_clock_precision >> 8);
640 uuid->data[7] = (unsigned char) (increased_clock_precision);
641
642 /* fill everything after the increased clock precision with random bytes */
643 if (!pg_strong_random(&uuid->data[8], UUID_LEN - 8))
646 errmsg("could not generate random values")));
647
648#if SUBMS_MINIMAL_STEP_BITS == 10
649
650 /*
651 * On systems that have only 10 bits of sub-ms precision, 2 least
652 * significant are dependent on other time-specific bits, and they do not
653 * contribute to uniqueness. To make these bit random we mix in two bits
654 * from CSPRNG. SUBMS_MINIMAL_STEP is chosen so that we still guarantee
655 * monotonicity despite altering these bits.
656 */
657 uuid->data[7] = uuid->data[7] ^ (uuid->data[8] >> 6);
658#endif
659
660 /*
661 * Set magic numbers for a "version 7" (pseudorandom) UUID and variant,
662 * see https://www.rfc-editor.org/rfc/rfc9562#name-version-field
663 */
665
666 return uuid;
667}
uint32_t uint32
Definition c.h:683
#define SUBMS_BITS
Definition uuid.c:56
#define NS_PER_MS
Definition uuid.c:32

References ereport, errcode(), errmsg, ERROR, fb(), NS_PER_MS, palloc(), pg_strong_random(), SUBMS_BITS, UUID_LEN, and uuid_set_version().

Referenced by uuidv7(), and uuidv7_interval().

◆ get_real_time_ns_ascending()

static int64 get_real_time_ns_ascending ( void  )
inlinestatic

Definition at line 566 of file uuid.c.

567{
568 static int64 previous_ns = 0;
569 int64 ns;
570
571 /* Get the current real timestamp */
572
573#ifdef _MSC_VER
574 struct timeval tmp;
575
576 gettimeofday(&tmp, NULL);
577 ns = tmp.tv_sec * NS_PER_S + tmp.tv_usec * NS_PER_US;
578#else
579 struct timespec tmp;
580
581 /*
582 * We don't use gettimeofday(), instead use clock_gettime() with
583 * CLOCK_REALTIME where available in order to get a high-precision
584 * (nanoseconds) real timestamp.
585 *
586 * Note while a timestamp returned by clock_gettime() with CLOCK_REALTIME
587 * is nanosecond-precision on most Unix-like platforms, on some platforms
588 * such as macOS it's restricted to microsecond-precision.
589 */
591 ns = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;
592#endif
593
594 /* Guarantee the minimal step advancement of the timestamp */
597 previous_ns = ns;
598
599 return ns;
600}
int64_t int64
Definition c.h:680
#define SUBMS_MINIMAL_STEP_NS
Definition uuid.c:57
#define NS_PER_S
Definition uuid.c:31
#define NS_PER_US
Definition uuid.c:33
int gettimeofday(struct timeval *tp, void *tzp)

References fb(), gettimeofday(), NS_PER_S, NS_PER_US, and SUBMS_MINIMAL_STEP_NS.

Referenced by uuidv7(), and uuidv7_interval().

◆ string_to_uuid()

static void string_to_uuid ( const char source,
pg_uuid_t uuid,
Node escontext 
)
static

Definition at line 131 of file uuid.c.

132{
133 const char *src = source;
134 bool braces = false;
135 int i;
136
137 if (src[0] == '{')
138 {
139 src++;
140 braces = true;
141 }
142
143 for (i = 0; i < UUID_LEN; i++)
144 {
145 char str_buf[3];
146
147 if (src[0] == '\0' || src[1] == '\0')
148 goto syntax_error;
149 memcpy(str_buf, src, 2);
150 if (!isxdigit((unsigned char) str_buf[0]) ||
151 !isxdigit((unsigned char) str_buf[1]))
152 goto syntax_error;
153
154 str_buf[2] = '\0';
155 uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
156 src += 2;
157 if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1)
158 src++;
159 }
160
161 if (braces)
162 {
163 if (*src != '}')
164 goto syntax_error;
165 src++;
166 }
167
168 if (*src != '\0')
169 goto syntax_error;
170
171 return;
172
174 ereturn(escontext,,
176 errmsg("invalid input syntax for type %s: \"%s\"",
177 "uuid", source)));
178}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define ereturn(context, dummy_value,...)
Definition elog.h:280
int i
Definition isn.c:77
static rewind_source * source
Definition pg_rewind.c:89
void syntax_error(const char *source, int lineno, const char *line, const char *command, const char *msg, const char *more, int column)
Definition pgbench.c:5580

References ereturn, errcode(), errmsg, fb(), i, memcpy(), source, syntax_error(), and UUID_LEN.

Referenced by uuid_in().

◆ uuid_abbrev_abort()

static bool uuid_abbrev_abort ( int  memtupcount,
SortSupport  ssup 
)
static

Definition at line 346 of file uuid.c.

347{
349 double abbr_card;
350
352 return false;
353
354 abbr_card = estimateHyperLogLog(&uss->abbr_card);
355
356 /*
357 * If we have >100k distinct values, then even if we were sorting many
358 * billion rows we'd likely still break even, and the penalty of undoing
359 * that many rows of abbrevs would probably not be worth it. Stop even
360 * counting at that point.
361 */
362 if (abbr_card > 100000.0)
363 {
364 if (trace_sort)
365 elog(LOG,
366 "uuid_abbrev: estimation ends at cardinality %f"
367 " after " INT64_FORMAT " values (%d rows)",
368 abbr_card, uss->input_count, memtupcount);
369 uss->estimating = false;
370 return false;
371 }
372
373 /*
374 * Target minimum cardinality is 1 per ~2k of non-null inputs. 0.5 row
375 * fudge factor allows us to abort earlier on genuinely pathological data
376 * where we've had exactly one abbreviated value in the first 2k
377 * (non-null) rows.
378 */
379 if (abbr_card < uss->input_count / 2000.0 + 0.5)
380 {
381 if (trace_sort)
382 elog(LOG,
383 "uuid_abbrev: aborting abbreviation at cardinality %f"
384 " below threshold %f after " INT64_FORMAT " values (%d rows)",
385 abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
386 memtupcount);
387 return true;
388 }
389
390 if (trace_sort)
391 elog(LOG,
392 "uuid_abbrev: cardinality %f after " INT64_FORMAT
393 " values (%d rows)", abbr_card, uss->input_count, memtupcount);
394
395 return false;
396}
#define INT64_FORMAT
Definition c.h:693
#define LOG
Definition elog.h:32
#define elog(elevel,...)
Definition elog.h:228
double estimateHyperLogLog(hyperLogLogState *cState)
bool trace_sort
Definition tuplesort.c:123

References elog, estimateHyperLogLog(), fb(), INT64_FORMAT, LOG, SortSupportData::ssup_extra, and trace_sort.

Referenced by uuid_sortsupport().

◆ uuid_abbrev_convert()

static Datum uuid_abbrev_convert ( Datum  original,
SortSupport  ssup 
)
static

Definition at line 406 of file uuid.c.

407{
410 Datum res;
411
412 memcpy(&res, authoritative->data, sizeof(Datum));
413 uss->input_count += 1;
414
415 if (uss->estimating)
416 {
417 uint32 tmp;
418
419 tmp = DatumGetUInt32(res) ^ (uint32) (DatumGetUInt64(res) >> 32);
420
421 addHyperLogLog(&uss->abbr_card, DatumGetUInt32(hash_uint32(tmp)));
422 }
423
424 /*
425 * Byteswap on little-endian machines.
426 *
427 * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
428 * 3-way comparator) works correctly on all platforms. If we didn't do
429 * this, the comparator would have to call memcmp() with a pair of
430 * pointers to the first byte of each abbreviated key, which is slower.
431 */
432 res = DatumBigEndianToNative(res);
433
434 return res;
435}
static Datum hash_uint32(uint32 k)
Definition hashfn.h:43
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
#define DatumBigEndianToNative(x)
Definition pg_bswap.h:145
static uint32 DatumGetUInt32(Datum X)
Definition postgres.h:222
static uint64 DatumGetUInt64(Datum X)
Definition postgres.h:436
uint64_t Datum
Definition postgres.h:70
static pg_uuid_t * DatumGetUUIDP(Datum X)
Definition uuid.h:35

References addHyperLogLog(), DatumBigEndianToNative, DatumGetUInt32(), DatumGetUInt64(), DatumGetUUIDP(), fb(), hash_uint32(), memcpy(), and SortSupportData::ssup_extra.

Referenced by uuid_sortsupport().

◆ uuid_cmp()

Datum uuid_cmp ( PG_FUNCTION_ARGS  )

Definition at line 265 of file uuid.c.

266{
269
271}
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
static int uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
Definition uuid.c:204
#define PG_GETARG_UUID_P(X)
Definition uuid.h:40

References fb(), PG_GETARG_UUID_P, PG_RETURN_INT32, and uuid_internal_cmp().

◆ uuid_decrement()

static Datum uuid_decrement ( Relation  rel,
Datum  existing,
bool underflow 
)
static

Definition at line 438 of file uuid.c.

439{
441
444 for (int i = UUID_LEN - 1; i >= 0; i--)
445 {
446 if (uuid->data[i] > 0)
447 {
448 uuid->data[i]--;
449 *underflow = false;
450 return UUIDPGetDatum(uuid);
451 }
452 uuid->data[i] = UCHAR_MAX;
453 }
454
455 pfree(uuid); /* cannot leak memory */
456
457 /* return value is undefined */
458 *underflow = true;
459 return (Datum) 0;
460}
void pfree(void *pointer)
Definition mcxt.c:1619
static Datum UUIDPGetDatum(const pg_uuid_t *X)
Definition uuid.h:27

References DatumGetUUIDP(), fb(), i, memcpy(), palloc(), pfree(), UUID_LEN, and UUIDPGetDatum().

Referenced by uuid_skipsupport().

◆ uuid_eq()

Datum uuid_eq ( PG_FUNCTION_ARGS  )

Definition at line 228 of file uuid.c.

229{
232
234}
#define PG_RETURN_BOOL(x)
Definition fmgr.h:360

References fb(), PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_extract_timestamp()

Datum uuid_extract_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 729 of file uuid.c.

730{
732 int version;
733 uint64 tms;
734 TimestampTz ts;
735
736 /* check if RFC 9562 variant */
737 if ((uuid->data[8] & 0xc0) != 0x80)
739
740 version = uuid->data[6] >> 4;
741
742 if (version == 1)
743 {
744 tms = ((uint64) uuid->data[0] << 24)
745 + ((uint64) uuid->data[1] << 16)
746 + ((uint64) uuid->data[2] << 8)
747 + ((uint64) uuid->data[3])
748 + ((uint64) uuid->data[4] << 40)
749 + ((uint64) uuid->data[5] << 32)
750 + (((uint64) uuid->data[6] & 0xf) << 56)
751 + ((uint64) uuid->data[7] << 48);
752
753 /* convert 100-ns intervals to us, then adjust */
754 ts = (TimestampTz) (tms / 10) -
757 }
758
759 if (version == 7)
760 {
761 tms = (uuid->data[5])
762 + (((uint64) uuid->data[4]) << 8)
763 + (((uint64) uuid->data[3]) << 16)
764 + (((uint64) uuid->data[2]) << 24)
765 + (((uint64) uuid->data[1]) << 32)
766 + (((uint64) uuid->data[0]) << 40);
767
768 /* convert ms to us, then adjust */
769 ts = (TimestampTz) (tms * US_PER_MS) -
771
773 }
774
775 /* not a timestamp-containing UUID version */
777}
uint64_t uint64
Definition c.h:684
int64 TimestampTz
Definition timestamp.h:39
#define USECS_PER_SEC
Definition timestamp.h:134
#define UNIX_EPOCH_JDATE
Definition timestamp.h:234
#define SECS_PER_DAY
Definition timestamp.h:126
#define POSTGRES_EPOCH_JDATE
Definition timestamp.h:235
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_RETURN_TIMESTAMPTZ(x)
Definition timestamp.h:68
#define US_PER_MS
Definition uuid.c:34
#define GREGORIAN_EPOCH_JDATE
Definition uuid.c:721

References fb(), GREGORIAN_EPOCH_JDATE, PG_GETARG_UUID_P, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, US_PER_MS, and USECS_PER_SEC.

◆ uuid_extract_version()

Datum uuid_extract_version ( PG_FUNCTION_ARGS  )

Definition at line 785 of file uuid.c.

786{
788 uint16 version;
789
790 /* check if RFC 9562 variant */
791 if ((uuid->data[8] & 0xc0) != 0x80)
793
794 version = uuid->data[6] >> 4;
795
796 PG_RETURN_UINT16(version);
797}
uint16_t uint16
Definition c.h:682
#define PG_RETURN_UINT16(x)
Definition fmgr.h:358

References fb(), PG_GETARG_UUID_P, PG_RETURN_NULL, and PG_RETURN_UINT16.

◆ uuid_fast_cmp()

static int uuid_fast_cmp ( Datum  x,
Datum  y,
SortSupport  ssup 
)
static

Definition at line 331 of file uuid.c.

332{
335
336 return uuid_internal_cmp(arg1, arg2);
337}
int y
Definition isn.c:76
int x
Definition isn.c:75

References DatumGetUUIDP(), fb(), uuid_internal_cmp(), x, and y.

Referenced by uuid_sortsupport().

◆ uuid_ge()

Datum uuid_ge ( PG_FUNCTION_ARGS  )

Definition at line 237 of file uuid.c.

References fb(), PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_gt()

Datum uuid_gt ( PG_FUNCTION_ARGS  )

Definition at line 246 of file uuid.c.

References fb(), PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_hash()

Datum uuid_hash ( PG_FUNCTION_ARGS  )

Definition at line 507 of file uuid.c.

508{
510
511 return hash_any(key->data, UUID_LEN);
512}
static Datum hash_any(const unsigned char *k, int keylen)
Definition hashfn.h:31

References hash_any(), PG_GETARG_UUID_P, and UUID_LEN.

◆ uuid_hash_extended()

Datum uuid_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 515 of file uuid.c.

516{
518
520}
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition hashfn.h:37

References hash_any_extended(), PG_GETARG_INT64, PG_GETARG_UUID_P, and UUID_LEN.

◆ uuid_in()

Datum uuid_in ( PG_FUNCTION_ARGS  )

Definition at line 78 of file uuid.c.

79{
80 char *uuid_str = PG_GETARG_CSTRING(0);
82
84 string_to_uuid(uuid_str, uuid, fcinfo->context);
86}
#define palloc_object(type)
Definition fe_memutils.h:89
#define PG_GETARG_CSTRING(n)
Definition fmgr.h:278
static void string_to_uuid(const char *source, pg_uuid_t *uuid, Node *escontext)
Definition uuid.c:131

References fb(), palloc_object, PG_GETARG_CSTRING, PG_RETURN_UUID_P, and string_to_uuid().

Referenced by uuid_generate_internal().

◆ uuid_increment()

static Datum uuid_increment ( Relation  rel,
Datum  existing,
bool overflow 
)
static

Definition at line 463 of file uuid.c.

464{
466
469 for (int i = UUID_LEN - 1; i >= 0; i--)
470 {
471 if (uuid->data[i] < UCHAR_MAX)
472 {
473 uuid->data[i]++;
474 *overflow = false;
475 return UUIDPGetDatum(uuid);
476 }
477 uuid->data[i] = 0;
478 }
479
480 pfree(uuid); /* cannot leak memory */
481
482 /* return value is undefined */
483 *overflow = true;
484 return (Datum) 0;
485}

References DatumGetUUIDP(), fb(), i, memcpy(), palloc(), pfree(), UUID_LEN, and UUIDPGetDatum().

Referenced by uuid_skipsupport().

◆ uuid_internal_cmp()

static int uuid_internal_cmp ( const pg_uuid_t arg1,
const pg_uuid_t arg2 
)
static

Definition at line 204 of file uuid.c.

205{
206 return memcmp(arg1->data, arg2->data, UUID_LEN);
207}

References fb(), and UUID_LEN.

Referenced by uuid_cmp(), uuid_eq(), uuid_fast_cmp(), uuid_ge(), uuid_gt(), uuid_larger(), uuid_le(), uuid_lt(), uuid_ne(), and uuid_smaller().

◆ uuid_larger()

Datum uuid_larger ( PG_FUNCTION_ARGS  )

Definition at line 274 of file uuid.c.

References fb(), PG_GETARG_UUID_P, PG_RETURN_UUID_P, and uuid_internal_cmp().

◆ uuid_le()

Datum uuid_le ( PG_FUNCTION_ARGS  )

Definition at line 219 of file uuid.c.

References fb(), PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

Referenced by brin_minmax_multi_distance_uuid().

◆ uuid_lt()

Datum uuid_lt ( PG_FUNCTION_ARGS  )

Definition at line 210 of file uuid.c.

References fb(), PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_ne()

Datum uuid_ne ( PG_FUNCTION_ARGS  )

Definition at line 255 of file uuid.c.

References fb(), PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_out()

Datum uuid_out ( PG_FUNCTION_ARGS  )

Definition at line 89 of file uuid.c.

90{
92 static const char hex_chars[] = "0123456789abcdef";
93 char *buf,
94 *p;
95 int i;
96
97 /* counts for the four hyphens and the zero-terminator */
98 buf = palloc(2 * UUID_LEN + 5);
99 p = buf;
100 for (i = 0; i < UUID_LEN; i++)
101 {
102 int hi;
103 int lo;
104
105 /*
106 * We print uuid values as a string of 8, 4, 4, 4, and then 12
107 * hexadecimal characters, with each group is separated by a hyphen
108 * ("-"). Therefore, add the hyphens at the appropriate places here.
109 */
110 if (i == 4 || i == 6 || i == 8 || i == 10)
111 *p++ = '-';
112
113 hi = uuid->data[i] >> 4;
114 lo = uuid->data[i] & 0x0F;
115
116 *p++ = hex_chars[hi];
117 *p++ = hex_chars[lo];
118 }
119 *p = '\0';
120
122}
#define PG_RETURN_CSTRING(x)
Definition fmgr.h:364
static char buf[DEFAULT_XLOG_SEG_SIZE]

References buf, fb(), i, palloc(), PG_GETARG_UUID_P, PG_RETURN_CSTRING, and UUID_LEN.

◆ uuid_recv()

Datum uuid_recv ( PG_FUNCTION_ARGS  )

Definition at line 181 of file uuid.c.

182{
185
187 memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN);
189}
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition pqformat.c:507
struct StringInfoData * StringInfo
Definition string.h:15

References fb(), memcpy(), palloc(), PG_GETARG_POINTER, PG_RETURN_POINTER, pq_getmsgbytes(), and UUID_LEN.

◆ uuid_send()

Datum uuid_send ( PG_FUNCTION_ARGS  )

Definition at line 192 of file uuid.c.

193{
195 StringInfoData buffer;
196
197 pq_begintypsend(&buffer);
198 pq_sendbytes(&buffer, uuid->data, UUID_LEN);
200}
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition pqformat.c:126
void pq_begintypsend(StringInfo buf)
Definition pqformat.c:325
bytea * pq_endtypsend(StringInfo buf)
Definition pqformat.c:345

References StringInfoData::data, fb(), PG_GETARG_UUID_P, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendbytes(), and UUID_LEN.

Referenced by uuid_bytea().

◆ uuid_set_version()

static void uuid_set_version ( pg_uuid_t uuid,
unsigned char  version 
)
inlinestatic

Definition at line 526 of file uuid.c.

527{
528 /* set version field, top four bits */
529 uuid->data[6] = (uuid->data[6] & 0x0f) | (version << 4);
530
531 /* set variant field, top two bits are 1, 0 */
532 uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80;
533}

References fb().

Referenced by gen_random_uuid(), and generate_uuidv7().

◆ uuid_skipsupport()

Datum uuid_skipsupport ( PG_FUNCTION_ARGS  )

Definition at line 488 of file uuid.c.

489{
493
494 memset(uuid_min->data, 0x00, UUID_LEN);
495 memset(uuid_max->data, 0xFF, UUID_LEN);
496
497 sksup->decrement = uuid_decrement;
498 sksup->increment = uuid_increment;
501
503}
#define PG_RETURN_VOID()
Definition fmgr.h:350
struct SkipSupportData * SkipSupport
Definition skipsupport.h:50
SkipSupportIncDec decrement
Definition skipsupport.h:91
SkipSupportIncDec increment
Definition skipsupport.h:92
static Datum uuid_increment(Relation rel, Datum existing, bool *overflow)
Definition uuid.c:463
static Datum uuid_decrement(Relation rel, Datum existing, bool *underflow)
Definition uuid.c:438

References SkipSupportData::decrement, fb(), SkipSupportData::high_elem, SkipSupportData::increment, SkipSupportData::low_elem, palloc(), PG_GETARG_POINTER, PG_RETURN_VOID, uuid_decrement(), uuid_increment(), UUID_LEN, and UUIDPGetDatum().

◆ uuid_smaller()

Datum uuid_smaller ( PG_FUNCTION_ARGS  )

Definition at line 283 of file uuid.c.

References fb(), PG_GETARG_UUID_P, PG_RETURN_UUID_P, and uuid_internal_cmp().

◆ uuid_sortsupport()

Datum uuid_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 295 of file uuid.c.

296{
298
300 ssup->ssup_extra = NULL;
301
302 if (ssup->abbreviate)
303 {
305 MemoryContext oldcontext;
306
307 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
308
310 uss->input_count = 0;
311 uss->estimating = true;
312 initHyperLogLog(&uss->abbr_card, 10);
313
314 ssup->ssup_extra = uss;
315
320
321 MemoryContextSwitchTo(oldcontext);
322 }
323
325}
struct SortSupportData * SortSupport
Definition execnodes.h:61
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition hyperloglog.c:66
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:138
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
MemoryContext ssup_cxt
Definition sortsupport.h:66
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
Definition tuplesort.c:3450
static bool uuid_abbrev_abort(int memtupcount, SortSupport ssup)
Definition uuid.c:346
static int uuid_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition uuid.c:331
static Datum uuid_abbrev_convert(Datum original, SortSupport ssup)
Definition uuid.c:406

References SortSupportData::abbrev_abort, SortSupportData::abbrev_converter, SortSupportData::abbrev_full_comparator, SortSupportData::abbreviate, SortSupportData::comparator, fb(), initHyperLogLog(), MemoryContextSwitchTo(), palloc_object, PG_GETARG_POINTER, PG_RETURN_VOID, SortSupportData::ssup_cxt, ssup_datum_unsigned_cmp(), SortSupportData::ssup_extra, uuid_abbrev_abort(), uuid_abbrev_convert(), and uuid_fast_cmp().

◆ uuidv7()

Datum uuidv7 ( PG_FUNCTION_ARGS  )

Definition at line 673 of file uuid.c.

674{
677
679}
static int64 get_real_time_ns_ascending(void)
Definition uuid.c:566
static pg_uuid_t * generate_uuidv7(uint64 unix_ts_ms, uint32 sub_ms)
Definition uuid.c:619

References fb(), generate_uuidv7(), get_real_time_ns_ascending(), NS_PER_MS, and PG_RETURN_UUID_P.

◆ uuidv7_interval()

Datum uuidv7_interval ( PG_FUNCTION_ARGS  )

Definition at line 685 of file uuid.c.

686{
687 Interval *shift = PG_GETARG_INTERVAL_P(0);
688 TimestampTz ts;
691 int64 us;
692
693 /*
694 * Shift the current timestamp by the given interval. To calculate time
695 * shift correctly, we convert the UNIX epoch to TimestampTz and use
696 * timestamptz_pl_interval(). This calculation is done with microsecond
697 * precision.
698 */
699
700 ts = (TimestampTz) (ns / NS_PER_US) -
702
703 /* Compute time shift */
706 IntervalPGetDatum(shift)));
707
708 /* Convert a TimestampTz value back to an UNIX epoch timestamp */
710
711 /* Generate an UUIDv7 */
713
715}
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition timestamp.c:3397
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:690
static Datum TimestampTzGetDatum(TimestampTz X)
Definition timestamp.h:52
static Datum IntervalPGetDatum(const Interval *X)
Definition timestamp.h:58
#define PG_GETARG_INTERVAL_P(n)
Definition timestamp.h:65
static TimestampTz DatumGetTimestampTz(Datum X)
Definition timestamp.h:34

References DatumGetTimestampTz(), DirectFunctionCall2, fb(), generate_uuidv7(), get_real_time_ns_ascending(), IntervalPGetDatum(), NS_PER_US, PG_GETARG_INTERVAL_P, PG_RETURN_UUID_P, POSTGRES_EPOCH_JDATE, SECS_PER_DAY, timestamptz_pl_interval(), TimestampTzGetDatum(), UNIX_EPOCH_JDATE, US_PER_MS, and USECS_PER_SEC.