PostgreSQL Source Code git master
Loading...
Searching...
No Matches
hashjoin.h File Reference
#include "nodes/execnodes.h"
#include "port/atomics.h"
#include "storage/barrier.h"
#include "storage/buffile.h"
#include "storage/lwlock.h"
Include dependency graph for hashjoin.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  HashJoinTupleData
 
struct  HashSkewBucket
 
struct  HashMemoryChunkData
 
struct  ParallelHashJoinBatch
 
struct  ParallelHashJoinBatchAccessor
 
struct  ParallelHashJoinState
 
struct  HashJoinTableData
 

Macros

#define HJTUPLE_OVERHEAD   MAXALIGN(sizeof(HashJoinTupleData))
 
#define HJTUPLE_MINTUPLE(hjtup)    ((MinimalTuple) ((char *) (hjtup) + HJTUPLE_OVERHEAD))
 
#define SKEW_BUCKET_OVERHEAD   MAXALIGN(sizeof(HashSkewBucket))
 
#define INVALID_SKEW_BUCKET_NO   (-1)
 
#define SKEW_HASH_MEM_PERCENT   2
 
#define SKEW_MIN_OUTER_FRACTION   0.01
 
#define HASH_CHUNK_SIZE   ((Size) (32 * 1024))
 
#define HASH_CHUNK_HEADER_SIZE   MAXALIGN(sizeof(HashMemoryChunkData))
 
#define HASH_CHUNK_DATA(hc)   (((char *) (hc)) + HASH_CHUNK_HEADER_SIZE)
 
#define HASH_CHUNK_THRESHOLD   (HASH_CHUNK_SIZE / 4)
 
#define ParallelHashJoinBatchInner(batch)
 
#define ParallelHashJoinBatchOuter(batch, nparticipants)
 
#define EstimateParallelHashJoinBatch(hashtable)
 
#define NthParallelHashJoinBatch(base, n)
 
#define PHJ_BUILD_ELECT   0
 
#define PHJ_BUILD_ALLOCATE   1
 
#define PHJ_BUILD_HASH_INNER   2
 
#define PHJ_BUILD_HASH_OUTER   3
 
#define PHJ_BUILD_RUN   4
 
#define PHJ_BUILD_FREE   5
 
#define PHJ_BATCH_ELECT   0
 
#define PHJ_BATCH_ALLOCATE   1
 
#define PHJ_BATCH_LOAD   2
 
#define PHJ_BATCH_PROBE   3
 
#define PHJ_BATCH_SCAN   4
 
#define PHJ_BATCH_FREE   5
 
#define PHJ_GROW_BATCHES_ELECT   0
 
#define PHJ_GROW_BATCHES_REALLOCATE   1
 
#define PHJ_GROW_BATCHES_REPARTITION   2
 
#define PHJ_GROW_BATCHES_DECIDE   3
 
#define PHJ_GROW_BATCHES_FINISH   4
 
#define PHJ_GROW_BATCHES_PHASE(n)   ((n) % 5) /* circular phases */
 
#define PHJ_GROW_BUCKETS_ELECT   0
 
#define PHJ_GROW_BUCKETS_REALLOCATE   1
 
#define PHJ_GROW_BUCKETS_REINSERT   2
 
#define PHJ_GROW_BUCKETS_PHASE(n)   ((n) % 3) /* circular phases */
 

Typedefs

typedef struct HashJoinTupleData HashJoinTupleData
 
typedef struct HashSkewBucket HashSkewBucket
 
typedef struct HashMemoryChunkData HashMemoryChunkData
 
typedef struct HashMemoryChunkDataHashMemoryChunk
 
typedef struct ParallelHashJoinBatch ParallelHashJoinBatch
 
typedef struct ParallelHashJoinBatchAccessor ParallelHashJoinBatchAccessor
 
typedef enum ParallelHashGrowth ParallelHashGrowth
 
typedef struct ParallelHashJoinState ParallelHashJoinState
 
typedef struct HashJoinTableData HashJoinTableData
 

Enumerations

enum  ParallelHashGrowth { PHJ_GROWTH_OK , PHJ_GROWTH_NEED_MORE_BUCKETS , PHJ_GROWTH_NEED_MORE_BATCHES , PHJ_GROWTH_DISABLED }
 

Macro Definition Documentation

◆ EstimateParallelHashJoinBatch

#define EstimateParallelHashJoinBatch (   hashtable)
Value:
MAXALIGN(sts_estimate((hashtable)->parallel_state->nparticipants)) * 2)
#define MAXALIGN(LEN)
Definition c.h:826
static int fb(int x)
size_t sts_estimate(int participants)

Definition at line 193 of file hashjoin.h.

207{
208 ParallelHashJoinBatch *shared; /* pointer to shared state */
209
210 /* Per-backend partial counters to reduce contention. */
211 size_t preallocated; /* pre-allocated space for this backend */
212 size_t ntuples; /* number of tuples */
213 size_t size; /* size of partition in memory */
214 size_t estimated_size; /* size of partition on disk */
215 size_t old_ntuples; /* how many tuples before repartitioning? */
216 bool at_least_one_chunk; /* has this backend allocated a chunk? */
217 bool outer_eof; /* has this process hit end of batch? */
218 bool done; /* flag to remember that a batch is done */
219 SharedTuplestoreAccessor *inner_tuples;
220 SharedTuplestoreAccessor *outer_tuples;
222
223/*
224 * While hashing the inner relation, any participant might determine that it's
225 * time to increase the number of buckets to reduce the load factor or batches
226 * to reduce the memory size. This is indicated by setting the growth flag to
227 * these values.
228 */
229typedef enum ParallelHashGrowth
230{
231 /* The current dimensions are sufficient. */
233 /* The load factor is too high, so we need to add buckets. */
235 /* The memory budget would be exhausted, so we need to repartition. */
237 /* Repartitioning didn't help last time, so don't try to do that again. */
240
241/*
242 * The shared state used to coordinate a Parallel Hash Join. This is stored
243 * in the DSM segment.
244 */
245typedef struct ParallelHashJoinState
246{
247 dsa_pointer batches; /* array of ParallelHashJoinBatch */
248 dsa_pointer old_batches; /* previous generation during repartition */
249 int nbatch; /* number of batches now */
250 int old_nbatch; /* previous number of batches */
251 int nbuckets; /* number of buckets */
252 ParallelHashGrowth growth; /* control batch/bucket growth */
253 dsa_pointer chunk_work_queue; /* chunk work queue */
254 int nparticipants;
255 size_t space_allowed;
256 size_t total_tuples; /* total number of inner tuples */
257 LWLock lock; /* lock protecting the above */
258
259 Barrier build_barrier; /* synchronization for the build phases */
262 pg_atomic_uint32 distributor; /* counter for load balancing */
263
264 SharedFileSet fileset; /* space for shared temporary files */
266
267/* The phases for building batches, used by build_barrier. */
268#define PHJ_BUILD_ELECT 0
269#define PHJ_BUILD_ALLOCATE 1
270#define PHJ_BUILD_HASH_INNER 2
271#define PHJ_BUILD_HASH_OUTER 3
272#define PHJ_BUILD_RUN 4
273#define PHJ_BUILD_FREE 5
274
275/* The phases for probing each batch, used by for batch_barrier. */
276#define PHJ_BATCH_ELECT 0
277#define PHJ_BATCH_ALLOCATE 1
278#define PHJ_BATCH_LOAD 2
279#define PHJ_BATCH_PROBE 3
280#define PHJ_BATCH_SCAN 4
281#define PHJ_BATCH_FREE 5
282
283/* The phases of batch growth while hashing, for grow_batches_barrier. */
284#define PHJ_GROW_BATCHES_ELECT 0
285#define PHJ_GROW_BATCHES_REALLOCATE 1
286#define PHJ_GROW_BATCHES_REPARTITION 2
287#define PHJ_GROW_BATCHES_DECIDE 3
288#define PHJ_GROW_BATCHES_FINISH 4
289#define PHJ_GROW_BATCHES_PHASE(n) ((n) % 5) /* circular phases */
290
291/* The phases of bucket growth while hashing, for grow_buckets_barrier. */
292#define PHJ_GROW_BUCKETS_ELECT 0
293#define PHJ_GROW_BUCKETS_REALLOCATE 1
294#define PHJ_GROW_BUCKETS_REINSERT 2
295#define PHJ_GROW_BUCKETS_PHASE(n) ((n) % 3) /* circular phases */
296
297typedef struct HashJoinTableData
298{
299 int nbuckets; /* # buckets in the in-memory hash table */
300 int log2_nbuckets; /* its log2 (nbuckets must be a power of 2) */
301
302 int nbuckets_original; /* # buckets when starting the first hash */
303 int nbuckets_optimal; /* optimal # buckets (per batch) */
304 int log2_nbuckets_optimal; /* log2(nbuckets_optimal) */
305
306 /* buckets[i] is head of list of tuples in i'th in-memory bucket */
307 union
308 {
309 /* unshared array is per-batch storage, as are all the tuples */
311 /* shared array is per-query DSA area, as are all the tuples */
313 } buckets;
314
315 bool skewEnabled; /* are we using skew optimization? */
316 HashSkewBucket **skewBucket; /* hashtable of skew buckets */
317 int skewBucketLen; /* size of skewBucket array (a power of 2!) */
318 int nSkewBuckets; /* number of active skew buckets */
319 int *skewBucketNums; /* array indexes of active skew buckets */
320
321 int nbatch; /* number of batches */
322 int curbatch; /* current batch #; 0 during 1st pass */
323
324 int nbatch_original; /* nbatch when we started inner scan */
325 int nbatch_outstart; /* nbatch when we started outer scan */
326
327 bool growEnabled; /* flag to shut off nbatch increases */
328
329 double totalTuples; /* # tuples obtained from inner plan */
330 double partialTuples; /* # tuples obtained from inner plan by me */
331 double skewTuples; /* # tuples inserted into skew tuples */
332
333 /*
334 * These arrays are allocated for the life of the hash join, but only if
335 * nbatch > 1. A file is opened only when we first write a tuple into it
336 * (otherwise its pointer remains NULL). Note that the zero'th array
337 * elements never get used, since we will process rather than dump out any
338 * tuples of batch zero.
339 */
340 BufFile **innerBatchFile; /* buffered virtual temp file per batch */
341 BufFile **outerBatchFile; /* buffered virtual temp file per batch */
342
343 Size spaceUsed; /* memory space currently used by tuples */
344 Size spaceAllowed; /* upper limit for space used */
345 Size spacePeak; /* peak space used */
346 Size spaceUsedSkew; /* skew hash table's current space usage */
347 Size spaceAllowedSkew; /* upper limit for skew hashtable */
348
349 MemoryContext hashCxt; /* context for whole-hash-join storage */
350 MemoryContext batchCxt; /* context for this-batch-only storage */
351 MemoryContext spillCxt; /* context for spilling to temp files */
352
353 /* used for dense allocation of tuples (into linked chunks) */
354 HashMemoryChunk chunks; /* one list for the whole batch */
355
356 /* Shared and private state for Parallel Hash. */
357 HashMemoryChunk current_chunk; /* this backend's current chunk */
358 dsa_area *area; /* DSA area to allocate memory from */
363
364#endif /* HASHJOIN_H */
size_t Size
Definition c.h:619
uint64 dsa_pointer
Definition dsa.h:62
ParallelHashGrowth
Definition hashjoin.h:231
@ PHJ_GROWTH_NEED_MORE_BUCKETS
Definition hashjoin.h:235
@ PHJ_GROWTH_OK
Definition hashjoin.h:233
@ PHJ_GROWTH_NEED_MORE_BATCHES
Definition hashjoin.h:237
@ PHJ_GROWTH_DISABLED
Definition hashjoin.h:239
struct HashJoinTupleData ** unshared
Definition hashjoin.h:311
union HashJoinTableData::@111 buckets
HashMemoryChunk chunks
Definition hashjoin.h:355
ParallelHashJoinBatchAccessor * batches
Definition hashjoin.h:361
MemoryContext hashCxt
Definition hashjoin.h:350
double partialTuples
Definition hashjoin.h:331
ParallelHashJoinState * parallel_state
Definition hashjoin.h:360
MemoryContext spillCxt
Definition hashjoin.h:352
HashMemoryChunk current_chunk
Definition hashjoin.h:358
BufFile ** innerBatchFile
Definition hashjoin.h:341
int log2_nbuckets_optimal
Definition hashjoin.h:305
dsa_pointer_atomic * shared
Definition hashjoin.h:313
dsa_area * area
Definition hashjoin.h:359
BufFile ** outerBatchFile
Definition hashjoin.h:342
dsa_pointer current_chunk_shared
Definition hashjoin.h:362
MemoryContext batchCxt
Definition hashjoin.h:351
HashSkewBucket ** skewBucket
Definition hashjoin.h:317
Barrier grow_batches_barrier
Definition hashjoin.h:261
dsa_pointer old_batches
Definition hashjoin.h:249
dsa_pointer chunk_work_queue
Definition hashjoin.h:254
Barrier grow_buckets_barrier
Definition hashjoin.h:262
ParallelHashGrowth growth
Definition hashjoin.h:253
pg_atomic_uint32 distributor
Definition hashjoin.h:263
SharedFileSet fileset
Definition hashjoin.h:265
dsa_pointer batches
Definition hashjoin.h:248

◆ HASH_CHUNK_DATA

#define HASH_CHUNK_DATA (   hc)    (((char *) (hc)) + HASH_CHUNK_HEADER_SIZE)

Definition at line 152 of file hashjoin.h.

◆ HASH_CHUNK_HEADER_SIZE

#define HASH_CHUNK_HEADER_SIZE   MAXALIGN(sizeof(HashMemoryChunkData))

Definition at line 151 of file hashjoin.h.

◆ HASH_CHUNK_SIZE

#define HASH_CHUNK_SIZE   ((Size) (32 * 1024))

Definition at line 150 of file hashjoin.h.

◆ HASH_CHUNK_THRESHOLD

#define HASH_CHUNK_THRESHOLD   (HASH_CHUNK_SIZE / 4)

Definition at line 154 of file hashjoin.h.

◆ HJTUPLE_MINTUPLE

#define HJTUPLE_MINTUPLE (   hjtup)     ((MinimalTuple) ((char *) (hjtup) + HJTUPLE_OVERHEAD))

Definition at line 91 of file hashjoin.h.

◆ HJTUPLE_OVERHEAD

#define HJTUPLE_OVERHEAD   MAXALIGN(sizeof(HashJoinTupleData))

Definition at line 90 of file hashjoin.h.

◆ INVALID_SKEW_BUCKET_NO

#define INVALID_SKEW_BUCKET_NO   (-1)

Definition at line 120 of file hashjoin.h.

◆ NthParallelHashJoinBatch

#define NthParallelHashJoinBatch (   base,
 
)
Value:
((char *) (base) + \
EstimateParallelHashJoinBatch(hashtable) * (n)))
#define EstimateParallelHashJoinBatch(hashtable)
Definition hashjoin.h:193

Definition at line 198 of file hashjoin.h.

◆ ParallelHashJoinBatchInner

#define ParallelHashJoinBatchInner (   batch)
Value:

Definition at line 182 of file hashjoin.h.

◆ ParallelHashJoinBatchOuter

#define ParallelHashJoinBatchOuter (   batch,
  nparticipants 
)
Value:
MAXALIGN(sts_estimate(nparticipants))))
#define ParallelHashJoinBatchInner(batch)
Definition hashjoin.h:182

Definition at line 187 of file hashjoin.h.

◆ PHJ_BATCH_ALLOCATE

#define PHJ_BATCH_ALLOCATE   1

Definition at line 278 of file hashjoin.h.

◆ PHJ_BATCH_ELECT

#define PHJ_BATCH_ELECT   0

Definition at line 277 of file hashjoin.h.

◆ PHJ_BATCH_FREE

#define PHJ_BATCH_FREE   5

Definition at line 282 of file hashjoin.h.

◆ PHJ_BATCH_LOAD

#define PHJ_BATCH_LOAD   2

Definition at line 279 of file hashjoin.h.

◆ PHJ_BATCH_PROBE

#define PHJ_BATCH_PROBE   3

Definition at line 280 of file hashjoin.h.

◆ PHJ_BATCH_SCAN

#define PHJ_BATCH_SCAN   4

Definition at line 281 of file hashjoin.h.

◆ PHJ_BUILD_ALLOCATE

#define PHJ_BUILD_ALLOCATE   1

Definition at line 270 of file hashjoin.h.

◆ PHJ_BUILD_ELECT

#define PHJ_BUILD_ELECT   0

Definition at line 269 of file hashjoin.h.

◆ PHJ_BUILD_FREE

#define PHJ_BUILD_FREE   5

Definition at line 274 of file hashjoin.h.

◆ PHJ_BUILD_HASH_INNER

#define PHJ_BUILD_HASH_INNER   2

Definition at line 271 of file hashjoin.h.

◆ PHJ_BUILD_HASH_OUTER

#define PHJ_BUILD_HASH_OUTER   3

Definition at line 272 of file hashjoin.h.

◆ PHJ_BUILD_RUN

#define PHJ_BUILD_RUN   4

Definition at line 273 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_DECIDE

#define PHJ_GROW_BATCHES_DECIDE   3

Definition at line 288 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_ELECT

#define PHJ_GROW_BATCHES_ELECT   0

Definition at line 285 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_FINISH

#define PHJ_GROW_BATCHES_FINISH   4

Definition at line 289 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_PHASE

#define PHJ_GROW_BATCHES_PHASE (   n)    ((n) % 5) /* circular phases */

Definition at line 290 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_REALLOCATE

#define PHJ_GROW_BATCHES_REALLOCATE   1

Definition at line 286 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_REPARTITION

#define PHJ_GROW_BATCHES_REPARTITION   2

Definition at line 287 of file hashjoin.h.

◆ PHJ_GROW_BUCKETS_ELECT

#define PHJ_GROW_BUCKETS_ELECT   0

Definition at line 293 of file hashjoin.h.

◆ PHJ_GROW_BUCKETS_PHASE

#define PHJ_GROW_BUCKETS_PHASE (   n)    ((n) % 3) /* circular phases */

Definition at line 296 of file hashjoin.h.

◆ PHJ_GROW_BUCKETS_REALLOCATE

#define PHJ_GROW_BUCKETS_REALLOCATE   1

Definition at line 294 of file hashjoin.h.

◆ PHJ_GROW_BUCKETS_REINSERT

#define PHJ_GROW_BUCKETS_REINSERT   2

Definition at line 295 of file hashjoin.h.

◆ SKEW_BUCKET_OVERHEAD

#define SKEW_BUCKET_OVERHEAD   MAXALIGN(sizeof(HashSkewBucket))

Definition at line 119 of file hashjoin.h.

◆ SKEW_HASH_MEM_PERCENT

#define SKEW_HASH_MEM_PERCENT   2

Definition at line 121 of file hashjoin.h.

◆ SKEW_MIN_OUTER_FRACTION

#define SKEW_MIN_OUTER_FRACTION   0.01

Definition at line 122 of file hashjoin.h.

Typedef Documentation

◆ HashJoinTableData

◆ HashJoinTupleData

◆ HashMemoryChunk

◆ HashMemoryChunkData

◆ HashSkewBucket

◆ ParallelHashGrowth

◆ ParallelHashJoinBatch

◆ ParallelHashJoinBatchAccessor

◆ ParallelHashJoinState

Enumeration Type Documentation

◆ ParallelHashGrowth

Enumerator
PHJ_GROWTH_OK 
PHJ_GROWTH_NEED_MORE_BUCKETS 
PHJ_GROWTH_NEED_MORE_BATCHES 
PHJ_GROWTH_DISABLED 

Definition at line 230 of file hashjoin.h.

231{
232 /* The current dimensions are sufficient. */
234 /* The load factor is too high, so we need to add buckets. */
236 /* The memory budget would be exhausted, so we need to repartition. */
238 /* Repartitioning didn't help last time, so don't try to do that again. */