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

Commitb81b5a9

Browse files
committed
Unbreak Finalize HashAggregate over Partial HashAggregate.
Commit5dfc198 introduced the useof a new type of hash table with linear reprobing for hash aggregates.Such a hash table behaves very poorly if keys are inserted in hashorder, which does in fact happen in the case where a query use aFinalize HashAggregate node fed (via Gather) by a PartialHashAggregate node. In fact, queries with this type of plan tendto run effectively forever.Fix that by seeding the hash value differently in each worker(and in the leader, if it participates).Andres Freund and Robert Haas
1 parent6a4fe11 commitb81b5a9

File tree

7 files changed

+30
-8
lines changed

7 files changed

+30
-8
lines changed

‎src/backend/executor/execGrouping.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
#include"postgres.h"
2020

21+
#include"access/hash.h"
22+
#include"access/parallel.h"
2123
#include"executor/executor.h"
2224
#include"miscadmin.h"
2325
#include"utils/lsyscache.h"
@@ -289,7 +291,8 @@ BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
289291
FmgrInfo*eqfunctions,
290292
FmgrInfo*hashfunctions,
291293
longnbuckets,Sizeadditionalsize,
292-
MemoryContexttablecxt,MemoryContexttempcxt)
294+
MemoryContexttablecxt,MemoryContexttempcxt,
295+
booluse_variable_hash_iv)
293296
{
294297
TupleHashTablehashtable;
295298
Sizeentrysize=sizeof(TupleHashEntryData)+additionalsize;
@@ -314,6 +317,19 @@ BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
314317
hashtable->in_hash_funcs=NULL;
315318
hashtable->cur_eq_funcs=NULL;
316319

320+
/*
321+
* If parallelism is in use, even if the master backend is performing the
322+
* scan itself, we don't want to create the hashtable exactly the same way
323+
* in all workers. As hashtables are iterated over in keyspace-order,
324+
* doing so in all processes in the same way is likely to lead to
325+
* "unbalanced" hashtables when the table size initially is
326+
* underestimated.
327+
*/
328+
if (use_variable_hash_iv)
329+
hashtable->hash_iv=hash_uint32(ParallelWorkerNumber);
330+
else
331+
hashtable->hash_iv=0;
332+
317333
hashtable->hashtab=tuplehash_create(tablecxt,nbuckets);
318334
hashtable->hashtab->private_data=hashtable;
319335

@@ -450,7 +466,7 @@ TupleHashTableHash(struct tuplehash_hash *tb, const MinimalTuple tuple)
450466
TupleHashTablehashtable= (TupleHashTable)tb->private_data;
451467
intnumCols=hashtable->numCols;
452468
AttrNumber*keyColIdx=hashtable->keyColIdx;
453-
uint32hashkey=0;
469+
uint32hashkey=hashtable->hash_iv;
454470
TupleTableSlot*slot;
455471
FmgrInfo*hashfunctions;
456472
inti;

‎src/backend/executor/nodeAgg.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,8 @@ build_hash_table(AggState *aggstate)
17231723
node->numGroups,
17241724
additionalsize,
17251725
aggstate->aggcontexts[0]->ecxt_per_tuple_memory,
1726-
tmpmem);
1726+
tmpmem,
1727+
!DO_AGGSPLIT_SKIPFINAL(aggstate->aggsplit));
17271728
}
17281729

17291730
/*

‎src/backend/executor/nodeRecursiveunion.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ build_hash_table(RecursiveUnionState *rustate)
4343
node->numGroups,
4444
0,
4545
rustate->tableContext,
46-
rustate->tempContext);
46+
rustate->tempContext,
47+
false);
4748
}
4849

4950

‎src/backend/executor/nodeSetOp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ build_hash_table(SetOpState *setopstate)
130130
node->numGroups,
131131
0,
132132
setopstate->tableContext,
133-
setopstate->tempContext);
133+
setopstate->tempContext,
134+
false);
134135
}
135136

136137
/*

‎src/backend/executor/nodeSubplan.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ buildSubPlanHash(SubPlanState *node, ExprContext *econtext)
510510
nbuckets,
511511
0,
512512
node->hashtablecxt,
513-
node->hashtempcxt);
513+
node->hashtempcxt,
514+
false);
514515

515516
if (!subplan->unknownEqFalse)
516517
{
@@ -529,7 +530,8 @@ buildSubPlanHash(SubPlanState *node, ExprContext *econtext)
529530
nbuckets,
530531
0,
531532
node->hashtablecxt,
532-
node->hashtempcxt);
533+
node->hashtempcxt,
534+
false);
533535
}
534536

535537
/*

‎src/include/executor/executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ extern TupleHashTable BuildTupleHashTable(int numCols, AttrNumber *keyColIdx,
143143
FmgrInfo*hashfunctions,
144144
longnbuckets,Sizeadditionalsize,
145145
MemoryContexttablecxt,
146-
MemoryContexttempcxt);
146+
MemoryContexttempcxt,booluse_variable_hash_iv);
147147
externTupleHashEntryLookupTupleHashEntry(TupleHashTablehashtable,
148148
TupleTableSlot*slot,
149149
bool*isnew);

‎src/include/nodes/execnodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ typedef struct TupleHashTableData
533533
TupleTableSlot*inputslot;/* current input tuple's slot */
534534
FmgrInfo*in_hash_funcs;/* hash functions for input datatype(s) */
535535
FmgrInfo*cur_eq_funcs;/* equality functions for input vs. table */
536+
uint32hash_iv;/* hash-function IV */
536537
}TupleHashTableData;
537538

538539
typedeftuplehash_iteratorTupleHashIterator;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp