Re: OOM kill in xmlelement() with large arrays

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>, Daniel Gustafsson <daniel(at)yesql(dot)se>, Jakub Wartak <jakub(dot)wartak(at)enterprisedb(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: OOM kill in xmlelement() with large arrays
Date: 2026-11-25 17:58:40
Message-ID: CALdSSPiUOBtv0VZW0QfqML8xZy_RU9HF138eGEVNAugTq+d1Uw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 25 Nov 2026 at 22:43, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> I wrote:
> > I wonder whether this would be a net negative for many use-cases.
> > Creating and dropping a memory context is hardly free. Have you
> > done any performance measurements on cases that don't eat enough
> > memory to be OOM hazards?
>
> I did this simple test:
>
> \timing on
> do $$
> declare arr text[] default array_fill(''::text, ARRAY[50]);
> begin
> for i in 1..1000000 loop
> perform xmlelement(name a, arr);
> end loop;
> end$$;
>
> With HEAD this takes about 6400ms for me, with the patch
> about 6700ms. (These numbers are in an assert-enabled
> build, so take them with a grain of salt.) That seems
> like it's not really enough of a hit to justify contorting
> the code, so consider this objection withdrawn.
>
> Another thing I noticed is that we are doing the type-output
> function lookup over again for each array element, though
> with some refactoring we could do it just once. Since that
> lookup is skipped for "bool" elements, I compared
>
> regression=# select length(xmlelement(name a, array_fill(true,
> ARRAY[16000000]))::text);
> length
> -----------
> 368000007
> (1 row)
>
> Time: 1196.610 ms (00:01.197)
> regression=# select length(xmlelement(name a, array_fill(''::text,
> ARRAY[16000000]))::text);
> length
> -----------
> 304000007
> (1 row)
>
> Time: 2005.366 ms (00:02.005)
>
> Now the time taken by textout is not zero, but it still looks like
> close to 50% of the runtime is going into those repeat lookups.
> Maybe that's enough to worry about?
>
> regards, tom lane

From my testing, it also turns out impact is measurable.
I used the following setup:

CREATE TEMP TABLE xml_perf (v integer[]);
INSERT INTO xml_perf
SELECT ARRAY(SELECT g FROM generate_series(1, 8192) AS g);
ANALYZE xml_perf;

query:
SELECT xmlelement(name r, v)
FROM xml_perf CROSS JOIN generate_series(1, 182);

results, 15 runs:

median min max
master 150.19 148.69 153.77
+ v2-0001 223.98 221.27 226.66

--
Best regards,
Kirill Reshke

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alberto Piai 2026-11-25 18:00:55 Re: Adding a stored generated column without long-lived locks
Previous Message Tom Lane 2026-11-25 17:52:50 Re: Fix OOM when rendering large geometry arrays