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 "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, Datum *values, bool *isnull)
 
void _h_indexbuild (HSpool *hspool, Relation heapRel)
 

Function Documentation

◆ _h_indexbuild()

void _h_indexbuild ( HSpool hspool,
Relation  heapRel 
)

Definition at line 118 of file hashsort.c.

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

119 {
120  IndexTuple itup;
121  int64 tups_done = 0;
122 #ifdef USE_ASSERT_CHECKING
123  uint32 hashkey = 0;
124 #endif
125 
127 
128  while ((itup = tuplesort_getindextuple(hspool->sortstate, true)) != NULL)
129  {
130  /*
131  * Technically, it isn't critical that hash keys be found in sorted
132  * order, since this sorting is only used to increase locality of
133  * access as a performance optimization. It still seems like a good
134  * idea to test tuplesort.c's handling of hash index tuple sorts
135  * through an assertion, though.
136  */
137 #ifdef USE_ASSERT_CHECKING
138  uint32 lasthashkey = hashkey;
139 
141  hspool->max_buckets, hspool->high_mask,
142  hspool->low_mask);
143  Assert(hashkey >= lasthashkey);
144 #endif
145 
146  _hash_doinsert(hspool->index, itup, heapRel);
147 
149  ++tups_done);
150  }
151 }
IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward)
Definition: tuplesort.c:2216
void _hash_doinsert(Relation rel, IndexTuple itup, Relation heapRel)
Definition: hashinsert.c:36
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1791
Bucket _hash_hashkey2bucket(uint32 hashkey, uint32 maxbucket, uint32 highmask, uint32 lowmask)
Definition: hashutil.c:125
void pgstat_progress_update_param(int index, int64 val)
Definition: pgstat.c:3220
Tuplesortstate * sortstate
Definition: hashsort.c:40
uint32 low_mask
Definition: hashsort.c:49
uint32 high_mask
Definition: hashsort.c:48
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:299
unsigned int uint32
Definition: c.h:358
uint32 max_buckets
Definition: hashsort.c:50
Relation index
Definition: hashsort.c:41
#define Assert(condition)
Definition: c.h:732
#define PROGRESS_CREATEIDX_TUPLES_DONE
Definition: progress.h:68

◆ _h_spool()

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

Definition at line 107 of file hashsort.c.

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

Referenced by hashbuildCallback().

108 {
110  self, values, isnull);
111 }
Tuplesortstate * sortstate
Definition: hashsort.c:40
Relation index
Definition: hashsort.c:41
void tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel, ItemPointer self, Datum *values, bool *isnull)
Definition: tuplesort.c:1478
static Datum values[MAXATTR]
Definition: bootstrap.c:167

◆ _h_spooldestroy()

void _h_spooldestroy ( HSpool hspool)

Definition at line 97 of file hashsort.c.

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

Referenced by hashbuild().

98 {
99  tuplesort_end(hspool->sortstate);
100  pfree(hspool);
101 }
Tuplesortstate * sortstate
Definition: hashsort.c:40
void pfree(void *pointer)
Definition: mcxt.c:1056
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1236

◆ _h_spoolinit()

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

Definition at line 58 of file hashsort.c.

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

Referenced by hashbuild().

59 {
60  HSpool *hspool = (HSpool *) palloc0(sizeof(HSpool));
61 
62  hspool->index = index;
63 
64  /*
65  * Determine the bitmask for hash code values. Since there are currently
66  * num_buckets buckets in the index, the appropriate mask can be computed
67  * as follows.
68  *
69  * NOTE : This hash mask calculation should be in sync with similar
70  * calculation in _hash_init_metabuffer.
71  */
72  hspool->high_mask = (((uint32) 1) << _hash_log2(num_buckets + 1)) - 1;
73  hspool->low_mask = (hspool->high_mask >> 1);
74  hspool->max_buckets = num_buckets - 1;
75 
76  /*
77  * We size the sort area as maintenance_work_mem rather than work_mem to
78  * speed index creation. This should be OK since a single backend can't
79  * run multiple index creations in parallel.
80  */
82  index,
83  hspool->high_mask,
84  hspool->low_mask,
85  hspool->max_buckets,
87  NULL,
88  false);
89 
90  return hspool;
91 }
Tuplesortstate * tuplesort_begin_index_hash(Relation heapRel, Relation indexRel, uint32 high_mask, uint32 low_mask, uint32 max_buckets, int workMem, SortCoordinate coordinate, bool randomAccess)
Definition: tuplesort.c:1053
Tuplesortstate * sortstate
Definition: hashsort.c:40
uint32 low_mask
Definition: hashsort.c:49
uint32 high_mask
Definition: hashsort.c:48
unsigned int uint32
Definition: c.h:358
void * palloc0(Size size)
Definition: mcxt.c:980
uint32 max_buckets
Definition: hashsort.c:50
Relation index
Definition: hashsort.c:41
int maintenance_work_mem
Definition: globals.c:122
uint32 _hash_log2(uint32 num)
Definition: hashutil.c:141