From 936c4af1c6876e09e914eca91c37949e6885b02e Mon Sep 17 00:00:00 2001
From: Tom Lane
Date: Sun, 28 Feb 2010 21:31:57 +0000
Subject: Fix up memory management problems in contrib/xml2.
Get rid of the code that attempted to funnel libxml2's memory allocations
into palloc. We already knew from experience with the core xml datatype
that trying to do this is simply not reliable. Unlike the core code, I
did not bother adding a lot of PG_TRY/PG_CATCH logic to try to ensure that
everything is cleaned up on error exit. Hence, we might leak some memory
if one of these functions fails partway through. Given the deprecated
status of this contrib module and the fact that errors partway through
the functions shouldn't be too common, it doesn't seem worth worrying about.
Also fix a separate bug in xpath_table, that it did the wrong things
if given a result tuple descriptor with less than 2 columns. While
such a case isn't very useful in practice, we shouldn't fail or stomp
memory when it occurs.
Add some simple regression tests based on all the reported crash cases
that I have on hand.
This should be back-patched, but let's see if the buildfarm likes it first.
---
contrib/xml2/sql/xml2.sql | 82 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+)
create mode 100644 contrib/xml2/sql/xml2.sql
(limited to 'contrib/xml2/sql')
diff --git a/contrib/xml2/sql/xml2.sql b/contrib/xml2/sql/xml2.sql
new file mode 100644
index 00000000000..73723b6be10
--- /dev/null
+++ b/contrib/xml2/sql/xml2.sql
@@ -0,0 +1,82 @@
+--
+-- first, define the functions. Turn off echoing so that expected file
+-- does not depend on contents of pgxml.sql.
+--
+SET client_min_messages = warning;
+\set ECHO none
+\i pgxml.sql
+\set ECHO all
+RESET client_min_messages;
+
+select query_to_xml('select 1 as x',true,false,'');
+
+select xslt_process( query_to_xml('select x from generate_series(1,5) as
+x',true,false,'')::text,
+$$
+
+
+
+
+
+
+
+
+
+
+
+$$::text);
+
+CREATE TABLE xpath_test (id integer NOT NULL, t xml);
+INSERT INTO xpath_test VALUES (1, '1');
+SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
+as t(id int4);
+SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
+as t(id int4, doc int4);
+
+DROP TABLE xpath_test;
+CREATE TABLE xpath_test (id integer NOT NULL, t text);
+INSERT INTO xpath_test VALUES (1, '1');
+SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
+as t(id int4);
+SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
+as t(id int4, doc int4);
+
+create table articles (article_id integer, article_xml xml, date_entered date);
+insert into articles (article_id, article_xml, date_entered)
+values (2, 'test37', now());
+SELECT * FROM
+xpath_table('article_id',
+ 'article_xml',
+ 'articles',
+ '/article/author|/article/pages|/article/title',
+ 'date_entered > ''2003-01-01'' ')
+AS t(article_id integer, author text, page_count integer, title text);
+
+-- this used to fail when invoked a second time
+select xslt_process('',$$
+
+
+
+
+
+$$)::xml;
+
+select xslt_process('',$$
+
+
+
+
+
+$$)::xml;
+
+create table t1 (id integer, xml_data xml);
+insert into t1 (id, xml_data)
+values
+(1, 'Some
+Value');
+
+create index idx_xpath on t1 ( xpath_string
+('/attributes/attribute[@name="attr_1"]/text()', xml_data::text));
--
cgit v1.2.3