Memory leak on subquery as scalar operand - Mailing list pgsql-bugs

From Lauri Laanmets
Subject Memory leak on subquery as scalar operand
Date
Msg-id CAPH-tTxLf44s3CvUUtQpkDr1D8Hxqc2NGDzGXS1ODsfiJ6WSqA@mail.gmail.com
Whole thread Raw
Responses Re: Memory leak on subquery as scalar operand
Re: Memory leak on subquery as scalar operand
List pgsql-bugs
There seems to be a memory leak in PostgreSQL for Linux (doesn't happen on Windows). At least in versions 12 to 14 (and not in 9.5). It can be reproduced with the following SQL script on an empty database.

* When repeated, Linux process starts using more and more RAM until OOM-killer will kill it.
* 'pg_backend_memory_contexts' doesn't show any increase.
* (gdb) p MemoryContextStats(TopMemoryContext) - doesn't show any increase.

-/
CREATE TABLE IF NOT EXISTS public.leak_test
(
   id integer NOT NULL,
   CONSTRAINT leak_test_pkey PRIMARY KEY (id)
);

INSERT INTO leak_test(id)
   SELECT id
   FROM generate_series(1,100000) id
ON CONFLICT DO NOTHING;

SELECT
    id,
    (SELECT count(*) FROM leak_test x WHERE x.id=l.id) as x_result,
    (SELECT count(*) FROM leak_test y WHERE y.id=l.id) as y_result
    /* Leaks memory around 80 kB on each query, but only if two sub-queries are used. */
FROM leak_test l;
/-

Regards
Lauri

pgsql-bugs by date:

Previous
From: Japin Li
Date:
Subject: Re: BUG #17670: Logical Replication data may be lost on the subscription under certain scenarios
Next
From: Tom Lane
Date:
Subject: Re: Problem with Avg function and Money type in PostgreSQL 15