mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-21 08:29:39 +08:00
936c4af1c6
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.
148 lines
5.2 KiB
Plaintext
148 lines
5.2 KiB
Plaintext
--
|
|
-- 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
|
|
RESET client_min_messages;
|
|
select query_to_xml('select 1 as x',true,false,'');
|
|
query_to_xml
|
|
---------------------------------------------------------------
|
|
<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">+
|
|
+
|
|
<row> +
|
|
<x>1</x> +
|
|
</row> +
|
|
+
|
|
</table> +
|
|
|
|
(1 row)
|
|
|
|
select xslt_process( query_to_xml('select x from generate_series(1,5) as
|
|
x',true,false,'')::text,
|
|
$$<xsl:stylesheet version="1.0"
|
|
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
<xsl:output method="xml" indent="yes" />
|
|
<xsl:template match="*">
|
|
<xsl:copy>
|
|
<xsl:copy-of select="@*" />
|
|
<xsl:apply-templates />
|
|
</xsl:copy>
|
|
</xsl:template>
|
|
<xsl:template match="comment()|processing-instruction()">
|
|
<xsl:copy />
|
|
</xsl:template>
|
|
</xsl:stylesheet>
|
|
$$::text);
|
|
xslt_process
|
|
---------------------------------------------------------------
|
|
<?xml version="1.0"?> +
|
|
<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">+
|
|
+
|
|
<row> +
|
|
<x>1</x> +
|
|
</row> +
|
|
+
|
|
<row> +
|
|
<x>2</x> +
|
|
</row> +
|
|
+
|
|
<row> +
|
|
<x>3</x> +
|
|
</row> +
|
|
+
|
|
<row> +
|
|
<x>4</x> +
|
|
</row> +
|
|
+
|
|
<row> +
|
|
<x>5</x> +
|
|
</row> +
|
|
+
|
|
</table> +
|
|
|
|
(1 row)
|
|
|
|
CREATE TABLE xpath_test (id integer NOT NULL, t xml);
|
|
INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
|
|
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
|
|
as t(id int4);
|
|
id
|
|
----
|
|
(0 rows)
|
|
|
|
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
|
|
as t(id int4, doc int4);
|
|
id | doc
|
|
----+-----
|
|
1 | 1
|
|
(1 row)
|
|
|
|
DROP TABLE xpath_test;
|
|
CREATE TABLE xpath_test (id integer NOT NULL, t text);
|
|
INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
|
|
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
|
|
as t(id int4);
|
|
id
|
|
----
|
|
(0 rows)
|
|
|
|
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
|
|
as t(id int4, doc int4);
|
|
id | doc
|
|
----+-----
|
|
1 | 1
|
|
(1 row)
|
|
|
|
create table articles (article_id integer, article_xml xml, date_entered date);
|
|
insert into articles (article_id, article_xml, date_entered)
|
|
values (2, '<article><author>test</author><pages>37</pages></article>', 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);
|
|
article_id | author | page_count | title
|
|
------------+--------+------------+-------
|
|
2 | test | 37 |
|
|
(1 row)
|
|
|
|
-- this used to fail when invoked a second time
|
|
select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
|
|
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
<xsl:template match="@*|node()">
|
|
<xsl:copy>
|
|
<xsl:apply-templates select="@*|node()"/>
|
|
</xsl:copy>
|
|
</xsl:template>
|
|
</xsl:stylesheet>$$)::xml;
|
|
xslt_process
|
|
--------------
|
|
<aaa/> +
|
|
|
|
(1 row)
|
|
|
|
select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
|
|
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
|
<xsl:template match="@*|node()">
|
|
<xsl:copy>
|
|
<xsl:apply-templates select="@*|node()"/>
|
|
</xsl:copy>
|
|
</xsl:template>
|
|
</xsl:stylesheet>$$)::xml;
|
|
xslt_process
|
|
--------------
|
|
<aaa/> +
|
|
|
|
(1 row)
|
|
|
|
create table t1 (id integer, xml_data xml);
|
|
insert into t1 (id, xml_data)
|
|
values
|
|
(1, '<attributes><attribute name="attr_1">Some
|
|
Value</attribute></attributes>');
|
|
create index idx_xpath on t1 ( xpath_string
|
|
('/attributes/attribute[@name="attr_1"]/text()', xml_data::text));
|