mirror of
https://github.com/postgres/postgres.git
synced 2025-05-17 06:41:24 +03:00
Fix possible "invalid memory alloc request size" failure in nodeHash.c.
Limit the size of the hashtable pointer array to not more than MaxAllocSize. We've seen reports of failures due to this in HEAD/9.5, and it seems possible in older branches as well. The change in NTUP_PER_BUCKET in 9.5 may have made the problem more likely, but surely it didn't introduce it. Tomas Vondra, slightly modified by me
This commit is contained in:
parent
0f6a046b67
commit
45dd7cdbab
@ -460,10 +460,12 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
|
|||||||
* Set nbuckets to achieve an average bucket load of NTUP_PER_BUCKET when
|
* Set nbuckets to achieve an average bucket load of NTUP_PER_BUCKET when
|
||||||
* memory is filled. Set nbatch to the smallest power of 2 that appears
|
* memory is filled. Set nbatch to the smallest power of 2 that appears
|
||||||
* sufficient. The Min() steps limit the results so that the pointer
|
* sufficient. The Min() steps limit the results so that the pointer
|
||||||
* arrays we'll try to allocate do not exceed work_mem.
|
* arrays we'll try to allocate do not exceed work_mem nor MaxAllocSize.
|
||||||
*/
|
*/
|
||||||
max_pointers = (work_mem * 1024L) / sizeof(void *);
|
max_pointers = (work_mem * 1024L) / sizeof(HashJoinTuple);
|
||||||
|
max_pointers = Min(max_pointers, MaxAllocSize / sizeof(HashJoinTuple));
|
||||||
/* also ensure we avoid integer overflow in nbatch and nbuckets */
|
/* also ensure we avoid integer overflow in nbatch and nbuckets */
|
||||||
|
/* (this step is redundant given the current value of MaxAllocSize) */
|
||||||
max_pointers = Min(max_pointers, INT_MAX / 2);
|
max_pointers = Min(max_pointers, INT_MAX / 2);
|
||||||
|
|
||||||
if (inner_rel_bytes > hash_table_bytes)
|
if (inner_rel_bytes > hash_table_bytes)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user