Rewrite tsqueryout() to use StringInfo to build the output string.

This patch started with noticing that tsquery.c's infix() function
uselessly multiplies the length of each operand string by
pg_database_encoding_max_length() + 1, where multiplying by 2
would be sufficient.  Unlike the related thinko in tsvectorout(),
this doesn't seem to risk integer overflow, since we're multiplying
only a rather short per-operand length.  Still, it's incorrect and
misleading.

However, I then started to question why tsqueryout() is using a
hand-rolled implementation of an expansible string buffer in the
first place.  Replacing that by using the StringInfo infrastructure
would make the code noticeably shorter and eliminate not only this
mistake but a bunch of other mistake-prone arithmetic.  It might
even be faster, given that we've spent effort on micro-optimizing
StringInfos; but even if it's slower, it's hard to visualize a
workload where tsqueryout() is a performance bottleneck.  So
that's what this patch does.

I also got rid of the overly-creative approach of deparsing a
binary operator's right operand into a separate buffer, then
deparsing the left operand into the main output buffer, then
copying the right operand's text back to the main buffer.
(Why the operands are stored in reverse order in the first place
seems lost in the mists of time, but I suppose we're stuck with
that choice.)  We can easily emit the desired text in-order by
moving the "curpol" next-item pointer around.

Author: Tom Lane <[email protected]>
Reviewed-by: Ayush Tiwari <[email protected]>
Discussion: https://postgr.es/m/[email protected]

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/2ecbc5e021be2cad14f837539b1661dd0c870c07

Modified Files
--------------
src/backend/utils/adt/tsquery.c | 151 ++++++++++++----------------------------
1 file changed, 45 insertions(+), 106 deletions(-)

Reply via email to