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 "utils/dsa.h"
#include "utils/sharedtuplestore.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:898
static int fb(int x)
size_t sts_estimate(int participants)

Definition at line 204 of file hashjoin.h.

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

◆ HASH_CHUNK_DATA

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

Definition at line 163 of file hashjoin.h.

◆ HASH_CHUNK_HEADER_SIZE

#define HASH_CHUNK_HEADER_SIZE   MAXALIGN(sizeof(HashMemoryChunkData))

Definition at line 162 of file hashjoin.h.

◆ HASH_CHUNK_SIZE

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

Definition at line 161 of file hashjoin.h.

◆ HASH_CHUNK_THRESHOLD

#define HASH_CHUNK_THRESHOLD   (HASH_CHUNK_SIZE / 4)

Definition at line 165 of file hashjoin.h.

◆ HJTUPLE_MINTUPLE

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

Definition at line 102 of file hashjoin.h.

◆ HJTUPLE_OVERHEAD

#define HJTUPLE_OVERHEAD   MAXALIGN(sizeof(HashJoinTupleData))

Definition at line 101 of file hashjoin.h.

◆ INVALID_SKEW_BUCKET_NO

#define INVALID_SKEW_BUCKET_NO   (-1)

Definition at line 131 of file hashjoin.h.

◆ NthParallelHashJoinBatch

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

Definition at line 209 of file hashjoin.h.

◆ ParallelHashJoinBatchInner

#define ParallelHashJoinBatchInner (   batch)
Value:

Definition at line 193 of file hashjoin.h.

◆ ParallelHashJoinBatchOuter

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

Definition at line 198 of file hashjoin.h.

◆ PHJ_BATCH_ALLOCATE

#define PHJ_BATCH_ALLOCATE   1

Definition at line 289 of file hashjoin.h.

◆ PHJ_BATCH_ELECT

#define PHJ_BATCH_ELECT   0

Definition at line 288 of file hashjoin.h.

◆ PHJ_BATCH_FREE

#define PHJ_BATCH_FREE   5

Definition at line 293 of file hashjoin.h.

◆ PHJ_BATCH_LOAD

#define PHJ_BATCH_LOAD   2

Definition at line 290 of file hashjoin.h.

◆ PHJ_BATCH_PROBE

#define PHJ_BATCH_PROBE   3

Definition at line 291 of file hashjoin.h.

◆ PHJ_BATCH_SCAN

#define PHJ_BATCH_SCAN   4

Definition at line 292 of file hashjoin.h.

◆ PHJ_BUILD_ALLOCATE

#define PHJ_BUILD_ALLOCATE   1

Definition at line 281 of file hashjoin.h.

◆ PHJ_BUILD_ELECT

#define PHJ_BUILD_ELECT   0

Definition at line 280 of file hashjoin.h.

◆ PHJ_BUILD_FREE

#define PHJ_BUILD_FREE   5

Definition at line 285 of file hashjoin.h.

◆ PHJ_BUILD_HASH_INNER

#define PHJ_BUILD_HASH_INNER   2

Definition at line 282 of file hashjoin.h.

◆ PHJ_BUILD_HASH_OUTER

#define PHJ_BUILD_HASH_OUTER   3

Definition at line 283 of file hashjoin.h.

◆ PHJ_BUILD_RUN

#define PHJ_BUILD_RUN   4

Definition at line 284 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_DECIDE

#define PHJ_GROW_BATCHES_DECIDE   3

Definition at line 299 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_ELECT

#define PHJ_GROW_BATCHES_ELECT   0

Definition at line 296 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_FINISH

#define PHJ_GROW_BATCHES_FINISH   4

Definition at line 300 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_PHASE

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

Definition at line 301 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_REALLOCATE

#define PHJ_GROW_BATCHES_REALLOCATE   1

Definition at line 297 of file hashjoin.h.

◆ PHJ_GROW_BATCHES_REPARTITION

#define PHJ_GROW_BATCHES_REPARTITION   2

Definition at line 298 of file hashjoin.h.

◆ PHJ_GROW_BUCKETS_ELECT

#define PHJ_GROW_BUCKETS_ELECT   0

Definition at line 304 of file hashjoin.h.

◆ PHJ_GROW_BUCKETS_PHASE

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

Definition at line 307 of file hashjoin.h.

◆ PHJ_GROW_BUCKETS_REALLOCATE

#define PHJ_GROW_BUCKETS_REALLOCATE   1

Definition at line 305 of file hashjoin.h.

◆ PHJ_GROW_BUCKETS_REINSERT

#define PHJ_GROW_BUCKETS_REINSERT   2

Definition at line 306 of file hashjoin.h.

◆ SKEW_BUCKET_OVERHEAD

#define SKEW_BUCKET_OVERHEAD   MAXALIGN(sizeof(HashSkewBucket))

Definition at line 130 of file hashjoin.h.

◆ SKEW_HASH_MEM_PERCENT

#define SKEW_HASH_MEM_PERCENT   2

Definition at line 132 of file hashjoin.h.

◆ SKEW_MIN_OUTER_FRACTION

#define SKEW_MIN_OUTER_FRACTION   0.01

Definition at line 133 of file hashjoin.h.

Typedef Documentation

◆ HashJoinTableData

◆ HashJoinTupleData

◆ HashMemoryChunk

Definition at line 159 of file hashjoin.h.

◆ 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 241 of file hashjoin.h.

242{
243 /* The current dimensions are sufficient. */
245 /* The load factor is too high, so we need to add buckets. */
247 /* The memory budget would be exhausted, so we need to repartition. */
249 /* Repartitioning didn't help last time, so don't try to do that again. */