Re: OOM kill in xmlelement() with large arrays

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

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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ayush Tiwari 2026-11-25 17:47:33 Re: pg_rewind: Fix buffer size passed when building WAL file name
Previous Message Robert Haas 2026-11-25 17:33:05 Re: Reducing output size of nodeToString