PostgreSQL Error 2200S: Fix Invalid XML Comment
In this article
If you're seeing PostgreSQL error 2200S (SQLSTATE 2200S), it means PostgreSQL rejected an XML value because it contains an invalid XML comment. The fix is to locate the malformed comment in your XML data and either correct it or remove it. This guide walks you through the exact steps to diagnose and resolve the issue.
What You'll Need
- Access to the PostgreSQL database where the error occurs (psql, pgAdmin, or your application's database connection).
- Read permission on the table or query that produces the XML.
- If the XML comes from an application, you may need access to the application logs or the ability to run a test query.
Step-by-Step
- Confirm the exact error message. Run the query that fails and note the full error text. It will look something like:
ERROR: invalid XML commentwith SQLSTATE 2200S. The message may also include the offending XML fragment or a line number. - Identify where the XML is coming from. Check if the XML is stored in a column of type
xml, generated by a function likexmlelementorxmlagg, or passed as a parameter from your application. This determines where to look for the invalid comment. - Extract the problematic XML. If the XML is in a table, run a simple query to retrieve it:
SELECT xml_column FROM your_table WHERE ...;(add conditions to narrow down to the row that fails). If the XML is generated dynamically, run the underlying query with the same parameters to see the output. - Look for comment syntax errors. In XML, comments start with
<!--and end with-->. The most common invalid patterns are:- A comment that starts but never ends (missing
-->). - A comment that contains a double hyphen (
--) inside the comment text. For example,<!-- this -- is invalid -->is not allowed. - A comment that ends with a hyphen before the closing bracket, like
<!-- text -- -->. - Extra hyphens after the closing
-->, such as<!-- text -->--.
- A comment that starts but never ends (missing
- Use a text editor or command-line tool to inspect the XML. Copy the XML into a text editor that supports syntax highlighting, or use a command-line XML parser like
xmllint(if available) to pinpoint the error:echo 'your_xml' | xmllint --noout -. This will give you a line and column number for the invalid comment. - Fix the comment. Edit the XML to correct the comment syntax. For example, remove extra hyphens, ensure the comment is properly closed, or remove the comment entirely if it's not needed. If the XML is stored in a table, update the row:
UPDATE your_table SET xml_column = '<root><!-- valid comment --></root>' WHERE ...;(make sure to escape single quotes if needed). - If the XML is generated by a function, modify the function logic. For instance, if you're concatenating strings to build XML, ensure that comment delimiters are correctly placed. Consider using
xmlcomment()to create comments safely:xmlcomment('text')returns a valid XML comment. - Re-run your original query. After fixing the XML, execute the query again. If it succeeds, the issue is resolved.
Common Mistakes
- Assuming the error is in the database when it's in the application. If your application sends XML as a string, the error may appear only when PostgreSQL tries to parse it. Check the application's XML generation code.
- Overlooking hidden characters. Invisible characters like non-breaking spaces or control characters inside a comment can cause parsing failure. Use a hex editor or
od -cto inspect the XML bytes if the error persists. - Forgetting that
--is not allowed inside comments. This is the most common violation. Even if the comment looks fine visually, a double hyphen anywhere inside will trigger the error. - Using
-->inside a comment. The sequence-->is interpreted as the end of the comment, so if it appears in the comment text, it will prematurely close the comment and cause an error.
Tips
- Validate XML before inserting. Use a client-side XML parser to validate your XML before sending it to PostgreSQL. This saves database round-trips and makes debugging easier.
- Use PostgreSQL's XML functions. Instead of building XML by string concatenation, use functions like
xmlelement,xmlforest, andxmlcomment. They automatically handle escaping and comment formatting. - Test with a minimal example. If you can't find the error, create a small test table and insert a simplified version of your XML to see if the error reproduces. Then gradually add parts until it fails.
- Check the PostgreSQL version. While the error code is consistent, some versions may give more detailed error messages. If you're on an older version, consider upgrading to get better diagnostics.
Troubleshooting
If the error persists after fixing the comment:
- Verify the fix was actually applied. Re-run the SELECT to ensure the XML in the table is updated. If you're using a transaction, commit it.
- Check for multiple invalid comments. The error may point to the first one, but there could be others. Use a loop in a scripting language to parse the XML and list all comment errors.
- Look at the exact byte position. The error message sometimes includes a position. Use
substring(xml_column from position for length)to extract the surrounding characters and inspect them. - If the XML is generated by a view or function, the source may be in the database. Use
pg_get_functiondef()orpg_get_viewdef()to see the definition and check for string concatenation that might produce invalid comments. - Consider the possibility of a non-UTF8 encoding issue. If your database encoding is not UTF8 and the XML contains characters outside that encoding, PostgreSQL may raise a different error, but it's worth checking with
convert_to(xml, 'UTF8')to see if that fails.
Frequently Asked Questions
Can PostgreSQL accept comments in XML at all?
Yes, PostgreSQL fully supports XML comments as long as they follow the XML specification. Use <!-- comment --> with no double hyphens inside.
What does SQLSTATE 2200S mean exactly?
It's a class 22 (data exception) error. The specific code 2200S is defined as "invalid XML comment" in PostgreSQL's error codes.
How do I find which row has the invalid XML?
Run a query that selects only the XML column and use a WHERE clause to narrow down, or use a loop in a PL/pgSQL function to test each row individually. You can also use xml_is_well_formed(xml_column) to check each row: SELECT * FROM table WHERE NOT xml_is_well_formed(xml_column);
Can I disable XML validation in PostgreSQL?
No, PostgreSQL always validates XML when it is stored in an xml column or when using XML functions. You cannot disable it. The only option is to fix the XML data.
Does this error occur only with the xml data type?
It can also occur when using XML functions like xpath() or xmlparse() on text that is not valid XML. If you're storing XML as text, you may not get this error until you try to parse it.
What is the best way to generate XML in PostgreSQL?
Use the built-in XML construction functions (xmlelement, xmlagg, xmlforest) or the xmlcomment() function for comments. They automatically escape special characters and format comments correctly.