Re: Regression with large XML data input
Jim Jones <jim.jones@uni-muenster.de>
From: Jim Jones <jim.jones@uni-muenster.de>
To: Michael Paquier <michael@paquier.xyz>, Tom Lane <tgl@sss.pgh.pa.us>
Cc: Robert Treat <rob@xzilla.net>,
Postgres hackers <pgsql-hackers@lists.postgresql.org>,
Erik Wienhold <ewie@ewie.name>
Date: 2025-07-28T07:45:14Z
Lists: pgsql-hackers
On 28.07.25 04:47, Michael Paquier wrote:
> I understand that from the point of view of a
> maintainer this is rather bad, but from the customer point of view the
> current situation is also bad to deal with in the scope of a minor
> upgrade, because applications suddenly break.
I totally get it --- from the user’s perspective, it’s hard to see this
as a bugfix.
I was wondering whether using XML_PARSE_HUGE in xml_parse's options
could help address this, for example:
options = XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_HUGE
| (preserve_whitespace ? 0 : XML_PARSE_NOBLANKS);
According to libxml2's parserInternals.h:
/**
* Maximum size allowed for a single text node when building a tree.
* This is not a limitation of the parser but a safety boundary feature,
* use XML_PARSE_HUGE option to override it.
* Introduced in 2.9.0
*/
#define XML_MAX_TEXT_LENGTH 10000000
/**
* Maximum size allowed when XML_PARSE_HUGE is set.
*/
#define XML_MAX_HUGE_LENGTH 1000000000
The XML_MAX_TEXT_LENGTH limit is what we're hitting now, but
XML_MAX_HUGE_LENGTH is extremely generous. Here's a quick PoC using
XML_PARSE_HUGE:
psql (19devel)
Type "help" for help.
postgres=# CREATE TABLE xmldata (message xml);
CREATE TABLE
postgres=# DO $$
DECLARE huge_size text := repeat('X', 1000000000);
BEGIN
INSERT INTO xmldata (message) VALUES
((('<foo><bar>' || huge_size ||'</bar></foo>')::xml));
END $$;
DO
postgres=# SELECT pg_size_pretty(length(message::text)::bigint) FROM
xmldata;
pg_size_pretty
----------------
954 MB
(1 row)
While XML_MAX_HUGE_LENGTH prevents unlimited memory usage, it still
opens the door to potential resource exhaustion. I couldn't find a way
to dynamically adjust this limit in libxml2.
One idea would be to guard XML_PARSE_HUGE behind a GUC --- say,
xml_enable_huge_parsing. That would at least allow controlled
environments to opt in. But of course, that wouldn't help current releases.
Best regards, Jim
Commits
-
Remove unnecessary complication around xmlParseBalancedChunkMemory.
- 902f92221889 19 (unreleased) landed
- cdcdabce5b70 14.19 landed
- 0ae824704070 13.22 landed
- 0928e18eb85a 15.14 landed
- d5f014d897c8 18.0 landed
- 762c6d8d26e0 16.10 landed
- 7571e0f6e924 17.6 landed
-
Avoid regression in the size of XML input that we will accept.
- 6d5e493b4a15 16.10 landed
- 589d6e6408b4 13.22 landed
- 0ffbd345e43d 15.14 landed
- 0395464aff02 14.19 landed
- fd4ad33fe223 17.6 landed
- 71c0921b649d 19 (unreleased) landed
- 637ead2e1aa1 18.0 landed
-
Use xmlParseInNodeContext not xmlParseBalancedChunkMemory.
- 6082b3d5d3d1 18.0 cited
-
Revert "Add support for parsing of large XML data (>= 10MB)"
- f2743a7d70e7 17.0 cited