pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Jim Jones <jim.jones@uni-muenster.de>
Cc: Peter Smith <smithpb2250@gmail.com>
Cc: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Cc: Nikolay Samokhvalov <samokhvalov@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Cc: Andrey Borodin <amborodin86@gmail.com>
Subject: Re: [PATCH] Add pretty-printed XML output option
Date: Wed, 21 May 2025 16:20:17 -0400
Message-ID: <1358967.1747858817@sss.pgh.pa.us> (raw)
In-Reply-To: <1c7cf9f0-14c3-0341-6894-8ca577bf325e@uni-muenster.de>
References: <2f5df461-dad8-6d7d-4568-08e10608a69b@uni-muenster.de>
	<CAHut+PtMHeBBZ2zv0Bk7-4KMkSrrxZ9PZxUCzhpwTGVk+8G3cw@mail.gmail.com>
	<2eac4d78-2b20-e8ec-54cc-22a88653a1cc@uni-muenster.de>
	<CAHut+PsgXxbzzsG+VqHUa4Kz3G4B-anNmF3-GENwuUdfMJtowA@mail.gmail.com>
	<fb2a543b-e615-d9ba-8402-909631f9b212@uni-muenster.de>
	<CAHut+Pvryt_+wA7TwKVaaF0e6Pm5LaPZQLKatp=78oFd8vMh3A@mail.gmail.com>
	<6cb296fe-4b49-3691-633e-2e2366997b04@uni-muenster.de>
	<3baf8f25-3bd7-7db5-0b31-e4c92b6269ba@enterprisedb.com>
	<1cbe1087-6acb-2b0a-dc9b-0a2d5b99e466@uni-muenster.de>
	<CAHut+PvBUTf9p+Y6+mpWpz59=3eL_EKQ+7Ysxpa9hYGdncDMdg@mail.gmail.com>
	<532259.1678393296@sss.pgh.pa.us>
	<906cfd1d-63a0-3cf5-7291-2dadb2e! 149ff@uni-muenster.de>
	<803304.1678458776@sss.pgh.pa.us>
	<efe0b19b-! d41c-f8ab-f3b8-afb0108f3706@uni-muenster.de>
	<2752578.1678815625@sss.pgh.pa.us>
	<abd25443-ef6d-7b8a-c593-a2a991d3e5ce@uni-! ! muenster.de>
	<3057762.1678836321@sss.pgh.pa.us>
	<3622266.1678914816@sss.pgh.pa.us>
	<1c7cf9f0-14c3-0341-6894! -8ca577bf325e@uni-muenster.de>

Jim Jones <jim.jones@uni-muenster.de> writes:
> In my environment (libxml2 v2.9.10 and Ubuntu 22.04) I couldn't 
> reproduce this memory leak.

Just when you thought it was safe to go back in the water ...

Experimenting with the improved valgrind leak detection code at [1],
I discovered that XMLSERIALIZE(... INDENT) has yet a different memory
leak problem.  It turns out that xmlDocSetRootElement() doesn't
merely install the given root node: it unlinks the document's old
root node and returns it to you.  If you don't free it, it's leaked
(for the session, since this is a malloc not palloc).  The amount of
leakage isn't that large, seems to be a couple hundred bytes per
iteration, which may explain why this escaped our notice in the
previous testing.  Still, it could add up under extensive usage.
So I think we need to apply the attached, back to PG 16.

			regards, tom lane

[1] https://www.postgresql.org/message-id/1295385.1747847681%40sss.pgh.pa.us

Attachments:

fix-leakage-in-xmlserialize-indent.patch (text/x-diff, 1.1K) download diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index db8d0d6a7e8..73fd4fa090c 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -754,6 +754,7 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent) * content nodes, and then iterate over the nodes. */ xmlNodePtr root; + xmlNodePtr oldroot; xmlNodePtr newline; root = xmlNewNode(NULL, (const xmlChar *) "content-root"); @@ -761,8 +762,14 @@ xmltotext_with_options(xmltype *data, XmlOptionType xmloption_arg, bool indent) xml_ereport(xmlerrcxt, ERROR, ERRCODE_OUT_OF_MEMORY, "could not allocate xml node"); - /* This attaches root to doc, so we need not free it separately. */ - xmlDocSetRootElement(doc, root); + /* + * This attaches root to doc, so we need not free it separately... + * but instead, we have to free the old root if there was one. + */ + oldroot = xmlDocSetRootElement(doc, root); + if (oldroot != NULL) + xmlFreeNode(oldroot); + xmlAddChildList(root, content_nodes); /*

view thread (70+ messages)  latest in thread

Message-ID: <1358967.1747858817@sss.pgh.pa.us>
Permalink:  ../1358967.1747858817@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1358967.1747858817@sss.pgh.pa.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, jim.jones@uni-muenster.de, smithpb2250@gmail.com, peter.eisentraut@enterprisedb.com, samokhvalov@gmail.com, pgsql-hackers@lists.postgresql.org, amborodin86@gmail.com
  Subject: Re: [PATCH] Add pretty-printed XML output option
  In-Reply-To: <1358967.1747858817@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox