Re: Avoiding OOM in a hash join with many duplicate inner keys

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>, David Hinkle <hinkle(at)cipafilter(dot)com>
Subject: Re: Avoiding OOM in a hash join with many duplicate inner keys
Date: 2017-02-16 22:13:31
Message-ID: 13698.1487283211@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Robert Haas <robertmhaas(at)gmail(dot)com> writes:
> On Thu, Feb 16, 2017 at 3:51 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> No, it'd be the *most* common MCV, because we're concerned about the
>> worst-case (largest) bucket size. But that's good, really, because the
>> highest MCV frequency will be the one we have most statistical
>> confidence in. There's generally a whole lot of noise in the tail-end
>> MCV numbers.

> Oh, right. That's reassuring, as it seems like it has a much better
> chance of actually being right.

Here's a version that does it that way. Unsurprisingly, it doesn't
cause any regression test changes, but you can confirm it's having an
effect with this test case:

create table tt(f1 int);
insert into tt select 1 from generate_series(1,1000000) g;
insert into tt select g from generate_series(1,1000000) g;
analyze tt;
explain select * from tt a natural join tt b;

Unpatched code will go for a hash join on this example.

For application to the back branches, we could do it just like this
(leaving the existing fields alone, and allowing sizeof(RestrictInfo)
to grow), or we could change the datatypes of the four fields involved
to float4 so that sizeof(RestrictInfo) stays the same. I'm not entirely
sure which way is the more hazardous from an ABI standpoint. If there
are any external modules doing their own palloc(sizeof(RestrictInfo))
calls, the first way would be bad, but really there shouldn't be; I'd
expect people to be using make_restrictinfo() and friends. (Note that
palloc's power-of-2 padding wouldn't save us, because sizeof(RestrictInfo)
is currently exactly 128 on 32-bit machines in several of the back
branches.) Conversely, if any non-core code is touching the bucketsize
fields, changing those field widths would break that; but that doesn't
seem all that likely either. On balance I think I might go for the first
way, because it would remove doubt about whether reducing the precision
of the bucketsize estimates would cause any unexpected plan changes.

Or we could decide not to back-patch because the problem doesn't come
up often enough to justify taking any risk for. But given that we've
gotten one confirmed field report, I'm not voting that way.

regards, tom lane

Attachment Content-Type Size
check-hash-bucket-size-against-work_mem-2.patch text/x-diff 14.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2017-02-16 22:34:45 Re: pgsql: Add new function dsa_allocate0.
Previous Message David Christensen 2017-02-16 20:58:57 [PATCH] Add pg_disable_checksums() and supporting infrastructure