Thread: pgsql: Fix minor deficiencies in XMLTABLE, xpath(), xmlexists()

pgsql: Fix minor deficiencies in XMLTABLE, xpath(), xmlexists()

From
Alvaro Herrera
Date:
Fix minor deficiencies in XMLTABLE, xpath(), xmlexists()

Correctly process nodes of more types than previously.  In some cases,
nodes were being ignored (nothing was output); in other cases, trying to
return them resulted in errors about unrecognized nodes.  In yet other
cases, necessary escaping (of XML special characters) was not being
done.  Fix all those (as far as the authors could find) and add
regression tests cases verifying the new behavior.

I (Álvaro) was of two minds about backpatching these changes.  They do
seem bugfixes that would benefit most users of the affected functions;
but on the other hand it would change established behavior in minor
releases, so it seems prudent not to.

Authors: Pavel Stehule, Markus Winand, Chapman Flack
Discussion:
   https://postgr.es/m/CAFj8pRA6J25CtAZ2TuRvxK3gat7-bBUYh0rfE2yM7Hj9GD14Dg@mail.gmail.com
   https://postgr.es/m/8BDB0627-2105-4564-AA76-7849F028B96E@winand.at

The elephant in the room as pointed out by Chapman Flack, not fixed in
this commit, is that we still have XMLTABLE operating on XPath 1.0
instead of the standard-mandated XQuery (or even its subset XPath 2.0).
Fixing that is a major undertaking, however.

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/251cf2e27bec98274e8bb002608680bdc211319e

Modified Files
--------------
doc/src/sgml/func.sgml              |   6 +-
src/backend/utils/adt/xml.c         | 155 ++++++++++++++++++++----------------
src/test/regress/expected/xml.out   |  27 ++++++-
src/test/regress/expected/xml_1.out |  19 +++++
src/test/regress/expected/xml_2.out |  27 ++++++-
src/test/regress/sql/xml.sql        |   8 ++
6 files changed, 164 insertions(+), 78 deletions(-)


Re: pgsql: Fix minor deficiencies in XMLTABLE, xpath(), xmlexists()

From
Thomas Munro
Date:
On Fri, Mar 8, 2019 at 10:20 AM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
> Fix minor deficiencies in XMLTABLE, xpath(), xmlexists()

This seems to have caused lapwing and grison to crash:

     xml                          ... FAILED (test process exited with
exit code 2)    15590 ms

Program terminated with signal 11, Segmentation fault.
#0  0x2a52a291 in free () from /lib/i386-linux-gnu/libc.so.6
#0  0x2a52a291 in free () from /lib/i386-linux-gnu/libc.so.6
#1  0x2a9327ec in xmlFreeNode () from /usr/lib/i386-linux-gnu/libxml2.so.2
#2  0x08538fec in xml_xmlnodetoxmltype (cur=<optimized out>,
xmlerrcxt=0x9f5a198) at xml.c:3751
#3  0x08539488 in XmlTableGetValue (state=0x9f28454, colnum=1,
typid=142, typmod=-1, isnull=0xffbc1e93) at xml.c:4540
#4  0x082ebf81 in tfuncLoadRows (econtext=0x9f28560, tstate=0x9f28454)
at nodeTableFuncscan.c:489
#5  tfuncFetchRows (tstate=tstate@entry=0x9f28454, econtext=0x9f28560)
at nodeTableFuncscan.c:318
#6  0x082ec282 in TableFuncNext (node=node@entry=0x9f28454) at
nodeTableFuncscan.c:65
#7  0x082b70e1 in ExecScanFetch (accessMtd=0x82ec230 <TableFuncNext>,
node=0x9f28454, recheckMtd=<optimized out>) at execScan.c:93
#8  ExecScanFetch (recheckMtd=0x82ebb50 <TableFuncRecheck>,
accessMtd=0x82ec230 <TableFuncNext>, node=0x9f28454) at execScan.c:119
#9  ExecScan (node=node@entry=0x9f28454,
accessMtd=accessMtd@entry=0x82ec230 <TableFuncNext>,
recheckMtd=recheckMtd@entry=0x82ebb50 <TableFuncRecheck>) at
execScan.c:143
#10 0x082ec2b8 in ExecTableFuncScan (pstate=0x9f28454) at
nodeTableFuncscan.c:101
#11 0x082ad5b7 in ExecProcNode (node=0x9f28454) at
../../../src/include/executor/executor.h:241
#12 ExecutePlan (execute_once=<optimized out>, dest=0x9f2cfbc,
direction=ForwardScanDirection, numberTuples=<optimized out>,
sendTuples=true, operation=CMD_SELECT, use_parallel_mode=<optimized
out>, planstate=0x9f28454, estate=0x9f28320) at execMain.c:1643
#13 standard_ExecutorRun (queryDesc=0x9ed7140,
direction=ForwardScanDirection, count=0, execute_once=true) at
execMain.c:362
#14 0x0842d443 in PortalRunSelect (portal=0x9e84e90,
portal@entry=0x9f2cfbc, forward=forward@entry=true, count=0,
count@entry=2147483647, dest=dest@entry=0x9f2cfbc) at pquery.c:929
#15 0x0842e9e8 in PortalRun (portal=portal@entry=0x9e84e90,
count=count@entry=2147483647, isTopLevel=isTopLevel@entry=true,
run_once=run_once@entry=true, dest=dest@entry=0x9f2cfbc,
altdest=altdest@entry=0x9f2cfbc,
completionTag=completionTag@entry=0xffbc2200 "") at pquery.c:770
#16 0x08429fd3 in exec_simple_query
(query_string=query_string@entry=0x9e36798 "SELECT * FROM XMLTABLE('*'
PASSING '<e>pre<!--c1--><?pi
arg?><![CDATA[&ent1]]><n2>&deep</n2>post</e>' COLUMNS x xml PATH
'node()', y xml PATH '/');") at postgres.c:1215
#17 0x0842aa79 in PostgresMain (argc=1, argv=argv@entry=0x9e5e868,
dbname=0x9e5e6cc "regression", username=0x9e5e6b0 "postgres") at
postgres.c:4256
#18 0x080c52b7 in BackendRun (port=<optimized out>) at postmaster.c:4399
#19 BackendStartup (port=0x9e5a2c0) at postmaster.c:4090
#20 ServerLoop () at postmaster.c:1703
#21 0x083a4dab in PostmasterMain (argc=argc@entry=8,
argv=argv@entry=0x9e324d8) at postmaster.c:1376
#22 0x080c6fd2 in main (argc=8, argv=0x9e324d8) at main.c:228

-- 
Thomas Munro
https://enterprisedb.com


Re: pgsql: Fix minor deficiencies in XMLTABLE, xpath(), xmlexists()

From
Thomas Munro
Date:
On Fri, Mar 8, 2019 at 9:14 PM Thomas Munro <thomas.munro@gmail.com> wrote:
> On Fri, Mar 8, 2019 at 10:20 AM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
> > Fix minor deficiencies in XMLTABLE, xpath(), xmlexists()
>
> This seems to have caused lapwing and grison to crash:

Oh, sorry for the noise, I see this was already discussed elsewhere.


--
Thomas Munro
https://enterprisedb.com