[PG19] Hash join writes null-keyed outer rows to temp files

Started by shihao zhongabout 1 month ago6 messageshackers
Jump to latest
#1shihao zhong
zhong950419@gmail.com

Hi hackers,

Since 1811f1af98f, a hash join keeps outer rows with a NULL join key in
a tuplestore and returns them at the end of the batch. Once they pass
work_mem / 16 they go to disk, even for a single-batch join. 18 returned
them right away and never stored them.

With default settings:

CREATE TABLE b (k int PRIMARY KEY);
INSERT INTO b SELECT generate_series(1, 1000);
CREATE TABLE a AS SELECT NULL::int AS k, repeat('x', 100) AS pad
FROM generate_series(1, 1000000);
VACUUM ANALYZE a, b;
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF)
SELECT count(b.k) FROM a LEFT JOIN b ON a.k = b.k;
SET temp_file_limit = '10MB';
SELECT count(b.k) FROM a LEFT JOIN b ON a.k = b.k;

On release builds 18 takes 22 ms with no temp files. 19 takes 110 ms
and writes 106 MB of temp files.

The original mail said returning them right away would need a big
rewrite [1]/messages/by-id/3061845.1746486714@sss.pgh.pa.us. The patch gives such a row a dummy hash value of 0 and
lets it probe as usual. That value always maps to the current batch,
and the row cannot match, so it comes out null-extended right away,
like in 18. Only the multi-batch parallel case still uses the
tuplestore, since the outer side is partitioned before probing.

Another way is to return such a row with a flag and jump straight to
HJ_FILL_OUTER_TUPLE, skipping the probe. That is twice the size, so I
went with the smaller one in the hope it can go into 19. I can post the
other one if people prefer it.

[1]: /messages/by-id/3061845.1746486714@sss.pgh.pa.us

Thanks,
Shihao

Attachments:

t254266_1
v1-0001-Emit-null-keyed-outer-hash-join-tuples-immediatel.patchapplication/octet-stream; name=v1-0001-Emit-null-keyed-outer-hash-join-tuples-immediatel.patchDownload+19-13
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: shihao zhong (#1)
Re: [PG19] Hash join writes null-keyed outer rows to temp files

shihao zhong <zhong950419@gmail.com> writes:

The original mail said returning them right away would need a big
rewrite [1]. The patch gives such a row a dummy hash value of 0 and
lets it probe as usual. That value always maps to the current batch,
and the row cannot match, so it comes out null-extended right away,
like in 18.

I do not think I believe this patch. In particular, it assumes that
hashvalue 0 will map to the current batch, which is only accidentally
true for ExecHashGetBucketAndBatch (but okay, we could add a provision
to its API that hashvalue 0 must map to batch 0) and more importantly
can fail entirely for ExecHashGetSkewBucket. There's no reason to
assume there couldn't be a real tuple with hashvalue 0, and if there
were one already in the skew table then the wrong things would happen.

Also, trading off storing to a tuplestore for scanning a hash bucket
is not obviously a win.

Another way is to return such a row with a flag and jump straight to
HJ_FILL_OUTER_TUPLE, skipping the probe.

I like this idea substantially better. More or less as attached?

regards, tom lane

Attachments:

t254266_2
v2-emit-null-keyed-outer-hash-join-tuples-immediately.patchtext/x-diff; charset=us-ascii; name=v2-emit-null-keyed-outer-hash-join-tuples-immediately.patchDownload+47-29
#3shihao zhong
zhong950419@gmail.com
In reply to: Tom Lane (#2)
Re: [PG19] Hash join writes null-keyed outer rows to temp files

Hi

Tom Lane <tgl@sss.pgh.pa.us> writes:

I like this idea substantially better. More or less as attached?

Yes, thanks for writing it.

It passed all the local tests I had before and the performance is
back.

One small thing. Looks like HJ_FILL_OUTER_NULL_TUPLES can
now only be reached in a parallel join, so maybe add
Assert(parallel) there.

Thanks,
Shihao

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: shihao zhong (#3)
Re: [PG19] Hash join writes null-keyed outer rows to temp files

shihao zhong <zhong950419@gmail.com> writes:

One small thing. Looks like HJ_FILL_OUTER_NULL_TUPLES can
now only be reached in a parallel join, so maybe add
Assert(parallel) there.

Why would that be an improvement? It has nothing to do with
the correctness of the code.

regards, tom lane

#5shihao zhong
zhong950419@gmail.com
In reply to: Tom Lane (#4)
Re: [PG19] Hash join writes null-keyed outer rows to temp files

Why would that be an improvement?

Fair enough. My thought was only that the tuplestore is now a
parallel-only thing, and I have seen a later change break an
assumption like that without anyone noticing.

But that code works for the serial case too, so there is nothing to
protect here. Please ignore it.

Thanks,
Shihao

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: shihao zhong (#5)
Re: [PG19] Hash join writes null-keyed outer rows to temp files

shihao zhong <zhong950419@gmail.com> writes:

But that code works for the serial case too, so there is nothing to
protect here. Please ignore it.

Cool, pushed then.

regards, tom lane