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 "storage/buf_internals.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)
 
uint32 _hash_spareindex (uint32 num_bucket)
 
uint32 _hash_get_totalbuckets (uint32 splitpoint_phase)
 
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)
 
void _hash_kill_items (IndexScanDesc scan)
 

Macro Definition Documentation

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

Definition at line 24 of file hashutil.c.

Referenced by _hash_get_newbucket_from_oldbucket().

Function Documentation

OffsetNumber _hash_binsearch ( Page  page,
uint32  hash_value 
)

Definition at line 358 of file hashutil.c.

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

Referenced by _hash_pgaddmultitup(), _hash_pgaddtup(), and _hash_step().

359 {
362 
363  /* Loop invariant: lower <= desired place <= upper */
364  upper = PageGetMaxOffsetNumber(page) + 1;
365  lower = FirstOffsetNumber;
366 
367  while (upper > lower)
368  {
369  OffsetNumber off;
370  IndexTuple itup;
371  uint32 hashkey;
372 
373  off = (upper + lower) / 2;
375 
376  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, off));
377  hashkey = _hash_get_indextuple_hashkey(itup);
378  if (hashkey < hash_value)
379  lower = off + 1;
380  else
381  upper = off;
382  }
383 
384  return lower;
385 }
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:353
uint16 OffsetNumber
Definition: off.h:24
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:299
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
unsigned int uint32
Definition: c.h:268
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
#define Assert(condition)
Definition: c.h:675
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
OffsetNumber _hash_binsearch_last ( Page  page,
uint32  hash_value 
)

Definition at line 396 of file hashutil.c.

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

Referenced by _hash_step().

397 {
400 
401  /* Loop invariant: lower <= desired place <= upper */
402  upper = PageGetMaxOffsetNumber(page);
403  lower = FirstOffsetNumber - 1;
404 
405  while (upper > lower)
406  {
407  IndexTuple itup;
408  OffsetNumber off;
409  uint32 hashkey;
410 
411  off = (upper + lower + 1) / 2;
413 
414  itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, off));
415  hashkey = _hash_get_indextuple_hashkey(itup);
416  if (hashkey > hash_value)
417  upper = off - 1;
418  else
419  lower = off;
420  }
421 
422  return lower;
423 }
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:353
uint16 OffsetNumber
Definition: off.h:24
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:299
#define FirstOffsetNumber
Definition: off.h:27
IndexTupleData * IndexTuple
Definition: itup.h:53
unsigned int uint32
Definition: c.h:268
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
#define Assert(condition)
Definition: c.h:675
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:40
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
void _hash_checkpage ( Relation  rel,
Buffer  buf,
int  flags 
)

Definition at line 225 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_next(), _hash_pgaddmultitup(), _hash_pgaddtup(), _hash_step(), and hashbulkdelete().

226 {
227  Page page = BufferGetPage(buf);
228 
229  /*
230  * ReadBuffer verifies that every newly-read page passes
231  * PageHeaderIsValid, which means it either contains a reasonably sane
232  * page header or is all-zero. We have to defend against the all-zero
233  * case, however.
234  */
235  if (PageIsNew(page))
236  ereport(ERROR,
237  (errcode(ERRCODE_INDEX_CORRUPTED),
238  errmsg("index \"%s\" contains unexpected zero page at block %u",
241  errhint("Please REINDEX it.")));
242 
243  /*
244  * Additionally check that the special area looks sane.
245  */
246  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(HashPageOpaqueData)))
247  ereport(ERROR,
248  (errcode(ERRCODE_INDEX_CORRUPTED),
249  errmsg("index \"%s\" contains corrupted page at block %u",
252  errhint("Please REINDEX it.")));
253 
254  if (flags)
255  {
257 
258  if ((opaque->hasho_flag & flags) == 0)
259  ereport(ERROR,
260  (errcode(ERRCODE_INDEX_CORRUPTED),
261  errmsg("index \"%s\" contains corrupted page at block %u",
264  errhint("Please REINDEX it.")));
265  }
266 
267  /*
268  * When checking the metapage, also verify magic number and version.
269  */
270  if (flags == LH_META_PAGE)
271  {
272  HashMetaPage metap = HashPageGetMeta(page);
273 
274  if (metap->hashm_magic != HASH_MAGIC)
275  ereport(ERROR,
276  (errcode(ERRCODE_INDEX_CORRUPTED),
277  errmsg("index \"%s\" is not a hash index",
278  RelationGetRelationName(rel))));
279 
280  if (metap->hashm_version != HASH_VERSION)
281  ereport(ERROR,
282  (errcode(ERRCODE_INDEX_CORRUPTED),
283  errmsg("index \"%s\" has wrong hash version",
285  errhint("Please REINDEX it.")));
286  }
287 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define LH_META_PAGE
Definition: hash.h:56
uint32 hashm_magic
Definition: hash.h:204
int errcode(int sqlerrcode)
Definition: elog.c:575
#define HASH_VERSION
Definition: hash.h:161
#define HASH_MAGIC
Definition: hash.h:160
#define ERROR
Definition: elog.h:43
uint32 hashm_version
Definition: hash.h:205
static char * buf
Definition: pg_test_fsync.c:66
#define RelationGetRelationName(relation)
Definition: rel.h:436
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:85
#define MAXALIGN(LEN)
Definition: c.h:588
#define PageGetSpecialSize(page)
Definition: bufpage.h:296
uint16 hasho_flag
Definition: hash.h:81
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2605
#define PageIsNew(page)
Definition: bufpage.h:225
#define HashPageGetMeta(page)
Definition: hash.h:266
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 31 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().

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

References _hash_datum2hashkey(), and UInt32GetDatum.

Referenced by hashbuildCallback(), and hashinsert().

329 {
330  uint32 hashkey;
331 
332  /*
333  * We do not insert null values into hash indexes. This is okay because
334  * the only supported search operator is '=', and we assume it is strict.
335  */
336  if (user_isnull[0])
337  return false;
338 
339  hashkey = _hash_datum2hashkey(index, user_values[0]);
340  index_values[0] = UInt32GetDatum(hashkey);
341  index_isnull[0] = false;
342  return true;
343 }
uint32 _hash_datum2hashkey(Relation rel, Datum key)
Definition: hashutil.c:82
unsigned int uint32
Definition: c.h:268
#define UInt32GetDatum(X)
Definition: postgres.h:499
uint32 _hash_datum2hashkey ( Relation  rel,
Datum  key 
)

Definition at line 82 of file hashutil.c.

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

Referenced by _hash_convert_tuple(), and _hash_first().

83 {
84  FmgrInfo *procinfo;
85  Oid collation;
86 
87  /* XXX assumes index has only one attribute */
88  procinfo = index_getprocinfo(rel, 1, HASHPROC);
89  collation = rel->rd_indcollation[0];
90 
91  return DatumGetUInt32(FunctionCall1Coll(procinfo, collation, key));
92 }
#define DatumGetUInt32(X)
Definition: postgres.h:492
Definition: fmgr.h:56
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:193
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition: fmgr.c:1027
#define HASHPROC
Definition: hash.h:297
uint32 _hash_datum2hashkey_type ( Relation  rel,
Datum  key,
Oid  keytype 
)

Definition at line 102 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().

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

Definition at line 299 of file hashutil.c.

References IndexInfoFindDataOffset, and IndexTupleData::t_info.

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

300 {
301  char *attp;
302 
303  /*
304  * We assume the hash key is the first attribute and can't be null, so
305  * this can be done crudely but very very cheaply ...
306  */
307  attp = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
308  return *((uint32 *) attp);
309 }
#define IndexInfoFindDataOffset(t_info)
Definition: itup.h:80
unsigned int uint32
Definition: c.h:268
unsigned short t_info
Definition: itup.h:49
BlockNumber _hash_get_newblock_from_oldbucket ( Relation  rel,
Bucket  old_bucket 
)

Definition at line 469 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().

470 {
471  Bucket new_bucket;
472  Buffer metabuf;
473  HashMetaPage metap;
474  BlockNumber blkno;
475 
477  metap = HashPageGetMeta(BufferGetPage(metabuf));
478 
479  new_bucket = _hash_get_newbucket_from_oldbucket(rel, old_bucket,
480  metap->hashm_lowmask,
481  metap->hashm_maxbucket);
482  blkno = BUCKET_TO_BLKNO(metap, new_bucket);
483 
484  _hash_relbuf(rel, metabuf);
485 
486  return blkno;
487 }
#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:78
uint32 hashm_lowmask
Definition: hash.h:214
#define BUCKET_TO_BLKNO(metap, B)
Definition: hash.h:38
#define HASH_READ
Definition: hash.h:282
uint32 Bucket
Definition: hash.h:34
Bucket _hash_get_newbucket_from_oldbucket(Relation rel, Bucket old_bucket, uint32 lowmask, uint32 maxbucket)
Definition: hashutil.c:502
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define HASH_METAPAGE
Definition: hash.h:158
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:274
uint32 hashm_maxbucket
Definition: hash.h:212
#define HashPageGetMeta(page)
Definition: hash.h:266
int Buffer
Definition: buf.h:23
Bucket _hash_get_newbucket_from_oldbucket ( Relation  rel,
Bucket  old_bucket,
uint32  lowmask,
uint32  maxbucket 
)

Definition at line 502 of file hashutil.c.

References CALC_NEW_BUCKET.

Referenced by _hash_get_newblock_from_oldbucket(), and hashbucketcleanup().

504 {
505  Bucket new_bucket;
506 
507  new_bucket = CALC_NEW_BUCKET(old_bucket, lowmask);
508  if (new_bucket > maxbucket)
509  {
510  lowmask = lowmask >> 1;
511  new_bucket = CALC_NEW_BUCKET(old_bucket, lowmask);
512  }
513 
514  return new_bucket;
515 }
#define CALC_NEW_BUCKET(old_bucket, lowmask)
Definition: hashutil.c:24
uint32 Bucket
Definition: hash.h:34
BlockNumber _hash_get_oldblock_from_newbucket ( Relation  rel,
Bucket  new_bucket 
)

Definition at line 430 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().

431 {
432  Bucket old_bucket;
433  uint32 mask;
434  Buffer metabuf;
435  HashMetaPage metap;
436  BlockNumber blkno;
437 
438  /*
439  * To get the old bucket from the current bucket, we need a mask to modulo
440  * into lower half of table. This mask is stored in meta page as
441  * hashm_lowmask, but here we can't rely on the same, because we need a
442  * value of lowmask that was prevalent at the time when bucket split was
443  * started. Masking the most significant bit of new bucket would give us
444  * old bucket.
445  */
446  mask = (((uint32) 1) << (fls(new_bucket) - 1)) - 1;
447  old_bucket = new_bucket & mask;
448 
450  metap = HashPageGetMeta(BufferGetPage(metabuf));
451 
452  blkno = BUCKET_TO_BLKNO(metap, old_bucket);
453 
454  _hash_relbuf(rel, metabuf);
455 
456  return blkno;
457 }
#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:78
#define BUCKET_TO_BLKNO(metap, B)
Definition: hash.h:38
int fls(int mask)
Definition: fls.c:55
#define HASH_READ
Definition: hash.h:282
uint32 Bucket
Definition: hash.h:34
unsigned int uint32
Definition: c.h:268
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define HASH_METAPAGE
Definition: hash.h:158
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:274
#define HashPageGetMeta(page)
Definition: hash.h:266
int Buffer
Definition: buf.h:23
uint32 _hash_get_totalbuckets ( uint32  splitpoint_phase)

Definition at line 189 of file hashutil.c.

References HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE, HASH_SPLITPOINT_PHASE_BITS, and HASH_SPLITPOINT_PHASE_MASK.

Referenced by _hash_expandtable(), _hash_init_metabuffer(), _hash_ovflblkno_to_bitno(), and bitno_to_blkno().

190 {
191  uint32 splitpoint_group;
192  uint32 total_buckets;
193  uint32 phases_within_splitpoint_group;
194 
195  if (splitpoint_phase < HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE)
196  return (1 << splitpoint_phase);
197 
198  /* get splitpoint's group */
199  splitpoint_group = HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE;
200  splitpoint_group +=
201  ((splitpoint_phase - HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE) >>
203 
204  /* account for buckets before splitpoint_group */
205  total_buckets = (1 << (splitpoint_group - 1));
206 
207  /* account for buckets within splitpoint_group */
208  phases_within_splitpoint_group =
209  (((splitpoint_phase - HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE) &
210  HASH_SPLITPOINT_PHASE_MASK) + 1); /* from 0-based to 1-based */
211  total_buckets +=
212  (((1 << (splitpoint_group - 1)) >> HASH_SPLITPOINT_PHASE_BITS) *
213  phases_within_splitpoint_group);
214 
215  return total_buckets;
216 }
#define HASH_SPLITPOINT_PHASE_MASK
Definition: hash.h:192
#define HASH_SPLITPOINT_PHASE_BITS
Definition: hash.h:190
unsigned int uint32
Definition: c.h:268
#define HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE
Definition: hash.h:193
Bucket _hash_hashkey2bucket ( uint32  hashkey,
uint32  maxbucket,
uint32  highmask,
uint32  lowmask 
)

Definition at line 125 of file hashutil.c.

Referenced by _h_indexbuild(), _hash_getbucketbuf_from_hashkey(), _hash_splitbucket(), comparetup_index_hash(), and hashbucketcleanup().

127 {
128  Bucket bucket;
129 
130  bucket = hashkey & highmask;
131  if (bucket > maxbucket)
132  bucket = bucket & lowmask;
133 
134  return bucket;
135 }
uint32 Bucket
Definition: hash.h:34
void _hash_kill_items ( IndexScanDesc  scan)

Definition at line 529 of file hashutil.c.

References Assert, BufferGetPage, HashScanOpaqueData::hashso_curbuf, HashScanPosItem::heapTid, i, HashScanPosItem::indexOffset, ItemIdMarkDead, ItemPointerEquals(), HashScanOpaqueData::killedItems, LH_PAGE_HAS_DEAD_TUPLES, MarkBufferDirtyHint(), NULL, HashScanOpaqueData::numKilled, OffsetNumberNext, IndexScanDescData::opaque, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageGetSpecialPointer, and IndexTupleData::t_tid.

Referenced by _hash_step(), hashendscan(), and hashrescan().

530 {
531  HashScanOpaque so = (HashScanOpaque) scan->opaque;
532  Page page;
533  HashPageOpaque opaque;
534  OffsetNumber offnum,
535  maxoff;
536  int numKilled = so->numKilled;
537  int i;
538  bool killedsomething = false;
539 
540  Assert(so->numKilled > 0);
541  Assert(so->killedItems != NULL);
542 
543  /*
544  * Always reset the scan state, so we don't look for same items on other
545  * pages.
546  */
547  so->numKilled = 0;
548 
549  page = BufferGetPage(so->hashso_curbuf);
550  opaque = (HashPageOpaque) PageGetSpecialPointer(page);
551  maxoff = PageGetMaxOffsetNumber(page);
552 
553  for (i = 0; i < numKilled; i++)
554  {
555  offnum = so->killedItems[i].indexOffset;
556 
557  while (offnum <= maxoff)
558  {
559  ItemId iid = PageGetItemId(page, offnum);
560  IndexTuple ituple = (IndexTuple) PageGetItem(page, iid);
561 
562  if (ItemPointerEquals(&ituple->t_tid, &so->killedItems[i].heapTid))
563  {
564  /* found the item */
565  ItemIdMarkDead(iid);
566  killedsomething = true;
567  break; /* out of inner search loop */
568  }
569  offnum = OffsetNumberNext(offnum);
570  }
571  }
572 
573  /*
574  * Since this can be redone later if needed, mark as dirty hint. Whenever
575  * we mark anything LP_DEAD, we also set the page's
576  * LH_PAGE_HAS_DEAD_TUPLES flag, which is likewise just a hint.
577  */
578  if (killedsomething)
579  {
580  opaque->hasho_flag |= LH_PAGE_HAS_DEAD_TUPLES;
582  }
583 }
HashScanOpaqueData * HashScanOpaque
Definition: hash.h:152
void MarkBufferDirtyHint(Buffer buffer, bool buffer_std)
Definition: bufmgr.c:3379
#define ItemIdMarkDead(itemId)
Definition: itemid.h:178
ItemPointerData t_tid
Definition: itup.h:37
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:353
uint16 OffsetNumber
Definition: off.h:24
IndexTupleData * IndexTuple
Definition: itup.h:53
HashScanPosItem * killedItems
Definition: hash.h:148
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:231
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:85
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
OffsetNumber indexOffset
Definition: hash.h:103
int i
ItemPointerData heapTid
Definition: hash.h:102
Buffer hashso_curbuf
Definition: hash.h:121
#define LH_PAGE_HAS_DEAD_TUPLES
Definition: hash.h:60
#define PageGetItem(page, itemId)
Definition: bufpage.h:336
Pointer Page
Definition: bufpage.h:74
uint32 _hash_log2 ( uint32  num)

Definition at line 141 of file hashutil.c.

References i.

Referenced by _h_spoolinit(), _hash_init_metabuffer(), and _hash_spareindex().

142 {
143  uint32 i,
144  limit;
145 
146  limit = 1;
147  for (i = 0; limit < num; limit <<= 1, i++)
148  ;
149  return i;
150 }
unsigned int uint32
Definition: c.h:268
int i
uint32 _hash_spareindex ( uint32  num_bucket)

Definition at line 157 of file hashutil.c.

References _hash_log2(), HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE, HASH_SPLITPOINT_PHASE_BITS, and HASH_SPLITPOINT_PHASE_MASK.

Referenced by _hash_expandtable(), and _hash_init_metabuffer().

158 {
159  uint32 splitpoint_group;
160  uint32 splitpoint_phases;
161 
162  splitpoint_group = _hash_log2(num_bucket);
163 
164  if (splitpoint_group < HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE)
165  return splitpoint_group;
166 
167  /* account for single-phase groups */
168  splitpoint_phases = HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE;
169 
170  /* account for multi-phase groups before splitpoint_group */
171  splitpoint_phases +=
172  ((splitpoint_group - HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE) <<
174 
175  /* account for phases within current group */
176  splitpoint_phases +=
177  (((num_bucket - 1) >>
178  (splitpoint_group - (HASH_SPLITPOINT_PHASE_BITS + 1))) &
179  HASH_SPLITPOINT_PHASE_MASK); /* to 0-based value. */
180 
181  return splitpoint_phases;
182 }
#define HASH_SPLITPOINT_PHASE_MASK
Definition: hash.h:192
#define HASH_SPLITPOINT_PHASE_BITS
Definition: hash.h:190
unsigned int uint32
Definition: c.h:268
uint32 _hash_log2(uint32 num)
Definition: hashutil.c:141
#define HASH_SPLITPOINT_GROUPS_WITH_ONE_PHASE
Definition: hash.h:193
bytea* hashoptions ( Datum  reloptions,
bool  validate 
)

Definition at line 290 of file hashutil.c.

References default_reloptions(), and RELOPT_KIND_HASH.

Referenced by hashhandler().

291 {
292  return default_reloptions(reloptions, validate, RELOPT_KIND_HASH);
293 }
bytea * default_reloptions(Datum reloptions, bool validate, relopt_kind kind)
Definition: reloptions.c:1316