Review
XMLTABLE: treat domains over xml like xml columns (v1)
- Proposal version
- 1
- Base commit
- c074737f687f604b64f6ee830fccbbe854719823
Summary
v1 is a three-line fix plus a regression test: XmlTableGetValue()
now calls getBaseType() on the column type before its two
typid == XMLOID tests, so an XMLTABLE column declared with a domain
over xml gets the same treatment as a plain xml column.
The fix is correct and I found no functional problem with it. The bug is somewhat worse than the proposal describes (see "Motivation"), which matters for the back-patch decision the thread has not yet discussed. Remaining points are minor: a docs touch-up, a thin commit message/test with respect to the worse symptoms, and an optional change to avoid a per-value syscache lookup.
Reviewed on master at c074737f687f604b64f6ee830fccbbe854719823.
What was tested
- Applies cleanly with
git am;git diff --checkis clean. - Builds with meson, cassert and libxml enabled.
- Core regression suite passes with libxml (243 subtests).
- Core regression suite passes with libxml disabled. The xml test exits early in that configuration, so xml_1.out correctly needs no update.
- The new test's query, run on an unpatched build, fails with "more than one value returned by column XPath expression", so the test does cover the fix.
- xml.c compiles without warnings (other files were not checked for warnings).
- Compared unpatched and patched release builds with a script covering single and multiple nodes, text nodes containing escaped markup, XPath string/boolean/number results, a domain over a domain with a CHECK constraint, a NOT NULL domain with an empty node set, DEFAULT, implicit path, a view (deparse and re-execution), a domain over text, and an xml[] column.
Motivation: the existing behavior is more than cosmetic
The proposal shows a node-set being reduced to its string value. Two further consequences of the same misclassification exist on unpatched master, because the string-valued result is passed unescaped to the domain's input function, which is xml_in:
create domain xmldom as xml;
select * from xmltable('/r'
passing '<r><a><b>z</b></a></r>'
columns c1 xml path 'a/text()', c2 xmldom path 'a/text()',
c3 xml path 'string(a)', c4 xmldom path 'string(a)');
c1 | c2 | c3 | c4
----------------------+----------+----------------------+----------
<b>z</b> | <b>z</b> | <b>z</b> | <b>z</b>
Character data in the source document turns into an element in the result: the xml value in c2/c4 is not the value the XPath expression selected. This affects the XPATH_STRING branch too, not only the node-set branch.
select * from xmltable('/r'
passing '<r><a>1 < 2 & 3</a></r>'
columns c2 xmldom path 'a/text()');
ERROR: invalid XML content
and a path matching several nodes raises "more than one value returned by column XPath expression" for the domain column while the xml column returns the concatenation.
With the patch all of these match the plain xml column. I would mention the escaping problem in the commit message, since it is the strongest argument that this is a bug rather than a behavior choice.
Correctness
- getBaseType() is the right tool: it handles domains over domains (verified with a two-level domain) and is a no-op for non-domains.
- Overwriting the
typidparameter is safe. After the change it is used only for the two XMLOID tests and for get_type_category_preferred(). The value is still converted with state->in_functions[colnum] / typioparams[colnum], which nodeTableFuncscan.c set up from the declared column type, so domain_in still runs and domain constraints are still enforced (verified: a CHECK (VALUE IS DOCUMENT) domain rejects a two-node result and a text-node result, and accepts a single element). - The claim that the boolean branch already looks through domains holds: a domain's typcategory is copied from its base type, so passing the base type there changes nothing.
- A NOT NULL domain with an empty node set, DEFAULT, and implicit path behave the same as before. xml[] and domains over non-xml types are unaffected.
- The call is placed before PG_TRY and before the libxml error handler is installed, which is fine; it cannot leave libxml state behind if the syscache lookup errors out.
- The other XMLOID tests in xml.c are either already preceded by getBaseType() (map_sql_value_to_xml_value) or operate on types where domains do not arise. I did not find another instance of the same mistake in the XMLTABLE path.
Compatibility and back-patching
The thread does not say whether this is meant for master only. The behavior has existed since XMLTABLE was added, so every supported branch is affected.
- Results change for existing queries and views that use a domain over xml as an XMLTABLE column type: previously the string value, now the serialized node(s). A view definition deparses the same before and after; only its output changes.
- Given the unescaped-text behavior above, I think treating this as a bug and back-patching is defensible, but it is a visible change in a minor release and should be decided explicitly on the list. If back-patched it deserves a release-note entry.
- The patch applies unchanged to REL_17_STABLE through REL_19_STABLE. On REL_14_STABLE through REL_16_STABLE it does not apply as-is (I checked only that it fails to apply, not what adjustment is needed; the code change itself is trivially portable).
Performance
getBaseType() is a syscache lookup, and XmlTableGetValue() runs once per column per row, including for columns that are not xml at all. Measured on release builds, 200k rows x 6 columns (5 int, 1 text) from one document, 8 executions per run, three runs each:
run 1 run 2 run 3
unpatched 365 ms 367 ms 376 ms
patched 376 ms 375 ms 376 ms
So at most roughly 2-3%, and within noise in one of the three runs. That is 1.2 million extra lookups for about 10 ms. I do not consider this a blocker.
If someone objects, the lookup can be done once per scan instead of once per value, for example by having the XML table builder record per column whether the base type is xml when the scan is initialized (the scan's tuple descriptor is available from the TableFuncScanState), or lazily on first use per column. That is more code than the present patch, and for a back-patch the three-line version is preferable.
Documentation
func-xml.sgml describes these rules in terms of "the column's PostgreSQL type is xml" / "a PostgreSQL type other than xml". With the patch a reader can reasonably take that to include domains, so no change is strictly needed, but a few words such as "(or a domain over xml)" at the first mention would make the behavior explicit. The patch has no doc change.
Tests
The added test covers the node-set branch (two nodes, which would error without the fix) and the XPATH_STRING branch with characters that need escaping. Suggestions, none essential:
- Use a text node whose content would parse as markup or fail to
parse without the fix (as in the examples above); the current
string(a[2])case does cover escaping, so this is mainly about making the intent obvious in a comment. - Add a domain with a constraint, to pin down that the domain's input function still runs after the type is flattened.
The test creates and drops its domain within the xml test, which is tidy. No alternate expected file needs updating.
Style and commit message
Code and comment follow project style. The commit message is accurate; it lacks a Discussion: link and, if back-patched, a "Backpatch-through" line, both of which the committer would add. I would add a sentence about the unescaped string value.
Discussion state
At the time of review the thread consists of the original posting only; there are no replies, no objections, and no CommitFest discussion to weigh.
Overall
A small, correct fix for a real bug, at the right place and of proportionate size. Ready for a committer's attention once the back-patch question is settled; the docs mention and test additions are optional polish.