liveBook · Manning

Xml

As explained in PostgreSQL Mistakes and How to Avoid Them

XML is a text-based markup language and document format for representing structured data with elements, attributes, and declarations.

PostgreSQL supports the XML data type, but XML is usually a poor choice for application document storage. Its text-based representation combines low efficiency with difficult parsing and only marginal human readability. section 3.8

Common production problems include:

  • Requiring one root element makes concatenating XML fragments harder because content must be parsed and combined correctly. section 3.8
  • Adding namespaces creates collision-handling work, while numeric values can vary textually across locales. section 3.8
  • Supporting different escaping rules for text, attributes, comments, and CDATA increases the likelihood of malformed input. section 3.8
  • Parsing large XML documents can create memory-allocation problems, requiring a deliberate choice between DOM and SAX or streaming parsers. section 3.8
  • PostgreSQL does not validate XML against a DTD and does not support XML Schema or other XML schema languages. section 3.8

A storage-size warning

Casting a property document to XML and measuring it with pg_column_size reports 72 bytes, while the comparable '{"color":0}'::JSON value reports 15 bytes. The exact values depend on the represented content, but the listing makes the storage tradeoff concrete.

Use XML mainly when immutable XML must be copied into the database without querying or manipulating it. For queryable or mutable document data, prefer JSON or JSONB; for exact numeric values, use NUMERIC or another suitable numeric format. section 3.9 section 3.8

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Details format, parsing, memory, DTD, and schema limitations, but stops short of describing XML query syntax or PostgreSQL internals.
Examples
Compares XML and JSON values with PostgreSQL casts and pg_column_size using a small color property document.
What you can do after
Evaluate XML storage decisions and choose JSON(B) for queryable documents or NUMERIC for exact numeric values.
How it connects
Connects XML with JSON, JSONB, namespaces, DTD, XML Schema, DOM, and SAX or streaming parsers.

Also explained in

Compare how 5 books explain xml →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.