pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Davis <pgsql@j-davis.com>
To: Andres Freund <andres@anarazel.de>
Cc: pgsql-bugs@postgresql.org
Subject: Re: HashAgg degenerate case
Date: Fri, 08 Nov 2024 12:40:39 -0800
Message-ID: <8c60e698428962f33fdfbfab2206eedba09029fc.camel@j-davis.com> (raw)
In-Reply-To: <33bcc1ef6c4acf80f4fb87c4d9d284c61abb3652.camel@j-davis.com>
References: <a050b6067334cf513bf34f9c7bc925d651160f3a.camel@j-davis.com>
	<4tbbgdqqxvmy37fk75p36azkovyhrjhnul46lntj52jlobphf3@nxaqlaqupg2l>
	<33bcc1ef6c4acf80f4fb87c4d9d284c61abb3652.camel@j-davis.com>

On Fri, 2024-11-08 at 10:48 -0800, Jeff Davis wrote:
> I can think of two approaches to solve it:

Another thought: the point of spilling is to avoid using additional
memory by adding a group.

If the bucket array is 89.8% full, adding a new group doesn't require
new buckets need to be allocated, so we only have the firstTuple and
pergroup data to worry about. If that causes the memory limit to be
exceeded, it's the perfect time to switch to spill mode.

But if the bucket array is 89.9% full, then adding a new group will
cause the bucket array to double. If that causes the memory limit to be
exceeded, then we can switch to spill mode, but it's wasteful to do so
because (a) we won't be using most of those new buckets; (b) the new
buckets will crowd out space for subsequent batches and even fewer of
the buckets will be used; and (c) the memory accounting can be off by
quite a bit.

What if we have a check where, if the metacxt is using more than 40% of
the memory, and if adding a new group would reach the grow_threshold,
then enter spill mode immediately? To make this work, I think we either
need to use a tuplehash_lookup() followed by a tuplehash_insert() (two
lookups for each new group), or we would need a new API into simplehash
like tuplehash_insert_without_growing() that would return NULL instead
of growing. This approach might not be backportable, but it might be a
good approach for 18+.

Regards,
	Jeff Davis






view thread (9+ messages)  latest in thread

Message-ID: <8c60e698428962f33fdfbfab2206eedba09029fc.camel@j-davis.com>
Permalink:  ../8c60e698428962f33fdfbfab2206eedba09029fc.camel@j-davis.com/
Also on:    postgresql.org/message-id/8c60e698428962f33fdfbfab2206eedba09029fc.camel@j-davis.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: pgsql@j-davis.com, andres@anarazel.de
  Subject: Re: HashAgg degenerate case
  In-Reply-To: <8c60e698428962f33fdfbfab2206eedba09029fc.camel@j-davis.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox