Re: OOM kill in xmlelement() with large arrays

From: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
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>
Subject: Re: OOM kill in xmlelement() with large arrays
Date: 2026-11-27 10:34:51
Message-ID: 5e95df54-25bf-4e46-ae74-c24fa87a0936@uni-muenster.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 26/11/2026 10:28, Jim Jones wrote:
>> On Wed, 25 Nov 2026 at 22:43, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>>> 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
>
> +1
> I will take a look at this today or tomorrow and post a separate 0003 patch.

I spent some time with this today. 0003 now moves the body of
map_sql_value_to_xml_value() into map_sql_value_to_xml_value_internal(),
which does the elemnt type lookups once per array and passes the results
down to the conversion of each element -- I added some small regression
tests for that.

here some tests:

(-O2 build without assertions)

== master ==

postgres=# select length(xmlelement(name a, array_fill(''::text,
ARRAY[16000000]))::text);
length
-----------
304000007
(1 row)

Time: 6029,250 ms (00:06,029)

== patched (0001, 0002) ==

postgres=# select length(xmlelement(name a, array_fill(''::text,
ARRAY[16000000]))::text);
length
-----------
304000007
(1 row)

Time: 2247,771 ms (00:02,248)

== patched (0001, 0002, 0003)==

postgres=# select length(xmlelement(name a, array_fill(''::text,
ARRAY[16000000]))::text);
length
-----------
304000007
(1 row)

Time: 1464,118 ms (00:01,464)

0001 and 0002 remain unchanged.

Best, Jim

Attachment Content-Type Size
v4-0001-Fix-memory-growth-when-mapping-arrays-to-XML.patch text/x-patch 2.6 KB
v4-0002-Fix-memory-growth-in-the-_to_xml-functions.patch text/x-patch 2.7 KB
v4-0003-Look-up-element-type-information-once-when-mappin.patch text/x-patch 7.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Álvaro Herrera 2026-11-27 10:34:22 Re: Set 1s WaitLatch timeout if standby limit has expired in ResolveRecoveryConflictWithBufferPin