PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 ()
 
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_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 707 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 528 of file uuid.c.

529{
530 pg_uuid_t *uuid = palloc(UUID_LEN);
531
532 if (!pg_strong_random(uuid, UUID_LEN))
534 (errcode(ERRCODE_INTERNAL_ERROR),
535 errmsg("could not generate random values")));
536
537 /*
538 * Set magic numbers for a "version 4" (pseudorandom) UUID and variant,
539 * see https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-4
540 */
541 uuid_set_version(uuid, 4);
542
543 PG_RETURN_UUID_P(uuid);
544}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void * palloc(Size size)
Definition: mcxt.c:1940
bool pg_strong_random(void *buf, size_t len)
Definition: uuid.h:21
static void uuid_set_version(pg_uuid_t *uuid, unsigned char version)
Definition: uuid.c:512
#define PG_RETURN_UUID_P(X)
Definition: uuid.h:32
#define UUID_LEN
Definition: uuid.h:18

References ereport, errcode(), errmsg(), ERROR, 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 605 of file uuid.c.

606{
607 pg_uuid_t *uuid = palloc(UUID_LEN);
608 uint32 increased_clock_precision;
609
610 /* Fill in time part */
611 uuid->data[0] = (unsigned char) (unix_ts_ms >> 40);
612 uuid->data[1] = (unsigned char) (unix_ts_ms >> 32);
613 uuid->data[2] = (unsigned char) (unix_ts_ms >> 24);
614 uuid->data[3] = (unsigned char) (unix_ts_ms >> 16);
615 uuid->data[4] = (unsigned char) (unix_ts_ms >> 8);
616 uuid->data[5] = (unsigned char) unix_ts_ms;
617
618 /*
619 * sub-millisecond timestamp fraction (SUBMS_BITS bits, not
620 * SUBMS_MINIMAL_STEP_BITS)
621 */
622 increased_clock_precision = (sub_ms * (1 << SUBMS_BITS)) / NS_PER_MS;
623
624 /* Fill the increased clock precision to "rand_a" bits */
625 uuid->data[6] = (unsigned char) (increased_clock_precision >> 8);
626 uuid->data[7] = (unsigned char) (increased_clock_precision);
627
628 /* fill everything after the increased clock precision with random bytes */
629 if (!pg_strong_random(&uuid->data[8], UUID_LEN - 8))
631 (errcode(ERRCODE_INTERNAL_ERROR),
632 errmsg("could not generate random values")));
633
634#if SUBMS_MINIMAL_STEP_BITS == 10
635
636 /*
637 * On systems that have only 10 bits of sub-ms precision, 2 least
638 * significant are dependent on other time-specific bits, and they do not
639 * contribute to uniqueness. To make these bit random we mix in two bits
640 * from CSPRNG. SUBMS_MINIMAL_STEP is chosen so that we still guarantee
641 * monotonicity despite altering these bits.
642 */
643 uuid->data[7] = uuid->data[7] ^ (uuid->data[8] >> 6);
644#endif
645
646 /*
647 * Set magic numbers for a "version 7" (pseudorandom) UUID and variant,
648 * see https://www.rfc-editor.org/rfc/rfc9562#name-version-field
649 */
650 uuid_set_version(uuid, 7);
651
652 return uuid;
653}
uint32_t uint32
Definition: c.h:502
unsigned char data[UUID_LEN]
Definition: uuid.h:22
#define SUBMS_BITS
Definition: uuid.c:56
#define NS_PER_MS
Definition: uuid.c:32

References pg_uuid_t::data, ereport, errcode(), errmsg(), ERROR, 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 ( )
inlinestatic

Definition at line 552 of file uuid.c.

553{
554 static int64 previous_ns = 0;
555 int64 ns;
556
557 /* Get the current real timestamp */
558
559#ifdef _MSC_VER
560 struct timeval tmp;
561
562 gettimeofday(&tmp, NULL);
563 ns = tmp.tv_sec * NS_PER_S + tmp.tv_usec * NS_PER_US;
564#else
565 struct timespec tmp;
566
567 /*
568 * We don't use gettimeofday(), instead use clock_gettime() with
569 * CLOCK_REALTIME where available in order to get a high-precision
570 * (nanoseconds) real timestamp.
571 *
572 * Note while a timestamp returned by clock_gettime() with CLOCK_REALTIME
573 * is nanosecond-precision on most Unix-like platforms, on some platforms
574 * such as macOS it's restricted to microsecond-precision.
575 */
576 clock_gettime(CLOCK_REALTIME, &tmp);
577 ns = tmp.tv_sec * NS_PER_S + tmp.tv_nsec;
578#endif
579
580 /* Guarantee the minimal step advancement of the timestamp */
581 if (previous_ns + SUBMS_MINIMAL_STEP_NS >= ns)
582 ns = previous_ns + SUBMS_MINIMAL_STEP_NS;
583 previous_ns = ns;
584
585 return ns;
586}
int64_t int64
Definition: c.h:499
#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 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,,
175 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
176 errmsg("invalid input syntax for type %s: \"%s\"",
177 "uuid", source)));
178}
#define ereturn(context, dummy_value,...)
Definition: elog.h:278
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:5517

References pg_uuid_t::data, ereturn, errcode(), errmsg(), i, 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 328 of file uuid.c.

329{
331 double abbr_card;
332
333 if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
334 return false;
335
336 abbr_card = estimateHyperLogLog(&uss->abbr_card);
337
338 /*
339 * If we have >100k distinct values, then even if we were sorting many
340 * billion rows we'd likely still break even, and the penalty of undoing
341 * that many rows of abbrevs would probably not be worth it. Stop even
342 * counting at that point.
343 */
344 if (abbr_card > 100000.0)
345 {
346 if (trace_sort)
347 elog(LOG,
348 "uuid_abbrev: estimation ends at cardinality %f"
349 " after " INT64_FORMAT " values (%d rows)",
350 abbr_card, uss->input_count, memtupcount);
351 uss->estimating = false;
352 return false;
353 }
354
355 /*
356 * Target minimum cardinality is 1 per ~2k of non-null inputs. 0.5 row
357 * fudge factor allows us to abort earlier on genuinely pathological data
358 * where we've had exactly one abbreviated value in the first 2k
359 * (non-null) rows.
360 */
361 if (abbr_card < uss->input_count / 2000.0 + 0.5)
362 {
363 if (trace_sort)
364 elog(LOG,
365 "uuid_abbrev: aborting abbreviation at cardinality %f"
366 " below threshold %f after " INT64_FORMAT " values (%d rows)",
367 abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
368 memtupcount);
369 return true;
370 }
371
372 if (trace_sort)
373 elog(LOG,
374 "uuid_abbrev: cardinality %f after " INT64_FORMAT
375 " values (%d rows)", abbr_card, uss->input_count, memtupcount);
376
377 return false;
378}
#define INT64_FORMAT
Definition: c.h:520
#define LOG
Definition: elog.h:31
#define elog(elevel,...)
Definition: elog.h:226
double estimateHyperLogLog(hyperLogLogState *cState)
Definition: hyperloglog.c:186
void * ssup_extra
Definition: sortsupport.h:87
hyperLogLogState abbr_card
Definition: uuid.c:65
bool trace_sort
Definition: tuplesort.c:124

References uuid_sortsupport_state::abbr_card, elog, estimateHyperLogLog(), uuid_sortsupport_state::estimating, uuid_sortsupport_state::input_count, 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 388 of file uuid.c.

389{
391 pg_uuid_t *authoritative = DatumGetUUIDP(original);
392 Datum res;
393
394 memcpy(&res, authoritative->data, sizeof(Datum));
395 uss->input_count += 1;
396
397 if (uss->estimating)
398 {
399 uint32 tmp;
400
401#if SIZEOF_DATUM == 8
402 tmp = (uint32) res ^ (uint32) ((uint64) res >> 32);
403#else /* SIZEOF_DATUM != 8 */
404 tmp = (uint32) res;
405#endif
406
408 }
409
410 /*
411 * Byteswap on little-endian machines.
412 *
413 * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
414 * 3-way comparator) works correctly on all platforms. If we didn't do
415 * this, the comparator would have to call memcmp() with a pair of
416 * pointers to the first byte of each abbreviated key, which is slower.
417 */
418 res = DatumBigEndianToNative(res);
419
420 return res;
421}
uint64_t uint64
Definition: c.h:503
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
Definition: hyperloglog.c:167
static uint32 DatumGetUInt32(Datum X)
Definition: postgres.h:227
uintptr_t Datum
Definition: postgres.h:69
static pg_uuid_t * DatumGetUUIDP(Datum X)
Definition: uuid.h:35

References uuid_sortsupport_state::abbr_card, addHyperLogLog(), pg_uuid_t::data, DatumGetUInt32(), DatumGetUUIDP(), uuid_sortsupport_state::estimating, hash_uint32(), uuid_sortsupport_state::input_count, 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{
267 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
268 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
269
271}
#define PG_RETURN_INT32(x)
Definition: fmgr.h:354
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 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 424 of file uuid.c.

425{
426 pg_uuid_t *uuid;
427
428 uuid = (pg_uuid_t *) palloc(UUID_LEN);
429 memcpy(uuid, DatumGetUUIDP(existing), UUID_LEN);
430 for (int i = UUID_LEN - 1; i >= 0; i--)
431 {
432 if (uuid->data[i] > 0)
433 {
434 uuid->data[i]--;
435 *underflow = false;
436 return UUIDPGetDatum(uuid);
437 }
438 uuid->data[i] = UCHAR_MAX;
439 }
440
441 pfree(uuid); /* cannot leak memory */
442
443 /* return value is undefined */
444 *underflow = true;
445 return (Datum) 0;
446}
void pfree(void *pointer)
Definition: mcxt.c:2147
static Datum UUIDPGetDatum(const pg_uuid_t *X)
Definition: uuid.h:27

References pg_uuid_t::data, DatumGetUUIDP(), i, 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{
230 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
231 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
232
233 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) == 0);
234}
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_extract_timestamp()

Datum uuid_extract_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 715 of file uuid.c.

716{
717 pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
718 int version;
719 uint64 tms;
720 TimestampTz ts;
721
722 /* check if RFC 9562 variant */
723 if ((uuid->data[8] & 0xc0) != 0x80)
725
726 version = uuid->data[6] >> 4;
727
728 if (version == 1)
729 {
730 tms = ((uint64) uuid->data[0] << 24)
731 + ((uint64) uuid->data[1] << 16)
732 + ((uint64) uuid->data[2] << 8)
733 + ((uint64) uuid->data[3])
734 + ((uint64) uuid->data[4] << 40)
735 + ((uint64) uuid->data[5] << 32)
736 + (((uint64) uuid->data[6] & 0xf) << 56)
737 + ((uint64) uuid->data[7] << 48);
738
739 /* convert 100-ns intervals to us, then adjust */
740 ts = (TimestampTz) (tms / 10) -
743 }
744
745 if (version == 7)
746 {
747 tms = (uuid->data[5])
748 + (((uint64) uuid->data[4]) << 8)
749 + (((uint64) uuid->data[3]) << 16)
750 + (((uint64) uuid->data[2]) << 24)
751 + (((uint64) uuid->data[1]) << 32)
752 + (((uint64) uuid->data[0]) << 40);
753
754 /* convert ms to us, then adjust */
755 ts = (TimestampTz) (tms * NS_PER_US) -
757
759 }
760
761 /* not a timestamp-containing UUID version */
763}
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:345
#define PG_RETURN_TIMESTAMPTZ(x)
Definition: timestamp.h:68
#define GREGORIAN_EPOCH_JDATE
Definition: uuid.c:707

References pg_uuid_t::data, GREGORIAN_EPOCH_JDATE, NS_PER_US, PG_GETARG_UUID_P, PG_RETURN_NULL, PG_RETURN_TIMESTAMPTZ, POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

◆ uuid_extract_version()

Datum uuid_extract_version ( PG_FUNCTION_ARGS  )

Definition at line 771 of file uuid.c.

772{
773 pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
774 uint16 version;
775
776 /* check if RFC 9562 variant */
777 if ((uuid->data[8] & 0xc0) != 0x80)
779
780 version = uuid->data[6] >> 4;
781
782 PG_RETURN_UINT16(version);
783}
uint16_t uint16
Definition: c.h:501
#define PG_RETURN_UINT16(x)
Definition: fmgr.h:357

References pg_uuid_t::data, 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 313 of file uuid.c.

314{
315 pg_uuid_t *arg1 = DatumGetUUIDP(x);
316 pg_uuid_t *arg2 = DatumGetUUIDP(y);
317
318 return uuid_internal_cmp(arg1, arg2);
319}
int y
Definition: isn.c:76
int x
Definition: isn.c:75

References DatumGetUUIDP(), 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.

238{
239 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
240 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
241
242 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) >= 0);
243}

References 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.

247{
248 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
249 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
250
251 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) > 0);
252}

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_hash()

Datum uuid_hash ( PG_FUNCTION_ARGS  )

Definition at line 493 of file uuid.c.

494{
496
497 return hash_any(key->data, UUID_LEN);
498}
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31

References hash_any(), sort-test::key, PG_GETARG_UUID_P, and UUID_LEN.

◆ uuid_hash_extended()

Datum uuid_hash_extended ( PG_FUNCTION_ARGS  )

Definition at line 501 of file uuid.c.

502{
504
506}
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37

References hash_any_extended(), sort-test::key, 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);
81 pg_uuid_t *uuid;
82
83 uuid = (pg_uuid_t *) palloc(sizeof(*uuid));
84 string_to_uuid(uuid_str, uuid, fcinfo->context);
85 PG_RETURN_UUID_P(uuid);
86}
#define PG_GETARG_CSTRING(n)
Definition: fmgr.h:277
static void string_to_uuid(const char *source, pg_uuid_t *uuid, Node *escontext)
Definition: uuid.c:131

References palloc(), 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 449 of file uuid.c.

450{
451 pg_uuid_t *uuid;
452
453 uuid = (pg_uuid_t *) palloc(UUID_LEN);
454 memcpy(uuid, DatumGetUUIDP(existing), UUID_LEN);
455 for (int i = UUID_LEN - 1; i >= 0; i--)
456 {
457 if (uuid->data[i] < UCHAR_MAX)
458 {
459 uuid->data[i]++;
460 *overflow = false;
461 return UUIDPGetDatum(uuid);
462 }
463 uuid->data[i] = 0;
464 }
465
466 pfree(uuid); /* cannot leak memory */
467
468 /* return value is undefined */
469 *overflow = true;
470 return (Datum) 0;
471}

References pg_uuid_t::data, DatumGetUUIDP(), i, 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 pg_uuid_t::data, and UUID_LEN.

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

◆ uuid_le()

Datum uuid_le ( PG_FUNCTION_ARGS  )

Definition at line 219 of file uuid.c.

220{
221 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
222 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
223
224 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) <= 0);
225}

References 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.

211{
212 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
213 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
214
215 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) < 0);
216}

References 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.

256{
257 pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
258 pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
259
260 PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) != 0);
261}

References 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{
91 pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
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:362
static char * buf
Definition: pg_test_fsync.c:72

References buf, pg_uuid_t::data, 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{
184 pg_uuid_t *uuid;
185
186 uuid = (pg_uuid_t *) palloc(UUID_LEN);
187 memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN);
188 PG_RETURN_POINTER(uuid);
189}
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
const char * pq_getmsgbytes(StringInfo msg, int datalen)
Definition: pqformat.c:508
StringInfoData * StringInfo
Definition: stringinfo.h:54

References pg_uuid_t::data, 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{
194 pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
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:371
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition: pqformat.c:126
void pq_begintypsend(StringInfo buf)
Definition: pqformat.c:326
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:346

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

◆ uuid_set_version()

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

Definition at line 512 of file uuid.c.

513{
514 /* set version field, top four bits */
515 uuid->data[6] = (uuid->data[6] & 0x0f) | (version << 4);
516
517 /* set variant field, top two bits are 1, 0 */
518 uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80;
519}

References pg_uuid_t::data.

Referenced by gen_random_uuid(), and generate_uuidv7().

◆ uuid_skipsupport()

Datum uuid_skipsupport ( PG_FUNCTION_ARGS  )

Definition at line 474 of file uuid.c.

475{
477 pg_uuid_t *uuid_min = palloc(UUID_LEN);
478 pg_uuid_t *uuid_max = palloc(UUID_LEN);
479
480 memset(uuid_min->data, 0x00, UUID_LEN);
481 memset(uuid_max->data, 0xFF, UUID_LEN);
482
483 sksup->decrement = uuid_decrement;
484 sksup->increment = uuid_increment;
485 sksup->low_elem = UUIDPGetDatum(uuid_min);
486 sksup->high_elem = UUIDPGetDatum(uuid_max);
487
489}
#define PG_RETURN_VOID()
Definition: fmgr.h:349
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:449
static Datum uuid_decrement(Relation rel, Datum existing, bool *underflow)
Definition: uuid.c:424

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

◆ uuid_sortsupport()

Datum uuid_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 277 of file uuid.c.

278{
280
282 ssup->ssup_extra = NULL;
283
284 if (ssup->abbreviate)
285 {
287 MemoryContext oldcontext;
288
289 oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
290
291 uss = palloc(sizeof(uuid_sortsupport_state));
292 uss->input_count = 0;
293 uss->estimating = true;
294 initHyperLogLog(&uss->abbr_card, 10);
295
296 ssup->ssup_extra = uss;
297
302
303 MemoryContextSwitchTo(oldcontext);
304 }
305
307}
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
Definition: hyperloglog.c:66
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
struct SortSupportData * SortSupport
Definition: sortsupport.h:58
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:106
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
Definition: sortsupport.h:172
MemoryContext ssup_cxt
Definition: sortsupport.h:66
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
Definition: sortsupport.h:191
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
Definition: sortsupport.h:182
int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
Definition: tuplesort.c:3139
static bool uuid_abbrev_abort(int memtupcount, SortSupport ssup)
Definition: uuid.c:328
static int uuid_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition: uuid.c:313
static Datum uuid_abbrev_convert(Datum original, SortSupport ssup)
Definition: uuid.c:388

References uuid_sortsupport_state::abbr_card, SortSupportData::abbrev_abort, SortSupportData::abbrev_converter, SortSupportData::abbrev_full_comparator, SortSupportData::abbreviate, SortSupportData::comparator, uuid_sortsupport_state::estimating, initHyperLogLog(), uuid_sortsupport_state::input_count, MemoryContextSwitchTo(), palloc(), 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 659 of file uuid.c.

660{
662 pg_uuid_t *uuid = generate_uuidv7(ns / NS_PER_MS, ns % NS_PER_MS);
663
664 PG_RETURN_UUID_P(uuid);
665}
static int64 get_real_time_ns_ascending()
Definition: uuid.c:552
static pg_uuid_t * generate_uuidv7(uint64 unix_ts_ms, uint32 sub_ms)
Definition: uuid.c:605

References 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 671 of file uuid.c.

672{
673 Interval *shift = PG_GETARG_INTERVAL_P(0);
674 TimestampTz ts;
675 pg_uuid_t *uuid;
677 int64 us;
678
679 /*
680 * Shift the current timestamp by the given interval. To calculate time
681 * shift correctly, we convert the UNIX epoch to TimestampTz and use
682 * timestamptz_pl_interval(). This calculation is done with microsecond
683 * precision.
684 */
685
686 ts = (TimestampTz) (ns / NS_PER_US) -
688
689 /* Compute time shift */
692 IntervalPGetDatum(shift)));
693
694 /* Convert a TimestampTz value back to an UNIX epoch timestamp */
696
697 /* Generate an UUIDv7 */
698 uuid = generate_uuidv7(us / US_PER_MS, (us % US_PER_MS) * NS_PER_US + ns % NS_PER_US);
699
700 PG_RETURN_UUID_P(uuid);
701}
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
Definition: timestamp.c:3398
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:684
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
#define US_PER_MS
Definition: uuid.c:34

References DatumGetTimestampTz(), DirectFunctionCall2, 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.