22#include "utils/fmgrprotos.h"
29#define NS_PER_S INT64CONST(1000000000)
30#define NS_PER_MS INT64CONST(1000000)
31#define NS_PER_US INT64CONST(1000)
48#if defined(__darwin__) || defined(_MSC_VER)
49#define SUBMS_MINIMAL_STEP_BITS 10
51#define SUBMS_MINIMAL_STEP_BITS 12
54#define SUBMS_MINIMAL_STEP_NS ((NS_PER_MS / (1 << SUBMS_MINIMAL_STEP_BITS)) + 1)
88 static const char hex_chars[] =
"0123456789abcdef";
106 if (
i == 4 ||
i == 6 ||
i == 8 ||
i == 10)
109 hi = uuid->
data[
i] >> 4;
110 lo = uuid->
data[
i] & 0x0F;
112 *p++ = hex_chars[hi];
113 *p++ = hex_chars[lo];
143 if (src[0] ==
'\0' || src[1] ==
'\0')
145 memcpy(str_buf, src, 2);
146 if (!isxdigit((
unsigned char) str_buf[0]) ||
147 !isxdigit((
unsigned char) str_buf[1]))
151 uuid->
data[
i] = (
unsigned char) strtoul(str_buf, NULL, 16);
153 if (src[0] ==
'-' && (
i % 2) == 1 &&
i <
UUID_LEN - 1)
171 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
172 errmsg(
"invalid input syntax for type %s: \"%s\"",
329 if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
340 if (abbr_card > 100000.0)
344 "uuid_abbrev: estimation ends at cardinality %f"
357 if (abbr_card < uss->input_count / 2000.0 + 0.5)
361 "uuid_abbrev: aborting abbreviation at cardinality %f"
362 " below threshold %f after " INT64_FORMAT " values (%d rows)",
371 " values (%d rows)", abbr_card, uss->
input_count, memtupcount);
414 res = DatumBigEndianToNative(
res);
443 uuid->
data[6] = (uuid->
data[6] & 0x0f) | (version << 4);
446 uuid->
data[8] = (uuid->
data[8] & 0x3f) | 0x80;
462 (
errcode(ERRCODE_INTERNAL_ERROR),
463 errmsg(
"could not generate random values")));
482 static int64 previous_ns = 0;
504 clock_gettime(CLOCK_REALTIME, &tmp);
505 ns = tmp.tv_sec *
NS_PER_S + tmp.tv_nsec;
534 int32 increased_clock_precision;
539 uuid->
data[0] = (
unsigned char) (unix_ts_ms >> 40);
540 uuid->
data[1] = (
unsigned char) (unix_ts_ms >> 32);
541 uuid->
data[2] = (
unsigned char) (unix_ts_ms >> 24);
542 uuid->
data[3] = (
unsigned char) (unix_ts_ms >> 16);
543 uuid->
data[4] = (
unsigned char) (unix_ts_ms >> 8);
544 uuid->
data[5] = (
unsigned char) unix_ts_ms;
553 uuid->
data[6] = (
unsigned char) (increased_clock_precision >> 8);
554 uuid->
data[7] = (
unsigned char) (increased_clock_precision);
559 (
errcode(ERRCODE_INTERNAL_ERROR),
560 errmsg(
"could not generate random values")));
562#if SUBMS_MINIMAL_STEP_BITS == 10
637#define GREGORIAN_EPOCH_JDATE INT64CONST(2299161)
653 if ((uuid->
data[8] & 0xc0) != 0x80)
656 version = uuid->
data[6] >> 4;
677 tms = (uuid->
data[5])
707 if ((uuid->
data[8] & 0xc0) != 0x80)
710 version = uuid->
data[6] >> 4;
Datum timestamptz_pl_interval(PG_FUNCTION_ARGS)
#define POSTGRES_EPOCH_JDATE
static void PGresult * res
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
#define PG_RETURN_BYTEA_P(x)
#define DirectFunctionCall2(func, arg1, arg2)
#define PG_GETARG_POINTER(n)
#define PG_RETURN_CSTRING(x)
#define PG_GETARG_CSTRING(n)
#define PG_GETARG_INT64(n)
#define PG_RETURN_INT32(x)
#define PG_RETURN_UINT16(x)
#define PG_RETURN_POINTER(x)
#define PG_RETURN_BOOL(x)
static Datum hash_uint32(uint32 k)
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
static Datum hash_any(const unsigned char *k, int keylen)
void initHyperLogLog(hyperLogLogState *cState, uint8 bwidth)
double estimateHyperLogLog(hyperLogLogState *cState)
void addHyperLogLog(hyperLogLogState *cState, uint32 hash)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static rewind_source * source
void syntax_error(const char *source, int lineno, const char *line, const char *command, const char *msg, const char *more, int column)
bool pg_strong_random(void *buf, size_t len)
static uint32 DatumGetUInt32(Datum X)
struct SortSupportData * SortSupport
StringInfoData * StringInfo
int(* comparator)(Datum x, Datum y, SortSupport ssup)
Datum(* abbrev_converter)(Datum original, SortSupport ssup)
int(* abbrev_full_comparator)(Datum x, Datum y, SortSupport ssup)
bool(* abbrev_abort)(int memtupcount, SortSupport ssup)
unsigned char data[UUID_LEN]
hyperLogLogState abbr_card
int ssup_datum_unsigned_cmp(Datum x, Datum y, SortSupport ssup)
static Datum TimestampTzGetDatum(TimestampTz X)
static Datum IntervalPGetDatum(const Interval *X)
#define PG_GETARG_INTERVAL_P(n)
#define PG_RETURN_TIMESTAMPTZ(x)
static TimestampTz DatumGetTimestampTz(Datum X)
static int64 get_real_time_ns_ascending()
Datum uuid_send(PG_FUNCTION_ARGS)
static void string_to_uuid(const char *source, pg_uuid_t *uuid, Node *escontext)
static bool uuid_abbrev_abort(int memtupcount, SortSupport ssup)
#define GREGORIAN_EPOCH_JDATE
Datum uuidv7_interval(PG_FUNCTION_ARGS)
Datum uuid_lt(PG_FUNCTION_ARGS)
Datum uuid_gt(PG_FUNCTION_ARGS)
Datum gen_random_uuid(PG_FUNCTION_ARGS)
Datum uuid_recv(PG_FUNCTION_ARGS)
Datum uuid_cmp(PG_FUNCTION_ARGS)
static void uuid_set_version(pg_uuid_t *uuid, unsigned char version)
Datum uuid_le(PG_FUNCTION_ARGS)
static pg_uuid_t * generate_uuidv7(int64 ns)
#define SUBMS_MINIMAL_STEP_NS
Datum uuid_hash(PG_FUNCTION_ARGS)
Datum uuid_extract_version(PG_FUNCTION_ARGS)
Datum uuid_hash_extended(PG_FUNCTION_ARGS)
Datum uuid_out(PG_FUNCTION_ARGS)
Datum uuid_ne(PG_FUNCTION_ARGS)
static int uuid_internal_cmp(const pg_uuid_t *arg1, const pg_uuid_t *arg2)
Datum uuid_ge(PG_FUNCTION_ARGS)
Datum uuid_eq(PG_FUNCTION_ARGS)
static int uuid_fast_cmp(Datum x, Datum y, SortSupport ssup)
Datum uuid_in(PG_FUNCTION_ARGS)
static Datum uuid_abbrev_convert(Datum original, SortSupport ssup)
Datum uuidv7(PG_FUNCTION_ARGS)
Datum uuid_extract_timestamp(PG_FUNCTION_ARGS)
Datum uuid_sortsupport(PG_FUNCTION_ARGS)
static pg_uuid_t * DatumGetUUIDP(Datum X)
#define PG_RETURN_UUID_P(X)
#define PG_GETARG_UUID_P(X)
int gettimeofday(struct timeval *tp, void *tzp)