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