OOM kill in xmlelement() with large arrays

From: Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Cc: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Subject: OOM kill in xmlelement() with large arrays
Date: 2026-11-25 11:50:16
Message-ID: 7581277e-e089-417d-867f-8ffd54b9e4c6@uni-muenster.de
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi

While answering a review in [1] I noticed that xmlelement() accumulates
the intermediate data produced for every array element until the end of
the statement. For large arrays this can quickly exhaust the server's
memory, and the loop does not check for interrupts, so it cannot be
cancelled while memory grows..

== PG19 =

psql (19.0 (Debian 19.0-2.pgdg13+1))
Type "help" for help.

postgres=# select length(xmlelement(name a, array_fill(''::text,
ARRAY[56000000]))::text);
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
The connection to the server was lost. Attempting reset: Failed.
!?>
\q

$ journalctl -k --since -1min | grep -i 'out of memory'
kernel: Out of memory: Killed process 350541 (postgres)
total-vm:57378464kB, anon-rss:55284684kB, file-rss:12kB,
shmem-rss:4044kB, UID:525286 pgtables:111956kB oom_score_adj:200

v1 patch converts each element in a temporary memory context
that is reset after every itertion, and adds CHECK_FOR_INTERRUPTS()
to the loop.

== patched ==

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

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

Thoughts?

Best, Jim

1 -
https://www.postgresql.org/message-id/flat/57a5a9ef-3436-4dda-8c7e-ec32813e6a84%40uni-muenster.de

Attachment Content-Type Size
v1-0001-Fix-memory-growth-when-mapping-arrays-to-XML.patch text/x-patch 2.4 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nisha Moond 2026-11-25 11:52:03 Re: Proposal: Conflict log history table for Logical Replication
Previous Message Amit Kapila 2026-11-25 11:11:02 Re: Detect mismatched subscription and replication slot properties