| 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-26 09:28:53 |
| Message-ID: | 496639de-eec0-4c6d-ab8c-4fe422994b1f@uni-muenster.de |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On 25/11/2026 16:30, Tom Lane wrote:
> A side point is that I think you don't need to switch in and out
> of the temp context each time through the loop. Once the StringInfo
> buffer is created it'll repalloc itself bigger in its original
> context, and there's nothing saying you can't reset the current
> context. This probably wouldn't be worth worrying about in itself,
> but it might make it less messy to implement the first idea.
Makes total sense.. thank you for clarifying! Done that way in 0001 and
0002.
> On Wed, 25 Nov 2026 at 22:43, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> 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.
I tested this query with v3-0001 in a -O2 build without --enable-cassert
and I see no slowdown:
master: n=19 min=6150.3 ms max=6617.7 ms avg=6254.1 ms
patched: n=19 min=6073.2 ms max=6188.7 ms avg=6117.0 ms
>> 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?
>>
+1
I will take a look at this today or tomorrow and post a separate 0003 patch.
On 25/11/2026 18:58, Kirill Reshke wrote:
>>From my testing, it also turns out impact is measurable.
> I used the following setup:
>
> CREATE TEMP TABLE xml_perf (v integer[]);
> INSERT INTO xml_perf
> SELECT ARRAY(SELECT g FROM generate_series(1, 8192) AS g);
> ANALYZE xml_perf;
>
> query:
> SELECT xmlelement(name r, v)
> FROM xml_perf CROSS JOIN generate_series(1, 182);
>
> results, 15 runs:
>
> median min max
> master 150.19 148.69 153.77
> + v2-0001 223.98 221.27 226.66
I was unable to reproduce these results with v3-0001 (now containing
Tom's suggestions). I tried your query and got these results:
master: n=19 min=155.6 ms max=161.9 ms avg=158.8 ms
patched: n=19 min=152.9 ms max=162.1 ms avg=156.9 ms
Also with -O2 and without --enable-assert. Can you share how your server
was built?
Thanks!
Best, Jim
| Attachment | Content-Type | Size |
|---|---|---|
| v3-0001-Fix-memory-growth-when-mapping-arrays-to-XML.patch | text/x-patch | 2.5 KB |
| v3-0002-Fix-memory-growth-in-the-_to_xml-functions.patch | text/x-patch | 2.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Koshino Taiki | 2026-11-26 09:36:24 | Re: Proposal: Include CHECK constraint expression in violation error DETAIL |
| Previous Message | shveta malik | 2026-11-26 09:20:39 | Re: Proposal: Conflict log history table for Logical Replication |