From 07aa0c99c95b66d960bad28fb60288a0d5946215 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 30 Dec 2003 20:05:15 +0000 Subject: [PATCH] Avoid running out of memory during hash_create, by not passing a number-of-buckets that exceeds the size we actually plan to allow the hash table to grow to. Per trouble report from Sean Shanny. --- src/backend/executor/nodeIndexscan.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/backend/executor/nodeIndexscan.c b/src/backend/executor/nodeIndexscan.c index e584000c14..a440cdf55f 100644 --- a/src/backend/executor/nodeIndexscan.c +++ b/src/backend/executor/nodeIndexscan.c @@ -1019,22 +1019,28 @@ static void create_duphash(IndexScanState *node) { HASHCTL hash_ctl; + long nbuckets; + node->iss_MaxHash = (SortMem * 1024L) / + (MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(sizeof(DupHashTabEntry))); MemSet(&hash_ctl, 0, sizeof(hash_ctl)); hash_ctl.keysize = SizeOfIptrData; hash_ctl.entrysize = sizeof(DupHashTabEntry); hash_ctl.hash = tag_hash; hash_ctl.hcxt = CurrentMemoryContext; + nbuckets = (long) ceil(node->ss.ps.plan->plan_rows); + if (nbuckets < 1) + nbuckets = 1; + if (nbuckets > node->iss_MaxHash) + nbuckets = node->iss_MaxHash; node->iss_DupHash = hash_create("DupHashTable", - (long) ceil(node->ss.ps.plan->plan_rows), + nbuckets, &hash_ctl, HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT); if (node->iss_DupHash == NULL) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); - node->iss_MaxHash = (SortMem * 1024L) / - (MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(sizeof(DupHashTabEntry))); } int -- 2.39.5