PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hashutil.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "access/reloptions.h"
#include "access/relscan.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
Include dependency graph for hashutil.c:

Go to the source code of this file.

Macros

#define CALC_NEW_BUCKET(old_bucket, lowmask)   old_bucket | (lowmask + 1)
 

Functions

bool _hash_checkqual (IndexScanDesc scan, IndexTuple itup)
 
uint32 _hash_datum2hashkey (Relation rel, Datum key)
 
uint32 _hash_datum2hashkey_type (Relation rel, Datum key, Oid keytype)
 
Bucket _hash_hashkey2bucket (uint32 hashkey, uint32 maxbucket, uint32 highmask, uint32 lowmask)
 
uint32 _hash_log2 (uint32 num)
 
void _hash_checkpage (Relation rel, Buffer buf, int flags)
 
byteahashoptions (Datum reloptions, bool validate)
 
uint32 _hash_get_indextuple_hashkey (IndexTuple itup)
 
bool _hash_convert_tuple (Relation index, Datum *user_values, bool *user_isnull, Datum *index_values, bool *index_isnull)
 
OffsetNumber _hash_binsearch (Page page, uint32 hash_value)
 
OffsetNumber _hash_binsearch_last (Page page, uint32 hash_value)
 
BlockNumber _hash_get_oldblock_from_newbucket (Relation rel, Bucket new_bucket)
 
BlockNumber _hash_get_newblock_from_oldbucket (Relation rel, Bucket old_bucket)
 
Bucket _hash_get_newbucket_from_oldbucket (Relation rel, Bucket old_bucket, uint32 lowmask, uint32 maxbucket)
 

Macro Definition Documentation

#define CALC_NEW_BUCKET (   old_bucket,
  lowmask 
)    old_bucket | (lowmask + 1)

Definition at line 23 of file hashutil.c.

Referenced by _hash_get_newbucket_from_oldbucket().

Function Documentation

OffsetNumber _hash_binsearch ( Page  page,
uint32  hash_value 
)

Definition at line 291 of file hashutil.c.

References _hash_get_indextuple_hashkey(), Assert, FirstOffsetNumber, lower(), OffsetNumberIsValid, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, and upper().

Referenced by _hash_pgaddtup(), and _hash_step().

292 {
295 
296  /* Loop invariant: lower <= desired place <= upper */
297  upper = PageGetMaxOffsetNumber(page) + 1;
298  lower = FirstOffsetNumber;
299 
300  while (upper > lower)
301  {
302  OffsetNumber off;
303  IndexTuple itup;
304  uint32 hashkey;
305 
306  off = (upper + lower) / 2;
308 
309  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, off));
310  hashkey = _hash_get_indextuple_hashkey(itup);
311  if (hashkey < hash_value)
312  lower = off + 1;
313  else
314  upper = off;
315  }
316 
317  return lower;
318 }
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:74
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
uint16 OffsetNumber
Definition: off.h:24
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:232
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
unsigned int uint32
Definition: c.h:265
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define Assert(condition)
Definition: c.h:671
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
OffsetNumber _hash_binsearch_last ( Page  page,
uint32  hash_value 
)

Definition at line 329 of file hashutil.c.

References _hash_get_indextuple_hashkey(), Assert, FirstOffsetNumber, lower(), OffsetNumberIsValid, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, and upper().

Referenced by _hash_step().

330 {
333 
334  /* Loop invariant: lower <= desired place <= upper */
335  upper = PageGetMaxOffsetNumber(page);
336  lower = FirstOffsetNumber - 1;
337 
338  while (upper > lower)
339  {
340  IndexTuple itup;
341  OffsetNumber off;
342  uint32 hashkey;
343 
344  off = (upper + lower + 1) / 2;
346 
347  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, off));
348  hashkey = _hash_get_indextuple_hashkey(itup);
349  if (hashkey > hash_value)
350  upper = off - 1;
351  else
352  lower = off;
353  }
354 
355  return lower;
356 }
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:43
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:74
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
uint16 OffsetNumber
Definition: off.h:24
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:232
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
unsigned int uint32
Definition: c.h:265
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define Assert(condition)
Definition: c.h:671
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
void _hash_checkpage ( Relation  rel,
Buffer  buf,
int  flags 
)

Definition at line 158 of file hashutil.c.

References BufferGetBlockNumber(), BufferGetPage, ereport, errcode(), errhint(), errmsg(), ERROR, HASH_MAGIC, HASH_VERSION, HashMetaPageData::hashm_magic, HashMetaPageData::hashm_version, HashPageOpaqueData::hasho_flag, HashPageGetMeta, LH_META_PAGE, MAXALIGN, PageGetSpecialPointer, PageGetSpecialSize, PageIsNew, and RelationGetRelationName.

Referenced by _hash_addovflpage(), _hash_expandtable(), _hash_first(), _hash_freeovflpage(), _hash_getbuf(), _hash_getbuf_with_condlock_cleanup(), _hash_getbuf_with_strategy(), _hash_getovflpage(), _hash_next(), _hash_pgaddtup(), _hash_step(), and hashbulkdelete().

159 {
160  Page page = BufferGetPage(buf);
161 
162  /*
163  * ReadBuffer verifies that every newly-read page passes
164  * PageHeaderIsValid, which means it either contains a reasonably sane
165  * page header or is all-zero. We have to defend against the all-zero
166  * case, however.
167  */
168  if (PageIsNew(page))
169  ereport(ERROR,
170  (errcode(ERRCODE_INDEX_CORRUPTED),
171  errmsg("index \"%s\" contains unexpected zero page at block %u",
174  errhint("Please REINDEX it.")));
175 
176  /*
177  * Additionally check that the special area looks sane.
178  */
179  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(HashPageOpaqueData)))
180  ereport(ERROR,
181  (errcode(ERRCODE_INDEX_CORRUPTED),
182  errmsg("index \"%s\" contains corrupted page at block %u",
185  errhint("Please REINDEX it.")));
186 
187  if (flags)
188  {
190 
191  if ((opaque->hasho_flag & flags) == 0)
192  ereport(ERROR,
193  (errcode(ERRCODE_INDEX_CORRUPTED),
194  errmsg("index \"%s\" contains corrupted page at block %u",
197  errhint("Please REINDEX it.")));
198  }
199 
200  /*
201  * When checking the metapage, also verify magic number and version.
202  */
203  if (flags == LH_META_PAGE)
204  {
205  HashMetaPage metap = HashPageGetMeta(page);
206 
207  if (metap->hashm_magic != HASH_MAGIC)
208  ereport(ERROR,
209  (errcode(ERRCODE_INDEX_CORRUPTED),
210  errmsg("index \"%s\" is not a hash index",
211  RelationGetRelationName(rel))));
212 
213  if (metap->hashm_version != HASH_VERSION)
214  ereport(ERROR,
215  (errcode(ERRCODE_INDEX_CORRUPTED),
216  errmsg("index \"%s\" has wrong hash version",
218  errhint("Please REINDEX it.")));
219  }
220 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define LH_META_PAGE
Definition: hash.h:56
uint32 hashm_magic
Definition: hash.h:176
int errcode(int sqlerrcode)
Definition: elog.c:575
#define HASH_VERSION
Definition: hash.h:149
#define HASH_MAGIC
Definition: hash.h:148
#define ERROR
Definition: elog.h:43
uint32 hashm_version
Definition: hash.h:177
static char * buf
Definition: pg_test_fsync.c:65
#define RelationGetRelationName(relation)
Definition: rel.h:433
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
#define PageGetSpecialPointer(page)
Definition: bufpage.h:323
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:84
#define MAXALIGN(LEN)
Definition: c.h:584
#define PageGetSpecialSize(page)
Definition: bufpage.h:297
uint16 hasho_flag
Definition: hash.h:80
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2588
#define PageIsNew(page)
Definition: bufpage.h:226
#define HashPageGetMeta(page)
Definition: hash.h:238
int errmsg(const char *fmt,...)
Definition: elog.c:797
Pointer Page
Definition: bufpage.h:74
bool _hash_checkqual ( IndexScanDesc  scan,
IndexTuple  itup 
)

Definition at line 30 of file hashutil.c.

References DatumGetBool, FunctionCall2Coll(), index_getattr, IndexScanDescData::indexRelation, IndexScanDescData::keyData, IndexScanDescData::numberOfKeys, RelationGetDescr, ScanKeyData::sk_argument, ScanKeyData::sk_attno, ScanKeyData::sk_collation, ScanKeyData::sk_flags, ScanKeyData::sk_func, SK_ISNULL, and test().

Referenced by _hash_step().

31 {
32  /*
33  * Currently, we can't check any of the scan conditions since we do not
34  * have the original index entry value to supply to the sk_func. Always
35  * return true; we expect that hashgettuple already set the recheck flag
36  * to make the main indexscan code do it.
37  */
38 #ifdef NOT_USED
39  TupleDesc tupdesc = RelationGetDescr(scan->indexRelation);
40  ScanKey key = scan->keyData;
41  int scanKeySize = scan->numberOfKeys;
42 
43  while (scanKeySize > 0)
44  {
45  Datum datum;
46  bool isNull;
47  Datum test;
48 
49  datum = index_getattr(itup,
50  key->sk_attno,
51  tupdesc,
52  &isNull);
53 
54  /* assume sk_func is strict */
55  if (isNull)
56  return false;
57  if (key->sk_flags & SK_ISNULL)
58  return false;
59 
60  test = FunctionCall2Coll(&key->sk_func, key->sk_collation,
61  datum, key->sk_argument);
62 
63  if (!DatumGetBool(test))
64  return false;
65 
66  key++;
67  scanKeySize--;
68  }
69 #endif
70 
71  return true;
72 }
static void test(void)
#define RelationGetDescr(relation)
Definition: rel.h:425
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1306
Relation indexRelation
Definition: relscan.h:89
FmgrInfo sk_func
Definition: skey.h:71
#define DatumGetBool(X)
Definition: postgres.h:401
#define SK_ISNULL
Definition: skey.h:115
uintptr_t Datum
Definition: postgres.h:374
int sk_flags
Definition: skey.h:66
#define index_getattr(tup, attnum, tupleDesc, isnull)
Definition: itup.h:100
ScanKey keyData
Definition: relscan.h:93
Oid sk_collation
Definition: skey.h:70
Datum sk_argument
Definition: skey.h:72
AttrNumber sk_attno
Definition: skey.h:67
bool _hash_convert_tuple ( Relation  index,
Datum user_values,
bool user_isnull,
Datum index_values,
bool index_isnull 
)

Definition at line 259 of file hashutil.c.

References _hash_datum2hashkey(), and UInt32GetDatum.

Referenced by hashbuildCallback(), and hashinsert().

262 {
263  uint32 hashkey;
264 
265  /*
266  * We do not insert null values into hash indexes. This is okay because
267  * the only supported search operator is '=', and we assume it is strict.
268  */
269  if (user_isnull[0])
270  return false;
271 
272  hashkey = _hash_datum2hashkey(index, user_values[0]);
273  index_values[0] = UInt32GetDatum(hashkey);
274  index_isnull[0] = false;
275  return true;
276 }
uint32 _hash_datum2hashkey(Relation rel, Datum key)
Definition: hashutil.c:81
unsigned int uint32
Definition: c.h:265
#define UInt32GetDatum(X)
Definition: postgres.h:501
uint32 _hash_datum2hashkey ( Relation  rel,
Datum  key 
)

Definition at line 81 of file hashutil.c.

References DatumGetUInt32, FunctionCall1Coll(), HASHPROC, index_getprocinfo(), and RelationData::rd_indcollation.

Referenced by _hash_convert_tuple(), and _hash_first().

82 {
83  FmgrInfo *procinfo;
84  Oid collation;
85 
86  /* XXX assumes index has only one attribute */
87  procinfo = index_getprocinfo(rel, 1, HASHPROC);
88  collation = rel->rd_indcollation[0];
89 
90  return DatumGetUInt32(FunctionCall1Coll(procinfo, collation, key));
91 }
#define DatumGetUInt32(X)
Definition: postgres.h:494
Definition: fmgr.h:53
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:855
unsigned int Oid
Definition: postgres_ext.h:31
Oid * rd_indcollation
Definition: rel.h:189
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1286
#define HASHPROC
Definition: hash.h:269
uint32 _hash_datum2hashkey_type ( Relation  rel,
Datum  key,
Oid  keytype 
)

Definition at line 101 of file hashutil.c.

References DatumGetUInt32, elog, ERROR, get_opfamily_proc(), HASHPROC, OidFunctionCall1Coll(), RelationData::rd_indcollation, RelationData::rd_opfamily, RegProcedureIsValid, and RelationGetRelationName.

Referenced by _hash_first().

102 {
103  RegProcedure hash_proc;
104  Oid collation;
105 
106  /* XXX assumes index has only one attribute */
107  hash_proc = get_opfamily_proc(rel->rd_opfamily[0],
108  keytype,
109  keytype,
110  HASHPROC);
111  if (!RegProcedureIsValid(hash_proc))
112  elog(ERROR, "missing support function %d(%u,%u) for index \"%s\"",
113  HASHPROC, keytype, keytype,
115  collation = rel->rd_indcollation[0];
116 
117  return DatumGetUInt32(OidFunctionCall1Coll(hash_proc, collation, key));
118 }
#define DatumGetUInt32(X)
Definition: postgres.h:494
regproc RegProcedure
Definition: c.h:392
unsigned int Oid
Definition: postgres_ext.h:31
Oid * rd_indcollation
Definition: rel.h:189
#define ERROR
Definition: elog.h:43
#define RegProcedureIsValid(p)
Definition: c.h:536
#define RelationGetRelationName(relation)
Definition: rel.h:433
Oid * rd_opfamily
Definition: rel.h:178
Datum OidFunctionCall1Coll(Oid functionId, Oid collation, Datum arg1)
Definition: fmgr.c:1578
Oid get_opfamily_proc(Oid opfamily, Oid lefttype, Oid righttype, int16 procnum)
Definition: lsyscache.c:744
#define HASHPROC
Definition: hash.h:269
#define elog
Definition: elog.h:219
uint32 _hash_get_indextuple_hashkey ( IndexTuple  itup)

Definition at line 232 of file hashutil.c.

References IndexInfoFindDataOffset, and IndexTupleData::t_info.

Referenced by _h_indexbuild(), _hash_binsearch(), _hash_binsearch_last(), _hash_doinsert(), _hash_pgaddtup(), _hash_splitbucket_guts(), _hash_step(), hash_page_items(), and hashbucketcleanup().

233 {
234  char *attp;
235 
236  /*
237  * We assume the hash key is the first attribute and can't be null, so
238  * this can be done crudely but very very cheaply ...
239  */
240  attp = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
241  return *((uint32 *) attp);
242 }
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
unsigned int uint32
Definition: c.h:265
unsigned short t_info
Definition: itup.h:49
BlockNumber _hash_get_newblock_from_oldbucket ( Relation  rel,
Bucket  old_bucket 
)

Definition at line 402 of file hashutil.c.

References _hash_get_newbucket_from_oldbucket(), _hash_getbuf(), _hash_relbuf(), BUCKET_TO_BLKNO, BufferGetPage, HASH_METAPAGE, HASH_READ, HashMetaPageData::hashm_lowmask, HashMetaPageData::hashm_maxbucket, HashPageGetMeta, and LH_META_PAGE.

Referenced by _hash_finish_split().

403 {
404  Bucket new_bucket;
405  Buffer metabuf;
406  HashMetaPage metap;
407  BlockNumber blkno;
408 
410  metap = HashPageGetMeta(BufferGetPage(metabuf));
411 
412  new_bucket = _hash_get_newbucket_from_oldbucket(rel, old_bucket,
413  metap->hashm_lowmask,
414  metap->hashm_maxbucket);
415  blkno = BUCKET_TO_BLKNO(metap, new_bucket);
416 
417  _hash_relbuf(rel, metabuf);
418 
419  return blkno;
420 }
#define LH_META_PAGE
Definition: hash.h:56
uint32 BlockNumber
Definition: block.h:31
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition: hashpage.c:79
uint32 hashm_lowmask
Definition: hash.h:186
#define BUCKET_TO_BLKNO(metap, B)
Definition: hash.h:38
#define HASH_READ
Definition: hash.h:254
uint32 Bucket
Definition: hash.h:34
Bucket _hash_get_newbucket_from_oldbucket(Relation rel, Bucket old_bucket, uint32 lowmask, uint32 maxbucket)
Definition: hashutil.c:435
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define HASH_METAPAGE
Definition: hash.h:146
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:245
uint32 hashm_maxbucket
Definition: hash.h:184
#define HashPageGetMeta(page)
Definition: hash.h:238
int Buffer
Definition: buf.h:23
Bucket _hash_get_newbucket_from_oldbucket ( Relation  rel,
Bucket  old_bucket,
uint32  lowmask,
uint32  maxbucket 
)

Definition at line 435 of file hashutil.c.

References CALC_NEW_BUCKET.

Referenced by _hash_get_newblock_from_oldbucket(), and hashbucketcleanup().

437 {
438  Bucket new_bucket;
439 
440  new_bucket = CALC_NEW_BUCKET(old_bucket, lowmask);
441  if (new_bucket > maxbucket)
442  {
443  lowmask = lowmask >> 1;
444  new_bucket = CALC_NEW_BUCKET(old_bucket, lowmask);
445  }
446 
447  return new_bucket;
448 }
#define CALC_NEW_BUCKET(old_bucket, lowmask)
Definition: hashutil.c:23
uint32 Bucket
Definition: hash.h:34
BlockNumber _hash_get_oldblock_from_newbucket ( Relation  rel,
Bucket  new_bucket 
)

Definition at line 363 of file hashutil.c.

References _hash_getbuf(), _hash_relbuf(), BUCKET_TO_BLKNO, BufferGetPage, fls(), HASH_METAPAGE, HASH_READ, HashPageGetMeta, and LH_META_PAGE.

Referenced by _hash_first().

364 {
365  Bucket old_bucket;
366  uint32 mask;
367  Buffer metabuf;
368  HashMetaPage metap;
369  BlockNumber blkno;
370 
371  /*
372  * To get the old bucket from the current bucket, we need a mask to modulo
373  * into lower half of table. This mask is stored in meta page as
374  * hashm_lowmask, but here we can't rely on the same, because we need a
375  * value of lowmask that was prevalent at the time when bucket split was
376  * started. Masking the most significant bit of new bucket would give us
377  * old bucket.
378  */
379  mask = (((uint32) 1) << (fls(new_bucket) - 1)) - 1;
380  old_bucket = new_bucket & mask;
381 
383  metap = HashPageGetMeta(BufferGetPage(metabuf));
384 
385  blkno = BUCKET_TO_BLKNO(metap, old_bucket);
386 
387  _hash_relbuf(rel, metabuf);
388 
389  return blkno;
390 }
#define LH_META_PAGE
Definition: hash.h:56
uint32 BlockNumber
Definition: block.h:31
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition: hashpage.c:79
#define BUCKET_TO_BLKNO(metap, B)
Definition: hash.h:38
int fls(int mask)
Definition: fls.c:55
#define HASH_READ
Definition: hash.h:254
uint32 Bucket
Definition: hash.h:34
unsigned int uint32
Definition: c.h:265
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define HASH_METAPAGE
Definition: hash.h:146
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:245
#define HashPageGetMeta(page)
Definition: hash.h:238
int Buffer
Definition: buf.h:23
Bucket _hash_hashkey2bucket ( uint32  hashkey,
uint32  maxbucket,
uint32  highmask,
uint32  lowmask 
)

Definition at line 124 of file hashutil.c.

Referenced by _hash_getbucketbuf_from_hashkey(), _hash_splitbucket_guts(), and hashbucketcleanup().

126 {
127  Bucket bucket;
128 
129  bucket = hashkey & highmask;
130  if (bucket > maxbucket)
131  bucket = bucket & lowmask;
132 
133  return bucket;
134 }
uint32 Bucket
Definition: hash.h:34
uint32 _hash_log2 ( uint32  num)

Definition at line 140 of file hashutil.c.

References i.

Referenced by _h_spoolinit(), _hash_expandtable(), and _hash_metapinit().

141 {
142  uint32 i,
143  limit;
144 
145  limit = 1;
146  for (i = 0; limit < num; limit <<= 1, i++)
147  ;
148  return i;
149 }
unsigned int uint32
Definition: c.h:265
int i
bytea* hashoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 223 of file hashutil.c.

References default_reloptions(), and RELOPT_KIND_HASH.

Referenced by hashhandler().

224 {
225  return default_reloptions(reloptions, validate, RELOPT_KIND_HASH);
226 }
bytea * default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
Definition: reloptions.c:1275