PostgreSQL Source Code  git master
uuid.c File Reference
#include "postgres.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/sortsupport.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
 

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)
 
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)
 
Datum uuid_hash (PG_FUNCTION_ARGS)
 
Datum uuid_hash_extended (PG_FUNCTION_ARGS)
 
Datum gen_random_uuid (PG_FUNCTION_ARGS)
 

Function Documentation

◆ gen_random_uuid()

Datum gen_random_uuid ( PG_FUNCTION_ARGS  )

Definition at line 410 of file uuid.c.

411 {
412  pg_uuid_t *uuid = palloc(UUID_LEN);
413 
414  if (!pg_strong_random(uuid, UUID_LEN))
415  ereport(ERROR,
416  (errcode(ERRCODE_INTERNAL_ERROR),
417  errmsg("could not generate random values")));
418 
419  /*
420  * Set magic numbers for a "version 4" (pseudorandom) UUID, see
421  * http://tools.ietf.org/html/rfc4122#section-4.4
422  */
423  uuid->data[6] = (uuid->data[6] & 0x0f) | 0x40; /* time_hi_and_version */
424  uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80; /* clock_seq_hi_and_reserved */
425 
426  PG_RETURN_UUID_P(uuid);
427 }
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void * palloc(Size size)
Definition: mcxt.c:1304
bool pg_strong_random(void *buf, size_t len)
Definition: uuid.h:21
unsigned char data[UUID_LEN]
Definition: uuid.h:22
#define PG_RETURN_UUID_P(X)
Definition: uuid.h:32
#define UUID_LEN
Definition: uuid.h:18

References pg_uuid_t::data, ereport, errcode(), errmsg(), ERROR, palloc(), PG_RETURN_UUID_P, pg_strong_random(), and UUID_LEN.

Referenced by pg_random_uuid().

◆ string_to_uuid()

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

Definition at line 94 of file uuid.c.

95 {
96  const char *src = source;
97  bool braces = false;
98  int i;
99 
100  if (src[0] == '{')
101  {
102  src++;
103  braces = true;
104  }
105 
106  for (i = 0; i < UUID_LEN; i++)
107  {
108  char str_buf[3];
109 
110  if (src[0] == '\0' || src[1] == '\0')
111  goto syntax_error;
112  memcpy(str_buf, src, 2);
113  if (!isxdigit((unsigned char) str_buf[0]) ||
114  !isxdigit((unsigned char) str_buf[1]))
115  goto syntax_error;
116 
117  str_buf[2] = '\0';
118  uuid->data[i] = (unsigned char) strtoul(str_buf, NULL, 16);
119  src += 2;
120  if (src[0] == '-' && (i % 2) == 1 && i < UUID_LEN - 1)
121  src++;
122  }
123 
124  if (braces)
125  {
126  if (*src != '}')
127  goto syntax_error;
128  src++;
129  }
130 
131  if (*src != '\0')
132  goto syntax_error;
133 
134  return;
135 
137  ereturn(escontext,,
138  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
139  errmsg("invalid input syntax for type %s: \"%s\"",
140  "uuid", source)));
141 }
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
int i
Definition: isn.c:73
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:5476

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

292 {
293  uuid_sortsupport_state *uss = ssup->ssup_extra;
294  double abbr_card;
295 
296  if (memtupcount < 10000 || uss->input_count < 10000 || !uss->estimating)
297  return false;
298 
299  abbr_card = estimateHyperLogLog(&uss->abbr_card);
300 
301  /*
302  * If we have >100k distinct values, then even if we were sorting many
303  * billion rows we'd likely still break even, and the penalty of undoing
304  * that many rows of abbrevs would probably not be worth it. Stop even
305  * counting at that point.
306  */
307  if (abbr_card > 100000.0)
308  {
309 #ifdef TRACE_SORT
310  if (trace_sort)
311  elog(LOG,
312  "uuid_abbrev: estimation ends at cardinality %f"
313  " after " INT64_FORMAT " values (%d rows)",
314  abbr_card, uss->input_count, memtupcount);
315 #endif
316  uss->estimating = false;
317  return false;
318  }
319 
320  /*
321  * Target minimum cardinality is 1 per ~2k of non-null inputs. 0.5 row
322  * fudge factor allows us to abort earlier on genuinely pathological data
323  * where we've had exactly one abbreviated value in the first 2k
324  * (non-null) rows.
325  */
326  if (abbr_card < uss->input_count / 2000.0 + 0.5)
327  {
328 #ifdef TRACE_SORT
329  if (trace_sort)
330  elog(LOG,
331  "uuid_abbrev: aborting abbreviation at cardinality %f"
332  " below threshold %f after " INT64_FORMAT " values (%d rows)",
333  abbr_card, uss->input_count / 2000.0 + 0.5, uss->input_count,
334  memtupcount);
335 #endif
336  return true;
337  }
338 
339 #ifdef TRACE_SORT
340  if (trace_sort)
341  elog(LOG,
342  "uuid_abbrev: cardinality %f after " INT64_FORMAT
343  " values (%d rows)", abbr_card, uss->input_count, memtupcount);
344 #endif
345 
346  return false;
347 }
#define INT64_FORMAT
Definition: c.h:535
#define LOG
Definition: elog.h:31
#define elog(elevel,...)
Definition: elog.h:224
double estimateHyperLogLog(hyperLogLogState *cState)
Definition: hyperloglog.c:186
void * ssup_extra
Definition: sortsupport.h:87
hyperLogLogState abbr_card
Definition: uuid.c:31
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 357 of file uuid.c.

358 {
359  uuid_sortsupport_state *uss = ssup->ssup_extra;
360  pg_uuid_t *authoritative = DatumGetUUIDP(original);
361  Datum res;
362 
363  memcpy(&res, authoritative->data, sizeof(Datum));
364  uss->input_count += 1;
365 
366  if (uss->estimating)
367  {
368  uint32 tmp;
369 
370 #if SIZEOF_DATUM == 8
371  tmp = (uint32) res ^ (uint32) ((uint64) res >> 32);
372 #else /* SIZEOF_DATUM != 8 */
373  tmp = (uint32) res;
374 #endif
375 
377  }
378 
379  /*
380  * Byteswap on little-endian machines.
381  *
382  * This is needed so that ssup_datum_unsigned_cmp() (an unsigned integer
383  * 3-way comparator) works correctly on all platforms. If we didn't do
384  * this, the comparator would have to call memcmp() with a pair of
385  * pointers to the first byte of each abbreviated key, which is slower.
386  */
387  res = DatumBigEndianToNative(res);
388 
389  return res;
390 }
unsigned int uint32
Definition: c.h:493
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:222
uintptr_t Datum
Definition: postgres.h:64
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, res, and SortSupportData::ssup_extra.

Referenced by uuid_sortsupport().

◆ uuid_cmp()

Datum uuid_cmp ( 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_INT32(uuid_internal_cmp(arg1, arg2));
234 }
#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:167
#define PG_GETARG_UUID_P(X)
Definition: uuid.h:40

References PG_GETARG_UUID_P, PG_RETURN_INT32, and uuid_internal_cmp().

◆ uuid_eq()

Datum uuid_eq ( PG_FUNCTION_ARGS  )

Definition at line 191 of file uuid.c.

192 {
193  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
194  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
195 
196  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) == 0);
197 }
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_fast_cmp()

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

Definition at line 276 of file uuid.c.

277 {
278  pg_uuid_t *arg1 = DatumGetUUIDP(x);
279  pg_uuid_t *arg2 = DatumGetUUIDP(y);
280 
281  return uuid_internal_cmp(arg1, arg2);
282 }
int y
Definition: isn.c:72
int x
Definition: isn.c:71

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

Referenced by uuid_sortsupport().

◆ uuid_ge()

Datum uuid_ge ( PG_FUNCTION_ARGS  )

Definition at line 200 of file uuid.c.

201 {
202  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
203  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
204 
205  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) >= 0);
206 }

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_gt()

Datum uuid_gt ( PG_FUNCTION_ARGS  )

Definition at line 209 of file uuid.c.

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

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_hash()

Datum uuid_hash ( PG_FUNCTION_ARGS  )

Definition at line 394 of file uuid.c.

395 {
397 
398  return hash_any(key->data, UUID_LEN);
399 }
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 402 of file uuid.c.

403 {
405 
406  return hash_any_extended(key->data, UUID_LEN, PG_GETARG_INT64(1));
407 }
#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 41 of file uuid.c.

42 {
43  char *uuid_str = PG_GETARG_CSTRING(0);
44  pg_uuid_t *uuid;
45 
46  uuid = (pg_uuid_t *) palloc(sizeof(*uuid));
47  string_to_uuid(uuid_str, uuid, fcinfo->context);
48  PG_RETURN_UUID_P(uuid);
49 }
#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:94

References palloc(), PG_GETARG_CSTRING, PG_RETURN_UUID_P, and string_to_uuid().

Referenced by uuid_generate_internal().

◆ uuid_internal_cmp()

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

Definition at line 167 of file uuid.c.

168 {
169  return memcmp(arg1->data, arg2->data, UUID_LEN);
170 }

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

183 {
184  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
185  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
186 
187  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) <= 0);
188 }

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

174 {
175  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
176  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
177 
178  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) < 0);
179 }

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_ne()

Datum uuid_ne ( PG_FUNCTION_ARGS  )

Definition at line 218 of file uuid.c.

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

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_out()

Datum uuid_out ( PG_FUNCTION_ARGS  )

Definition at line 52 of file uuid.c.

53 {
54  pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
55  static const char hex_chars[] = "0123456789abcdef";
56  char *buf,
57  *p;
58  int i;
59 
60  /* counts for the four hyphens and the zero-terminator */
61  buf = palloc(2 * UUID_LEN + 5);
62  p = buf;
63  for (i = 0; i < UUID_LEN; i++)
64  {
65  int hi;
66  int lo;
67 
68  /*
69  * We print uuid values as a string of 8, 4, 4, 4, and then 12
70  * hexadecimal characters, with each group is separated by a hyphen
71  * ("-"). Therefore, add the hyphens at the appropriate places here.
72  */
73  if (i == 4 || i == 6 || i == 8 || i == 10)
74  *p++ = '-';
75 
76  hi = uuid->data[i] >> 4;
77  lo = uuid->data[i] & 0x0F;
78 
79  *p++ = hex_chars[hi];
80  *p++ = hex_chars[lo];
81  }
82  *p = '\0';
83 
85 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
static char * buf
Definition: pg_test_fsync.c:73

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

145 {
147  pg_uuid_t *uuid;
148 
149  uuid = (pg_uuid_t *) palloc(UUID_LEN);
150  memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN);
151  PG_RETURN_POINTER(uuid);
152 }
#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 155 of file uuid.c.

156 {
157  pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
158  StringInfoData buffer;
159 
160  pq_begintypsend(&buffer);
161  pq_sendbytes(&buffer, uuid->data, UUID_LEN);
163 }
#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_sortsupport()

Datum uuid_sortsupport ( PG_FUNCTION_ARGS  )

Definition at line 240 of file uuid.c.

241 {
243 
244  ssup->comparator = uuid_fast_cmp;
245  ssup->ssup_extra = NULL;
246 
247  if (ssup->abbreviate)
248  {
250  MemoryContext oldcontext;
251 
252  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
253 
254  uss = palloc(sizeof(uuid_sortsupport_state));
255  uss->input_count = 0;
256  uss->estimating = true;
257  initHyperLogLog(&uss->abbr_card, 10);
258 
259  ssup->ssup_extra = uss;
260 
265 
266  MemoryContextSwitchTo(oldcontext);
267  }
268 
269  PG_RETURN_VOID();
270 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349
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:3171
static bool uuid_abbrev_abort(int memtupcount, SortSupport ssup)
Definition: uuid.c:291
static int uuid_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition: uuid.c:276
static Datum uuid_abbrev_convert(Datum original, SortSupport ssup)
Definition: uuid.c:357

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().