A potential memory leak on Merge Join when Sort node is not below Materialize node

From: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: "onderkalaci(at)gmail(dot)com" <onderkalaci(at)gmail(dot)com>
Subject: A potential memory leak on Merge Join when Sort node is not below Materialize node
Date: 2022-09-28 16:56:34
Message-ID: CAEudQAr9jaGCfR+5zf=yPqQir5MGmRF9D=Jv61o43VWiR+PQoQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

>CREATE TABLE t1 (a text);
>CREATE TABLE t2 (a text);

>-- make the text a little large by adding 100000000000
>INSERT INTO t1 SELECT (100000000000+i%1000)::text FROM
>generate_series(0,10000000) i;

>-- make the text a little large by adding 100000000000
>INSERT INTO t2 SELECT (100000000000+i%10000)::text FROM
>generate_series(0,10000000) i;

>-- to simplify the explain plan, not strictly necessary
>SET max_parallel_workers_per_gather TO 0;

>-- these two are necessary so that the problem is triggered
>-- these are helpful to use Merge join and avoid materialization
>SET enable_hashjoin TO false;
>SET enable_material TO false;

>-- the join is on a TEXT column
>-- when the join is on INT column with a similar setup, I do not observe
>this problem
>SELECT count(*) FROM t1 JOIN t2 USING (a);
>```

>The explain output for the query like the following:
>```
>explain SELECT count(*) FROM t1 JOIN t2 USING (a);

I run your test here with a fix attached.

Can you retake your test with the patch attached?
regards,

Ranier Vilela

Attachment Content-Type Size
fix_possible_memory_leak_merge_join_sort.patch application/octet-stream 677 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Aleksander Alekseev 2022-09-28 17:14:23 Refactor UnpinBuffer()
Previous Message Peter Geoghegan 2022-09-28 16:49:01 Re: longfin and tamandua aren't too happy but I'm not sure why