PostgreSQL Source Code  git master
hashsort.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "commands/progress.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "port/pg_bitutils.h"
#include "utils/tuplesort.h"
Include dependency graph for hashsort.c:

Go to the source code of this file.

Data Structures

struct  HSpool
 

Functions

HSpool_h_spoolinit (Relation heap, Relation index, uint32 num_buckets)
 
void _h_spooldestroy (HSpool *hspool)
 
void _h_spool (HSpool *hspool, ItemPointer self, const Datum *values, const bool *isnull)
 
void _h_indexbuild (HSpool *hspool, Relation heapRel)
 

Function Documentation

◆ _h_indexbuild()

void _h_indexbuild ( HSpool hspool,
Relation  heapRel 
)

Definition at line 120 of file hashsort.c.

121 {
122  IndexTuple itup;
123  int64 tups_done = 0;
124 #ifdef USE_ASSERT_CHECKING
125  uint32 hashkey = 0;
126 #endif
127 
129 
130  while ((itup = tuplesort_getindextuple(hspool->sortstate, true)) != NULL)
131  {
132  /*
133  * Technically, it isn't critical that hash keys be found in sorted
134  * order, since this sorting is only used to increase locality of
135  * access as a performance optimization. It still seems like a good
136  * idea to test tuplesort.c's handling of hash index tuple sorts
137  * through an assertion, though.
138  */
139 #ifdef USE_ASSERT_CHECKING
140  uint32 lasthashkey = hashkey;
141 
143  hspool->max_buckets, hspool->high_mask,
144  hspool->low_mask);
145  Assert(hashkey >= lasthashkey);
146 #endif
147 
148  /* the tuples are sorted by hashkey, so pass 'sorted' as true */
149  _hash_doinsert(hspool->index, itup, heapRel, true);
150 
152  ++tups_done);
153  }
154 }
void pgstat_progress_update_param(int index, int64 val)
unsigned int uint32
Definition: c.h:493
void _hash_doinsert(Relation rel, IndexTuple itup, Relation heapRel, bool sorted)
Definition: hashinsert.c:38
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
Assert(fmt[strlen(fmt) - 1] !='\n')
#define PROGRESS_CREATEIDX_TUPLES_DONE
Definition: progress.h:87
uint32 low_mask
Definition: hashsort.c:51
Tuplesortstate * sortstate
Definition: hashsort.c:41
uint32 high_mask
Definition: hashsort.c:50
uint32 max_buckets
Definition: hashsort.c:52
Relation index
Definition: hashsort.c:42
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1379
IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward)

References _hash_doinsert(), _hash_get_indextuple_hashkey(), _hash_hashkey2bucket(), Assert(), HSpool::high_mask, HSpool::index, HSpool::low_mask, HSpool::max_buckets, pgstat_progress_update_param(), PROGRESS_CREATEIDX_TUPLES_DONE, HSpool::sortstate, tuplesort_getindextuple(), and tuplesort_performsort().

Referenced by hashbuild().

◆ _h_spool()

void _h_spool ( HSpool hspool,
ItemPointer  self,
const Datum values,
const bool isnull 
)

Definition at line 109 of file hashsort.c.

110 {
112  self, values, isnull);
113 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
void tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel, ItemPointer self, const Datum *values, const bool *isnull)

References HSpool::index, HSpool::sortstate, tuplesort_putindextuplevalues(), and values.

Referenced by hashbuildCallback().

◆ _h_spooldestroy()

void _h_spooldestroy ( HSpool hspool)

Definition at line 99 of file hashsort.c.

100 {
101  tuplesort_end(hspool->sortstate);
102  pfree(hspool);
103 }
void pfree(void *pointer)
Definition: mcxt.c:1508
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:966

References pfree(), HSpool::sortstate, and tuplesort_end().

Referenced by hashbuild().

◆ _h_spoolinit()

HSpool* _h_spoolinit ( Relation  heap,
Relation  index,
uint32  num_buckets 
)

Definition at line 60 of file hashsort.c.

61 {
62  HSpool *hspool = (HSpool *) palloc0(sizeof(HSpool));
63 
64  hspool->index = index;
65 
66  /*
67  * Determine the bitmask for hash code values. Since there are currently
68  * num_buckets buckets in the index, the appropriate mask can be computed
69  * as follows.
70  *
71  * NOTE : This hash mask calculation should be in sync with similar
72  * calculation in _hash_init_metabuffer.
73  */
74  hspool->high_mask = pg_nextpower2_32(num_buckets + 1) - 1;
75  hspool->low_mask = (hspool->high_mask >> 1);
76  hspool->max_buckets = num_buckets - 1;
77 
78  /*
79  * We size the sort area as maintenance_work_mem rather than work_mem to
80  * speed index creation. This should be OK since a single backend can't
81  * run multiple index creations in parallel.
82  */
84  index,
85  hspool->high_mask,
86  hspool->low_mask,
87  hspool->max_buckets,
89  NULL,
91 
92  return hspool;
93 }
int maintenance_work_mem
Definition: globals.c:130
void * palloc0(Size size)
Definition: mcxt.c:1334
static uint32 pg_nextpower2_32(uint32 num)
Definition: pg_bitutils.h:189
Definition: type.h:95
#define TUPLESORT_NONE
Definition: tuplesort.h:93
Tuplesortstate * tuplesort_begin_index_hash(Relation heapRel, Relation indexRel, uint32 high_mask, uint32 low_mask, uint32 max_buckets, int workMem, SortCoordinate coordinate, int sortopt)

References HSpool::high_mask, HSpool::index, HSpool::low_mask, maintenance_work_mem, HSpool::max_buckets, palloc0(), pg_nextpower2_32(), HSpool::sortstate, tuplesort_begin_index_hash(), and TUPLESORT_NONE.

Referenced by hashbuild().