How to fix 2200S: invalid_xml_comment in PostgreSQL

PostgreSQLINTERMEDIATEMEDIUMUpdated Dec 22, 2025ยท 107 views

PostgreSQL throws SQLSTATE 2200S (invalid_xml_comment) when processing XML data that contains malformed XML comments. This error occurs when using PostgreSQL's XML functions or when parsing XML data that includes comments with invalid syntax, such as containing double hyphens (--) or not being properly closed with -->.

Quick fix

Fixes 2200S

SELECT xml_is_well_formed_document('<!-- Valid comment --><root/>');
SELECT xml_is_well_formed_document('<!-- Invalid -- comment --><root/>');
  1. Identify the problematic XML comment
  2. Check for double hyphens in comments
  3. Ensure proper comment closure
  4. Fix or remove invalid comments
postgresql โ€” error
SELECT xml_is_well_formed_document('<!-- Valid comment --><root/>');
SELECT xml_is_well_formed_document('<!-- Invalid -- comment --><root/>');
2200S: invalid_xml_comment

What this error means

SQLSTATE 2200S falls under Class 22 (Data Exception) and specifically indicates an invalid XML comment. XML comments are text annotations that begin with <!-- and end with -->, used to add human-readable notes within XML documents. PostgreSQL validates these comments when using XML functions or when storing/processing XML data. This error typically occurs when: 1. XML data contains comments with double hyphens (--) inside the comment content 2. Comments are not properly closed with --> 3. Comment syntax violates XML specification rules 4. Using XML functions with data containing malformed comments XML comments must follow strict syntax rules: they must begin with <!--, end with -->, and cannot contain the -- character sequence within the content. PostgreSQL enforces these rules to ensure XML data integrity and prevent parsing ambiguities.

Typical symptoms

  • ERROR: 2200S: invalid_xml_comment when parsing XML data with xmlparse()
  • XML validation fails with invalid_xml_comment when inserting XML data into xml columns
  • Queries using XML functions fail with SQLSTATE 2200S
  • Application errors when processing XML data containing comments
  • Database operations fail when XML data includes malformed <!-- ... --> constructs

Common causes

  1. XML data contains comments with double hyphens (--) in the comment text
  2. Comments are missing the closing --> tag
  3. Comment syntax uses incorrect delimiters (e.g., <!--- or <!--->)
  4. XML data generated by external systems contains malformed comments
  5. Application code incorrectly constructs XML comments or fails to escape -- sequences

How to fix "2200S: invalid_xml_comment"

1Identify the problematic XML comment

First, locate the XML comment causing the error. Check your XML data for comment sections starting with <!-- and ending with -->. Look for:
1. Comments containing -- within the text
2. Comments missing the closing -->
3. Malformed comment syntax

You can use PostgreSQL's xml_is_well_formed_document() function to test your XML:

sql
SELECT xml_is_well_formed_document('<!-- Valid comment --><root/>');
SELECT xml_is_well_formed_document('<!-- Invalid -- comment --><root/>');
2Check for double hyphens in comments

XML comments cannot contain double hyphens (--) anywhere within the comment text. This is a strict XML specification rule. If your comments contain --, you must:
1. Remove the double hyphens
2. Replace with single hyphens or other characters
3. Use CDATA sections for content that needs --

Example of problematic comment:

xml
<!-- This is -- invalid -->

Corrected version:

xml
<!-- This is - invalid -->
3Ensure proper comment closure

Verify all comments are properly closed with -->. Common issues include:
1. Missing closing tag: <!-- Comment only
2. Extra hyphens: <!-- Comment --->
3. Wrong delimiter: <!-- Comment ->

Use an XML validator or text editor with XML syntax highlighting to identify unclosed comments. PostgreSQL's XML parser is strict about comment closure.

4Fix or remove invalid comments

Depending on your needs:
1. Remove unnecessary comments: If comments are not essential, strip them from your XML data
2. Fix syntax errors: Ensure all <!-- ... --> constructs are properly formed
3. Replace -- sequences: Change double hyphens to single hyphens or other separators
4. Use CDATA sections: For content that might contain problematic sequences

For application-generated XML, ensure your XML generation library properly handles comment syntax.

5Test with corrected XML

After making corrections, test your XML data:

sql
-- Test with valid comment
SELECT xmlparse(document '<!-- Single hyphen comment --><root/>');

-- Test with your actual data
SELECT xml_is_well_formed_document(your_xml_data);

-- If using xml columns
INSERT INTO xml_table (xml_data) VALUES ('<!-- Valid --><data/>');

Ensure all comments follow XML syntax rules before attempting to parse or store the data.

6Validate external XML data sources

If you're importing XML from external sources:
1. Implement XML validation before processing
2. Use PostgreSQL's xml_is_well_formed_document() as a guard
3. Consider stripping comments if they're not needed
4. Use XML transformation to fix comment syntax

Example validation pattern:

sql
WITH xml_input AS (
  SELECT '<!-- External data --><root/>' as xml_text
)
SELECT 
  CASE WHEN xml_is_well_formed_document(xml_text) 
    THEN xmlparse(document xml_text)
    ELSE NULL
  END as parsed_xml
FROM xml_input;

Advanced notes

XML comments are different from processing instructions and declarations. The XML specification (XML 1.0 and 1.1) strictly prohibits -- within comment content to prevent parsing ambiguities. PostgreSQL uses libxml2 for XML processing, which enforces this rule. When building XML dynamically, consider that comments are often unnecessary for machine processing and can be safely removed. If you need to preserve comment-like annotations, consider using XML attributes or processing instructions instead. Remember that xml_is_well_formed_document() and xml_is_well_formed_content() can help identify comment-related issues before they cause runtime errors.

Frequently asked questions

What does "2200S" mean?
SQLSTATE 2200S falls under Class 22 (Data Exception) and specifically indicates an invalid XML comment. -- and end with -->, used to add human-readable notes within XML documents. PostgreSQL validates these comments when using XML functions or when storing/processing XML data.
How do I fix "2200S"?
PostgreSQL throws SQLSTATE 2200S (invalid_xml_comment) when processing XML data that contains malformed XML comments. This error occurs when using PostgreSQL's XML functions or when parsing XML data that includes comments with invalid syntax, such as containing double hyphens (--) or not being properly closed with -->.
What causes "2200S"?
XML data contains comments with double hyphens (--) in the comment text Comments are missing the closing --> tag Comment syntax uses incorrect delimiters (e.g., <!--- or <!--->)
Is the "2200S" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading