PostgreSQL Source Code git master
Loading...
Searching...
No Matches
hash.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * hash.c
4 * Implementation of Margo Seltzer's Hashing package for postgres.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/access/hash/hash.c
12 *
13 * NOTES
14 * This file contains only the public interface routines.
15 *
16 *-------------------------------------------------------------------------
17 */
18
19#include "postgres.h"
20
21#include "access/hash.h"
22#include "access/hash_xlog.h"
23#include "access/relscan.h"
24#include "access/stratnum.h"
25#include "access/tableam.h"
26#include "access/xloginsert.h"
27#include "commands/progress.h"
28#include "commands/vacuum.h"
29#include "miscadmin.h"
30#include "nodes/execnodes.h"
31#include "optimizer/plancat.h"
32#include "pgstat.h"
33#include "storage/read_stream.h"
34#include "utils/fmgrprotos.h"
36#include "utils/rel.h"
37
38/* Working state for hashbuild and its callback */
39typedef struct
40{
41 HSpool *spool; /* NULL if not using spooling */
42 double indtuples; /* # tuples accepted into index */
43 Relation heapRel; /* heap relation descriptor */
45
46/* Working state for streaming reads in hashbulkdelete */
47typedef struct
48{
49 HashMetaPage metap; /* cached metapage for BUCKET_TO_BLKNO */
50 Bucket next_bucket; /* next bucket to prefetch */
51 Bucket max_bucket; /* stop when next_bucket > max_bucket */
53
55 ItemPointer tid,
57 bool *isnull,
58 bool tupleIsAlive,
59 void *state);
61 void *callback_private_data,
62 void *per_buffer_data);
63
64
65/*
66 * Hash handler function: return IndexAmRoutine with access method parameters
67 * and callbacks.
68 */
71{
72 static const IndexAmRoutine amroutine = {
74 .amstrategies = HTMaxStrategyNumber,
75 .amsupport = HASHNProcs,
76 .amoptsprocnum = HASHOPTIONS_PROC,
77 .amcanorder = false,
78 .amcanorderbyop = false,
79 .amcanhash = true,
80 .amconsistentequality = true,
81 .amconsistentordering = false,
82 .amcanbackward = true,
83 .amcanunique = false,
84 .amcanmulticol = false,
85 .amoptionalkey = false,
86 .amsearcharray = false,
87 .amsearchnulls = false,
88 .amstorage = false,
89 .amclusterable = false,
90 .ampredlocks = true,
91 .amcanparallel = false,
92 .amcanbuildparallel = false,
93 .amcaninclude = false,
94 .amusemaintenanceworkmem = false,
95 .amsummarizing = false,
96 .amparallelvacuumoptions =
98 .amkeytype = INT4OID,
99
100 .ambuild = hashbuild,
101 .ambuildempty = hashbuildempty,
102 .aminsert = hashinsert,
103 .aminsertcleanup = NULL,
104 .ambulkdelete = hashbulkdelete,
105 .amvacuumcleanup = hashvacuumcleanup,
106 .amcanreturn = NULL,
107 .amcostestimate = hashcostestimate,
108 .amgettreeheight = NULL,
109 .amoptions = hashoptions,
110 .amproperty = NULL,
111 .ambuildphasename = NULL,
112 .amvalidate = hashvalidate,
113 .amadjustmembers = hashadjustmembers,
114 .ambeginscan = hashbeginscan,
115 .amrescan = hashrescan,
116 .amgettuple = hashgettuple,
117 .amgetbitmap = hashgetbitmap,
118 .amendscan = hashendscan,
119 .ammarkpos = NULL,
120 .amrestrpos = NULL,
121 .amestimateparallelscan = NULL,
122 .aminitparallelscan = NULL,
123 .amparallelrescan = NULL,
124 .amtranslatestrategy = hashtranslatestrategy,
125 .amtranslatecmptype = hashtranslatecmptype,
126 };
127
129}
130
131/*
132 * hashbuild() -- build a new hash index.
133 */
136{
137 IndexBuildResult *result;
138 BlockNumber relpages;
139 double reltuples;
140 double allvisfrac;
144
145 /*
146 * We expect to be called exactly once for any index relation. If that's
147 * not the case, big trouble's what we have.
148 */
150 elog(ERROR, "index \"%s\" already contains data",
152
153 /* Estimate the number of rows currently present in the table */
154 estimate_rel_size(heap, NULL, &relpages, &reltuples, &allvisfrac);
155
156 /* Initialize the hash index metadata page and initial buckets */
158
159 /*
160 * If we just insert the tuples into the index in scan order, then
161 * (assuming their hash codes are pretty random) there will be no locality
162 * of access to the index, and if the index is bigger than available RAM
163 * then we'll thrash horribly. To prevent that scenario, we can sort the
164 * tuples by (expected) bucket number. However, such a sort is useless
165 * overhead when the index does fit in RAM. We choose to sort if the
166 * initial index size exceeds maintenance_work_mem, or the number of
167 * buffers usable for the index, whichever is less. (Limiting by the
168 * number of buffers should reduce thrashing between PG buffers and kernel
169 * buffers, which seems useful even if no physical I/O results. Limiting
170 * by maintenance_work_mem is useful to allow easy testing of the sort
171 * code path, and may be useful to DBAs as an additional control knob.)
172 *
173 * NOTE: this test will need adjustment if a bucket is ever different from
174 * one page. Also, "initial index size" accounting does not include the
175 * metapage, nor the first bitmap page.
176 */
178 if (index->rd_rel->relpersistence != RELPERSISTENCE_TEMP)
180 else
182
185 else
186 buildstate.spool = NULL;
187
188 /* prepare to build the index */
189 buildstate.indtuples = 0;
190 buildstate.heapRel = heap;
191
192 /* do the heap scan */
193 reltuples = table_index_build_scan(heap, index, indexInfo, true, true,
195 &buildstate, NULL);
197 buildstate.indtuples);
198
199 if (buildstate.spool)
200 {
201 /* sort the tuples and insert them into the index */
202 _h_indexbuild(buildstate.spool, buildstate.heapRel);
204 }
205
206 /*
207 * Return statistics
208 */
210
211 result->heap_tuples = reltuples;
212 result->index_tuples = buildstate.indtuples;
213
214 return result;
215}
216
217/*
218 * hashbuildempty() -- build an empty hash index in the initialization fork
219 */
220void
225
226/*
227 * Per-tuple callback for table_index_build_scan
228 */
229static void
231 ItemPointer tid,
232 Datum *values,
233 bool *isnull,
234 bool tupleIsAlive,
235 void *state)
236{
239 bool index_isnull[1];
240 IndexTuple itup;
241
242 /* convert data to a hash key; on failure, do not insert anything */
244 values, isnull,
246 return;
247
248 /* Either spool the tuple for sorting, or just put it into the index */
249 if (buildstate->spool)
251 else
252 {
253 /* form an index tuple and point it at the heap tuple */
256 itup->t_tid = *tid;
257 _hash_doinsert(index, itup, buildstate->heapRel, false);
258 pfree(itup);
259 }
260
261 buildstate->indtuples += 1;
262}
263
264/*
265 * hashinsert() -- insert an index tuple into a hash table.
266 *
267 * Hash on the heap tuple's key, form an index tuple with hash code.
268 * Find the appropriate location for the new tuple, and put it there.
269 */
270bool
271hashinsert(Relation rel, Datum *values, bool *isnull,
274 bool indexUnchanged,
275 IndexInfo *indexInfo)
276{
278 bool index_isnull[1];
279 IndexTuple itup;
280
281 /* convert data to a hash key; on failure, do not insert anything */
282 if (!_hash_convert_tuple(rel,
283 values, isnull,
285 return false;
286
287 /* form an index tuple and point it at the heap tuple */
289 itup->t_tid = *ht_ctid;
290
291 _hash_doinsert(rel, itup, heapRel, false);
292
293 pfree(itup);
294
295 return false;
296}
297
298
299/*
300 * hashgettuple() -- Get the next tuple in the scan.
301 */
302bool
304{
306 bool res;
307
308 /* Hash indexes are always lossy since we store only the hash code */
309 scan->xs_recheck = true;
310
311 /*
312 * If we've already initialized this scan, we can just advance it in the
313 * appropriate direction. If we haven't done so yet, we call a routine to
314 * get the first item in the scan.
315 */
316 if (!HashScanPosIsValid(so->currPos))
317 res = _hash_first(scan, dir);
318 else
319 {
320 /*
321 * Check to see if we should kill the previously-fetched tuple.
322 */
323 if (scan->kill_prior_tuple)
324 {
325 /*
326 * Yes, so remember it for later. (We'll deal with all such tuples
327 * at once right after leaving the index page or at end of scan.)
328 * In case if caller reverses the indexscan direction it is quite
329 * possible that the same item might get entered multiple times.
330 * But, we don't detect that; instead, we just forget any excess
331 * entries.
332 */
333 if (so->killedItems == NULL)
334 so->killedItems = palloc_array(int, MaxIndexTuplesPerPage);
335
336 if (so->numKilled < MaxIndexTuplesPerPage)
337 so->killedItems[so->numKilled++] = so->currPos.itemIndex;
338 }
339
340 /*
341 * Now continue the scan.
342 */
343 res = _hash_next(scan, dir);
344 }
345
346 return res;
347}
348
349
350/*
351 * hashgetbitmap() -- get all tuples at once
352 */
353int64
355{
357 bool res;
358 int64 ntids = 0;
360
362
363 while (res)
364 {
365 currItem = &so->currPos.items[so->currPos.itemIndex];
366
367 /*
368 * _hash_first and _hash_next handle eliminate dead index entries
369 * whenever scan->ignore_killed_tuples is true. Therefore, there's
370 * nothing to do here except add the results to the TIDBitmap.
371 */
372 tbm_add_tuples(tbm, &(currItem->heapTid), 1, true);
373 ntids++;
374
375 res = _hash_next(scan, ForwardScanDirection);
376 }
377
378 return ntids;
379}
380
381
382/*
383 * hashbeginscan() -- start a scan on a hash index
384 */
386hashbeginscan(Relation rel, int nkeys, int norderbys)
387{
388 IndexScanDesc scan;
390
391 /* no order by operators allowed */
392 Assert(norderbys == 0);
393
394 scan = RelationGetIndexScan(rel, nkeys, norderbys);
395
397 HashScanPosInvalidate(so->currPos);
398 so->hashso_bucket_buf = InvalidBuffer;
399 so->hashso_split_bucket_buf = InvalidBuffer;
400
401 so->hashso_buc_populated = false;
402 so->hashso_buc_split = false;
403
404 so->killedItems = NULL;
405 so->numKilled = 0;
406
407 scan->opaque = so;
408
409 return scan;
410}
411
412/*
413 * hashrescan() -- rescan an index relation
414 */
415void
417 ScanKey orderbys, int norderbys)
418{
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 /* set position invalid (this will cause _hash_first call) */
432 HashScanPosInvalidate(so->currPos);
433
434 /* Update scan key, if a new one is given */
435 if (scankey && scan->numberOfKeys > 0)
436 memcpy(scan->keyData, scankey, scan->numberOfKeys * sizeof(ScanKeyData));
437
438 so->hashso_buc_populated = false;
439 so->hashso_buc_split = false;
440}
441
442/*
443 * hashendscan() -- close down a scan
444 */
445void
447{
449 Relation rel = scan->indexRelation;
450
451 if (HashScanPosIsValid(so->currPos))
452 {
453 /* Before leaving current page, deal with any killed items */
454 if (so->numKilled > 0)
455 _hash_kill_items(scan);
456 }
457
458 _hash_dropscanbuf(rel, so);
459
460 if (so->killedItems != NULL)
461 pfree(so->killedItems);
462 pfree(so);
463 scan->opaque = NULL;
464}
465
466/*
467 * Read stream callback for hashbulkdelete.
468 *
469 * Returns the block number of the primary page for the next bucket to
470 * vacuum, using the BUCKET_TO_BLKNO mapping from the cached metapage.
471 */
472static BlockNumber
474 void *callback_private_data,
475 void *per_buffer_data)
476{
477 HashBulkDeleteStreamPrivate *p = callback_private_data;
479
480 if (p->next_bucket > p->max_bucket)
481 return InvalidBlockNumber;
482
483 bucket = p->next_bucket++;
484 return BUCKET_TO_BLKNO(p->metap, bucket);
485}
486
487/*
488 * Bulk deletion of all index entries pointing to a set of heap tuples.
489 * The set of target tuples is specified via a callback routine that tells
490 * whether any given heap tuple (identified by ItemPointer) is being deleted.
491 *
492 * This function also deletes the tuples that are moved by split to other
493 * bucket.
494 *
495 * Result: a palloc'd struct containing statistical info for VACUUM displays.
496 */
499 IndexBulkDeleteCallback callback, void *callback_state)
500{
501 Relation rel = info->index;
502 double tuples_removed;
503 double num_index_tuples;
504 double orig_ntuples;
509 HashMetaPage metap;
512 ReadStream *stream = NULL;
513
514 tuples_removed = 0;
515 num_index_tuples = 0;
516
517 /*
518 * We need a copy of the metapage so that we can use its hashm_spares[]
519 * values to compute bucket page addresses, but a cached copy should be
520 * good enough. (If not, we'll detect that further down and refresh the
521 * cache as necessary.)
522 */
525
526 orig_maxbucket = cachedmetap->hashm_maxbucket;
527 orig_ntuples = cachedmetap->hashm_ntuples;
528
529 /* Scan the buckets that we know exist */
530 cur_bucket = 0;
532
533 /* Set up streaming read for primary bucket pages */
535 stream_private.next_bucket = cur_bucket;
536 stream_private.max_bucket = cur_maxbucket;
537
538 /*
539 * It is safe to use batchmode as hash_bulkdelete_read_stream_cb takes no
540 * locks.
541 */
544 info->strategy,
545 rel,
549 0);
550
552 while (cur_bucket <= cur_maxbucket)
553 {
555 BlockNumber blkno;
557 Buffer buf;
559 Page page;
560 bool split_cleanup = false;
561
562 /* Get address of bucket's start page */
564
565 blkno = bucket_blkno;
566
567 /*
568 * We need to acquire a cleanup lock on the primary bucket page to out
569 * wait concurrent scans before deleting the dead tuples.
570 */
575
576 page = BufferGetPage(buf);
578
579 /*
580 * If the bucket contains tuples that are moved by split, then we need
581 * to delete such tuples. We can't delete such tuples if the split
582 * operation on bucket is not finished as those are needed by scans.
583 */
586 {
587 split_cleanup = true;
588
589 /*
590 * This bucket might have been split since we last held a lock on
591 * the metapage. If so, hashm_maxbucket, hashm_highmask and
592 * hashm_lowmask might be old enough to cause us to fail to remove
593 * tuples left behind by the most recent split. To prevent that,
594 * now that the primary page of the target bucket has been locked
595 * (and thus can't be further split), check whether we need to
596 * update our cached metapage data.
597 */
598 Assert(bucket_opaque->hasho_prevblkno != InvalidBlockNumber);
599 if (bucket_opaque->hasho_prevblkno > cachedmetap->hashm_maxbucket)
600 {
603
604 /*
605 * Reset stream with updated metadata for remaining buckets.
606 * The BUCKET_TO_BLKNO mapping depends on hashm_spares[],
607 * which may have changed.
608 */
610 stream_private.next_bucket = cur_bucket + 1;
611 stream_private.max_bucket = cur_maxbucket;
612 read_stream_reset(stream);
613 }
614 }
615
616 bucket_buf = buf;
617
619 cachedmetap->hashm_maxbucket,
620 cachedmetap->hashm_highmask,
621 cachedmetap->hashm_lowmask, &tuples_removed,
622 &num_index_tuples, split_cleanup,
623 callback, callback_state);
624
626
627 /* Advance to next bucket */
628 cur_bucket++;
629 }
630
633
634 /* Write-lock metapage and check for split since we started */
637
638 if (cur_maxbucket != metap->hashm_maxbucket)
639 {
640 /* There's been a split, so process the additional bucket(s) */
644 cur_maxbucket = cachedmetap->hashm_maxbucket;
645
646 /* Reset stream to process additional buckets from split */
648 stream_private.next_bucket = cur_bucket;
649 stream_private.max_bucket = cur_maxbucket;
650 read_stream_reset(stream);
651 goto bucket_loop;
652 }
653
654 /* Stream should be exhausted since we processed all buckets */
656 read_stream_end(stream);
657
658 /* Okay, we're really done. Update tuple count in metapage. */
660
661 if (orig_maxbucket == metap->hashm_maxbucket &&
662 orig_ntuples == metap->hashm_ntuples)
663 {
664 /*
665 * No one has split or inserted anything since start of scan, so
666 * believe our count as gospel.
667 */
668 metap->hashm_ntuples = num_index_tuples;
669 }
670 else
671 {
672 /*
673 * Otherwise, our count is untrustworthy since we may have
674 * double-scanned tuples in split buckets. Proceed by dead-reckoning.
675 * (Note: we still return estimated_count = false, because using this
676 * count is better than not updating reltuples at all.)
677 */
678 if (metap->hashm_ntuples > tuples_removed)
679 metap->hashm_ntuples -= tuples_removed;
680 else
681 metap->hashm_ntuples = 0;
682 num_index_tuples = metap->hashm_ntuples;
683 }
684
686
687 /* XLOG stuff */
688 if (RelationNeedsWAL(rel))
689 {
692
693 xlrec.ntuples = metap->hashm_ntuples;
694
697
699
702 }
703
705
706 _hash_relbuf(rel, metabuf);
707
708 /* return statistics */
709 if (stats == NULL)
711 stats->estimated_count = false;
712 stats->num_index_tuples = num_index_tuples;
713 stats->tuples_removed += tuples_removed;
714 /* hashvacuumcleanup will fill in num_pages */
715
716 return stats;
717}
718
719/*
720 * Post-VACUUM cleanup.
721 *
722 * Result: a palloc'd struct containing statistical info for VACUUM displays.
723 */
726{
727 Relation rel = info->index;
728 BlockNumber num_pages;
729
730 /* If hashbulkdelete wasn't called, return NULL signifying no change */
731 /* Note: this covers the analyze_only case too */
732 if (stats == NULL)
733 return NULL;
734
735 /* update statistics */
736 num_pages = RelationGetNumberOfBlocks(rel);
737 stats->num_pages = num_pages;
738
739 return stats;
740}
741
742/*
743 * Helper function to perform deletion of index entries from a bucket.
744 *
745 * This function expects that the caller has acquired a cleanup lock on the
746 * primary bucket page, and will return with a write lock again held on the
747 * primary bucket page. The lock won't necessarily be held continuously,
748 * though, because we'll release it when visiting overflow pages.
749 *
750 * There can't be any concurrent scans in progress when we first enter this
751 * function because of the cleanup lock we hold on the primary bucket page,
752 * but as soon as we release that lock, there might be. If those scans got
753 * ahead of our cleanup scan, they might see a tuple before we kill it and
754 * wake up only after VACUUM has completed and the TID has been recycled for
755 * an unrelated tuple. To avoid that calamity, we prevent scans from passing
756 * our cleanup scan by locking the next page in the bucket chain before
757 * releasing the lock on the previous page. (This type of lock chaining is not
758 * ideal, so we might want to look for a better solution at some point.)
759 *
760 * We need to retain a pin on the primary bucket to ensure that no concurrent
761 * split can start.
762 */
763void
767 double *tuples_removed, double *num_index_tuples,
768 bool split_cleanup,
769 IndexBulkDeleteCallback callback, void *callback_state)
770{
771 BlockNumber blkno;
772 Buffer buf;
774 bool bucket_dirty = false;
775
776 blkno = bucket_blkno;
777 buf = bucket_buf;
778
779 if (split_cleanup)
782
783 /* Scan each page in bucket */
784 for (;;)
785 {
786 HashPageOpaque opaque;
790 Page page;
792 int ndeletable = 0;
793 bool retain_pin = false;
794 bool clear_dead_marking = false;
795
796 vacuum_delay_point(false);
797
798 page = BufferGetPage(buf);
799 opaque = HashPageGetOpaque(page);
800
801 /* Scan each tuple in page */
804 offno <= maxoffno;
806 {
807 ItemPointer htup;
808 IndexTuple itup;
810 bool kill_tuple = false;
811
812 itup = (IndexTuple) PageGetItem(page,
813 PageGetItemId(page, offno));
814 htup = &(itup->t_tid);
815
816 /*
817 * To remove the dead tuples, we strictly want to rely on results
818 * of callback function. refer btvacuumpage for detailed reason.
819 */
820 if (callback && callback(htup, callback_state))
821 {
822 kill_tuple = true;
823 if (tuples_removed)
824 *tuples_removed += 1;
825 }
826 else if (split_cleanup)
827 {
828 /* delete the tuples that are moved by split. */
830 maxbucket,
831 highmask,
832 lowmask);
833 /* mark the item for deletion */
834 if (bucket != cur_bucket)
835 {
836 /*
837 * We expect tuples to either belong to current bucket or
838 * new_bucket. This is ensured because we don't allow
839 * further splits from bucket that contains garbage. See
840 * comments in _hash_expandtable.
841 */
842 Assert(bucket == new_bucket);
843 kill_tuple = true;
844 }
845 }
846
847 if (kill_tuple)
848 {
849 /* mark the item for deletion */
851 }
852 else
853 {
854 /* we're keeping it, so count it */
855 if (num_index_tuples)
856 *num_index_tuples += 1;
857 }
858 }
859
860 /* retain the pin on primary bucket page till end of bucket scan */
861 if (blkno == bucket_blkno)
862 retain_pin = true;
863 else
864 retain_pin = false;
865
866 blkno = opaque->hasho_nextblkno;
867
868 /*
869 * Apply deletions, advance to next page and write page if needed.
870 */
871 if (ndeletable > 0)
872 {
873 /* No ereport(ERROR) until changes are logged */
875
877 bucket_dirty = true;
878
879 /*
880 * Let us mark the page as clean if vacuum removes the DEAD tuples
881 * from an index page. We do this by clearing
882 * LH_PAGE_HAS_DEAD_TUPLES flag.
883 */
884 if (tuples_removed && *tuples_removed > 0 &&
885 H_HAS_DEAD_TUPLES(opaque))
886 {
888 clear_dead_marking = true;
889 }
890
892
893 /* XLOG stuff */
894 if (RelationNeedsWAL(rel))
895 {
898
899 xlrec.clear_dead_marking = clear_dead_marking;
900 xlrec.is_primary_bucket_page = (buf == bucket_buf);
901
904
905 /*
906 * bucket buffer was not changed, but still needs to be
907 * registered to ensure that we can acquire a cleanup lock on
908 * it during replay.
909 */
910 if (!xlrec.is_primary_bucket_page)
911 {
913
915 }
916
919 ndeletable * sizeof(OffsetNumber));
920
923 }
924
926 }
927
928 /* bail out if there are no more pages to scan. */
929 if (!BlockNumberIsValid(blkno))
930 break;
931
934 bstrategy);
935
936 /*
937 * release the lock on previous page after acquiring the lock on next
938 * page
939 */
940 if (retain_pin)
942 else
943 _hash_relbuf(rel, buf);
944
945 buf = next_buf;
946 }
947
948 /*
949 * lock the bucket page to clear the garbage flag and squeeze the bucket.
950 * if the current buffer is same as bucket buffer, then we already have
951 * lock on bucket page.
952 */
953 if (buf != bucket_buf)
954 {
955 _hash_relbuf(rel, buf);
957 }
958
959 /*
960 * Clear the garbage flag from bucket after deleting the tuples that are
961 * moved by split. We purposefully clear the flag before squeeze bucket,
962 * so that after restart, vacuum shouldn't again try to delete the moved
963 * by split tuples.
964 */
965 if (split_cleanup)
966 {
968 Page page;
969
972
973 /* No ereport(ERROR) until changes are logged */
975
978
979 /* XLOG stuff */
980 if (RelationNeedsWAL(rel))
981 {
983
986
988 PageSetLSN(page, recptr);
989 }
990
992 }
993
994 /*
995 * If we have deleted anything, try to compact free space. For squeezing
996 * the bucket, we must have a cleanup lock, else it can impact the
997 * ordering of tuples for a scan that has started before it.
998 */
1001 bstrategy);
1002 else
1004}
1005
1008{
1009 if (strategy == HTEqualStrategyNumber)
1010 return COMPARE_EQ;
1011 return COMPARE_INVALID;
1012}
1013
1016{
1017 if (cmptype == COMPARE_EQ)
1018 return HTEqualStrategyNumber;
1019 return InvalidStrategy;
1020}
void pgstat_progress_update_param(int index, int64 val)
uint32 BlockNumber
Definition block.h:31
#define InvalidBlockNumber
Definition block.h:33
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition block.h:71
static Datum values[MAXATTR]
Definition bootstrap.c:188
int Buffer
Definition buf.h:23
#define BufferIsInvalid(buffer)
Definition buf.h:31
#define InvalidBuffer
Definition buf.h:25
bool IsBufferCleanupOK(Buffer buffer)
Definition bufmgr.c:6768
void MarkBufferDirty(Buffer buffer)
Definition bufmgr.c:3063
void LockBufferForCleanup(Buffer buffer)
Definition bufmgr.c:6537
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:307
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:470
@ BUFFER_LOCK_EXCLUSIVE
Definition bufmgr.h:220
@ BUFFER_LOCK_UNLOCK
Definition bufmgr.h:205
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:332
static bool BufferIsValid(Buffer bufnum)
Definition bufmgr.h:421
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition bufpage.c:1160
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition bufpage.h:269
static void * PageGetItem(PageData *page, const ItemIdData *itemId)
Definition bufpage.h:379
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition bufpage.h:417
PageData * Page
Definition bufpage.h:81
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition bufpage.h:397
#define Min(x, y)
Definition c.h:1093
uint8_t uint8
Definition c.h:616
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:243
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
uint32_t uint32
Definition c.h:618
size_t Size
Definition c.h:691
CompareType
Definition cmptype.h:32
@ COMPARE_INVALID
Definition cmptype.h:33
@ COMPARE_EQ
Definition cmptype.h:36
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define palloc0_object(type)
Definition fe_memutils.h:75
#define PG_RETURN_POINTER(x)
Definition fmgr.h:363
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition genam.c:80
bool(* IndexBulkDeleteCallback)(ItemPointer itemptr, void *state)
Definition genam.h:95
IndexUniqueCheck
Definition genam.h:124
int NBuffers
Definition globals.c:142
int maintenance_work_mem
Definition globals.c:133
bool hashinsert(Relation rel, Datum *values, bool *isnull, ItemPointer ht_ctid, Relation heapRel, IndexUniqueCheck checkUnique, bool indexUnchanged, IndexInfo *indexInfo)
Definition hash.c:271
IndexBulkDeleteResult * hashvacuumcleanup(IndexVacuumInfo *info, IndexBulkDeleteResult *stats)
Definition hash.c:725
IndexBuildResult * hashbuild(Relation heap, Relation index, IndexInfo *indexInfo)
Definition hash.c:135
Datum hashhandler(PG_FUNCTION_ARGS)
Definition hash.c:70
bool hashgettuple(IndexScanDesc scan, ScanDirection dir)
Definition hash.c:303
CompareType hashtranslatestrategy(StrategyNumber strategy, Oid opfamily)
Definition hash.c:1007
static void hashbuildCallback(Relation index, ItemPointer tid, Datum *values, bool *isnull, bool tupleIsAlive, void *state)
Definition hash.c:230
void hashbuildempty(Relation index)
Definition hash.c:221
IndexScanDesc hashbeginscan(Relation rel, int nkeys, int norderbys)
Definition hash.c:386
StrategyNumber hashtranslatecmptype(CompareType cmptype, Oid opfamily)
Definition hash.c:1015
IndexBulkDeleteResult * hashbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats, IndexBulkDeleteCallback callback, void *callback_state)
Definition hash.c:498
void hashrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition hash.c:416
void hashendscan(IndexScanDesc scan)
Definition hash.c:446
int64 hashgetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
Definition hash.c:354
static BlockNumber hash_bulkdelete_read_stream_cb(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
Definition hash.c:473
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:764
#define HASH_NOLOCK
Definition hash.h:341
#define HashPageGetOpaque(page)
Definition hash.h:88
#define LH_BUCKET_PAGE
Definition hash.h:55
#define HASH_WRITE
Definition hash.h:340
#define H_BUCKET_BEING_SPLIT(opaque)
Definition hash.h:91
#define LH_META_PAGE
Definition hash.h:57
#define HashScanPosInvalidate(scanpos)
Definition hash.h:144
#define HashPageGetMeta(page)
Definition hash.h:323
#define BUCKET_TO_BLKNO(metap, B)
Definition hash.h:39
#define HASH_METAPAGE
Definition hash.h:198
#define H_HAS_DEAD_TUPLES(opaque)
Definition hash.h:93
#define H_NEEDS_SPLIT_CLEANUP(opaque)
Definition hash.h:90
uint32 Bucket
Definition hash.h:35
#define HASHNProcs
Definition hash.h:358
HashScanOpaqueData * HashScanOpaque
Definition hash.h:192
#define HASHOPTIONS_PROC
Definition hash.h:357
#define HashScanPosIsValid(scanpos)
Definition hash.h:137
#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 XLOG_HASH_UPDATE_META_PAGE
Definition hash_xlog.h:38
#define SizeOfHashDelete
Definition hash_xlog.h:187
#define XLOG_HASH_DELETE
Definition hash_xlog.h:36
#define SizeOfHashUpdateMetaPage
Definition hash_xlog.h:201
void _hash_doinsert(Relation rel, IndexTuple itup, Relation heapRel, bool sorted)
Definition hashinsert.c:38
void _hash_squeezebucket(Relation rel, Bucket bucket, BlockNumber bucket_blkno, Buffer bucket_buf, BufferAccessStrategy bstrategy)
Definition hashovfl.c:842
HashMetaPage _hash_getcachedmetap(Relation rel, Buffer *metabuf, bool force_refresh)
Definition hashpage.c:1501
void _hash_relbuf(Relation rel, Buffer buf)
Definition hashpage.c:266
uint32 _hash_init(Relation rel, double num_tuples, ForkNumber forkNum)
Definition hashpage.c:327
void _hash_dropbuf(Relation rel, Buffer buf)
Definition hashpage.c:277
void _hash_dropscanbuf(Relation rel, HashScanOpaque so)
Definition hashpage.c:289
Buffer _hash_getbuf(Relation rel, BlockNumber blkno, int access, int flags)
Definition hashpage.c:70
Buffer _hash_getbuf_with_strategy(Relation rel, BlockNumber blkno, int access, int flags, BufferAccessStrategy bstrategy)
Definition hashpage.c:239
bool _hash_first(IndexScanDesc scan, ScanDirection dir)
Definition hashsearch.c:289
bool _hash_next(IndexScanDesc scan, ScanDirection dir)
Definition hashsearch.c:49
void _h_indexbuild(HSpool *hspool, Relation heapRel)
Definition hashsort.c:120
HSpool * _h_spoolinit(Relation heap, Relation index, uint32 num_buckets)
Definition hashsort.c:60
void _h_spool(HSpool *hspool, const ItemPointerData *self, const Datum *values, const bool *isnull)
Definition hashsort.c:109
void _h_spooldestroy(HSpool *hspool)
Definition hashsort.c:99
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition hashutil.c:291
Bucket _hash_hashkey2bucket(uint32 hashkey, uint32 maxbucket, uint32 highmask, uint32 lowmask)
Definition hashutil.c:125
void _hash_checkpage(Relation rel, Buffer buf, int flags)
Definition hashutil.c:210
Bucket _hash_get_newbucket_from_oldbucket(Relation rel, Bucket old_bucket, uint32 lowmask, uint32 maxbucket)
Definition hashutil.c:494
bytea * hashoptions(Datum reloptions, bool validate)
Definition hashutil.c:275
bool _hash_convert_tuple(Relation index, const Datum *user_values, const bool *user_isnull, Datum *index_values, bool *index_isnull)
Definition hashutil.c:318
void _hash_kill_items(IndexScanDesc scan)
Definition hashutil.c:536
void hashadjustmembers(Oid opfamilyoid, Oid opclassoid, List *operators, List *functions)
bool hashvalidate(Oid opclassoid)
IndexTuple index_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition indextuple.c:44
IndexTupleData * IndexTuple
Definition itup.h:53
#define MaxIndexTuplesPerPage
Definition itup.h:181
int NLocBuffer
Definition localbuf.c:45
void pfree(void *pointer)
Definition mcxt.c:1616
#define START_CRIT_SECTION()
Definition miscadmin.h:150
#define END_CRIT_SECTION()
Definition miscadmin.h:152
#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[DEFAULT_XLOG_SEG_SIZE]
void estimate_rel_size(Relation rel, int32 *attr_widths, BlockNumber *pages, double *tuples, double *allvisfrac)
Definition plancat.c:1305
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
#define PROGRESS_CREATEIDX_TUPLES_TOTAL
Definition progress.h:112
void read_stream_reset(ReadStream *stream)
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
ReadStream * read_stream_begin_relation(int flags, BufferAccessStrategy strategy, Relation rel, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
void read_stream_end(ReadStream *stream)
#define READ_STREAM_MAINTENANCE
Definition read_stream.h:28
#define READ_STREAM_USE_BATCHING
Definition read_stream.h:64
#define RelationGetDescr(relation)
Definition rel.h:540
#define RelationGetRelationName(relation)
Definition rel.h:548
#define RelationNeedsWAL(relation)
Definition rel.h:637
@ MAIN_FORKNUM
Definition relpath.h:58
@ INIT_FORKNUM
Definition relpath.h:61
ScanDirection
Definition sdir.h:25
@ ForwardScanDirection
Definition sdir.h:28
void hashcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, Cost *indexStartupCost, Cost *indexTotalCost, Selectivity *indexSelectivity, double *indexCorrelation, double *indexPages)
Definition selfuncs.c:8162
uint16 StrategyNumber
Definition stratnum.h:22
#define HTMaxStrategyNumber
Definition stratnum.h:43
#define InvalidStrategy
Definition stratnum.h:24
#define HTEqualStrategyNumber
Definition stratnum.h:41
HSpool * spool
Definition hash.c:41
Relation heapRel
Definition hash.c:43
double indtuples
Definition hash.c:42
HashMetaPage metap
Definition hash.c:49
uint32 hashm_maxbucket
Definition hash.h:254
double hashm_ntuples
Definition hash.h:248
BlockNumber hasho_nextblkno
Definition hash.h:80
uint16 hasho_flag
Definition hash.h:82
NodeTag type
Definition amapi.h:234
double heap_tuples
Definition genam.h:40
double index_tuples
Definition genam.h:41
BlockNumber num_pages
Definition genam.h:85
double tuples_removed
Definition genam.h:88
double num_index_tuples
Definition genam.h:87
struct ScanKeyData * keyData
Definition relscan.h:142
bool kill_prior_tuple
Definition relscan.h:148
Relation indexRelation
Definition relscan.h:138
ItemPointerData t_tid
Definition itup.h:37
Relation index
Definition genam.h:54
BufferAccessStrategy strategy
Definition genam.h:61
Definition type.h:96
bool clear_dead_marking
Definition hash_xlog.h:181
static double table_index_build_scan(Relation table_rel, Relation index_rel, IndexInfo *index_info, bool allow_sync, bool progress, IndexBuildCallback callback, void *callback_state, TableScanDesc scan)
Definition tableam.h:1765
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
void tbm_add_tuples(TIDBitmap *tbm, const ItemPointerData *tids, int ntids, bool recheck)
Definition tidbitmap.c:367
void vacuum_delay_point(bool is_analyze)
Definition vacuum.c:2431
#define VACUUM_OPTION_PARALLEL_BULKDEL
Definition vacuum.h:48
uint64 XLogRecPtr
Definition xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition xloginsert.c:479
void XLogRegisterBufData(uint8 block_id, const void *data, uint32 len)
Definition xloginsert.c:410
void XLogRegisterData(const void *data, uint32 len)
Definition xloginsert.c:369
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition xloginsert.c:246
void XLogBeginInsert(void)
Definition xloginsert.c:153
#define REGBUF_NO_CHANGE
Definition xloginsert.h:37
#define REGBUF_STANDARD
Definition xloginsert.h:35
#define REGBUF_NO_IMAGE
Definition xloginsert.h:33