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 "catalog/index.h"
#include "commands/vacuum.h"
#include "miscadmin.h"
#include "optimizer/plancat.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, HeapTuple htup, 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, 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 353 of file hash.c.

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

354 {
355  IndexScanDesc scan;
356  HashScanOpaque so;
357 
358  /* no order by operators allowed */
359  Assert(norderbys == 0);
360 
361  scan = RelationGetIndexScan(rel, nkeys, norderbys);
362 
363  so = (HashScanOpaque) palloc(sizeof(HashScanOpaqueData));
367 
368  so->hashso_buc_populated = false;
369  so->hashso_buc_split = false;
370 
371  so->killedItems = NULL;
372  so->numKilled = 0;
373 
374  scan->opaque = so;
375 
376  return scan;
377 }
HashScanOpaqueData * HashScanOpaque
Definition: hash.h:200
#define InvalidBuffer
Definition: buf.h:25
int * killedItems
Definition: hash.h:190
Buffer hashso_bucket_buf
Definition: hash.h:172
#define HashScanPosInvalidate(scanpos)
Definition: hash.h:152
bool hashso_buc_populated
Definition: hash.h:182
#define Assert(condition)
Definition: c.h:699
HashScanPosData currPos
Definition: hash.h:197
bool hashso_buc_split
Definition: hash.h:188
void * palloc(Size size)
Definition: mcxt.c:924
Buffer hashso_split_bucket_buf
Definition: hash.h:179
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:78

◆ 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 673 of file hash.c.

References _hash_get_indextuple_hashkey(), _hash_get_newbucket_from_oldbucket(), _hash_getbuf_with_strategy(), _hash_hashkey2bucket(), _hash_relbuf(), _hash_squeezebucket(), Assert, GistBDItem::blkno, 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().

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

◆ hashbuild()

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

Definition at line 104 of file hash.c.

References _h_indexbuild(), _h_spooldestroy(), _h_spoolinit(), _hash_init(), elog, ERROR, estimate_rel_size(), hashbuildCallback(), IndexBuildResult::heap_tuples, HashBuildState::heapRel, IndexBuildResult::index_tuples, IndexBuildHeapScan(), HashBuildState::indtuples, MAIN_FORKNUM, maintenance_work_mem, Min, NBuffers, NLocBuffer, palloc(), RelationData::rd_rel, RelationGetNumberOfBlocks, RelationGetRelationName, relpages, reltuples, and HashBuildState::spool.

Referenced by hashhandler().

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

◆ hashbuildCallback()

static void hashbuildCallback ( Relation  index,
HeapTuple  htup,
Datum values,
bool isnull,
bool  tupleIsAlive,
void *  state 
)
static

Definition at line 196 of file hash.c.

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

Referenced by hashbuild().

202 {
203  HashBuildState *buildstate = (HashBuildState *) state;
204  Datum index_values[1];
205  bool index_isnull[1];
206  IndexTuple itup;
207 
208  /* convert data to a hash key; on failure, do not insert anything */
209  if (!_hash_convert_tuple(index,
210  values, isnull,
211  index_values, index_isnull))
212  return;
213 
214  /* Either spool the tuple for sorting, or just put it into the index */
215  if (buildstate->spool)
216  _h_spool(buildstate->spool, &htup->t_self,
217  index_values, index_isnull);
218  else
219  {
220  /* form an index tuple and point it at the heap tuple */
221  itup = index_form_tuple(RelationGetDescr(index),
222  index_values, index_isnull);
223  itup->t_tid = htup->t_self;
224  _hash_doinsert(index, itup, buildstate->heapRel);
225  pfree(itup);
226  }
227 
228  buildstate->indtuples += 1;
229 }
void _hash_doinsert(Relation rel, IndexTuple itup, Relation heapRel)
Definition: hashinsert.c:37
#define RelationGetDescr(relation)
Definition: rel.h:433
ItemPointerData t_tid
Definition: itup.h:37
double indtuples
Definition: hash.c:38
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:40
void pfree(void *pointer)
Definition: mcxt.c:1031
ItemPointerData t_self
Definition: htup.h:65
bool _hash_convert_tuple(Relation index, Datum *user_values, bool *user_isnull, Datum *index_values, bool *index_isnull)
Definition: hashutil.c:326
void _h_spool(HSpool *hspool, ItemPointer self, Datum *values, bool *isnull)
Definition: hashsort.c:105
HSpool * spool
Definition: hash.c:37
uintptr_t Datum
Definition: postgres.h:367
Relation heapRel
Definition: hash.c:39
Definition: regguts.h:298
static Datum values[MAXATTR]
Definition: bootstrap.c:164

◆ hashbuildempty()

void hashbuildempty ( Relation  index)

Definition at line 187 of file hash.c.

References _hash_init(), and INIT_FORKNUM.

Referenced by hashhandler().

188 {
189  _hash_init(index, 0, INIT_FORKNUM);
190 }
uint32 _hash_init(Relation rel, double num_tuples, ForkNumber forkNum)
Definition: hashpage.c:336

◆ hashbulkdelete()

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

Definition at line 448 of file hash.c.

References _hash_checkpage(), _hash_dropbuf(), _hash_getbuf(), _hash_getcachedmetap(), _hash_relbuf(), Assert, GistBDItem::blkno, BUCKET_TO_BLKNO, buf, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage, BufferIsInvalid, 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().

450 {
451  Relation rel = info->index;
452  double tuples_removed;
453  double num_index_tuples;
454  double orig_ntuples;
455  Bucket orig_maxbucket;
456  Bucket cur_maxbucket;
457  Bucket cur_bucket;
458  Buffer metabuf = InvalidBuffer;
459  HashMetaPage metap;
460  HashMetaPage cachedmetap;
461 
462  tuples_removed = 0;
463  num_index_tuples = 0;
464 
465  /*
466  * We need a copy of the metapage so that we can use its hashm_spares[]
467  * values to compute bucket page addresses, but a cached copy should be
468  * good enough. (If not, we'll detect that further down and refresh the
469  * cache as necessary.)
470  */
471  cachedmetap = _hash_getcachedmetap(rel, &metabuf, false);
472  Assert(cachedmetap != NULL);
473 
474  orig_maxbucket = cachedmetap->hashm_maxbucket;
475  orig_ntuples = cachedmetap->hashm_ntuples;
476 
477  /* Scan the buckets that we know exist */
478  cur_bucket = 0;
479  cur_maxbucket = orig_maxbucket;
480 
481 loop_top:
482  while (cur_bucket <= cur_maxbucket)
483  {
484  BlockNumber bucket_blkno;
485  BlockNumber blkno;
486  Buffer bucket_buf;
487  Buffer buf;
488  HashPageOpaque bucket_opaque;
489  Page page;
490  bool split_cleanup = false;
491 
492  /* Get address of bucket's start page */
493  bucket_blkno = BUCKET_TO_BLKNO(cachedmetap, cur_bucket);
494 
495  blkno = bucket_blkno;
496 
497  /*
498  * We need to acquire a cleanup lock on the primary bucket page to out
499  * wait concurrent scans before deleting the dead tuples.
500  */
501  buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_NORMAL, info->strategy);
503  _hash_checkpage(rel, buf, LH_BUCKET_PAGE);
504 
505  page = BufferGetPage(buf);
506  bucket_opaque = (HashPageOpaque) PageGetSpecialPointer(page);
507 
508  /*
509  * If the bucket contains tuples that are moved by split, then we need
510  * to delete such tuples. We can't delete such tuples if the split
511  * operation on bucket is not finished as those are needed by scans.
512  */
513  if (!H_BUCKET_BEING_SPLIT(bucket_opaque) &&
514  H_NEEDS_SPLIT_CLEANUP(bucket_opaque))
515  {
516  split_cleanup = true;
517 
518  /*
519  * This bucket might have been split since we last held a lock on
520  * the metapage. If so, hashm_maxbucket, hashm_highmask and
521  * hashm_lowmask might be old enough to cause us to fail to remove
522  * tuples left behind by the most recent split. To prevent that,
523  * now that the primary page of the target bucket has been locked
524  * (and thus can't be further split), check whether we need to
525  * update our cached metapage data.
526  */
527  Assert(bucket_opaque->hasho_prevblkno != InvalidBlockNumber);
528  if (bucket_opaque->hasho_prevblkno > cachedmetap->hashm_maxbucket)
529  {
530  cachedmetap = _hash_getcachedmetap(rel, &metabuf, true);
531  Assert(cachedmetap != NULL);
532  }
533  }
534 
535  bucket_buf = buf;
536 
537  hashbucketcleanup(rel, cur_bucket, bucket_buf, blkno, info->strategy,
538  cachedmetap->hashm_maxbucket,
539  cachedmetap->hashm_highmask,
540  cachedmetap->hashm_lowmask, &tuples_removed,
541  &num_index_tuples, split_cleanup,
542  callback, callback_state);
543 
544  _hash_dropbuf(rel, bucket_buf);
545 
546  /* Advance to next bucket */
547  cur_bucket++;
548  }
549 
550  if (BufferIsInvalid(metabuf))
552 
553  /* Write-lock metapage and check for split since we started */
555  metap = HashPageGetMeta(BufferGetPage(metabuf));
556 
557  if (cur_maxbucket != metap->hashm_maxbucket)
558  {
559  /* There's been a split, so process the additional bucket(s) */
560  LockBuffer(metabuf, BUFFER_LOCK_UNLOCK);
561  cachedmetap = _hash_getcachedmetap(rel, &metabuf, true);
562  Assert(cachedmetap != NULL);
563  cur_maxbucket = cachedmetap->hashm_maxbucket;
564  goto loop_top;
565  }
566 
567  /* Okay, we're really done. Update tuple count in metapage. */
569 
570  if (orig_maxbucket == metap->hashm_maxbucket &&
571  orig_ntuples == metap->hashm_ntuples)
572  {
573  /*
574  * No one has split or inserted anything since start of scan, so
575  * believe our count as gospel.
576  */
577  metap->hashm_ntuples = num_index_tuples;
578  }
579  else
580  {
581  /*
582  * Otherwise, our count is untrustworthy since we may have
583  * double-scanned tuples in split buckets. Proceed by dead-reckoning.
584  * (Note: we still return estimated_count = false, because using this
585  * count is better than not updating reltuples at all.)
586  */
587  if (metap->hashm_ntuples > tuples_removed)
588  metap->hashm_ntuples -= tuples_removed;
589  else
590  metap->hashm_ntuples = 0;
591  num_index_tuples = metap->hashm_ntuples;
592  }
593 
594  MarkBufferDirty(metabuf);
595 
596  /* XLOG stuff */
597  if (RelationNeedsWAL(rel))
598  {
600  XLogRecPtr recptr;
601 
602  xlrec.ntuples = metap->hashm_ntuples;
603 
604  XLogBeginInsert();
605  XLogRegisterData((char *) &xlrec, SizeOfHashUpdateMetaPage);
606 
607  XLogRegisterBuffer(0, metabuf, REGBUF_STANDARD);
608 
609  recptr = XLogInsert(RM_HASH_ID, XLOG_HASH_UPDATE_META_PAGE);
610  PageSetLSN(BufferGetPage(metabuf), recptr);
611  }
612 
614 
615  _hash_relbuf(rel, metabuf);
616 
617  /* return statistics */
618  if (stats == NULL)
620  stats->estimated_count = false;
621  stats->num_index_tuples = num_index_tuples;
622  stats->tuples_removed += tuples_removed;
623  /* hashvacuumcleanup will fill in num_pages */
624 
625  return stats;
626 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3603
double tuples_removed
Definition: genam.h:77
#define LH_META_PAGE
Definition: hash.h:67
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1450
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:213
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
BufferAccessStrategy strategy
Definition: genam.h:51
uint32 hashm_highmask
Definition: hash.h:260
#define InvalidBuffer
Definition: buf.h:25
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
Relation index
Definition: genam.h:46
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:673
uint32 BlockNumber
Definition: block.h:31
void _hash_dropbuf(Relation rel, Buffer buf)
Definition: hashpage.c:286
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition: hashpage.c:79
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
uint32 hashm_lowmask
Definition: hash.h:261
#define BUCKET_TO_BLKNO(metap, B)
Definition: hash.h:38
uint32 Bucket
Definition: hash.h:34
#define H_NEEDS_SPLIT_CLEANUP(opaque)
Definition: hash.h:98
BlockNumber hasho_prevblkno
Definition: hash.h:89
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
#define BufferIsInvalid(buffer)
Definition: buf.h:31
static char * buf
Definition: pg_test_fsync.c:67
#define HASH_NOLOCK
Definition: hash.h:331
#define REGBUF_STANDARD
Definition: xloginsert.h:34
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define SizeOfHashUpdateMetaPage
Definition: hash_xlog.h:219
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
void _hash_checkpage(Relation rel, Buffer buf, int flags)
Definition: hashutil.c:225
void * palloc0(Size size)
Definition: mcxt.c:955
#define HASH_METAPAGE
Definition: hash.h:206
double hashm_ntuples
Definition: hash.h:253
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
HashMetaPage _hash_getcachedmetap(Relation rel, Buffer *metabuf, bool force_refresh)
Definition: hashpage.c:1509
void _hash_relbuf(Relation rel, Buffer buf)
Definition: hashpage.c:275
#define LH_BUCKET_PAGE
Definition: hash.h:65
#define H_BUCKET_BEING_SPLIT(opaque)
Definition: hash.h:99
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:699
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
#define InvalidBlockNumber
Definition: block.h:33
HashPageOpaqueData * HashPageOpaque
Definition: hash.h:96
#define RelationNeedsWAL(relation)
Definition: rel.h:510
uint32 hashm_maxbucket
Definition: hash.h:259
#define HashPageGetMeta(page)
Definition: hash.h:313
void XLogBeginInsert(void)
Definition: xloginsert.c:120
#define XLOG_HASH_UPDATE_META_PAGE
Definition: hash_xlog.h:43
#define PageSetLSN(page, lsn)
Definition: bufpage.h:364
double num_index_tuples
Definition: genam.h:76
int Buffer
Definition: buf.h:23
bool estimated_count
Definition: genam.h:75
Pointer Page
Definition: bufpage.h:74

◆ hashendscan()

void hashendscan ( IndexScanDesc  scan)

Definition at line 417 of file hash.c.

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

Referenced by hashhandler().

418 {
419  HashScanOpaque so = (HashScanOpaque) scan->opaque;
420  Relation rel = scan->indexRelation;
421 
422  if (HashScanPosIsValid(so->currPos))
423  {
424  /* Before leaving current page, deal with any killed items */
425  if (so->numKilled > 0)
426  _hash_kill_items(scan);
427  }
428 
429  _hash_dropscanbuf(rel, so);
430 
431  if (so->killedItems != NULL)
432  pfree(so->killedItems);
433  pfree(so);
434  scan->opaque = NULL;
435 }
HashScanOpaqueData * HashScanOpaque
Definition: hash.h:200
void _hash_dropscanbuf(Relation rel, HashScanOpaque so)
Definition: hashpage.c:298
#define HashScanPosIsValid(scanpos)
Definition: hash.h:145
Relation indexRelation
Definition: relscan.h:91
int * killedItems
Definition: hash.h:190
void pfree(void *pointer)
Definition: mcxt.c:1031
void _hash_kill_items(IndexScanDesc scan)
Definition: hashutil.c:544
HashScanPosData currPos
Definition: hash.h:197

◆ hashgetbitmap()

int64 hashgetbitmap ( IndexScanDesc  scan,
TIDBitmap tbm 
)

Definition at line 321 of file hash.c.

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

Referenced by hashhandler().

322 {
323  HashScanOpaque so = (HashScanOpaque) scan->opaque;
324  bool res;
325  int64 ntids = 0;
326  HashScanPosItem *currItem;
327 
328  res = _hash_first(scan, ForwardScanDirection);
329 
330  while (res)
331  {
332  currItem = &so->currPos.items[so->currPos.itemIndex];
333 
334  /*
335  * _hash_first and _hash_next handle eliminate dead index entries
336  * whenever scan->ignored_killed_tuples is true. Therefore, there's
337  * nothing to do here except add the results to the TIDBitmap.
338  */
339  tbm_add_tuples(tbm, &(currItem->heapTid), 1, true);
340  ntids++;
341 
342  res = _hash_next(scan, ForwardScanDirection);
343  }
344 
345  return ntids;
346 }
HashScanOpaqueData * HashScanOpaque
Definition: hash.h:200
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointer tids, int ntids, bool recheck)
Definition: tidbitmap.c:376
bool _hash_first(IndexScanDesc scan, ScanDirection dir)
Definition: hashsearch.c:292
bool _hash_next(IndexScanDesc scan, ScanDirection dir)
Definition: hashsearch.c:48
HashScanPosData currPos
Definition: hash.h:197
int itemIndex
Definition: hash.h:133
HashScanPosItem items[MaxIndexTuplesPerPage]
Definition: hash.h:135

◆ hashgettuple()

bool hashgettuple ( IndexScanDesc  scan,
ScanDirection  dir 
)

Definition at line 269 of file hash.c.

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

Referenced by hashhandler().

270 {
271  HashScanOpaque so = (HashScanOpaque) scan->opaque;
272  bool res;
273 
274  /* Hash indexes are always lossy since we store only the hash code */
275  scan->xs_recheck = true;
276 
277  /*
278  * If we've already initialized this scan, we can just advance it in the
279  * appropriate direction. If we haven't done so yet, we call a routine to
280  * get the first item in the scan.
281  */
282  if (!HashScanPosIsValid(so->currPos))
283  res = _hash_first(scan, dir);
284  else
285  {
286  /*
287  * Check to see if we should kill the previously-fetched tuple.
288  */
289  if (scan->kill_prior_tuple)
290  {
291  /*
292  * Yes, so remember it for later. (We'll deal with all such tuples
293  * at once right after leaving the index page or at end of scan.)
294  * In case if caller reverses the indexscan direction it is quite
295  * possible that the same item might get entered multiple times.
296  * But, we don't detect that; instead, we just forget any excess
297  * entries.
298  */
299  if (so->killedItems == NULL)
300  so->killedItems = (int *)
301  palloc(MaxIndexTuplesPerPage * sizeof(int));
302 
304  so->killedItems[so->numKilled++] = so->currPos.itemIndex;
305  }
306 
307  /*
308  * Now continue the scan.
309  */
310  res = _hash_next(scan, dir);
311  }
312 
313  return res;
314 }
HashScanOpaqueData * HashScanOpaque
Definition: hash.h:200
#define HashScanPosIsValid(scanpos)
Definition: hash.h:145
int * killedItems
Definition: hash.h:190
bool _hash_first(IndexScanDesc scan, ScanDirection dir)
Definition: hashsearch.c:292
bool _hash_next(IndexScanDesc scan, ScanDirection dir)
Definition: hashsearch.c:48
HashScanPosData currPos
Definition: hash.h:197
#define MaxIndexTuplesPerPage
Definition: itup.h:145
void * palloc(Size size)
Definition: mcxt.c:924
bool kill_prior_tuple
Definition: relscan.h:101
int itemIndex
Definition: hash.h:133

◆ hashhandler()

Datum hashhandler ( PG_FUNCTION_ARGS  )

Definition at line 55 of file hash.c.

References IndexAmRoutine::ambeginscan, IndexAmRoutine::ambuild, IndexAmRoutine::ambuildempty, 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::aminitparallelscan, IndexAmRoutine::aminsert, IndexAmRoutine::amkeytype, IndexAmRoutine::ammarkpos, IndexAmRoutine::amoptionalkey, IndexAmRoutine::amoptions, IndexAmRoutine::amparallelrescan, IndexAmRoutine::ampredlocks, IndexAmRoutine::amproperty, IndexAmRoutine::amrescan, IndexAmRoutine::amrestrpos, IndexAmRoutine::amsearcharray, IndexAmRoutine::amsearchnulls, IndexAmRoutine::amstorage, IndexAmRoutine::amstrategies, IndexAmRoutine::amsupport, IndexAmRoutine::amvacuumcleanup, IndexAmRoutine::amvalidate, hashbeginscan(), hashbuild(), hashbuildempty(), hashbulkdelete(), hashcostestimate(), hashendscan(), hashgetbitmap(), hashgettuple(), hashinsert(), HASHNProcs, hashoptions(), hashrescan(), hashvacuumcleanup(), hashvalidate(), HTMaxStrategyNumber, makeNode, and PG_RETURN_POINTER.

56 {
58 
59  amroutine->amstrategies = HTMaxStrategyNumber;
60  amroutine->amsupport = HASHNProcs;
61  amroutine->amcanorder = false;
62  amroutine->amcanorderbyop = false;
63  amroutine->amcanbackward = true;
64  amroutine->amcanunique = false;
65  amroutine->amcanmulticol = false;
66  amroutine->amoptionalkey = false;
67  amroutine->amsearcharray = false;
68  amroutine->amsearchnulls = false;
69  amroutine->amstorage = false;
70  amroutine->amclusterable = false;
71  amroutine->ampredlocks = true;
72  amroutine->amcanparallel = false;
73  amroutine->amcaninclude = false;
74  amroutine->amkeytype = INT4OID;
75 
76  amroutine->ambuild = hashbuild;
77  amroutine->ambuildempty = hashbuildempty;
78  amroutine->aminsert = hashinsert;
79  amroutine->ambulkdelete = hashbulkdelete;
81  amroutine->amcanreturn = NULL;
82  amroutine->amcostestimate = hashcostestimate;
83  amroutine->amoptions = hashoptions;
84  amroutine->amproperty = NULL;
85  amroutine->amvalidate = hashvalidate;
86  amroutine->ambeginscan = hashbeginscan;
87  amroutine->amrescan = hashrescan;
88  amroutine->amgettuple = hashgettuple;
89  amroutine->amgetbitmap = hashgetbitmap;
90  amroutine->amendscan = hashendscan;
91  amroutine->ammarkpos = NULL;
92  amroutine->amrestrpos = NULL;
93  amroutine->amestimateparallelscan = NULL;
94  amroutine->aminitparallelscan = NULL;
95  amroutine->amparallelrescan = NULL;
96 
97  PG_RETURN_POINTER(amroutine);
98 }
ambeginscan_function ambeginscan
Definition: amapi.h:217
bytea * hashoptions(Datum reloptions, bool validate)
Definition: hashutil.c:290
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:326
ambulkdelete_function ambulkdelete
Definition: amapi.h:210
bool hashgettuple(IndexScanDesc scan, ScanDirection dir)
Definition: hash.c:269
bool amcanmulticol
Definition: amapi.h:180
uint16 amsupport
Definition: amapi.h:170
#define HASHNProcs
Definition: hash.h:353
amgettuple_function amgettuple
Definition: amapi.h:219
bool amcanorderbyop
Definition: amapi.h:174
IndexBulkDeleteResult * hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition: hash.c:448
amproperty_function amproperty
Definition: amapi.h:215
amparallelrescan_function amparallelrescan
Definition: amapi.h:228
void hashcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition: selfuncs.c:7235
bool amstorage
Definition: amapi.h:188
bool ampredlocks
Definition: amapi.h:192
IndexScanDesc hashbeginscan(Relation rel, int nkeys, int norderbys)
Definition: hash.c:353
#define HTMaxStrategyNumber
Definition: hash.h:337
aminsert_function aminsert
Definition: amapi.h:209
bool hashinsert(Relation rel, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, IndexInfo *indexInfo)
Definition: hash.c:238
Oid amkeytype
Definition: amapi.h:198
bool amoptionalkey
Definition: amapi.h:182
amvalidate_function amvalidate
Definition: amapi.h:216
void hashbuildempty(Relation index)
Definition: hash.c:187
amgetbitmap_function amgetbitmap
Definition: amapi.h:220
ambuild_function ambuild
Definition: amapi.h:207
amoptions_function amoptions
Definition: amapi.h:214
IndexBuildResult * hashbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition: hash.c:104
bool amcaninclude
Definition: amapi.h:196
amcostestimate_function amcostestimate
Definition: amapi.h:213
bool amcanunique
Definition: amapi.h:178
amvacuumcleanup_function amvacuumcleanup
Definition: amapi.h:211
amendscan_function amendscan
Definition: amapi.h:221
bool amcanbackward
Definition: amapi.h:176
amrescan_function amrescan
Definition: amapi.h:218
bool amcanparallel
Definition: amapi.h:194
int64 hashgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition: hash.c:321
bool amsearchnulls
Definition: amapi.h:186
void hashendscan(IndexScanDesc scan)
Definition: hash.c:417
bool amclusterable
Definition: amapi.h:190
bool amsearcharray
Definition: amapi.h:184
IndexBulkDeleteResult * hashvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition: hash.c:634
#define makeNode(_type_)
Definition: nodes.h:564
ammarkpos_function ammarkpos
Definition: amapi.h:222
bool amcanorder
Definition: amapi.h:172
amestimateparallelscan_function amestimateparallelscan
Definition: amapi.h:226
bool hashvalidate(Oid opclassoid)
Definition: hashvalidate.c:44
uint16 amstrategies
Definition: amapi.h:168
ambuildempty_function ambuildempty
Definition: amapi.h:208
amcanreturn_function amcanreturn
Definition: amapi.h:212
void hashrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: hash.c:383
aminitparallelscan_function aminitparallelscan
Definition: amapi.h:227
amrestrpos_function amrestrpos
Definition: amapi.h:223

◆ hashinsert()

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

Definition at line 238 of file hash.c.

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

Referenced by hashhandler().

242 {
243  Datum index_values[1];
244  bool index_isnull[1];
245  IndexTuple itup;
246 
247  /* convert data to a hash key; on failure, do not insert anything */
248  if (!_hash_convert_tuple(rel,
249  values, isnull,
250  index_values, index_isnull))
251  return false;
252 
253  /* form an index tuple and point it at the heap tuple */
254  itup = index_form_tuple(RelationGetDescr(rel), index_values, index_isnull);
255  itup->t_tid = *ht_ctid;
256 
257  _hash_doinsert(rel, itup, heapRel);
258 
259  pfree(itup);
260 
261  return false;
262 }
void _hash_doinsert(Relation rel, IndexTuple itup, Relation heapRel)
Definition: hashinsert.c:37
#define RelationGetDescr(relation)
Definition: rel.h:433
ItemPointerData t_tid
Definition: itup.h:37
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: indextuple.c:40
void pfree(void *pointer)
Definition: mcxt.c:1031
bool _hash_convert_tuple(Relation index, Datum *user_values, bool *user_isnull, Datum *index_values, bool *index_isnull)
Definition: hashutil.c:326
uintptr_t Datum
Definition: postgres.h:367
static Datum values[MAXATTR]
Definition: bootstrap.c:164

◆ hashrescan()

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

Definition at line 383 of file hash.c.

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

Referenced by hashhandler().

385 {
386  HashScanOpaque so = (HashScanOpaque) scan->opaque;
387  Relation rel = scan->indexRelation;
388 
389  if (HashScanPosIsValid(so->currPos))
390  {
391  /* Before leaving current page, deal with any killed items */
392  if (so->numKilled > 0)
393  _hash_kill_items(scan);
394  }
395 
396  _hash_dropscanbuf(rel, so);
397 
398  /* set position invalid (this will cause _hash_first call) */
400 
401  /* Update scan key, if a new one is given */
402  if (scankey && scan->numberOfKeys > 0)
403  {
404  memmove(scan->keyData,
405  scankey,
406  scan->numberOfKeys * sizeof(ScanKeyData));
407  }
408 
409  so->hashso_buc_populated = false;
410  so->hashso_buc_split = false;
411 }
HashScanOpaqueData * HashScanOpaque
Definition: hash.h:200
void _hash_dropscanbuf(Relation rel, HashScanOpaque so)
Definition: hashpage.c:298
#define HashScanPosIsValid(scanpos)
Definition: hash.h:145
Relation indexRelation
Definition: relscan.h:91
#define memmove(d, s, c)
Definition: c.h:1135
#define HashScanPosInvalidate(scanpos)
Definition: hash.h:152
bool hashso_buc_populated
Definition: hash.h:182
void _hash_kill_items(IndexScanDesc scan)
Definition: hashutil.c:544
ScanKey keyData
Definition: relscan.h:95
HashScanPosData currPos
Definition: hash.h:197
bool hashso_buc_split
Definition: hash.h:188

◆ hashvacuumcleanup()

IndexBulkDeleteResult* hashvacuumcleanup ( IndexVacuumInfo info,
IndexBulkDeleteResult stats 
)

Definition at line 634 of file hash.c.

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

Referenced by hashhandler().

635 {
636  Relation rel = info->index;
637  BlockNumber num_pages;
638 
639  /* If hashbulkdelete wasn't called, return NULL signifying no change */
640  /* Note: this covers the analyze_only case too */
641  if (stats == NULL)
642  return NULL;
643 
644  /* update statistics */
645  num_pages = RelationGetNumberOfBlocks(rel);
646  stats->num_pages = num_pages;
647 
648  return stats;
649 }
Relation index
Definition: genam.h:46
uint32 BlockNumber
Definition: block.h:31
BlockNumber num_pages
Definition: genam.h:73
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:199