PostgreSQL Source Code  git master
hash.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "access/hash_xlog.h"
#include "access/relscan.h"
#include "access/tableam.h"
#include "catalog/index.h"
#include "commands/progress.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "optimizer/plancat.h"
#include "pgstat.h"
#include "utils/builtins.h"
#include "utils/index_selfuncs.h"
#include "utils/rel.h"
Include dependency graph for hash.c:

Go to the source code of this file.

Data Structures

struct  HashBuildState
 

Functions

static void hashbuildCallback (Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
 
Datum hashhandler (PG_FUNCTION_ARGS)
 
IndexBuildResulthashbuild (Relation heap, Relation index, IndexInfo *indexInfo)
 
void hashbuildempty (Relation index)
 
bool hashinsert (Relation rel, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
 
bool hashgettuple (IndexScanDesc scan, ScanDirection dir)
 
int64 hashgetbitmap (IndexScanDesc scan, TIDBitmap *tbm)
 
IndexScanDesc hashbeginscan (Relation rel, int nkeys, int norderbys)
 
void hashrescan (IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
 
void hashendscan (IndexScanDesc scan)
 
IndexBulkDeleteResulthashbulkdelete (IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
 
IndexBulkDeleteResulthashvacuumcleanup (IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
 
void hashbucketcleanup (Relation rel, Bucket cur_bucket, Buffer bucket_buf, BlockNumber bucket_blkno, BufferAccessStrategy bstrategy, uint32 maxbucket, uint32 highmask, uint32 lowmask, double *tuples_removed, double *num_index_tuples, bool split_cleanup, IndexBulkDeleteCallback callback, void *callback_state)
 

Function Documentation

◆ hashbeginscan()

IndexScanDesc hashbeginscan ( Relation  rel,
int  nkeys,
int  norderbys 
)

Definition at line 364 of file hash.c.

365 {
366  IndexScanDesc scan;
367  HashScanOpaque so;
368 
369  /* no order by operators allowed */
370  Assert(norderbys == 0);
371 
372  scan = RelationGetIndexScan(rel, nkeys, norderbys);
373 
374  so = (HashScanOpaque) palloc(sizeof(HashScanOpaqueData));
378 
379  so->hashso_buc_populated = false;
380  so->hashso_buc_split = false;
381 
382  so->killedItems = NULL;
383  so->numKilled = 0;
384 
385  scan->opaque = so;
386 
387  return scan;
388 }
#define InvalidBuffer
Definition: buf.h:25
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:81
#define HashScanPosInvalidate(scanpos)
Definition: hash.h:142
HashScanOpaqueData * HashScanOpaque
Definition: hash.h:190
Assert(fmt[strlen(fmt) - 1] !='\n')
void * palloc(Size size)
Definition: mcxt.c:1062
bool hashso_buc_split
Definition: hash.h:178
HashScanPosData currPos
Definition: hash.h:187
bool hashso_buc_populated
Definition: hash.h:172
Buffer hashso_split_bucket_buf
Definition: hash.h:169
Buffer hashso_bucket_buf
Definition: hash.h:162
int * killedItems
Definition: hash.h:180

References Assert(), HashScanOpaqueData::currPos, HashScanPosInvalidate, HashScanOpaqueData::hashso_buc_populated, HashScanOpaqueData::hashso_buc_split, HashScanOpaqueData::hashso_bucket_buf, HashScanOpaqueData::hashso_split_bucket_buf, InvalidBuffer, HashScanOpaqueData::killedItems, HashScanOpaqueData::numKilled, IndexScanDescData::opaque, palloc(), and RelationGetIndexScan().

Referenced by hashhandler().

◆ hashbucketcleanup()

void hashbucketcleanup ( Relation  rel,
Bucket  cur_bucket,
Buffer  bucket_buf,
BlockNumber  bucket_blkno,
BufferAccessStrategy  bstrategy,
uint32  maxbucket,
uint32  highmask,
uint32  lowmask,
double *  tuples_removed,
double *  num_index_tuples,
bool  split_cleanup,
IndexBulkDeleteCallback  callback,
void *  callback_state 
)

Definition at line 684 of file hash.c.

690 {
691  BlockNumber blkno;
692  Buffer buf;
694  bool bucket_dirty = false;
695 
696  blkno = bucket_blkno;
697  buf = bucket_buf;
698 
699  if (split_cleanup)
700  new_bucket = _hash_get_newbucket_from_oldbucket(rel, cur_bucket,
701  lowmask, maxbucket);
702 
703  /* Scan each page in bucket */
704  for (;;)
705  {
706  HashPageOpaque opaque;
707  OffsetNumber offno;
708  OffsetNumber maxoffno;
709  Buffer next_buf;
710  Page page;
711  OffsetNumber deletable[MaxOffsetNumber];
712  int ndeletable = 0;
713  bool retain_pin = false;
714  bool clear_dead_marking = false;
715 
717 
718  page = BufferGetPage(buf);
719  opaque = (HashPageOpaque) PageGetSpecialPointer(page);
720 
721  /* Scan each tuple in page */
722  maxoffno = PageGetMaxOffsetNumber(page);
723  for (offno = FirstOffsetNumber;
724  offno <= maxoffno;
725  offno = OffsetNumberNext(offno))
726  {
727  ItemPointer htup;
728  IndexTuple itup;
729  Bucket bucket;
730  bool kill_tuple = false;
731 
732  itup = (IndexTuple) PageGetItem(page,
733  PageGetItemId(page, offno));
734  htup = &(itup->t_tid);
735 
736  /*
737  * To remove the dead tuples, we strictly want to rely on results
738  * of callback function. refer btvacuumpage for detailed reason.
739  */
740  if (callback && callback(htup, callback_state))
741  {
742  kill_tuple = true;
743  if (tuples_removed)
744  *tuples_removed += 1;
745  }
746  else if (split_cleanup)
747  {
748  /* delete the tuples that are moved by split. */
750  maxbucket,
751  highmask,
752  lowmask);
753  /* mark the item for deletion */
754  if (bucket != cur_bucket)
755  {
756  /*
757  * We expect tuples to either belong to current bucket or
758  * new_bucket. This is ensured because we don't allow
759  * further splits from bucket that contains garbage. See
760  * comments in _hash_expandtable.
761  */
762  Assert(bucket == new_bucket);
763  kill_tuple = true;
764  }
765  }
766 
767  if (kill_tuple)
768  {
769  /* mark the item for deletion */
770  deletable[ndeletable++] = offno;
771  }
772  else
773  {
774  /* we're keeping it, so count it */
775  if (num_index_tuples)
776  *num_index_tuples += 1;
777  }
778  }
779 
780  /* retain the pin on primary bucket page till end of bucket scan */
781  if (blkno == bucket_blkno)
782  retain_pin = true;
783  else
784  retain_pin = false;
785 
786  blkno = opaque->hasho_nextblkno;
787 
788  /*
789  * Apply deletions, advance to next page and write page if needed.
790  */
791  if (ndeletable > 0)
792  {
793  /* No ereport(ERROR) until changes are logged */
795 
796  PageIndexMultiDelete(page, deletable, ndeletable);
797  bucket_dirty = true;
798 
799  /*
800  * Let us mark the page as clean if vacuum removes the DEAD tuples
801  * from an index page. We do this by clearing
802  * LH_PAGE_HAS_DEAD_TUPLES flag.
803  */
804  if (tuples_removed && *tuples_removed > 0 &&
805  H_HAS_DEAD_TUPLES(opaque))
806  {
808  clear_dead_marking = true;
809  }
810 
812 
813  /* XLOG stuff */
814  if (RelationNeedsWAL(rel))
815  {
816  xl_hash_delete xlrec;
817  XLogRecPtr recptr;
818 
819  xlrec.clear_dead_marking = clear_dead_marking;
820  xlrec.is_primary_bucket_page = (buf == bucket_buf);
821 
822  XLogBeginInsert();
823  XLogRegisterData((char *) &xlrec, SizeOfHashDelete);
824 
825  /*
826  * bucket buffer needs to be registered to ensure that we can
827  * acquire a cleanup lock on it during replay.
828  */
829  if (!xlrec.is_primary_bucket_page)
831 
833  XLogRegisterBufData(1, (char *) deletable,
834  ndeletable * sizeof(OffsetNumber));
835 
836  recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_DELETE);
837  PageSetLSN(BufferGetPage(buf), recptr);
838  }
839 
841  }
842 
843  /* bail out if there are no more pages to scan. */
844  if (!BlockNumberIsValid(blkno))
845  break;
846 
847  next_buf = _hash_getbuf_with_strategy(rel, blkno, HASH_WRITE,
849  bstrategy);
850 
851  /*
852  * release the lock on previous page after acquiring the lock on next
853  * page
854  */
855  if (retain_pin)
857  else
858  _hash_relbuf(rel, buf);
859 
860  buf = next_buf;
861  }
862 
863  /*
864  * lock the bucket page to clear the garbage flag and squeeze the bucket.
865  * if the current buffer is same as bucket buffer, then we already have
866  * lock on bucket page.
867  */
868  if (buf != bucket_buf)
869  {
870  _hash_relbuf(rel, buf);
871  LockBuffer(bucket_buf, BUFFER_LOCK_EXCLUSIVE);
872  }
873 
874  /*
875  * Clear the garbage flag from bucket after deleting the tuples that are
876  * moved by split. We purposefully clear the flag before squeeze bucket,
877  * so that after restart, vacuum shouldn't again try to delete the moved
878  * by split tuples.
879  */
880  if (split_cleanup)
881  {
882  HashPageOpaque bucket_opaque;
883  Page page;
884 
885  page = BufferGetPage(bucket_buf);
886  bucket_opaque = (HashPageOpaque) PageGetSpecialPointer(page);
887 
888  /* No ereport(ERROR) until changes are logged */
890 
891  bucket_opaque->hasho_flag &= ~LH_BUCKET_NEEDS_SPLIT_CLEANUP;
892  MarkBufferDirty(bucket_buf);
893 
894  /* XLOG stuff */
895  if (RelationNeedsWAL(rel))
896  {
897  XLogRecPtr recptr;
898 
899  XLogBeginInsert();
900  XLogRegisterBuffer(0, bucket_buf, REGBUF_STANDARD);
901 
902  recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_SPLIT_CLEANUP);
903  PageSetLSN(page, recptr);
904  }
905 
907  }
908 
909  /*
910  * If we have deleted anything, try to compact free space. For squeezing
911  * the bucket, we must have a cleanup lock, else it can impact the
912  * ordering of tuples for a scan that has started before it.
913  */
914  if (bucket_dirty && IsBufferCleanupOK(bucket_buf))
915  _hash_squeezebucket(rel, cur_bucket, bucket_blkno, bucket_buf,
916  bstrategy);
917  else
918  LockBuffer(bucket_buf, BUFFER_LOCK_UNLOCK);
919 }
uint32 BlockNumber
Definition: block.h:31
#define BlockNumberIsValid(blockNumber)
Definition: block.h:70
int Buffer
Definition: buf.h:23
bool IsBufferCleanupOK(Buffer buffer)
Definition: bufmgr.c:4286
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1565
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3996
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition: bufpage.c:1154
Pointer Page
Definition: bufpage.h:78
#define PageGetSpecialPointer(page)
Definition: bufpage.h:325
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:356
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:234
#define PageGetItem(page, itemId)
Definition: bufpage.h:339
#define PageSetLSN(page, lsn)
Definition: bufpage.h:367
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:155
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:86
#define HASH_WRITE
Definition: hash.h:338
#define H_HAS_DEAD_TUPLES(opaque)
Definition: hash.h:91
uint32 Bucket
Definition: hash.h:35
#define LH_BUCKET_NEEDS_SPLIT_CLEANUP
Definition: hash.h:60
#define LH_PAGE_HAS_DEAD_TUPLES
Definition: hash.h:61
#define LH_OVERFLOW_PAGE
Definition: hash.h:54
#define InvalidBucket
Definition: hash.h:37
#define XLOG_HASH_SPLIT_CLEANUP
Definition: hash_xlog.h:37
#define SizeOfHashDelete
Definition: hash_xlog.h:186
#define XLOG_HASH_DELETE
Definition: hash_xlog.h:36
void _hash_squeezebucket(Relation rel, Bucket bucket, BlockNumber bucket_blkno, Buffer bucket_buf, BufferAccessStrategy bstrategy)
Definition: hashovfl.c:805
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:265
Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno, int access, int flags, BufferAccessStrategy bstrategy)
Definition: hashpage.c:238
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:292
Bucket _hash_hashkey2bucket(uint32 hashkey, uint32 maxbucket, uint32 highmask, uint32 lowmask)
Definition: hashutil.c:126
Bucket _hash_get_newbucket_from_oldbucket(Relation rel, Bucket old_bucket, uint32 lowmask, uint32 maxbucket)
Definition: hashutil.c:495
IndexTupleData * IndexTuple
Definition: itup.h:53
#define START_CRIT_SECTION()
Definition: miscadmin.h:147
#define END_CRIT_SECTION()
Definition: miscadmin.h:149
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
#define MaxOffsetNumber
Definition: off.h:28
static char * buf
Definition: pg_test_fsync.c:70
#define RelationNeedsWAL(relation)
Definition: rel.h:602
BlockNumber hasho_nextblkno
Definition: hash.h:80
uint16 hasho_flag
Definition: hash.h:82
ItemPointerData t_tid
Definition: itup.h:37
bool clear_dead_marking
Definition: hash_xlog.h:180
bool is_primary_bucket_page
Definition: hash_xlog.h:182
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
void vacuum_delay_point(void)
Definition: vacuum.c:2156
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:429
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:375
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:229
void XLogBeginInsert(void)
Definition: xloginsert.c:136
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:337
#define REGBUF_STANDARD
Definition: xloginsert.h:34
#define REGBUF_NO_IMAGE
Definition: xloginsert.h:32

References _hash_get_indextuple_hashkey(), _hash_get_newbucket_from_oldbucket(), _hash_getbuf_with_strategy(), _hash_hashkey2bucket(), _hash_relbuf(), _hash_squeezebucket(), Assert(), BlockNumberIsValid, buf, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage, callback(), xl_hash_delete::clear_dead_marking, END_CRIT_SECTION, FirstOffsetNumber, H_HAS_DEAD_TUPLES, HASH_WRITE, HashPageOpaqueData::hasho_flag, HashPageOpaqueData::hasho_nextblkno, InvalidBucket, xl_hash_delete::is_primary_bucket_page, IsBufferCleanupOK(), LH_BUCKET_NEEDS_SPLIT_CLEANUP, LH_OVERFLOW_PAGE, LH_PAGE_HAS_DEAD_TUPLES, LockBuffer(), MarkBufferDirty(), MaxOffsetNumber, OffsetNumberNext, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageGetSpecialPointer, PageIndexMultiDelete(), PageSetLSN, PG_USED_FOR_ASSERTS_ONLY, REGBUF_NO_IMAGE, REGBUF_STANDARD, RelationNeedsWAL, SizeOfHashDelete, START_CRIT_SECTION, IndexTupleData::t_tid, vacuum_delay_point(), XLOG_HASH_DELETE, XLOG_HASH_SPLIT_CLEANUP, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by _hash_expandtable(), _hash_splitbucket(), and hashbulkdelete().

◆ hashbuild()

IndexBuildResult* hashbuild ( Relation  heap,
Relation  index,
IndexInfo indexInfo 
)

Definition at line 112 of file hash.c.

113 {
114  IndexBuildResult *result;
115  BlockNumber relpages;
116  double reltuples;
117  double allvisfrac;
118  uint32 num_buckets;
119  long sort_threshold;
120  HashBuildState buildstate;
121 
122  /*
123  * We expect to be called exactly once for any index relation. If that's
124  * not the case, big trouble's what we have.
125  */
127  elog(ERROR, "index \"%s\" already contains data",
129 
130  /* Estimate the number of rows currently present in the table */
131  estimate_rel_size(heap, NULL, &relpages, &reltuples, &allvisfrac);
132 
133  /* Initialize the hash index metadata page and initial buckets */
134  num_buckets = _hash_init(index, reltuples, MAIN_FORKNUM);
135 
136  /*
137  * If we just insert the tuples into the index in scan order, then
138  * (assuming their hash codes are pretty random) there will be no locality
139  * of access to the index, and if the index is bigger than available RAM
140  * then we'll thrash horribly. To prevent that scenario, we can sort the
141  * tuples by (expected) bucket number. However, such a sort is useless
142  * overhead when the index does fit in RAM. We choose to sort if the
143  * initial index size exceeds maintenance_work_mem, or the number of
144  * buffers usable for the index, whichever is less. (Limiting by the
145  * number of buffers should reduce thrashing between PG buffers and kernel
146  * buffers, which seems useful even if no physical I/O results. Limiting
147  * by maintenance_work_mem is useful to allow easy testing of the sort
148  * code path, and may be useful to DBAs as an additional control knob.)
149  *
150  * NOTE: this test will need adjustment if a bucket is ever different from
151  * one page. Also, "initial index size" accounting does not include the
152  * metapage, nor the first bitmap page.
153  */
154  sort_threshold = (maintenance_work_mem * 1024L) / BLCKSZ;
155  if (index->rd_rel->relpersistence != RELPERSISTENCE_TEMP)
156  sort_threshold = Min(sort_threshold, NBuffers);
157  else
158  sort_threshold = Min(sort_threshold, NLocBuffer);
159 
160  if (num_buckets >= (uint32) sort_threshold)
161  buildstate.spool = _h_spoolinit(heap, index, num_buckets);
162  else
163  buildstate.spool = NULL;
164 
165  /* prepare to build the index */
166  buildstate.indtuples = 0;
167  buildstate.heapRel = heap;
168 
169  /* do the heap scan */
170  reltuples = table_index_build_scan(heap, index, indexInfo, true, true,
172  (void *) &buildstate, NULL);
174  buildstate.indtuples);
175 
176  if (buildstate.spool)
177  {
178  /* sort the tuples and insert them into the index */
179  _h_indexbuild(buildstate.spool, buildstate.heapRel);
180  _h_spooldestroy(buildstate.spool);
181  }
182 
183  /*
184  * Return statistics
185  */
186  result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
187 
188  result->heap_tuples = reltuples;
189  result->index_tuples = buildstate.indtuples;
190 
191  return result;
192 }
void pgstat_progress_update_param(int index, int64 val)
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:212
unsigned int uint32
Definition: c.h:441
#define Min(x, y)
Definition: c.h:986
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
int NBuffers
Definition: globals.c:135
int maintenance_work_mem
Definition: globals.c:126
static void hashbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition: hash.c:207
uint32 _hash_init(Relation rel, double num_tuples, ForkNumber forkNum)
Definition: hashpage.c:326
void _h_indexbuild(HSpool *hspool, Relation heapRel)
Definition: hashsort.c:119
HSpool * _h_spoolinit(Relation heap, Relation index, uint32 num_buckets)
Definition: hashsort.c:59
void _h_spooldestroy(HSpool *hspool)
Definition: hashsort.c:98
int NLocBuffer
Definition: localbuf.c:41
void estimate_rel_size(Relation rel, int32 *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac)
Definition: plancat.c:960
#define PROGRESS_CREATEIDX_TUPLES_TOTAL
Definition: progress.h:84
#define RelationGetRelationName(relation)
Definition: rel.h:512
@ MAIN_FORKNUM
Definition: relpath.h:43
HSpool * spool
Definition: hash.c:38
Relation heapRel
Definition: hash.c:40
double indtuples
Definition: hash.c:39
double heap_tuples
Definition: genam.h:32
double index_tuples
Definition: genam.h:33
Definition: type.h:90
static double table_index_build_scan(Relation table_rel, Relation index_rel, struct IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition: tableam.h:1747

References _h_indexbuild(), _h_spooldestroy(), _h_spoolinit(), _hash_init(), elog, ERROR, estimate_rel_size(), hashbuildCallback(), IndexBuildResult::heap_tuples, HashBuildState::heapRel, IndexBuildResult::index_tuples, HashBuildState::indtuples, MAIN_FORKNUM, maintenance_work_mem, Min, NBuffers, NLocBuffer, palloc(), pgstat_progress_update_param(), PROGRESS_CREATEIDX_TUPLES_TOTAL, RelationGetNumberOfBlocks, RelationGetRelationName, HashBuildState::spool, and table_index_build_scan().

Referenced by hashhandler().

◆ hashbuildCallback()

static void hashbuildCallback ( Relation  index,
ItemPointer  tid,
Datum values,
bool isnull,
bool  tupleIsAlive,
void *  state 
)
static

Definition at line 207 of file hash.c.

213 {
214  HashBuildState *buildstate = (HashBuildState *) state;
215  Datum index_values[1];
216  bool index_isnull[1];
217  IndexTuple itup;
218 
219  /* convert data to a hash key; on failure, do not insert anything */
221  values, isnull,
222  index_values, index_isnull))
223  return;
224 
225  /* Either spool the tuple for sorting, or just put it into the index */
226  if (buildstate->spool)
227  _h_spool(buildstate->spool, tid, index_values, index_isnull);
228  else
229  {
230  /* form an index tuple and point it at the heap tuple */
232  index_values, index_isnull);
233  itup->t_tid = *tid;
234  _hash_doinsert(index, itup, buildstate->heapRel);
235  pfree(itup);
236  }
237 
238  buildstate->indtuples += 1;
239 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
void _hash_doinsert(Relation rel, IndexTuple itup, Relation heapRel)
Definition: hashinsert.c:36
void _h_spool(HSpool *hspool, ItemPointer self, Datum *values, bool *isnull)
Definition: hashsort.c:108
bool _hash_convert_tuple(Relation index, Datum *user_values, bool *user_isnull, Datum *index_values, bool *index_isnull)
Definition: hashutil.c:319
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:47
void pfree(void *pointer)
Definition: mcxt.c:1169
uintptr_t Datum
Definition: postgres.h:411
#define RelationGetDescr(relation)
Definition: rel.h:504
Definition: regguts.h:318

References _h_spool(), _hash_convert_tuple(), _hash_doinsert(), HashBuildState::heapRel, index_form_tuple(), HashBuildState::indtuples, pfree(), RelationGetDescr, HashBuildState::spool, IndexTupleData::t_tid, and values.

Referenced by hashbuild().

◆ hashbuildempty()

void hashbuildempty ( Relation  index)

Definition at line 198 of file hash.c.

199 {
201 }
@ INIT_FORKNUM
Definition: relpath.h:46

References _hash_init(), and INIT_FORKNUM.

Referenced by hashhandler().

◆ hashbulkdelete()

IndexBulkDeleteResult* hashbulkdelete ( IndexVacuumInfo info,
IndexBulkDeleteResult stats,
IndexBulkDeleteCallback  callback,
void *  callback_state 
)

Definition at line 459 of file hash.c.

461 {
462  Relation rel = info->index;
463  double tuples_removed;
464  double num_index_tuples;
465  double orig_ntuples;
466  Bucket orig_maxbucket;
467  Bucket cur_maxbucket;
468  Bucket cur_bucket;
469  Buffer metabuf = InvalidBuffer;
470  HashMetaPage metap;
471  HashMetaPage cachedmetap;
472 
473  tuples_removed = 0;
474  num_index_tuples = 0;
475 
476  /*
477  * We need a copy of the metapage so that we can use its hashm_spares[]
478  * values to compute bucket page addresses, but a cached copy should be
479  * good enough. (If not, we'll detect that further down and refresh the
480  * cache as necessary.)
481  */
482  cachedmetap = _hash_getcachedmetap(rel, &metabuf, false);
483  Assert(cachedmetap != NULL);
484 
485  orig_maxbucket = cachedmetap->hashm_maxbucket;
486  orig_ntuples = cachedmetap->hashm_ntuples;
487 
488  /* Scan the buckets that we know exist */
489  cur_bucket = 0;
490  cur_maxbucket = orig_maxbucket;
491 
492 loop_top:
493  while (cur_bucket <= cur_maxbucket)
494  {
495  BlockNumber bucket_blkno;
496  BlockNumber blkno;
497  Buffer bucket_buf;
498  Buffer buf;
499  HashPageOpaque bucket_opaque;
500  Page page;
501  bool split_cleanup = false;
502 
503  /* Get address of bucket's start page */
504  bucket_blkno = BUCKET_TO_BLKNO(cachedmetap, cur_bucket);
505 
506  blkno = bucket_blkno;
507 
508  /*
509  * We need to acquire a cleanup lock on the primary bucket page to out
510  * wait concurrent scans before deleting the dead tuples.
511  */
512  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, info->strategy);
515 
516  page = BufferGetPage(buf);
517  bucket_opaque = (HashPageOpaque) PageGetSpecialPointer(page);
518 
519  /*
520  * If the bucket contains tuples that are moved by split, then we need
521  * to delete such tuples. We can't delete such tuples if the split
522  * operation on bucket is not finished as those are needed by scans.
523  */
524  if (!H_BUCKET_BEING_SPLIT(bucket_opaque) &&
525  H_NEEDS_SPLIT_CLEANUP(bucket_opaque))
526  {
527  split_cleanup = true;
528 
529  /*
530  * This bucket might have been split since we last held a lock on
531  * the metapage. If so, hashm_maxbucket, hashm_highmask and
532  * hashm_lowmask might be old enough to cause us to fail to remove
533  * tuples left behind by the most recent split. To prevent that,
534  * now that the primary page of the target bucket has been locked
535  * (and thus can't be further split), check whether we need to
536  * update our cached metapage data.
537  */
538  Assert(bucket_opaque->hasho_prevblkno != InvalidBlockNumber);
539  if (bucket_opaque->hasho_prevblkno > cachedmetap->hashm_maxbucket)
540  {
541  cachedmetap = _hash_getcachedmetap(rel, &metabuf, true);
542  Assert(cachedmetap != NULL);
543  }
544  }
545 
546  bucket_buf = buf;
547 
548  hashbucketcleanup(rel, cur_bucket, bucket_buf, blkno, info->strategy,
549  cachedmetap->hashm_maxbucket,
550  cachedmetap->hashm_highmask,
551  cachedmetap->hashm_lowmask, &tuples_removed,
552  &num_index_tuples, split_cleanup,
553  callback, callback_state);
554 
555  _hash_dropbuf(rel, bucket_buf);
556 
557  /* Advance to next bucket */
558  cur_bucket++;
559  }
560 
561  if (BufferIsInvalid(metabuf))
563 
564  /* Write-lock metapage and check for split since we started */
566  metap = HashPageGetMeta(BufferGetPage(metabuf));
567 
568  if (cur_maxbucket != metap->hashm_maxbucket)
569  {
570  /* There's been a split, so process the additional bucket(s) */
571  LockBuffer(metabuf, BUFFER_LOCK_UNLOCK);
572  cachedmetap = _hash_getcachedmetap(rel, &metabuf, true);
573  Assert(cachedmetap != NULL);
574  cur_maxbucket = cachedmetap->hashm_maxbucket;
575  goto loop_top;
576  }
577 
578  /* Okay, we're really done. Update tuple count in metapage. */
580 
581  if (orig_maxbucket == metap->hashm_maxbucket &&
582  orig_ntuples == metap->hashm_ntuples)
583  {
584  /*
585  * No one has split or inserted anything since start of scan, so
586  * believe our count as gospel.
587  */
588  metap->hashm_ntuples = num_index_tuples;
589  }
590  else
591  {
592  /*
593  * Otherwise, our count is untrustworthy since we may have
594  * double-scanned tuples in split buckets. Proceed by dead-reckoning.
595  * (Note: we still return estimated_count = false, because using this
596  * count is better than not updating reltuples at all.)
597  */
598  if (metap->hashm_ntuples > tuples_removed)
599  metap->hashm_ntuples -= tuples_removed;
600  else
601  metap->hashm_ntuples = 0;
602  num_index_tuples = metap->hashm_ntuples;
603  }
604 
605  MarkBufferDirty(metabuf);
606 
607  /* XLOG stuff */
608  if (RelationNeedsWAL(rel))
609  {
611  XLogRecPtr recptr;
612 
613  xlrec.ntuples = metap->hashm_ntuples;
614 
615  XLogBeginInsert();
616  XLogRegisterData((char *) &xlrec, SizeOfHashUpdateMetaPage);
617 
618  XLogRegisterBuffer(0, metabuf, REGBUF_STANDARD);
619 
620  recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_UPDATE_META_PAGE);
621  PageSetLSN(BufferGetPage(metabuf), recptr);
622  }
623 
625 
626  _hash_relbuf(rel, metabuf);
627 
628  /* return statistics */
629  if (stats == NULL)
631  stats->estimated_count = false;
632  stats->num_index_tuples = num_index_tuples;
633  stats->tuples_removed += tuples_removed;
634  /* hashvacuumcleanup will fill in num_pages */
635 
636  return stats;
637 }
#define InvalidBlockNumber
Definition: block.h:33
#define BufferIsInvalid(buffer)
Definition: buf.h:31
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4053
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:741
@ RBM_NORMAL
Definition: bufmgr.h:39
void hashbucketcleanup(Relation rel, Bucket cur_bucket, Buffer bucket_buf, BlockNumber bucket_blkno, BufferAccessStrategy bstrategy, uint32 maxbucket, uint32 highmask, uint32 lowmask, double *tuples_removed, double *num_index_tuples, bool split_cleanup, IndexBulkDeleteCallback callback, void *callback_state)
Definition: hash.c:684
#define HASH_NOLOCK
Definition: hash.h:339
#define LH_BUCKET_PAGE
Definition: hash.h:55
#define H_BUCKET_BEING_SPLIT(opaque)
Definition: hash.h:89
#define LH_META_PAGE
Definition: hash.h:57
#define HashPageGetMeta(page)
Definition: hash.h:321
#define BUCKET_TO_BLKNO(metap, B)
Definition: hash.h:39
#define HASH_METAPAGE
Definition: hash.h:196
#define H_NEEDS_SPLIT_CLEANUP(opaque)
Definition: hash.h:88
#define XLOG_HASH_UPDATE_META_PAGE
Definition: hash_xlog.h:38
#define SizeOfHashUpdateMetaPage
Definition: hash_xlog.h:200
HashMetaPage _hash_getcachedmetap(Relation rel, Buffer *metabuf, bool force_refresh)
Definition: hashpage.c:1496
void _hash_dropbuf(Relation rel, Buffer buf)
Definition: hashpage.c:276
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition: hashpage.c:69
void _hash_checkpage(Relation rel, Buffer buf, int flags)
Definition: hashutil.c:211
void * palloc0(Size size)
Definition: mcxt.c:1093
uint32 hashm_lowmask
Definition: hash.h:254
uint32 hashm_maxbucket
Definition: hash.h:252
double hashm_ntuples
Definition: hash.h:246
uint32 hashm_highmask
Definition: hash.h:253
BlockNumber hasho_prevblkno
Definition: hash.h:79
bool estimated_count
Definition: genam.h:77
double tuples_removed
Definition: genam.h:79
double num_index_tuples
Definition: genam.h:78
Relation index
Definition: genam.h:46
BufferAccessStrategy strategy
Definition: genam.h:52

References _hash_checkpage(), _hash_dropbuf(), _hash_getbuf(), _hash_getcachedmetap(), _hash_relbuf(), Assert(), BUCKET_TO_BLKNO, buf, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage, BufferIsInvalid, callback(), END_CRIT_SECTION, IndexBulkDeleteResult::estimated_count, H_BUCKET_BEING_SPLIT, H_NEEDS_SPLIT_CLEANUP, HASH_METAPAGE, HASH_NOLOCK, hashbucketcleanup(), HashMetaPageData::hashm_highmask, HashMetaPageData::hashm_lowmask, HashMetaPageData::hashm_maxbucket, HashMetaPageData::hashm_ntuples, HashPageOpaqueData::hasho_prevblkno, HashPageGetMeta, IndexVacuumInfo::index, InvalidBlockNumber, InvalidBuffer, LH_BUCKET_PAGE, LH_META_PAGE, LockBuffer(), LockBufferForCleanup(), MAIN_FORKNUM, MarkBufferDirty(), xl_hash_update_meta_page::ntuples, IndexBulkDeleteResult::num_index_tuples, PageGetSpecialPointer, PageSetLSN, palloc0(), RBM_NORMAL, ReadBufferExtended(), REGBUF_STANDARD, RelationNeedsWAL, SizeOfHashUpdateMetaPage, START_CRIT_SECTION, IndexVacuumInfo::strategy, IndexBulkDeleteResult::tuples_removed, XLOG_HASH_UPDATE_META_PAGE, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by hashhandler().

◆ hashendscan()

void hashendscan ( IndexScanDesc  scan)

Definition at line 428 of file hash.c.

429 {
430  HashScanOpaque so = (HashScanOpaque) scan->opaque;
431  Relation rel = scan->indexRelation;
432 
434  {
435  /* Before leaving current page, deal with any killed items */
436  if (so->numKilled > 0)
437  _hash_kill_items(scan);
438  }
439 
440  _hash_dropscanbuf(rel, so);
441 
442  if (so->killedItems != NULL)
443  pfree(so->killedItems);
444  pfree(so);
445  scan->opaque = NULL;
446 }
#define HashScanPosIsValid(scanpos)
Definition: hash.h:135
void _hash_dropscanbuf(Relation rel, HashScanOpaque so)
Definition: hashpage.c:288
void _hash_kill_items(IndexScanDesc scan)
Definition: hashutil.c:537
Relation indexRelation
Definition: relscan.h:118

References _hash_dropscanbuf(), _hash_kill_items(), HashScanOpaqueData::currPos, HashScanPosIsValid, if(), IndexScanDescData::indexRelation, HashScanOpaqueData::killedItems, HashScanOpaqueData::numKilled, IndexScanDescData::opaque, and pfree().

Referenced by hashhandler().

◆ hashgetbitmap()

int64 hashgetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 332 of file hash.c.

333 {
334  HashScanOpaque so = (HashScanOpaque) scan->opaque;
335  bool res;
336  int64 ntids = 0;
337  HashScanPosItem *currItem;
338 
340 
341  while (res)
342  {
343  currItem = &so->currPos.items[so->currPos.itemIndex];
344 
345  /*
346  * _hash_first and _hash_next handle eliminate dead index entries
347  * whenever scan->ignore_killed_tuples is true. Therefore, there's
348  * nothing to do here except add the results to the TIDBitmap.
349  */
350  tbm_add_tuples(tbm, &(currItem->heapTid), 1, true);
351  ntids++;
352 
354  }
355 
356  return ntids;
357 }
bool _hash_first(IndexScanDesc scan, ScanDirection dir)
Definition: hashsearch.c:292
bool _hash_next(IndexScanDesc scan, ScanDirection dir)
Definition: hashsearch.c:48
@ ForwardScanDirection
Definition: sdir.h:26
HashScanPosItem items[MaxIndexTuplesPerPage]
Definition: hash.h:125
int itemIndex
Definition: hash.h:123
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:376

References _hash_first(), _hash_next(), HashScanOpaqueData::currPos, ForwardScanDirection, HashScanPosData::itemIndex, HashScanPosData::items, IndexScanDescData::opaque, res, and tbm_add_tuples().

Referenced by hashhandler().

◆ hashgettuple()

bool hashgettuple ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 280 of file hash.c.

281 {
282  HashScanOpaque so = (HashScanOpaque) scan->opaque;
283  bool res;
284 
285  /* Hash indexes are always lossy since we store only the hash code */
286  scan->xs_recheck = true;
287 
288  /*
289  * If we've already initialized this scan, we can just advance it in the
290  * appropriate direction. If we haven't done so yet, we call a routine to
291  * get the first item in the scan.
292  */
294  res = _hash_first(scan, dir);
295  else
296  {
297  /*
298  * Check to see if we should kill the previously-fetched tuple.
299  */
300  if (scan->kill_prior_tuple)
301  {
302  /*
303  * Yes, so remember it for later. (We'll deal with all such tuples
304  * at once right after leaving the index page or at end of scan.)
305  * In case if caller reverses the indexscan direction it is quite
306  * possible that the same item might get entered multiple times.
307  * But, we don't detect that; instead, we just forget any excess
308  * entries.
309  */
310  if (so->killedItems == NULL)
311  so->killedItems = (int *)
312  palloc(MaxIndexTuplesPerPage * sizeof(int));
313 
315  so->killedItems[so->numKilled++] = so->currPos.itemIndex;
316  }
317 
318  /*
319  * Now continue the scan.
320  */
321  res = _hash_next(scan, dir);
322  }
323 
324  return res;
325 }
#define MaxIndexTuplesPerPage
Definition: itup.h:144
bool kill_prior_tuple
Definition: relscan.h:128

References _hash_first(), _hash_next(), HashScanOpaqueData::currPos, HashScanPosIsValid, if(), HashScanPosData::itemIndex, IndexScanDescData::kill_prior_tuple, HashScanOpaqueData::killedItems, MaxIndexTuplesPerPage, HashScanOpaqueData::numKilled, IndexScanDescData::opaque, palloc(), res, and IndexScanDescData::xs_recheck.

Referenced by hashhandler().

◆ hashhandler()

Datum hashhandler ( PG_FUNCTION_ARGS  )

Definition at line 56 of file hash.c.

57 {
59 
60  amroutine->amstrategies = HTMaxStrategyNumber;
61  amroutine->amsupport = HASHNProcs;
62  amroutine->amoptsprocnum = HASHOPTIONS_PROC;
63  amroutine->amcanorder = false;
64  amroutine->amcanorderbyop = false;
65  amroutine->amcanbackward = true;
66  amroutine->amcanunique = false;
67  amroutine->amcanmulticol = false;
68  amroutine->amoptionalkey = false;
69  amroutine->amsearcharray = false;
70  amroutine->amsearchnulls = false;
71  amroutine->amstorage = false;
72  amroutine->amclusterable = false;
73  amroutine->ampredlocks = true;
74  amroutine->amcanparallel = false;
75  amroutine->amcaninclude = false;
76  amroutine->amusemaintenanceworkmem = false;
77  amroutine->amhotblocking = true;
78  amroutine->amparallelvacuumoptions =
80  amroutine->amkeytype = INT4OID;
81 
82  amroutine->ambuild = hashbuild;
83  amroutine->ambuildempty = hashbuildempty;
84  amroutine->aminsert = hashinsert;
85  amroutine->ambulkdelete = hashbulkdelete;
87  amroutine->amcanreturn = NULL;
88  amroutine->amcostestimate = hashcostestimate;
89  amroutine->amoptions = hashoptions;
90  amroutine->amproperty = NULL;
91  amroutine->ambuildphasename = NULL;
92  amroutine->amvalidate = hashvalidate;
94  amroutine->ambeginscan = hashbeginscan;
95  amroutine->amrescan = hashrescan;
96  amroutine->amgettuple = hashgettuple;
97  amroutine->amgetbitmap = hashgetbitmap;
98  amroutine->amendscan = hashendscan;
99  amroutine->ammarkpos = NULL;
100  amroutine->amrestrpos = NULL;
101  amroutine->amestimateparallelscan = NULL;
102  amroutine->aminitparallelscan = NULL;
103  amroutine->amparallelrescan = NULL;
104 
105  PG_RETURN_POINTER(amroutine);
106 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
bool hashinsert(Relation rel, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition: hash.c:248
IndexBulkDeleteResult * hashvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: hash.c:645
IndexBulkDeleteResult * hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: hash.c:459
bool hashgettuple(IndexScanDesc scan, ScanDirection dir)
Definition: hash.c:280
void hashbuildempty(Relation index)
Definition: hash.c:198
IndexScanDesc hashbeginscan(Relation rel, int nkeys, int norderbys)
Definition: hash.c:364
void hashrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: hash.c:394
void hashendscan(IndexScanDesc scan)
Definition: hash.c:428
IndexBuildResult * hashbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: hash.c:112
int64 hashgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: hash.c:332
#define HASHNProcs
Definition: hash.h:356
#define HASHOPTIONS_PROC
Definition: hash.h:355
bytea * hashoptions(Datum reloptions, bool validate)
Definition: hashutil.c:276
void hashadjustmembers(Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
Definition: hashvalidate.c:352
bool hashvalidate(Oid opclassoid)
Definition: hashvalidate.c:47
#define makeNode(_type_)
Definition: nodes.h:587
void hashcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:6914
#define HTMaxStrategyNumber
Definition: stratnum.h:43
ambuildphasename_function ambuildphasename
Definition: amapi.h:270
ambuildempty_function ambuildempty
Definition: amapi.h:262
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:265
bool amclusterable
Definition: amapi.h:238
amoptions_function amoptions
Definition: amapi.h:268
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:282
amrestrpos_function amrestrpos
Definition: amapi.h:279
aminsert_function aminsert
Definition: amapi.h:263
amendscan_function amendscan
Definition: amapi.h:277
uint16 amoptsprocnum
Definition: amapi.h:218
amparallelrescan_function amparallelrescan
Definition: amapi.h:284
Oid amkeytype
Definition: amapi.h:252
bool ampredlocks
Definition: amapi.h:240
uint16 amsupport
Definition: amapi.h:216
amcostestimate_function amcostestimate
Definition: amapi.h:267
bool amcanorderbyop
Definition: amapi.h:222
amadjustmembers_function amadjustmembers
Definition: amapi.h:272
ambuild_function ambuild
Definition: amapi.h:261
bool amstorage
Definition: amapi.h:236
uint16 amstrategies
Definition: amapi.h:214
bool amoptionalkey
Definition: amapi.h:230
amgettuple_function amgettuple
Definition: amapi.h:275
amcanreturn_function amcanreturn
Definition: amapi.h:266
bool amcanunique
Definition: amapi.h:226
amgetbitmap_function amgetbitmap
Definition: amapi.h:276
amproperty_function amproperty
Definition: amapi.h:269
ambulkdelete_function ambulkdelete
Definition: amapi.h:264
bool amsearcharray
Definition: amapi.h:232
amvalidate_function amvalidate
Definition: amapi.h:271
ammarkpos_function ammarkpos
Definition: amapi.h:278
bool amcanmulticol
Definition: amapi.h:228
bool amusemaintenanceworkmem
Definition: amapi.h:246
ambeginscan_function ambeginscan
Definition: amapi.h:273
bool amcanparallel
Definition: amapi.h:242
amrescan_function amrescan
Definition: amapi.h:274
bool amcanorder
Definition: amapi.h:220
bool amhotblocking
Definition: amapi.h:248
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:283
uint8 amparallelvacuumoptions
Definition: amapi.h:250
bool amcanbackward
Definition: amapi.h:224
bool amcaninclude
Definition: amapi.h:244
bool amsearchnulls
Definition: amapi.h:234
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition: vacuum.h:47

References IndexAmRoutine::amadjustmembers, IndexAmRoutine::ambeginscan, IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, IndexAmRoutine::ambuildphasename, IndexAmRoutine::ambulkdelete, IndexAmRoutine::amcanbackward, IndexAmRoutine::amcaninclude, IndexAmRoutine::amcanmulticol, IndexAmRoutine::amcanorder, IndexAmRoutine::amcanorderbyop, IndexAmRoutine::amcanparallel, IndexAmRoutine::amcanreturn, IndexAmRoutine::amcanunique, IndexAmRoutine::amclusterable, IndexAmRoutine::amcostestimate, IndexAmRoutine::amendscan, IndexAmRoutine::amestimateparallelscan, IndexAmRoutine::amgetbitmap, IndexAmRoutine::amgettuple, IndexAmRoutine::amhotblocking, IndexAmRoutine::aminitparallelscan, IndexAmRoutine::aminsert, IndexAmRoutine::amkeytype, IndexAmRoutine::ammarkpos, IndexAmRoutine::amoptionalkey, IndexAmRoutine::amoptions, IndexAmRoutine::amoptsprocnum, IndexAmRoutine::amparallelrescan, IndexAmRoutine::amparallelvacuumoptions, IndexAmRoutine::ampredlocks, IndexAmRoutine::amproperty, IndexAmRoutine::amrescan, IndexAmRoutine::amrestrpos, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, IndexAmRoutine::amstorage, IndexAmRoutine::amstrategies, IndexAmRoutine::amsupport, IndexAmRoutine::amusemaintenanceworkmem, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, hashadjustmembers(), hashbeginscan(), hashbuild(), hashbuildempty(), hashbulkdelete(), hashcostestimate(), hashendscan(), hashgetbitmap(), hashgettuple(), hashinsert(), HASHNProcs, hashoptions(), HASHOPTIONS_PROC, hashrescan(), hashvacuumcleanup(), hashvalidate(), HTMaxStrategyNumber, makeNode, PG_RETURN_POINTER, and VACUUM_OPTION_PARALLEL_BULKDEL.

◆ hashinsert()

bool hashinsert ( Relation  rel,
Datum values,
bool isnull,
ItemPointer  ht_ctid,
Relation  heapRel,
IndexUniqueCheck  checkUnique,
bool  indexUnchanged,
IndexInfo indexInfo 
)

Definition at line 248 of file hash.c.

253 {
254  Datum index_values[1];
255  bool index_isnull[1];
256  IndexTuple itup;
257 
258  /* convert data to a hash key; on failure, do not insert anything */
259  if (!_hash_convert_tuple(rel,
260  values, isnull,
261  index_values, index_isnull))
262  return false;
263 
264  /* form an index tuple and point it at the heap tuple */
265  itup = index_form_tuple(RelationGetDescr(rel), index_values, index_isnull);
266  itup->t_tid = *ht_ctid;
267 
268  _hash_doinsert(rel, itup, heapRel);
269 
270  pfree(itup);
271 
272  return false;
273 }

References _hash_convert_tuple(), _hash_doinsert(), index_form_tuple(), pfree(), RelationGetDescr, IndexTupleData::t_tid, and values.

Referenced by hashhandler().

◆ hashrescan()

void hashrescan ( IndexScanDesc  scan,
ScanKey  scankey,
int  nscankeys,
ScanKey  orderbys,
int  norderbys 
)

Definition at line 394 of file hash.c.

396 {
397  HashScanOpaque so = (HashScanOpaque) scan->opaque;
398  Relation rel = scan->indexRelation;
399 
401  {
402  /* Before leaving current page, deal with any killed items */
403  if (so->numKilled > 0)
404  _hash_kill_items(scan);
405  }
406 
407  _hash_dropscanbuf(rel, so);
408 
409  /* set position invalid (this will cause _hash_first call) */
411 
412  /* Update scan key, if a new one is given */
413  if (scankey && scan->numberOfKeys > 0)
414  {
415  memmove(scan->keyData,
416  scankey,
417  scan->numberOfKeys * sizeof(ScanKeyData));
418  }
419 
420  so->hashso_buc_populated = false;
421  so->hashso_buc_split = false;
422 }
struct ScanKeyData * keyData
Definition: relscan.h:122

References _hash_dropscanbuf(), _hash_kill_items(), HashScanOpaqueData::currPos, HashScanPosInvalidate, HashScanPosIsValid, HashScanOpaqueData::hashso_buc_populated, HashScanOpaqueData::hashso_buc_split, if(), IndexScanDescData::indexRelation, IndexScanDescData::keyData, IndexScanDescData::numberOfKeys, HashScanOpaqueData::numKilled, and IndexScanDescData::opaque.

Referenced by hashhandler().

◆ hashvacuumcleanup()

IndexBulkDeleteResult* hashvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 645 of file hash.c.

646 {
647  Relation rel = info->index;
648  BlockNumber num_pages;
649 
650  /* If hashbulkdelete wasn't called, return NULL signifying no change */
651  /* Note: this covers the analyze_only case too */
652  if (stats == NULL)
653  return NULL;
654 
655  /* update statistics */
656  num_pages = RelationGetNumberOfBlocks(rel);
657  stats->num_pages = num_pages;
658 
659  return stats;
660 }
BlockNumber num_pages
Definition: genam.h:76

References IndexVacuumInfo::index, IndexBulkDeleteResult::num_pages, and RelationGetNumberOfBlocks.

Referenced by hashhandler().