Re: OOM kill in xmlelement() with large arrays

From: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
To: Daniel Gustafsson <daniel(at)yesql(dot)se>, Jakub Wartak <jakub(dot)wartak(at)enterprisedb(dot)com>
Cc: 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 13:34:57
Message-ID: 5ae6da3f-e6ef-4a72-b696-94415abdc9fd@uni-muenster.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Jakub & Daniel

Thanks for the reviews.

On 25/11/2026 14:12, Daniel Gustafsson wrote:
>> On 25 Nov 2026, at 14:01, Jakub Wartak <jakub(dot)wartak(at)enterprisedb(dot)com> wrote:
>> +1 to fixing this. Patch LGMT, maybe just two tiny things:
>>
>> - perhaps move MemoryContext oldcxt to the loop

Done in v2-0001.

>> - "map_sql_value_to_xml_value temporary cxt" is a bit lengthy blob, maybe
>> "elements of XML array" would suffice there (or maybe check further wihtin
>> pg_backend_memory_contexts for better examples)

I kept the context label like this for now. My rationale is that it
makes it a bit easier to identify the context without looking into the
code.. let me know if there is a standard here.

> +1. Patch looks good on a first glance, expanding to the *_to_xml cases seems
> like a good idea.

Thanks.

0002 attached applies the same approach to the *to_xml functions.

== patched ==

psql (20devel)
Type "help" for help.

postgres=# select length(query_to_xml('SELECT g,g,g,g,g FROM
generate_series(1, 10000000) g', false, false, '')::text);
length
-----------
984444557
(1 row)

postgres=# create table t as select g a, g b, g c, g d, g e FROM
generate_series(1, 10000000) g;
SELECT 10000000
postgres=# SELECT length(table_to_xml('t', false, false, '')::text);
length
-----------
984444549
(1 row)

Best, Jim

Attachment Content-Type Size
v2-0001-Fix-memory-growth-when-mapping-arrays-to-XML.patch text/x-patch 2.6 KB
v2-0002-Fix-memory-growth-in-the-_to_xml-functions.patch text/x-patch 2.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Salma Elsayed 2026-11-25 13:36:02 Re: [GSoC 2026] - B-tree Index Bloat Reduction - Approach & Questions
Previous Message Ayush Tiwari 2026-11-25 13:19:47 pg_commit_ts pages can be written before WAL is flushed