diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c
index 8893be5682..447e72b21e 100644
--- a/src/backend/utils/adt/xml.c
+++ b/src/backend/utils/adt/xml.c
@@ -2114,6 +2114,19 @@ xml_errorHandler(void *data, PgXmlErrorPtr error)
switch (domain)
{
case XML_FROM_PARSER:
+
+ /*
+ * XML_ERR_NOT_WELL_BALANCED is typically reported after some
+ * other, more on-point error. Furthermore, libxml2 2.13 reports
+ * it under a completely different set of rules than prior
+ * versions. To avoid cross-version behavioral differences,
+ * suppress it so long as we already logged some error.
+ */
+ if (error->code == XML_ERR_NOT_WELL_BALANCED &&
+ xmlerrcxt->err_occurred)
+ return;
+ /* fall through */
+
case XML_FROM_NONE:
case XML_FROM_MEMORY:
case XML_FROM_IO:
diff --git a/src/test/regress/expected/xml.out b/src/test/regress/expected/xml.out
index 6500cff885..d6a51f9e38 100644
--- a/src/test/regress/expected/xml.out
+++ b/src/test/regress/expected/xml.out
@@ -254,17 +254,11 @@ ERROR: invalid XML content
DETAIL: line 1: xmlParseEntityRef: no name
&
^
-line 1: chunk is not well balanced
-&
- ^
SELECT xmlparse(content '&idontexist;');
ERROR: invalid XML content
DETAIL: line 1: Entity 'idontexist' not defined
&idontexist;
^
-line 1: chunk is not well balanced
-&idontexist;
- ^
SELECT xmlparse(content '');
xmlparse
---------------------------
@@ -283,9 +277,6 @@ DETAIL: line 1: Entity 'idontexist' not defined
&idontexist;
^
line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
-&idontexist;
- ^
-line 1: chunk is not well balanced
&idontexist;
^
SELECT xmlparse(content '');
@@ -294,6 +285,19 @@ SELECT xmlparse(content '');
(1 row)
+SELECT xmlparse(content '');
+ERROR: invalid XML content
+DETAIL: line 1: Premature end of data in tag unclosed line 1
+
+ ^
+SELECT xmlparse(content '');
+ERROR: invalid XML content
+DETAIL: line 1: Opening and ending tag mismatch: child line 1 and parent
+
+ ^
+line 1: Opening and ending tag mismatch: parent line 1 and child
+
+ ^
SELECT xmlparse(document ' ');
ERROR: invalid XML document
DETAIL: line 1: Start tag expected, '<' not found
@@ -352,6 +356,19 @@ SELECT xmlparse(document '');
(1 row)
+SELECT xmlparse(document '');
+ERROR: invalid XML document
+DETAIL: line 1: Premature end of data in tag unclosed line 1
+
+ ^
+SELECT xmlparse(document '');
+ERROR: invalid XML document
+DETAIL: line 1: Opening and ending tag mismatch: child line 1 and parent
+
+ ^
+line 1: Opening and ending tag mismatch: parent line 1 and child
+
+ ^
SELECT xmlpi(name foo);
xmlpi
---------
diff --git a/src/test/regress/expected/xml_1.out b/src/test/regress/expected/xml_1.out
index 9323b84ae2..d3f2bdfc14 100644
--- a/src/test/regress/expected/xml_1.out
+++ b/src/test/regress/expected/xml_1.out
@@ -180,6 +180,12 @@ DETAIL: This functionality requires the server to be built with libxml support.
SELECT xmlparse(content '');
ERROR: unsupported XML feature
DETAIL: This functionality requires the server to be built with libxml support.
+SELECT xmlparse(content '');
+ERROR: unsupported XML feature
+DETAIL: This functionality requires the server to be built with libxml support.
+SELECT xmlparse(content '');
+ERROR: unsupported XML feature
+DETAIL: This functionality requires the server to be built with libxml support.
SELECT xmlparse(document ' ');
ERROR: unsupported XML feature
DETAIL: This functionality requires the server to be built with libxml support.
@@ -207,6 +213,12 @@ DETAIL: This functionality requires the server to be built with libxml support.
SELECT xmlparse(document '');
ERROR: unsupported XML feature
DETAIL: This functionality requires the server to be built with libxml support.
+SELECT xmlparse(document '');
+ERROR: unsupported XML feature
+DETAIL: This functionality requires the server to be built with libxml support.
+SELECT xmlparse(document '');
+ERROR: unsupported XML feature
+DETAIL: This functionality requires the server to be built with libxml support.
SELECT xmlpi(name foo);
ERROR: unsupported XML feature
DETAIL: This functionality requires the server to be built with libxml support.
diff --git a/src/test/regress/expected/xml_2.out b/src/test/regress/expected/xml_2.out
index e1d165c6c9..376b7bd015 100644
--- a/src/test/regress/expected/xml_2.out
+++ b/src/test/regress/expected/xml_2.out
@@ -250,13 +250,11 @@ ERROR: invalid XML content
DETAIL: line 1: xmlParseEntityRef: no name
&
^
-line 1: chunk is not well balanced
SELECT xmlparse(content '&idontexist;');
ERROR: invalid XML content
DETAIL: line 1: Entity 'idontexist' not defined
&idontexist;
^
-line 1: chunk is not well balanced
SELECT xmlparse(content '');
xmlparse
---------------------------
@@ -275,13 +273,25 @@ DETAIL: line 1: Entity 'idontexist' not defined
&idontexist;
^
line 1: Opening and ending tag mismatch: twoerrors line 1 and unbalanced
-line 1: chunk is not well balanced
SELECT xmlparse(content '');
xmlparse
---------------------
(1 row)
+SELECT xmlparse(content '');
+ERROR: invalid XML content
+DETAIL: line 1: Premature end of data in tag unclosed line 1
+
+ ^
+SELECT xmlparse(content '');
+ERROR: invalid XML content
+DETAIL: line 1: Opening and ending tag mismatch: child line 1 and parent
+
+ ^
+line 1: Opening and ending tag mismatch: parent line 1 and child
+
+ ^
SELECT xmlparse(document ' ');
ERROR: invalid XML document
DETAIL: line 1: Start tag expected, '<' not found
@@ -332,6 +342,19 @@ SELECT xmlparse(document '');
(1 row)
+SELECT xmlparse(document '');
+ERROR: invalid XML document
+DETAIL: line 1: Premature end of data in tag unclosed line 1
+
+ ^
+SELECT xmlparse(document '');
+ERROR: invalid XML document
+DETAIL: line 1: Opening and ending tag mismatch: child line 1 and parent
+
+ ^
+line 1: Opening and ending tag mismatch: parent line 1 and child
+
+ ^
SELECT xmlpi(name foo);
xmlpi
---------
diff --git a/src/test/regress/sql/xml.sql b/src/test/regress/sql/xml.sql
index 953bac09e4..15ccbe1d35 100644
--- a/src/test/regress/sql/xml.sql
+++ b/src/test/regress/sql/xml.sql
@@ -77,6 +77,8 @@ SELECT xmlparse(content '');
SELECT xmlparse(content '');
SELECT xmlparse(content '&idontexist;');
SELECT xmlparse(content '');
+SELECT xmlparse(content '');
+SELECT xmlparse(content '');
SELECT xmlparse(document ' ');
SELECT xmlparse(document 'abc');
@@ -87,6 +89,8 @@ SELECT xmlparse(document '');
SELECT xmlparse(document '');
SELECT xmlparse(document '&idontexist;');
SELECT xmlparse(document '');
+SELECT xmlparse(document '');
+SELECT xmlparse(document '');
SELECT xmlpi(name foo);