Re: Small optimization with expanding dynamic hash table

From: Rahila Syed <rahilasyed90(at)gmail(dot)com>
To: cca5507 <cca5507(at)qq(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Small optimization with expanding dynamic hash table
Date: 2025-07-08 06:30:14
Message-ID: CAH2L28sYmXEm7vHqkc0Sdf_P=pqyiPUSyrZHyqTTa8fby0Q8kA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

> If I understand correctly, we only need to check the specific bit
> to determine whether a hash element needs relocation:
>

> diff --git a/src/backend/utils/hash/dynahash.c
> b/src/backend/utils/hash/dynahash.c
> index 1ad155d446e..32fbae71995 100644
> --- a/src/backend/utils/hash/dynahash.c
> +++ b/src/backend/utils/hash/dynahash.c
> @@ -1626,7 +1626,7 @@ expand_table(HTAB *hashp)
> currElement = nextElement)
> {
> nextElement = currElement->link;
> - if ((long) calc_bucket(hctl, currElement->hashvalue) ==
> old_bucket)
> + if (!(currElement->hashvalue & (hctl->low_mask + 1)))
> {
> *oldlink = currElement;
> oldlink = &currElement->link;
>
>
Will this still work if new_bucket is not equal to hctl->low_mask + 1?

The above situation seems possible because we increment new_bucket every
time expand_table is called,
but we only update low_mask when new_bucket exceeds high_mask.

This change has successfully passed the tests on Github CI. According to
[1], the code has decent test coverage,
but I'm not certain if the specific case mentioned above is included in the
tests.

[1] LCOV - PostgreSQL 19devel - src/backend/utils/hash/dynahash.c
<https://coverage.postgresql.org/src/backend/utils/hash/dynahash.c.gcov.html>

Thank you,
Rahila Syed

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Konstantin Knizhnik 2025-07-08 06:36:22 Logical replication prefetch
Previous Message Dilip Kumar 2025-07-08 06:02:12 Re: A recent message added to pg_upgade