pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Rewrite tsqueryout() to use StringInfo to build the output strin
Date: Thu, 20 Aug 2026 15:36:24 +0000
Message-ID: <E1wx4oZ-00000001NUK-3isn@gemulon.postgresql.org> (raw)

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 <tgl@sss.pgh.pa.us>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/3428771.1786635967@sss.pgh.pa.us

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(-)



Message-ID: <E1wx4oZ-00000001NUK-3isn@gemulon.postgresql.org>
Permalink:  ../E1wx4oZ-00000001NUK-3isn@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1wx4oZ-00000001NUK-3isn@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Rewrite tsqueryout() to use StringInfo to build the output strin
  In-Reply-To: <E1wx4oZ-00000001NUK-3isn@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox