BUG #19030: Hash join leads to extremely high memory usage - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #19030: Hash join leads to extremely high memory usage
Date
Msg-id 19030-944dd78d7ef94c0f@postgresql.org
Whole thread Raw
Responses Re: BUG #19030: Hash join leads to extremely high memory usage
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      19030
Logged by:          Marc-Olaf Jaschke
Email address:      moj@dshare.de
PostgreSQL version: 17.6
Operating system:   Linux
Description:

Description
- Two tables, left join
- The left table has significantly fewer rows than the right table
- The left table has very large rows (many columns with high memory usage)
- The left table has many null values in the join column
- A hash join is used
- The hash node is built from the left table
- The query results in extremely high memory usage (100x work_mem in the
example, > 1000x in real case)
- Reliably causing a PostgreSQL server to crash in production
- With enable_hashjoin = false, the query runs without any issues

Example
- Simplified artificial example – but I hope it simulates a real problem on
a production system well.
- Simulate large rows with one big column
- Running the newest version with mostly default settings


==========================================


select version();
-- > PostgreSQL 17.6 (Debian 17.6-1.pgdg13+1) on aarch64-unknown-linux-gnu,
compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit

show work_mem;
-- > 4MB

show hash_mem_multiplier;
-- > 2

-- speed up the test case
set default_toast_compression = lz4;

-- show high memory usage without crashing the server
set max_parallel_workers_per_gather = 0;


create table left_ as
select
case when i% 2 = 0 then i::text end c,
repeat('x', 10_000) big
from
generate_series(1, 10_000_000) i;

create table right_ as
select
case when i% 2 = 0 then i::text end c
from
generate_series(1, 20_000_000) i;

analyze left_, right_;


explain (analyze, memory)
select
*
from
left_
natural left join
right_;

-- > Buckets: 131072 (originally 131072)  Batches: 131072 (originally 256)
Memory Usage: 440482kB


pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #19029: Replication Slot size keeps increasing while logical subscription works fine
Next
From: Tom Lane
Date:
Subject: Re: BUG #19030: Hash join leads to extremely high memory usage