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@postgresql.org
Subject: pgsql: Fix strange behavior (and possible crashes) in full text phrase
Date: Wed, 21 Dec 2016 20:18:58 +0000
Message-ID: <E1cJnLm-0003Vi-Bz@gemulon.postgresql.org> (raw)
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-committers>

Fix strange behavior (and possible crashes) in full text phrase search.

In an attempt to simplify the tsquery matching engine, the original
phrase search patch invented rewrite rules that would rearrange a
tsquery so that no AND/OR/NOT operator appeared below a PHRASE operator.
But this approach had numerous problems.  The rearrangement step was
missed by ts_rewrite (and perhaps other places), allowing tsqueries
to be created that would cause Assert failures or perhaps crashes at
execution, as reported by Andreas Seltenreich.  The rewrite rules
effectively defined semantics for operators underneath PHRASE that were
buggy, or at least unintuitive.  And because rewriting was done in
tsqueryin() rather than at execution, the rearrangement was user-visible,
which is not very desirable --- for example, it might cause unexpected
matches or failures to match in ts_rewrite.

As a somewhat independent problem, the behavior of nested PHRASE operators
was only sane for left-deep trees; queries like "x <-> (y <-> z)" did not
behave intuitively at all.

To fix, get rid of the rewrite logic altogether, and instead teach the
tsquery execution engine to manage AND/OR/NOT below a PHRASE operator
by explicitly computing the match location(s) and match widths for these
operators.

This requires introducing some additional fields into the publicly visible
ExecPhraseData struct; but since there's no way for third-party code to
pass such a struct to TS_phrase_execute, it shouldn't create an ABI problem
as long as we don't move the offsets of the existing fields.

Another related problem was that index searches supposed that "!x <-> y"
could be lossily approximated as "!x & y", which isn't correct because
the latter will reject, say, "x q y" which the query itself accepts.
This required some tweaking in TS_execute_ternary along with the main
tsquery engine.

Back-patch to 9.6 where phrase operators were introduced.  While this
could be argued to change behavior more than we'd like in a stable branch,
we have to do something about the crash hazards and index-vs-seqscan
inconsistency, and it doesn't seem desirable to let the unintuitive
behaviors induced by the rewriting implementation stand as precedent.

Discussion: https://postgr.es/m/28215.1481999808@sss.pgh.pa.us
Discussion: https://postgr.es/m/26706.1482087250@sss.pgh.pa.us

Branch
------
REL9_6_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/4e2477b7b8b6c025d273a316852f2dbf62fff5bc

Modified Files
--------------
doc/src/sgml/datatype.sgml              |   8 -
doc/src/sgml/textsearch.sgml            |  26 +-
src/backend/utils/adt/tsginidx.c        |  28 +-
src/backend/utils/adt/tsgistidx.c       |  17 +-
src/backend/utils/adt/tsquery.c         |  25 +-
src/backend/utils/adt/tsquery_cleanup.c | 242 +---------------
src/backend/utils/adt/tsquery_op.c      |   4 +-
src/backend/utils/adt/tsvector_op.c     | 477 ++++++++++++++++++++++++--------
src/include/tsearch/ts_utils.h          |  33 ++-
src/test/regress/expected/tsdicts.out   |  12 +-
src/test/regress/expected/tsearch.out   |  40 ++-
src/test/regress/expected/tstypes.out   | 298 ++++++++++----------
src/test/regress/sql/tsearch.sql        |   6 +
src/test/regress/sql/tstypes.sql        |  56 ++--
14 files changed, 673 insertions(+), 599 deletions(-)


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers


view thread (2+ messages)

Message-ID: <E1cJnLm-0003Vi-Bz@gemulon.postgresql.org>
Permalink:  ../E1cJnLm-0003Vi-Bz@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1cJnLm-0003Vi-Bz@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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Fix strange behavior (and possible crashes) in full text phrase
  In-Reply-To: <E1cJnLm-0003Vi-Bz@gemulon.postgresql.org>
  References: <E1cJnLm-0003Vi-Bz@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