PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
hashsort.c File Reference
#include "postgres.h"
#include "access/hash.h"
#include "miscadmin.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)
 

Function Documentation

void _h_indexbuild ( HSpool hspool)

Definition at line 104 of file hashsort.c.

References _hash_doinsert(), _hash_get_indextuple_hashkey(), Assert, HSpool::hash_mask, HSpool::index, NULL, HSpool::sortstate, tuplesort_getindextuple(), and tuplesort_performsort().

Referenced by hashbuild().

105 {
106  IndexTuple itup;
107 #ifdef USE_ASSERT_CHECKING
108  uint32 hashkey = 0;
109 #endif
110 
112 
113  while ((itup = tuplesort_getindextuple(hspool->sortstate, true)) != NULL)
114  {
115  /*
116  * Technically, it isn't critical that hash keys be found in sorted
117  * order, since this sorting is only used to increase locality of
118  * access as a performance optimization. It still seems like a good
119  * idea to test tuplesort.c's handling of hash index tuple sorts
120  * through an assertion, though.
121  */
122 #ifdef USE_ASSERT_CHECKING
123  uint32 lasthashkey = hashkey;
124 
125  hashkey = _hash_get_indextuple_hashkey(itup) & hspool->hash_mask;
126  Assert(hashkey >= lasthashkey);
127 #endif
128 
129  _hash_doinsert(hspool->index, itup);
130  }
131 }
IndexTuple tuplesort_getindextuple(Tuplesortstate *state, bool forward)
Definition: tuplesort.c:2154
void tuplesort_performsort(Tuplesortstate *state)
Definition: tuplesort.c:1763
Tuplesortstate * sortstate
Definition: hashsort.c:38
uint32 _hash_get_indextuple_hashkey(IndexTuple itup)
Definition: hashutil.c:232
unsigned int uint32
Definition: c.h:265
void _hash_doinsert(Relation rel, IndexTuple itup)
Definition: hashinsert.c:29
Relation index
Definition: hashsort.c:39
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
uint32 hash_mask
Definition: hashsort.c:40
void _h_spool ( HSpool hspool,
ItemPointer  self,
Datum values,
bool isnull 
)

Definition at line 93 of file hashsort.c.

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

Referenced by hashbuildCallback().

94 {
96  self, values, isnull);
97 }
Tuplesortstate * sortstate
Definition: hashsort.c:38
Relation index
Definition: hashsort.c:39
void tuplesort_putindextuplevalues(Tuplesortstate *state, Relation rel, ItemPointer self, Datum *values, bool *isnull)
Definition: tuplesort.c:1397
static Datum values[MAXATTR]
Definition: bootstrap.c:162
void _h_spooldestroy ( HSpool hspool)

Definition at line 83 of file hashsort.c.

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

Referenced by hashbuild().

84 {
85  tuplesort_end(hspool->sortstate);
86  pfree(hspool);
87 }
Tuplesortstate * sortstate
Definition: hashsort.c:38
void pfree(void *pointer)
Definition: mcxt.c:992
void tuplesort_end(Tuplesortstate *state)
Definition: tuplesort.c:1157
HSpool* _h_spoolinit ( Relation  heap,
Relation  index,
uint32  num_buckets 
)

Definition at line 48 of file hashsort.c.

References _hash_log2(), HSpool::hash_mask, HSpool::index, maintenance_work_mem, palloc0(), HSpool::sortstate, and tuplesort_begin_index_hash().

Referenced by hashbuild().

49 {
50  HSpool *hspool = (HSpool *) palloc0(sizeof(HSpool));
51 
52  hspool->index = index;
53 
54  /*
55  * Determine the bitmask for hash code values. Since there are currently
56  * num_buckets buckets in the index, the appropriate mask can be computed
57  * as follows.
58  *
59  * Note: at present, the passed-in num_buckets is always a power of 2, so
60  * we could just compute num_buckets - 1. We prefer not to assume that
61  * here, though.
62  */
63  hspool->hash_mask = (((uint32) 1) << _hash_log2(num_buckets)) - 1;
64 
65  /*
66  * We size the sort area as maintenance_work_mem rather than work_mem to
67  * speed index creation. This should be OK since a single backend can't
68  * run multiple index creations in parallel.
69  */
71  index,
72  hspool->hash_mask,
74  false);
75 
76  return hspool;
77 }
Tuplesortstate * sortstate
Definition: hashsort.c:38
Tuplesortstate * tuplesort_begin_index_hash(Relation heapRel, Relation indexRel, uint32 hash_mask, int workMem, bool randomAccess)
Definition: tuplesort.c:992
unsigned int uint32
Definition: c.h:265
void * palloc0(Size size)
Definition: mcxt.c:920
Relation index
Definition: hashsort.c:39
int maintenance_work_mem
Definition: globals.c:113
uint32 _hash_log2(uint32 num)
Definition: hashutil.c:140
uint32 hash_mask
Definition: hashsort.c:40