1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* hashjoin.h
|
1999-05-18 23:33:06 +02:00
|
|
|
* internal structures for hash joins
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2006-07-13 20:01:02 +02:00
|
|
|
* $PostgreSQL: pgsql/src/include/executor/hashjoin.h,v 1.41 2006/07/13 18:01:02 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifndef HASHJOIN_H
|
1996-07-09 08:22:35 +02:00
|
|
|
#define HASHJOIN_H
|
|
|
|
|
2006-07-13 19:47:02 +02:00
|
|
|
#include "fmgr.h"
|
1999-10-13 17:02:32 +02:00
|
|
|
#include "storage/buffile.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-05-18 23:33:06 +02:00
|
|
|
/* ----------------------------------------------------------------
|
|
|
|
* hash-join hash table structures
|
|
|
|
*
|
2005-03-06 23:15:05 +01:00
|
|
|
* Each active hashjoin has a HashJoinTable control block, which is
|
2000-07-12 04:37:39 +02:00
|
|
|
* palloc'd in the executor's per-query context. All other storage needed
|
|
|
|
* for the hashjoin is kept in private memory contexts, two for each hashjoin.
|
1999-05-18 23:33:06 +02:00
|
|
|
* This makes it easy and fast to release the storage when we don't need it
|
2005-03-06 23:15:05 +01:00
|
|
|
* anymore. (Exception: data associated with the temp files lives in the
|
|
|
|
* per-query context too, since we always call buffile.c in that context.)
|
1999-05-18 23:33:06 +02:00
|
|
|
*
|
2000-08-22 06:06:22 +02:00
|
|
|
* The hashtable contexts are made children of the per-query context, ensuring
|
2000-06-28 05:33:33 +02:00
|
|
|
* that they will be discarded at end of statement even if the join is
|
1999-05-18 23:33:06 +02:00
|
|
|
* aborted early by an error. (Likewise, any temporary files we make will
|
|
|
|
* be cleaned up by the virtual file manager in event of an error.)
|
1999-05-06 02:30:47 +02:00
|
|
|
*
|
1999-05-18 23:33:06 +02:00
|
|
|
* Storage that should live through the entire join is allocated from the
|
2000-06-28 05:33:33 +02:00
|
|
|
* "hashCxt", while storage that is only wanted for the current batch is
|
|
|
|
* allocated in the "batchCxt". By resetting the batchCxt at the end of
|
|
|
|
* each batch, we free all the per-batch storage reliably and without tedium.
|
2005-03-06 23:15:05 +01:00
|
|
|
*
|
|
|
|
* During first scan of inner relation, we get its tuples from executor.
|
|
|
|
* If nbatch > 1 then tuples that don't belong in first batch get saved
|
|
|
|
* into inner-batch temp files. The same statements apply for the
|
|
|
|
* first scan of the outer relation, except we write tuples to outer-batch
|
2005-10-15 04:49:52 +02:00
|
|
|
* temp files. After finishing the first scan, we do the following for
|
2005-03-06 23:15:05 +01:00
|
|
|
* each remaining batch:
|
|
|
|
* 1. Read tuples from inner batch file, load into hash buckets.
|
|
|
|
* 2. Read tuples from outer batch file, match to hash buckets and output.
|
|
|
|
*
|
|
|
|
* It is possible to increase nbatch on the fly if the in-memory hash table
|
|
|
|
* gets too big. The hash-value-to-batch computation is arranged so that this
|
|
|
|
* can only cause a tuple to go into a later batch than previously thought,
|
|
|
|
* never into an earlier batch. When we increase nbatch, we rescan the hash
|
|
|
|
* table and dump out any tuples that are now of a later batch to the correct
|
|
|
|
* inner batch file. Subsequently, while reading either inner or outer batch
|
|
|
|
* files, we might find tuples that no longer belong to the current batch;
|
|
|
|
* if so, we just dump them out to the correct batch file.
|
1999-05-18 23:33:06 +02:00
|
|
|
* ----------------------------------------------------------------
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
|
|
|
|
2005-03-06 23:15:05 +01:00
|
|
|
/* these are in nodes/execnodes.h: */
|
|
|
|
/* typedef struct HashJoinTupleData *HashJoinTuple; */
|
|
|
|
/* typedef struct HashJoinTableData *HashJoinTable; */
|
|
|
|
|
1999-05-18 23:33:06 +02:00
|
|
|
typedef struct HashJoinTupleData
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
struct HashJoinTupleData *next; /* link to next tuple in same bucket */
|
2005-03-06 23:15:05 +01:00
|
|
|
uint32 hashvalue; /* tuple's hash code */
|
2006-06-27 23:31:20 +02:00
|
|
|
/* Tuple data, in MinimalTuple format, follows on a MAXALIGN boundary */
|
1999-05-26 00:43:53 +02:00
|
|
|
} HashJoinTupleData;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2006-06-27 23:31:20 +02:00
|
|
|
#define HJTUPLE_OVERHEAD MAXALIGN(sizeof(HashJoinTupleData))
|
|
|
|
#define HJTUPLE_MINTUPLE(hjtup) \
|
|
|
|
((MinimalTuple) ((char *) (hjtup) + HJTUPLE_OVERHEAD))
|
|
|
|
|
|
|
|
|
2004-09-22 21:13:52 +02:00
|
|
|
typedef struct HashJoinTableData
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2005-03-06 23:15:05 +01:00
|
|
|
int nbuckets; /* # buckets in the in-memory hash table */
|
|
|
|
/* buckets[i] is head of list of tuples in i'th in-memory bucket */
|
|
|
|
struct HashJoinTupleData **buckets;
|
1999-05-18 23:33:06 +02:00
|
|
|
/* buckets array is per-batch storage, as are all the tuples */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-03-06 23:15:05 +01:00
|
|
|
int nbatch; /* number of batches */
|
|
|
|
int curbatch; /* current batch #; 0 during 1st pass */
|
|
|
|
|
|
|
|
int nbatch_original; /* nbatch when we started inner scan */
|
|
|
|
int nbatch_outstart; /* nbatch when we started outer scan */
|
|
|
|
|
|
|
|
bool growEnabled; /* flag to shut off nbatch increases */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2005-04-16 22:07:35 +02:00
|
|
|
double totalTuples; /* # tuples obtained from inner plan */
|
2004-09-22 21:13:52 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* These arrays are allocated for the life of the hash join, but only if
|
|
|
|
* nbatch > 1. A file is opened only when we first write a tuple into it
|
|
|
|
* (otherwise its pointer remains NULL). Note that the zero'th array
|
|
|
|
* elements never get used, since we will process rather than dump out any
|
|
|
|
* tuples of batch zero.
|
1999-05-18 23:33:06 +02:00
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
BufFile **innerBatchFile; /* buffered virtual temp file per batch */
|
|
|
|
BufFile **outerBatchFile; /* buffered virtual temp file per batch */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-07-12 04:37:39 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Info about the datatype-specific hash functions for the datatypes being
|
|
|
|
* hashed. We assume that the inner and outer sides of each hashclause
|
|
|
|
* are the same type, or at least share the same hash function. This is an
|
|
|
|
* array of the same length as the number of hash keys.
|
2000-07-12 04:37:39 +02:00
|
|
|
*/
|
2003-06-23 00:04:55 +02:00
|
|
|
FmgrInfo *hashfunctions; /* lookup data for hash functions */
|
2000-07-12 04:37:39 +02:00
|
|
|
|
2005-03-06 23:15:05 +01:00
|
|
|
Size spaceUsed; /* memory space currently used by tuples */
|
|
|
|
Size spaceAllowed; /* upper limit for space used */
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
MemoryContext hashCxt; /* context for whole-hash-join storage */
|
|
|
|
MemoryContext batchCxt; /* context for this-batch-only storage */
|
2004-09-22 21:13:52 +02:00
|
|
|
} HashJoinTableData;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* HASHJOIN_H */
|