chris=# select '<a/>'::xml; xml ------ <a/> (1 row) chris=# select '<a>'::xml; ERROR: invalid XML content LINE 1: select '<a>'::xml; ^ DETAIL: Entity: line 1: parser error : Premature end of data in tag a line 1 chris=# select xmlelement(name person); xmlelement ------------ <person/> (1 row) chris=# select xmlelement(name person, 'chris'); xmlelement ------------------------ <person>chris</person> (1 row) chris=# select subject, count(winner) from nobel group by subject; subject | count ------------+------- Medicine | 184 Chemistry | 149 Physics | 177 Economics | 57 Literature | 102 Peace | 114 (6 rows) chris=# select subject, count(winner), xmlagg(xmlelement(name winner, winner)) from nobel group by subject; subject | count | [...] ------------+-------+-----------------------------------------------[...] Medicine | 184 | <winner>Emil von Behring</winner><winner>Ronal[...] Chemistry | 149 | <winner>Jacobus H. van 't Hoff</winner><winner[...] Physics | 177 | <winner>Wilhelm Conrad Röntgen</winner><winner[...] Economics | 57 | <winner>Ragnar Frisch</winner><winner>Jan Tinb[...] Literature | 102 | <winner>Sully Prudhomme</winner><winner>Theodo[...] Peace | 114 | <winner>Henry Dunant</winner><winner>Fréric Pa[...] (6 rows) chris=# CREATE TABLE test (a int PRIMARY KEY, b varchar(200)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE chris=# select table_to_xmlschema('test', false, false, ''); table_to_xmlschema --------------------------------------------------------------------------------------------------- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:simpleType name="INTEGER"> <xsd:restriction base="xsd:int"> <xsd:maxInclusive value="2147483647"/> <xsd:minInclusive value="-2147483648"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="VARCHAR"> <xsd:restriction base="xsd:string"> </xsd:restriction> </xsd:simpleType> <xsd:complexType name="RowType.chris.public.test"> <xsd:sequence> <xsd:element name="a" type="INTEGER" minOccurs="0"></xsd:element> <xsd:element name="b" type="VARCHAR" minOccurs="0"></xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name="TableType.chris.public.test"> <xsd:sequence> <xsd:element name="row" type="RowType.chris.public.test" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:element name="test" type="TableType.chris.public.test"/> </xsd:schema> (1 row) chris=# select table_to_xml('test', false, false, ''); table_to_xml -------------------------------------------------------------- <test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </test> (1 row) chris=# \d test Table "public.test" Column | Type | Modifiers --------+------------------------+----------- a | integer | not null b | character varying(200) | Indexes: "test_pkey" PRIMARY KEY, btree (a) chris=# insert into test values (1, 'chris'); INSERT 0 1 chris=# insert into test values (2, 'jakob'); INSERT 0 1 chris=# select table_to_xml('test', false, false, ''); table_to_xml -------------------------------------------------------------- <test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <row> <a>1</a> <b>chris</b> </row> <row> <a>2</a> <b>jakob</b> </row> </test> chris=# select table_to_xml('test', false, true, ''); table_to_xml -------------------------------------------------------------- <test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <a>1</a> <b>chris</b> </test> <test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <a>2</a> <b>jakob</b> </test> chris=# select xmlelement(name person, xmlattributes(random() as name)) as person into people from generate_series(1, 100000); chris=# select * from people limit 2; person ------------------------------------ <person name="0.246969635598361"/> <person name="0.569889863952994"/> (2 rows) chris=# select * from people order by random() limit 1; person ------------------------------------ <person name="0.441761581227183"/> (1 row) chris=# select * from people where array_to_string(xpath('//person/@name', person), '') = '0.441761581227183'; person ------------------------------------ <person name="0.441761581227183"/> (1 row) chris=# \timing Timing is on. chris=# select * from people where array_to_string(xpath('//person/@name', person), '') = '0.441761581227183'; person ------------------------------------ <person name="0.441761581227183"/> (1 row) Time: 4470.633 ms chris=# select * from people where array_to_string(xpath('//person/@name', person), '') = '0.441761581227183'; person ------------------------------------ <person name="0.441761581227183"/> (1 row) Time: 4580.359 ms chris=# explain select * from people where array_to_string(xpath('//person/@name', person), '') = '0.441761581227183'; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Seq Scan on people (cost=0.00..2539.33 rows=500 width=32) Filter: (array_to_string(xpath('//person/@name'::text, person, '{}'::text[]), ''::text) = '0.441761581227183'::text) (2 rows) Time: 1.150 ms chris=# create index people_name_ix on people( (array_to_string(xpath('//person/@name', person), '')) ); CREATE INDEX Time: 8892.866 ms chris=# select * from people where array_to_string(xpath('//person/@name', person), '') = '0.441761581227183'; person ------------------------------------ <person name="0.441761581227183"/> (1 row) Time: 2.454 ms chris=# explain select * from people where array_to_string(xpath('//person/@name', person), '') = '0.441761581227183'; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on people (cost=16.15..753.25 rows=500 width=32) Recheck Cond: (array_to_string(xpath('//person/@name'::text, person, '{}'::text[]), ''::text) = '0.441761581227183'::text) -> Bitmap Index Scan on people_name_ix (cost=0.00..16.02 rows=500 width=0) Index Cond: (array_to_string(xpath('//person/@name'::text, person, '{}'::text[]), ''::text) = '0.441761581227183'::text) (4 rows) Time: 1.160 ms