Re: Adjusting hash join memory limit to handle batch explosion

From: Tomas Vondra <tomas(at)vondra(dot)me>
To: Melanie Plageman <melanieplageman(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Adjusting hash join memory limit to handle batch explosion
Date: 2025-02-06 18:48:10
Message-ID: 04cc34f5-47ae-4ed7-b7b6-c6ba8bf2f8ae@vondra.me
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Here's a slightly simplified version of the "balancing" patch. I decided
to stop increasing the nbucket value at runtime, even if the hashtable
grows larger than the memory limit (which is what we used to calculate
the initial nbucket value in ExecChooseHashTableSize).

Without the nbucket changes the patch does not need to change how
ExecHashGetBucketAndBatch() calculates bucketno/batchno, which added
quite a bit of complexity (the patch from a couple days ago simply
inverted bits in the hash when calculating batchno, but I'm sure there
are other ways to do this).

This doesn't mean we can't adjust nbucket at all - we just can't do that
at runtime, after we started building the hash. We still can adjust
nbucket in ExecChooseHashTableSize(), if we know we'll need many batches
at that point.

FWIW this is not a new issue, introduced by this patch. We can already
have issues with nbucket being too low if we disable growth of the hash
table early, in which case it can get almost arbitrarily large. And we
don't resize nbucket in that case either. Granted, we usually disable
growth in cases when there are duplicate values/hashes, so increasing
nbucket would not really do much (the tuples would still go to the same
bucket). But there are ways to disable disable growth early.

The 0002 patch helps with those cases a bit by retrying the nbatch
growth after a while, instead of disabling it forever.

0003 is just a WIP patch with a couple reproducers for the issues.

regards

--
Tomas Vondra

Attachment Content-Type Size
v20250206-0003-hashjoin-patch-tests.patch text/x-patch 691.9 KB
v20250206-0002-Postpone-hashtable-growth-instead-of-disab.patch text/x-patch 5.7 KB
v20250206-0001-Balance-memory-usage-with-hashjoin-batch-e.patch text/x-patch 11.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Sergei Kornilov 2025-02-06 19:00:17 outdated comment in table_tuple_update definition
Previous Message Masahiko Sawada 2025-02-06 18:41:20 Re: Fix assert failure when decoding XLOG_PARAMETER_CHANGE on primary