@ -139,7 +139,8 @@ execTuplesHashPrepare(int numCols,
* hashfunctions : datatype - specific hashing functions to use
* hashfunctions : datatype - specific hashing functions to use
* nbuckets : initial estimate of hashtable size
* nbuckets : initial estimate of hashtable size
* additionalsize : size of data stored in - > additional
* additionalsize : size of data stored in - > additional
* tablecxt : memory context in which to store table and table entries
* metacxt : memory context for long - lived allocation , but not per - entry data
* tablecxt : memory context in which to store table entries
* tempcxt : short - lived context for evaluation hash and comparison functions
* tempcxt : short - lived context for evaluation hash and comparison functions
*
*
* The function arrays may be made with execTuplesHashPrepare ( ) . Note they
* The function arrays may be made with execTuplesHashPrepare ( ) . Note they
@ -150,14 +151,16 @@ execTuplesHashPrepare(int numCols,
* storage that will live as long as the hashtable does .
* storage that will live as long as the hashtable does .
*/
*/
TupleHashTable
TupleHashTable
BuildTupleHashTable ( PlanState * parent ,
BuildTupleHashTableExt ( PlanState * parent ,
TupleDesc inputDesc ,
TupleDesc inputDesc ,
int numCols , AttrNumber * keyColIdx ,
int numCols , AttrNumber * keyColIdx ,
const Oid * eqfuncoids ,
const Oid * eqfuncoids ,
FmgrInfo * hashfunctions ,
FmgrInfo * hashfunctions ,
long nbuckets , Size additionalsize ,
long nbuckets , Size additionalsize ,
MemoryContext tablecxt , MemoryContext tempcxt ,
MemoryContext metacxt ,
bool use_variable_hash_iv )
MemoryContext tablecxt ,
MemoryContext tempcxt ,
bool use_variable_hash_iv )
{
{
TupleHashTable hashtable ;
TupleHashTable hashtable ;
Size entrysize = sizeof ( TupleHashEntryData ) + additionalsize ;
Size entrysize = sizeof ( TupleHashEntryData ) + additionalsize ;
@ -168,8 +171,9 @@ BuildTupleHashTable(PlanState *parent,
/* Limit initial table size request to not more than work_mem */
/* Limit initial table size request to not more than work_mem */
nbuckets = Min ( nbuckets , ( long ) ( ( work_mem * 1024L ) / entrysize ) ) ;
nbuckets = Min ( nbuckets , ( long ) ( ( work_mem * 1024L ) / entrysize ) ) ;
hashtable = ( TupleHashTable )
oldcontext = MemoryContextSwitchTo ( metacxt ) ;
MemoryContextAlloc ( tablecxt , sizeof ( TupleHashTableData ) ) ;
hashtable = ( TupleHashTable ) palloc ( sizeof ( TupleHashTableData ) ) ;
hashtable - > numCols = numCols ;
hashtable - > numCols = numCols ;
hashtable - > keyColIdx = keyColIdx ;
hashtable - > keyColIdx = keyColIdx ;
@ -195,9 +199,7 @@ BuildTupleHashTable(PlanState *parent,
else
else
hashtable - > hash_iv = 0 ;
hashtable - > hash_iv = 0 ;
hashtable - > hashtab = tuplehash_create ( tablecxt , nbuckets , hashtable ) ;
hashtable - > hashtab = tuplehash_create ( metacxt , nbuckets , hashtable ) ;
oldcontext = MemoryContextSwitchTo ( hashtable - > tablecxt ) ;
/*
/*
* We copy the input tuple descriptor just for safety - - - we assume all
* We copy the input tuple descriptor just for safety - - - we assume all
@ -227,6 +229,46 @@ BuildTupleHashTable(PlanState *parent,
return hashtable ;
return hashtable ;
}
}
/*
* BuildTupleHashTable is a backwards - compatibilty wrapper for
* BuildTupleHashTableExt ( ) , that allocates the hashtable ' s metadata in
* tablecxt . Note that hashtables created this way cannot be reset leak - free
* with ResetTupleHashTable ( ) .
*/
TupleHashTable
BuildTupleHashTable ( PlanState * parent ,
TupleDesc inputDesc ,
int numCols , AttrNumber * keyColIdx ,
const Oid * eqfuncoids ,
FmgrInfo * hashfunctions ,
long nbuckets , Size additionalsize ,
MemoryContext tablecxt ,
MemoryContext tempcxt ,
bool use_variable_hash_iv )
{
return BuildTupleHashTableExt ( parent ,
inputDesc ,
numCols , keyColIdx ,
eqfuncoids ,
hashfunctions ,
nbuckets , additionalsize ,
tablecxt ,
tablecxt ,
tempcxt ,
use_variable_hash_iv ) ;
}
/*
* Reset contents of the hashtable to be empty , preserving all the non - content
* state . Note that the tablecxt passed to BuildTupleHashTableExt ( ) should
* also be reset , otherwise there will be leaks .
*/
void
ResetTupleHashTable ( TupleHashTable hashtable )
{
tuplehash_reset ( hashtable - > hashtab ) ;
}
/*
/*
* Find or create a hashtable entry for the tuple group containing the
* Find or create a hashtable entry for the tuple group containing the
* given tuple . The tuple must be the same type as the hashtable entries .
* given tuple . The tuple must be the same type as the hashtable entries .