Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit317ffdf

Browse files
committed
Allow to reset execGrouping.c style tuple hashtables.
This has the advantage that the comparator expression, the table'sslot, etc do not have to be rebuilt. Additionally the simplehash.hhashtable within the tuple hashtable now keeps its previous size anddoesn't need to be reallocated. That both reduces allocator overhead,and improves performance in cases where the input estimation was offby a significant factor.To avoid an API/ABI break, the new parameter is exposed via the newBuildTupleHashTableExt(), and BuildTupleHashTable() now is a wrapperaround the former, that continues to allocate the table itself in thetablecxt.Using this fixes performance issues discovered in the two bugsreferenced. This commit however has not converted the callers, that'sdone in a separate commit.Bug: #15592 #15486Reported-By: Jakub Janeček, Dmitry MarakasovAuthor: Andres FreundDiscussion:https://postgr.es/m/15486-05850f065da42931@postgresql.orghttps://postgr.es/m/20190114180423.ywhdg2iagzvh43we@alap3.anarazel.deBackpatch: 11, this is a prerequisite for other fixes
1 parent3b632a5 commit317ffdf

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed

‎src/backend/executor/execGrouping.c

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ execTuplesHashPrepare(int numCols,
139139
*hashfunctions: datatype-specific hashing functions to use
140140
*nbuckets: initial estimate of hashtable size
141141
*additionalsize: size of data stored in ->additional
142-
*tablecxt: memory context in which to store table and table entries
142+
*metacxt: memory context for long-lived allocation, but not per-entry data
143+
*tablecxt: memory context in which to store table entries
143144
*tempcxt: short-lived context for evaluation hash and comparison functions
144145
*
145146
* The function arrays may be made with execTuplesHashPrepare(). Note they
@@ -150,14 +151,16 @@ execTuplesHashPrepare(int numCols,
150151
* storage that will live as long as the hashtable does.
151152
*/
152153
TupleHashTable
153-
BuildTupleHashTable(PlanState*parent,
154-
TupleDescinputDesc,
155-
intnumCols,AttrNumber*keyColIdx,
156-
constOid*eqfuncoids,
157-
FmgrInfo*hashfunctions,
158-
longnbuckets,Sizeadditionalsize,
159-
MemoryContexttablecxt,MemoryContexttempcxt,
160-
booluse_variable_hash_iv)
154+
BuildTupleHashTableExt(PlanState*parent,
155+
TupleDescinputDesc,
156+
intnumCols,AttrNumber*keyColIdx,
157+
constOid*eqfuncoids,
158+
FmgrInfo*hashfunctions,
159+
longnbuckets,Sizeadditionalsize,
160+
MemoryContextmetacxt,
161+
MemoryContexttablecxt,
162+
MemoryContexttempcxt,
163+
booluse_variable_hash_iv)
161164
{
162165
TupleHashTablehashtable;
163166
Sizeentrysize=sizeof(TupleHashEntryData)+additionalsize;
@@ -168,8 +171,9 @@ BuildTupleHashTable(PlanState *parent,
168171
/* Limit initial table size request to not more than work_mem */
169172
nbuckets=Min(nbuckets, (long) ((work_mem*1024L) /entrysize));
170173

171-
hashtable= (TupleHashTable)
172-
MemoryContextAlloc(tablecxt,sizeof(TupleHashTableData));
174+
oldcontext=MemoryContextSwitchTo(metacxt);
175+
176+
hashtable= (TupleHashTable)palloc(sizeof(TupleHashTableData));
173177

174178
hashtable->numCols=numCols;
175179
hashtable->keyColIdx=keyColIdx;
@@ -195,9 +199,7 @@ BuildTupleHashTable(PlanState *parent,
195199
else
196200
hashtable->hash_iv=0;
197201

198-
hashtable->hashtab=tuplehash_create(tablecxt,nbuckets,hashtable);
199-
200-
oldcontext=MemoryContextSwitchTo(hashtable->tablecxt);
202+
hashtable->hashtab=tuplehash_create(metacxt,nbuckets,hashtable);
201203

202204
/*
203205
* We copy the input tuple descriptor just for safety --- we assume all
@@ -227,6 +229,46 @@ BuildTupleHashTable(PlanState *parent,
227229
returnhashtable;
228230
}
229231

232+
/*
233+
* BuildTupleHashTable is a backwards-compatibilty wrapper for
234+
* BuildTupleHashTableExt(), that allocates the hashtable's metadata in
235+
* tablecxt. Note that hashtables created this way cannot be reset leak-free
236+
* with ResetTupleHashTable().
237+
*/
238+
TupleHashTable
239+
BuildTupleHashTable(PlanState*parent,
240+
TupleDescinputDesc,
241+
intnumCols,AttrNumber*keyColIdx,
242+
constOid*eqfuncoids,
243+
FmgrInfo*hashfunctions,
244+
longnbuckets,Sizeadditionalsize,
245+
MemoryContexttablecxt,
246+
MemoryContexttempcxt,
247+
booluse_variable_hash_iv)
248+
{
249+
returnBuildTupleHashTableExt(parent,
250+
inputDesc,
251+
numCols,keyColIdx,
252+
eqfuncoids,
253+
hashfunctions,
254+
nbuckets,additionalsize,
255+
tablecxt,
256+
tablecxt,
257+
tempcxt,
258+
use_variable_hash_iv);
259+
}
260+
261+
/*
262+
* Reset contents of the hashtable to be empty, preserving all the non-content
263+
* state. Note that the tablecxt passed to BuildTupleHashTableExt() should
264+
* also be reset, otherwise there will be leaks.
265+
*/
266+
void
267+
ResetTupleHashTable(TupleHashTablehashtable)
268+
{
269+
tuplehash_reset(hashtable->hashtab);
270+
}
271+
230272
/*
231273
* Find or create a hashtable entry for the tuple group containing the
232274
* given tuple. The tuple must be the same type as the hashtable entries.

‎src/include/executor/executor.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,23 @@ extern TupleHashTable BuildTupleHashTable(PlanState *parent,
124124
longnbuckets,Sizeadditionalsize,
125125
MemoryContexttablecxt,
126126
MemoryContexttempcxt,booluse_variable_hash_iv);
127+
externTupleHashTableBuildTupleHashTableExt(PlanState*parent,
128+
TupleDescinputDesc,
129+
intnumCols,AttrNumber*keyColIdx,
130+
constOid*eqfuncoids,
131+
FmgrInfo*hashfunctions,
132+
longnbuckets,Sizeadditionalsize,
133+
MemoryContextmetacxt,
134+
MemoryContexttablecxt,
135+
MemoryContexttempcxt,booluse_variable_hash_iv);
127136
externTupleHashEntryLookupTupleHashEntry(TupleHashTablehashtable,
128137
TupleTableSlot*slot,
129138
bool*isnew);
130139
externTupleHashEntryFindTupleHashEntry(TupleHashTablehashtable,
131140
TupleTableSlot*slot,
132141
ExprState*eqcomp,
133142
FmgrInfo*hashfunctions);
143+
externvoidResetTupleHashTable(TupleHashTablehashtable);
134144

135145
/*
136146
* prototypes from functions in execJunk.c

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp