PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
hashfunc.c File Reference
#include "postgres.h"
#include "common/hashfn.h"
#include "utils/float.h"
#include "utils/fmgrprotos.h"
#include "utils/pg_locale.h"
#include "varatt.h"
Include dependency graph for hashfunc.c:

Go to the source code of this file.

Functions

Datum hashchar (PG_FUNCTION_ARGS)
 
Datum hashcharextended (PG_FUNCTION_ARGS)
 
Datum hashint2 (PG_FUNCTION_ARGS)
 
Datum hashint2extended (PG_FUNCTION_ARGS)
 
Datum hashint4 (PG_FUNCTION_ARGS)
 
Datum hashint4extended (PG_FUNCTION_ARGS)
 
Datum hashint8 (PG_FUNCTION_ARGS)
 
Datum hashint8extended (PG_FUNCTION_ARGS)
 
Datum hashoid (PG_FUNCTION_ARGS)
 
Datum hashoidextended (PG_FUNCTION_ARGS)
 
Datum hashenum (PG_FUNCTION_ARGS)
 
Datum hashenumextended (PG_FUNCTION_ARGS)
 
Datum hashfloat4 (PG_FUNCTION_ARGS)
 
Datum hashfloat4extended (PG_FUNCTION_ARGS)
 
Datum hashfloat8 (PG_FUNCTION_ARGS)
 
Datum hashfloat8extended (PG_FUNCTION_ARGS)
 
Datum hashoidvector (PG_FUNCTION_ARGS)
 
Datum hashoidvectorextended (PG_FUNCTION_ARGS)
 
Datum hashname (PG_FUNCTION_ARGS)
 
Datum hashnameextended (PG_FUNCTION_ARGS)
 
Datum hashtext (PG_FUNCTION_ARGS)
 
Datum hashtextextended (PG_FUNCTION_ARGS)
 
Datum hashvarlena (PG_FUNCTION_ARGS)
 
Datum hashvarlenaextended (PG_FUNCTION_ARGS)
 
Datum hashbytea (PG_FUNCTION_ARGS)
 
Datum hashbyteaextended (PG_FUNCTION_ARGS)
 

Function Documentation

◆ hashbytea()

Datum hashbytea ( PG_FUNCTION_ARGS  )

Definition at line 416 of file hashfunc.c.

417 {
418  return hashvarlena(fcinfo);
419 }
Datum hashvarlena(PG_FUNCTION_ARGS)
Definition: hashfunc.c:386

References hashvarlena().

◆ hashbyteaextended()

Datum hashbyteaextended ( PG_FUNCTION_ARGS  )

Definition at line 422 of file hashfunc.c.

423 {
424  return hashvarlenaextended(fcinfo);
425 }
Datum hashvarlenaextended(PG_FUNCTION_ARGS)
Definition: hashfunc.c:401

References hashvarlenaextended().

◆ hashchar()

Datum hashchar ( PG_FUNCTION_ARGS  )

Definition at line 47 of file hashfunc.c.

48 {
49  return hash_uint32((int32) PG_GETARG_CHAR(0));
50 }
signed int int32
Definition: c.h:508
#define PG_GETARG_CHAR(n)
Definition: fmgr.h:273
static Datum hash_uint32(uint32 k)
Definition: hashfn.h:43

References hash_uint32(), and PG_GETARG_CHAR.

◆ hashcharextended()

Datum hashcharextended ( PG_FUNCTION_ARGS  )

Definition at line 53 of file hashfunc.c.

54 {
56 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static Datum hash_uint32_extended(uint32 k, uint64 seed)
Definition: hashfn.h:49

References hash_uint32_extended(), PG_GETARG_CHAR, and PG_GETARG_INT64.

Referenced by JsonbHashScalarValueExtended().

◆ hashenum()

Datum hashenum ( PG_FUNCTION_ARGS  )

Definition at line 128 of file hashfunc.c.

129 {
130  return hash_uint32((uint32) PG_GETARG_OID(0));
131 }
unsigned int uint32
Definition: c.h:518
#define PG_GETARG_OID(n)
Definition: fmgr.h:275

References hash_uint32(), and PG_GETARG_OID.

◆ hashenumextended()

Datum hashenumextended ( PG_FUNCTION_ARGS  )

Definition at line 134 of file hashfunc.c.

135 {
137 }

References hash_uint32_extended(), PG_GETARG_INT64, and PG_GETARG_OID.

◆ hashfloat4()

Datum hashfloat4 ( PG_FUNCTION_ARGS  )

Definition at line 140 of file hashfunc.c.

141 {
143  float8 key8;
144 
145  /*
146  * On IEEE-float machines, minus zero and zero have different bit patterns
147  * but should compare as equal. We must ensure that they have the same
148  * hash value, which is most reliably done this way:
149  */
150  if (key == (float4) 0)
151  PG_RETURN_UINT32(0);
152 
153  /*
154  * To support cross-type hashing of float8 and float4, we want to return
155  * the same hash value hashfloat8 would produce for an equal float8 value.
156  * So, widen the value to float8 and hash that. (We must do this rather
157  * than have hashfloat8 try to narrow its value to float4; that could fail
158  * on overflow.)
159  */
160  key8 = key;
161 
162  /*
163  * Similarly, NaNs can have different bit patterns but they should all
164  * compare as equal. For backwards-compatibility reasons we force them to
165  * have the hash value of a standard float8 NaN. (You'd think we could
166  * replace key with a float4 NaN and then widen it; but on some old
167  * platforms, that way produces a different bit pattern.)
168  */
169  if (isnan(key8))
170  key8 = get_float8_nan();
171 
172  return hash_any((unsigned char *) &key8, sizeof(key8));
173 }
double float8
Definition: c.h:633
float float4
Definition: c.h:632
static float8 get_float8_nan(void)
Definition: float.h:123
#define PG_RETURN_UINT32(x)
Definition: fmgr.h:355
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
static Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.h:31

References get_float8_nan(), hash_any(), sort-test::key, PG_GETARG_FLOAT4, and PG_RETURN_UINT32.

◆ hashfloat4extended()

Datum hashfloat4extended ( PG_FUNCTION_ARGS  )

Definition at line 176 of file hashfunc.c.

177 {
179  uint64 seed = PG_GETARG_INT64(1);
180  float8 key8;
181 
182  /* Same approach as hashfloat4 */
183  if (key == (float4) 0)
184  PG_RETURN_UINT64(seed);
185  key8 = key;
186  if (isnan(key8))
187  key8 = get_float8_nan();
188 
189  return hash_any_extended((unsigned char *) &key8, sizeof(key8), seed);
190 }
#define PG_RETURN_UINT64(x)
Definition: fmgr.h:369
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37

References get_float8_nan(), hash_any_extended(), sort-test::key, PG_GETARG_FLOAT4, PG_GETARG_INT64, and PG_RETURN_UINT64.

◆ hashfloat8()

Datum hashfloat8 ( PG_FUNCTION_ARGS  )

Definition at line 193 of file hashfunc.c.

194 {
196 
197  /*
198  * On IEEE-float machines, minus zero and zero have different bit patterns
199  * but should compare as equal. We must ensure that they have the same
200  * hash value, which is most reliably done this way:
201  */
202  if (key == (float8) 0)
203  PG_RETURN_UINT32(0);
204 
205  /*
206  * Similarly, NaNs can have different bit patterns but they should all
207  * compare as equal. For backwards-compatibility reasons we force them to
208  * have the hash value of a standard NaN.
209  */
210  if (isnan(key))
211  key = get_float8_nan();
212 
213  return hash_any((unsigned char *) &key, sizeof(key));
214 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282

References get_float8_nan(), hash_any(), sort-test::key, PG_GETARG_FLOAT8, and PG_RETURN_UINT32.

Referenced by tablesample_init().

◆ hashfloat8extended()

Datum hashfloat8extended ( PG_FUNCTION_ARGS  )

Definition at line 217 of file hashfunc.c.

218 {
220  uint64 seed = PG_GETARG_INT64(1);
221 
222  /* Same approach as hashfloat8 */
223  if (key == (float8) 0)
224  PG_RETURN_UINT64(seed);
225  if (isnan(key))
226  key = get_float8_nan();
227 
228  return hash_any_extended((unsigned char *) &key, sizeof(key), seed);
229 }

References get_float8_nan(), hash_any_extended(), sort-test::key, PG_GETARG_FLOAT8, PG_GETARG_INT64, and PG_RETURN_UINT64.

◆ hashint2()

Datum hashint2 ( PG_FUNCTION_ARGS  )

Definition at line 59 of file hashfunc.c.

60 {
61  return hash_uint32((int32) PG_GETARG_INT16(0));
62 }
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

References hash_uint32(), and PG_GETARG_INT16.

◆ hashint2extended()

Datum hashint2extended ( PG_FUNCTION_ARGS  )

Definition at line 65 of file hashfunc.c.

66 {
68 }

References hash_uint32_extended(), PG_GETARG_INT16, and PG_GETARG_INT64.

◆ hashint4()

Datum hashint4 ( PG_FUNCTION_ARGS  )

Definition at line 71 of file hashfunc.c.

72 {
73  return hash_uint32(PG_GETARG_INT32(0));
74 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269

References hash_uint32(), and PG_GETARG_INT32.

◆ hashint4extended()

Datum hashint4extended ( PG_FUNCTION_ARGS  )

Definition at line 77 of file hashfunc.c.

78 {
80 }

References hash_uint32_extended(), PG_GETARG_INT32, and PG_GETARG_INT64.

◆ hashint8()

Datum hashint8 ( PG_FUNCTION_ARGS  )

Definition at line 83 of file hashfunc.c.

84 {
85  /*
86  * The idea here is to produce a hash value compatible with the values
87  * produced by hashint4 and hashint2 for logically equal inputs; this is
88  * necessary to support cross-type hash joins across these input types.
89  * Since all three types are signed, we can xor the high half of the int8
90  * value if the sign is positive, or the complement of the high half when
91  * the sign is negative.
92  */
93  int64 val = PG_GETARG_INT64(0);
94  uint32 lohalf = (uint32) val;
95  uint32 hihalf = (uint32) (val >> 32);
96 
97  lohalf ^= (val >= 0) ? hihalf : ~hihalf;
98 
99  return hash_uint32(lohalf);
100 }
long val
Definition: informix.c:689

References hash_uint32(), PG_GETARG_INT64, and val.

Referenced by hashxid8(), interval_hash(), pg_lsn_hash(), time_hash(), timestamp_hash(), timestamptz_hash(), and timetz_hash().

◆ hashint8extended()

Datum hashint8extended ( PG_FUNCTION_ARGS  )

Definition at line 103 of file hashfunc.c.

104 {
105  /* Same approach as hashint8 */
106  int64 val = PG_GETARG_INT64(0);
107  uint32 lohalf = (uint32) val;
108  uint32 hihalf = (uint32) (val >> 32);
109 
110  lohalf ^= (val >= 0) ? hihalf : ~hihalf;
111 
112  return hash_uint32_extended(lohalf, PG_GETARG_INT64(1));
113 }

References hash_uint32_extended(), PG_GETARG_INT64, and val.

Referenced by hashxid8extended(), interval_hash_extended(), pg_lsn_hash_extended(), time_hash_extended(), timestamp_hash_extended(), timestamptz_hash_extended(), and timetz_hash_extended().

◆ hashname()

Datum hashname ( PG_FUNCTION_ARGS  )

Definition at line 250 of file hashfunc.c.

251 {
252  char *key = NameStr(*PG_GETARG_NAME(0));
253 
254  return hash_any((unsigned char *) key, strlen(key));
255 }
#define NameStr(name)
Definition: c.h:749
#define PG_GETARG_NAME(n)
Definition: fmgr.h:278

References hash_any(), sort-test::key, NameStr, and PG_GETARG_NAME.

◆ hashnameextended()

Datum hashnameextended ( PG_FUNCTION_ARGS  )

Definition at line 258 of file hashfunc.c.

259 {
260  char *key = NameStr(*PG_GETARG_NAME(0));
261 
262  return hash_any_extended((unsigned char *) key, strlen(key),
263  PG_GETARG_INT64(1));
264 }

References hash_any_extended(), sort-test::key, NameStr, PG_GETARG_INT64, and PG_GETARG_NAME.

◆ hashoid()

Datum hashoid ( PG_FUNCTION_ARGS  )

Definition at line 116 of file hashfunc.c.

117 {
118  return hash_uint32((uint32) PG_GETARG_OID(0));
119 }

References hash_uint32(), and PG_GETARG_OID.

◆ hashoidextended()

Datum hashoidextended ( PG_FUNCTION_ARGS  )

Definition at line 122 of file hashfunc.c.

123 {
125 }

References hash_uint32_extended(), PG_GETARG_INT64, and PG_GETARG_OID.

◆ hashoidvector()

Datum hashoidvector ( PG_FUNCTION_ARGS  )

Definition at line 232 of file hashfunc.c.

233 {
235 
236  return hash_any((unsigned char *) key->values, key->dim1 * sizeof(Oid));
237 }
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
unsigned int Oid
Definition: postgres_ext.h:31
Definition: c.h:729

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

Referenced by oidvectorhashfast().

◆ hashoidvectorextended()

Datum hashoidvectorextended ( PG_FUNCTION_ARGS  )

Definition at line 240 of file hashfunc.c.

241 {
243 
244  return hash_any_extended((unsigned char *) key->values,
245  key->dim1 * sizeof(Oid),
246  PG_GETARG_INT64(1));
247 }

References hash_any_extended(), sort-test::key, PG_GETARG_INT64, and PG_GETARG_POINTER.

◆ hashtext()

Datum hashtext ( PG_FUNCTION_ARGS  )

Definition at line 267 of file hashfunc.c.

268 {
269  text *key = PG_GETARG_TEXT_PP(0);
271  pg_locale_t mylocale;
272  Datum result;
273 
274  if (!collid)
275  ereport(ERROR,
276  (errcode(ERRCODE_INDETERMINATE_COLLATION),
277  errmsg("could not determine which collation to use for string hashing"),
278  errhint("Use the COLLATE clause to set the collation explicitly.")));
279 
281 
282  if (mylocale->deterministic)
283  {
284  result = hash_any((unsigned char *) VARDATA_ANY(key),
286  }
287  else
288  {
289  Size bsize,
290  rsize;
291  char *buf;
292  const char *keydata = VARDATA_ANY(key);
293  size_t keylen = VARSIZE_ANY_EXHDR(key);
294 
295 
296  bsize = pg_strnxfrm(NULL, 0, keydata, keylen, mylocale);
297  buf = palloc(bsize + 1);
298 
299  rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
300 
301  /* the second call may return a smaller value than the first */
302  if (rsize > bsize)
303  elog(ERROR, "pg_strnxfrm() returned unexpected result");
304 
305  /*
306  * In principle, there's no reason to include the terminating NUL
307  * character in the hash, but it was done before and the behavior must
308  * be preserved.
309  */
310  result = hash_any((uint8_t *) buf, bsize + 1);
311 
312  pfree(buf);
313  }
314 
315  /* Avoid leaking memory for toasted inputs */
316  PG_FREE_IF_COPY(key, 0);
317 
318  return result;
319 }
size_t Size
Definition: c.h:608
Oid collid
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_FREE_IF_COPY(ptr, n)
Definition: fmgr.h:260
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GET_COLLATION()
Definition: fmgr.h:198
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
size_t pg_strnxfrm(char *dest, size_t destsize, const char *src, ssize_t srclen, pg_locale_t locale)
Definition: pg_locale.c:1733
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1451
static char * buf
Definition: pg_test_fsync.c:72
uintptr_t Datum
Definition: postgres.h:64
bool deterministic
Definition: pg_locale.h:82
Definition: c.h:690
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317

References buf, collid, pg_locale_struct::deterministic, elog, ereport, errcode(), errhint(), errmsg(), ERROR, hash_any(), sort-test::key, palloc(), pfree(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_TEXT_PP, pg_newlocale_from_collation(), pg_strnxfrm(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by texthashfast().

◆ hashtextextended()

Datum hashtextextended ( PG_FUNCTION_ARGS  )

Definition at line 322 of file hashfunc.c.

323 {
324  text *key = PG_GETARG_TEXT_PP(0);
326  pg_locale_t mylocale;
327  Datum result;
328 
329  if (!collid)
330  ereport(ERROR,
331  (errcode(ERRCODE_INDETERMINATE_COLLATION),
332  errmsg("could not determine which collation to use for string hashing"),
333  errhint("Use the COLLATE clause to set the collation explicitly.")));
334 
336 
337  if (mylocale->deterministic)
338  {
339  result = hash_any_extended((unsigned char *) VARDATA_ANY(key),
341  PG_GETARG_INT64(1));
342  }
343  else
344  {
345  Size bsize,
346  rsize;
347  char *buf;
348  const char *keydata = VARDATA_ANY(key);
349  size_t keylen = VARSIZE_ANY_EXHDR(key);
350 
351  bsize = pg_strnxfrm(NULL, 0, keydata, keylen, mylocale);
352  buf = palloc(bsize + 1);
353 
354  rsize = pg_strnxfrm(buf, bsize + 1, keydata, keylen, mylocale);
355 
356  /* the second call may return a smaller value than the first */
357  if (rsize > bsize)
358  elog(ERROR, "pg_strnxfrm() returned unexpected result");
359 
360  /*
361  * In principle, there's no reason to include the terminating NUL
362  * character in the hash, but it was done before and the behavior must
363  * be preserved.
364  */
365  result = hash_any_extended((uint8_t *) buf, bsize + 1,
366  PG_GETARG_INT64(1));
367 
368  pfree(buf);
369  }
370 
371  PG_FREE_IF_COPY(key, 0);
372 
373  return result;
374 }

References buf, collid, pg_locale_struct::deterministic, elog, ereport, errcode(), errhint(), errmsg(), ERROR, hash_any_extended(), sort-test::key, palloc(), pfree(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_INT64, PG_GETARG_TEXT_PP, pg_newlocale_from_collation(), pg_strnxfrm(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

◆ hashvarlena()

Datum hashvarlena ( PG_FUNCTION_ARGS  )

Definition at line 386 of file hashfunc.c.

387 {
388  struct varlena *key = PG_GETARG_VARLENA_PP(0);
389  Datum result;
390 
391  result = hash_any((unsigned char *) VARDATA_ANY(key),
393 
394  /* Avoid leaking memory for toasted inputs */
395  PG_FREE_IF_COPY(key, 0);
396 
397  return result;
398 }
#define PG_GETARG_VARLENA_PP(n)
Definition: fmgr.h:289

References hash_any(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_VARLENA_PP, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by hashbytea().

◆ hashvarlenaextended()

Datum hashvarlenaextended ( PG_FUNCTION_ARGS  )

Definition at line 401 of file hashfunc.c.

402 {
403  struct varlena *key = PG_GETARG_VARLENA_PP(0);
404  Datum result;
405 
406  result = hash_any_extended((unsigned char *) VARDATA_ANY(key),
408  PG_GETARG_INT64(1));
409 
410  PG_FREE_IF_COPY(key, 0);
411 
412  return result;
413 }

References hash_any_extended(), sort-test::key, PG_FREE_IF_COPY, PG_GETARG_INT64, PG_GETARG_VARLENA_PP, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by hashbyteaextended().