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/builtins.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 406 of file uuid.c.

407 {
408  pg_uuid_t *uuid = palloc(UUID_LEN);
409 
410  if (!pg_strong_random(uuid, UUID_LEN))
411  ereport(ERROR,
412  (errcode(ERRCODE_INTERNAL_ERROR),
413  errmsg("could not generate random values")));
414 
415  /*
416  * Set magic numbers for a "version 4" (pseudorandom) UUID, see
417  * http://tools.ietf.org/html/rfc4122#section-4.4
418  */
419  uuid->data[6] = (uuid->data[6] & 0x0f) | 0x40; /* time_hi_and_version */
420  uuid->data[8] = (uuid->data[8] & 0x3f) | 0x80; /* clock_seq_hi_and_reserved */
421 
422  PG_RETURN_UUID_P(uuid);
423 }
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void * palloc(Size size)
Definition: mcxt.c:1226
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 90 of file uuid.c.

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

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

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

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

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

225 {
226  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
227  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
228 
229  PG_RETURN_INT32(uuid_internal_cmp(arg1, arg2));
230 }
#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:163
#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 187 of file uuid.c.

188 {
189  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
190  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
191 
192  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) == 0);
193 }
#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 272 of file uuid.c.

273 {
274  pg_uuid_t *arg1 = DatumGetUUIDP(x);
275  pg_uuid_t *arg2 = DatumGetUUIDP(y);
276 
277  return uuid_internal_cmp(arg1, arg2);
278 }
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 196 of file uuid.c.

197 {
198  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
199  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
200 
201  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) >= 0);
202 }

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_gt()

Datum uuid_gt ( PG_FUNCTION_ARGS  )

Definition at line 205 of file uuid.c.

206 {
207  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
208  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
209 
210  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) > 0);
211 }

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_hash()

Datum uuid_hash ( PG_FUNCTION_ARGS  )

Definition at line 390 of file uuid.c.

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

399 {
401 
402  return hash_any_extended(key->data, UUID_LEN, PG_GETARG_INT64(1));
403 }
#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:90

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

164 {
165  return memcmp(arg1->data, arg2->data, UUID_LEN);
166 }

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

179 {
180  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
181  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
182 
183  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) <= 0);
184 }

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

170 {
171  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
172  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
173 
174  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) < 0);
175 }

References PG_GETARG_UUID_P, PG_RETURN_BOOL, and uuid_internal_cmp().

◆ uuid_ne()

Datum uuid_ne ( PG_FUNCTION_ARGS  )

Definition at line 214 of file uuid.c.

215 {
216  pg_uuid_t *arg1 = PG_GETARG_UUID_P(0);
217  pg_uuid_t *arg2 = PG_GETARG_UUID_P(1);
218 
219  PG_RETURN_BOOL(uuid_internal_cmp(arg1, arg2) != 0);
220 }

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";
57  int i;
58 
60  for (i = 0; i < UUID_LEN; i++)
61  {
62  int hi;
63  int lo;
64 
65  /*
66  * We print uuid values as a string of 8, 4, 4, 4, and then 12
67  * hexadecimal characters, with each group is separated by a hyphen
68  * ("-"). Therefore, add the hyphens at the appropriate places here.
69  */
70  if (i == 4 || i == 6 || i == 8 || i == 10)
72 
73  hi = uuid->data[i] >> 4;
74  lo = uuid->data[i] & 0x0F;
75 
76  appendStringInfoChar(&buf, hex_chars[hi]);
77  appendStringInfoChar(&buf, hex_chars[lo]);
78  }
79 
80  PG_RETURN_CSTRING(buf.data);
81 }
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
static char * buf
Definition: pg_test_fsync.c:73
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References appendStringInfoChar(), buf, pg_uuid_t::data, i, initStringInfo(), PG_GETARG_UUID_P, PG_RETURN_CSTRING, and UUID_LEN.

◆ uuid_recv()

Datum uuid_recv ( PG_FUNCTION_ARGS  )

Definition at line 140 of file uuid.c.

141 {
143  pg_uuid_t *uuid;
144 
145  uuid = (pg_uuid_t *) palloc(UUID_LEN);
146  memcpy(uuid->data, pq_getmsgbytes(buffer, UUID_LEN), UUID_LEN);
147  PG_RETURN_POINTER(uuid);
148 }
#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:511
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 151 of file uuid.c.

152 {
153  pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
154  StringInfoData buffer;
155 
156  pq_begintypsend(&buffer);
157  pq_sendbytes(&buffer, uuid->data, UUID_LEN);
159 }
#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:329
bytea * pq_endtypsend(StringInfo buf)
Definition: pqformat.c:349

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

237 {
239 
240  ssup->comparator = uuid_fast_cmp;
241  ssup->ssup_extra = NULL;
242 
243  if (ssup->abbreviate)
244  {
246  MemoryContext oldcontext;
247 
248  oldcontext = MemoryContextSwitchTo(ssup->ssup_cxt);
249 
250  uss = palloc(sizeof(uuid_sortsupport_state));
251  uss->input_count = 0;
252  uss->estimating = true;
253  initHyperLogLog(&uss->abbr_card, 10);
254 
255  ssup->ssup_extra = uss;
256 
261 
262  MemoryContextSwitchTo(oldcontext);
263  }
264 
265  PG_RETURN_VOID();
266 }
#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:138
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:3174
static bool uuid_abbrev_abort(int memtupcount, SortSupport ssup)
Definition: uuid.c:287
static int uuid_fast_cmp(Datum x, Datum y, SortSupport ssup)
Definition: uuid.c:272
static Datum uuid_abbrev_convert(Datum original, SortSupport ssup)
Definition: uuid.c:353

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