PostgreSQL Error 2200T: Fix Invalid XML Processing Instruction
In this article
PostgreSQL error 2200T (SQLSTATE 2200T) occurs when you try to insert or process XML data that contains an invalid processing instruction. A processing instruction in XML is a special construct that starts with <? and ends with ?>, like <?xml version="1.0"?> or <?php echo "hi"; ?>. PostgreSQL rejects XML that has a processing instruction with a syntax error, an invalid target name, or an illegal character. The fix is to correct the XML before inserting it, or to sanitize the input so it conforms to the XML standard. This guide walks you through the exact steps to identify, fix, and prevent this error.
What You'll Need
- Access to a PostgreSQL database (local or remote) with a client tool like
psql, pgAdmin, or a programming language library (Python psycopg2, Java JDBC, etc.) - Basic familiarity with SQL and XML syntax
- The offending XML value or the query that triggers the error
- If you're using an application, the ability to modify its code or the data it sends
Step-by-Step
- Reproduce the error. Run the query that fails, or insert the XML value directly, to confirm the exact error message. For example:
SELECT XMLPARSE(DOCUMENT '<?xml version="1.0"?><root/>');– this should work. Now trySELECT XMLPARSE(DOCUMENT '<?xml version="1.0"?><root>');– this will fail with 2200T because the XML is malformed. - Isolate the XML value. If the error comes from a table, select the problematic column:
SELECT id, xml_column FROM my_table WHERE ...– find the row that fails. If it's from an application, capture the exact XML string being sent. - Validate the XML. Use an external XML validator (like xmllint or an online tool) to check the structure. The error 2200T specifically points to a processing instruction, so look for any
<?...?>in the XML. Common issues:- The target name (the word right after
<?) must be a valid XML name – no spaces, no digits at the start, no special characters except underscore, colon, hyphen, and period. - The processing instruction must end with
?>– a missing?or>causes the error. - The content between
<?and?>must not contain the sequence?>inside a quoted value. - The XML declaration (if present) must be at the very beginning of the document, with no leading whitespace.
- The target name (the word right after
- Fix the XML string. Edit the XML to correct the processing instruction. For example, change
<?xml version="1.0"?><root>to<?xml version="1.0"?><root/>(add the closing slash). If the processing instruction is not needed, remove it entirely. - Re-test the query. After fixing the XML, run the query again. If it works, you've solved the immediate issue.
- Sanitize application input. If the XML comes from user input or an external system, add validation in your application code. For example, in Python with lxml, use
etree.fromstring(xml_string)to parse and catchXMLSyntaxError. In Java, useDocumentBuilderFactoryand catchSAXParseException. Only send the XML to PostgreSQL after it passes validation. - Use a parameterized query. If you're building SQL dynamically, use prepared statements with parameters instead of concatenating strings. This prevents accidental malformation and also protects against SQL injection.
- Consider a CHECK constraint or trigger. To prevent bad XML from ever being stored, add a check constraint that validates the XML. For example:
ALTER TABLE my_table ADD CONSTRAINT xml_valid CHECK (xml_column IS NULL OR xml_column IS DOCUMENT);– this ensures the column contains a well-formed XML document. Note: this only checks well-formedness, not schema validity.
Common Mistakes
- Forgetting the XML declaration rules. The XML declaration
<?xml version="1.0"?>is optional, but if present, it must be the first thing in the document, with no spaces before it. Also, the version must be a valid value like "1.0" or "1.1". - Using a processing instruction for non-XML content. Some people try to embed PHP or other code in XML using
<?php ... ?>. This is only valid if the target is registered and the content is well-formed. For data storage, it's better to avoid processing instructions altogether. - Mismatched quotes in the instruction. For example,
<?xml version="1.0'?>– the quote style must match and close properly. - Not escaping special characters. If the XML contains an ampersand or less-than sign in text, it must be escaped as
&and<. This can cause a different error (2200N), but it's often confused with 2200T. - Assuming the error is from the whole document. The error might point to a specific line and column, but PostgreSQL sometimes gives a generic message. Always validate the entire XML, not just the part you suspect.
Tips
- Use a linter in your text editor or IDE that supports XML to catch syntax errors before you even run the query.
- If you're generating XML programmatically, use a library that builds the XML tree for you, rather than string concatenation. For example, in Python use
xml.etree.ElementTreeorlxml; in Java use JAXB or DOM. - When debugging, try to insert a minimal XML document that works, then add parts back until it fails. This pinpoints the exact problem.
- Remember that PostgreSQL's XML type stores the XML as a string internally, but it validates it on input. So the error always happens at insert/update time, not when you select the data.
- If you're using an ORM like Hibernate or SQLAlchemy, the ORM might be adding or modifying the XML. Check the generated SQL to see the actual value being sent.
Troubleshooting
If the error persists after fixing the XML: First, double-check that you've actually replaced the value in the database. If you're updating a table, run a SELECT to see the current value. If the column is defined as xml, PostgreSQL stores it in a canonical form, so any processing instruction must be valid from the start.
If the error comes from a function or trigger: The XML might be generated inside a function. Check the function's source code for string concatenation that could produce invalid XML. Use \df+ function_name in psql to see the definition.
If the error happens on a SELECT: This is rare, because the XML is already stored. It could happen if you're using an XML function like xpath() or xmltable() that re-parses the XML. In that case, the stored XML might be valid but the function's processing instruction handling could be different. Try casting to text and back: SELECT xml_column::text::xml FROM my_table; – if that fails, the stored XML is actually invalid (which shouldn't happen if the column type is xml, but can if you've altered the table or used a trigger).
If you're using a client library and the error is not 2200T but something else: Check the PostgreSQL documentation for the exact SQLSTATE. 2200T is specific to processing instructions. Other XML errors include 2200M (invalid comment), 2200N (invalid content), and 2200S (invalid character). The fix is similar – validate the XML.
If the XML is too large to debug easily: Use a tool like xmllint --format to pretty-print it, then inspect the processing instructions. Or write a small script that reads the XML and reports the position of the first invalid character.
Frequently Asked Questions
What exactly is a processing instruction in XML?
A processing instruction is a way to pass information to the application that parses the XML. It has the form <?target data?>. The target is a name that identifies the application, and the data is a string. The XML declaration itself is a special processing instruction.
Can I disable the XML validation in PostgreSQL?
No, PostgreSQL always validates XML when you insert it into an xml column or use XMLPARSE. You cannot turn it off. The only way to avoid the error is to provide valid XML.
Why does my XML work in other databases but not PostgreSQL?
PostgreSQL follows the XML standard strictly. Some other databases may be more lenient. For example, they might allow a processing instruction with a leading space or a missing closing bracket. PostgreSQL does not.
How do I fix a processing instruction that contains a question mark?
If your data contains the sequence ?> inside the instruction, you need to escape it. In XML, you can't escape it directly, so you should avoid using that sequence. If you must include a question mark, use a different representation, like a character reference ?, but that won't work inside a processing instruction because character references are not expanded there. The safest is to redesign your data to not use processing instructions for content that might contain ?>.
What is the difference between error 2200T and 2200N?
2200T is specifically for invalid processing instructions, while 2200N is for invalid XML content (like unescaped special characters). The fix for 2200T is to correct the processing instruction syntax; for 2200N, you need to escape or remove illegal characters.
Can I store XML with a processing instruction that is not the XML declaration?
Yes, PostgreSQL allows any well-formed processing instruction, as long as the target is a valid XML name. For example, <?myapp action="run"?> is valid. However, keep in mind that the XML declaration must be at the start if present.