On Wed, Nov 25, 2026 at 12:50 PM Jim Jones <[email protected]> wrote: > > 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
Oh my! Reproduced, this oneliner almost crashed my 32GB laptop: Out of memory: Killed process 66563 (postgres) total-vm:29998880kB, anon-rss:22659436kB[..] (yeah, that's literally 22-29GB) +1 to fixing this. Patch LGMT, maybe just two tiny things: - perhaps move MemoryContext oldcxt to the loop - "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) --- Not related to the thread: I wish we had a way to limit backend total RSS somehow. I've lost count on how many occassions we hit such issues without any safety net. Not that the patch fixes it, but it can still DoS the thing. Andres told me that setrlimit(RLIMIT_DATA) SIGSEGVs in case of hitting the limit when growing stack and not just overallocating memory (if I recall correctly). I've tried to simulate such stuff, but it always worked properly for me... -J.
