PostgreSQL XML Data Type
Summary: in this tutorial, you will learn how to use the PostgreSQL XML data type to store XML documents in the database.
Introduction to the PostgreSQL XML data type
PostgreSQL supports built-in XML
data type that allows you to store XML documents directly within the database.
Here’s the syntax for declaring a column with the XML
type:
The XML data type offers the following benefits:
- Type Safety: PostgreSQL can validate when inserting/updating data, ensuring XML data conforms to XML standards.
- Built-in XML functions and operators: PostgreSQL supports many XML functions and operators to manipulate XML data effectively.
PostgreSQL XML data type example
First, create a table called person
:
In this person
table:
id
is an identity column that serves as the primary key column of the table.info
is a column with the type XML that will store the XML data.
Second, insert a row into the person
table:
In this statement:
DOCUMENT
indicates that the input string is a complete XML document starting with the XML declaration<?xml version="1.0" encoding="UTF-8"?>
and having the root element<person>
XMLPARSE
function converts the string into an XML document.- The
INSERT
statement inserts the new XML document into the info column of thepersons
table.
Third, insert multiple rows into the person
table:
Fourth, retrieve the names of persons from the XML documents using xpath()
function:
Output:
Each row in the result set is an array of XML values representing person names. Since each person has one name, the result array has only one element.
Fourth, retrieve person names as text from the XML documents using xpath()
function:
Output:
How it works.
- First, the XPath
'/person/name/text()'
returns the text of the name node of the XML document. It returns an array that includes all matching values. - Second, the
[1]
subscript returns the first element of the array. - Third, the
::text
casts the XML value to the text.
Fifth, retrieve the ages of persons:
Output:
In this query:
- The xpath
/person/age/text()
returns the text of the age nodes as an array of text. - The
[1]
subscript returns the first element of the array. - The
::text
cast the element to the text. - The
::integer
casts the text to an integer.
In this example, we cast an XML value to text and text to an integer because we cannot cast an XML value directly to an integer.
Sixth, retrieve the name, age, and city from the XML document:
Output:
Seventh, find the person with the name “Jane Doe”:
Output:
Creating indexes for XML data
If the person table has many rows, finding the person by name will be slow. You can create an expression index for the XML documents to improve the query performance.
First, create an index expression that extracts the name of a person as an array of text:
Second, create a function that inserts 1000 rows into the person
table for testing purposes:
Third, call the generate_persons
to insert 1000 rows into the person
table:
Fifth, find a person with the name Jane Doe
:
Output:
The output indicates that the query utilizes the index expression of the person
table.
Summary
- Use the
XML
data type to store XML documents in the database. - Use the
xpath()
function to retrieve a value from XML documents.