1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-11 20:28:21 +03:00

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
This commit is contained in:
Tom Lane
2016-12-21 15:18:25 -05:00
parent 2d1018ca56
commit 89fcea1ace
14 changed files with 678 additions and 606 deletions

View File

@ -3959,15 +3959,7 @@ SELECT 'fat &amp; rat &amp; ! cat'::tsquery;
tsquery
------------------------
'fat' &amp; 'rat' &amp; !'cat'
SELECT '(fat | rat) &lt;-&gt; cat'::tsquery;
tsquery
-----------------------------------
'fat' &lt;-&gt; 'cat' | 'rat' &lt;-&gt; 'cat'
</programlisting>
The last example demonstrates that <type>tsquery</type> sometimes
rearranges nested operators into a logically equivalent formulation.
</para>
<para>

View File

@ -264,7 +264,7 @@ SELECT 'fat &amp; cow'::tsquery @@ 'a fat cat sat on a mat and ate a fat rat'::t
text, any more than a <type>tsvector</type> is. A <type>tsquery</type>
contains search terms, which must be already-normalized lexemes, and
may combine multiple terms using AND, OR, NOT, and FOLLOWED BY operators.
(For details see <xref linkend="datatype-tsquery">.) There are
(For syntax details see <xref linkend="datatype-tsquery">.) There are
functions <function>to_tsquery</>, <function>plainto_tsquery</>,
and <function>phraseto_tsquery</>
that are helpful in converting user-written text into a proper
@ -323,6 +323,8 @@ text @@ text
at least one of its arguments must appear, while the <literal>!</> (NOT)
operator specifies that its argument must <emphasis>not</> appear in
order to have a match.
For example, the query <literal>fat &amp; ! rat</> matches documents that
contain <literal>fat</> but not <literal>rat</>.
</para>
<para>
@ -377,6 +379,28 @@ SELECT phraseto_tsquery('the cats ate the rats');
then <literal>&amp;</literal>, then <literal>&lt;-&gt;</literal>,
and <literal>!</literal> most tightly.
</para>
<para>
It's worth noticing that the AND/OR/NOT operators mean something subtly
different when they are within the arguments of a FOLLOWED BY operator
than when they are not, because within FOLLOWED BY the exact position of
the match is significant. For example, normally <literal>!x</> matches
only documents that do not contain <literal>x</> anywhere.
But <literal>!x &lt;-&gt; y</> matches <literal>y</> if it is not
immediately after an <literal>x</>; an occurrence of <literal>x</>
elsewhere in the document does not prevent a match. Another example is
that <literal>x &amp; y</> normally only requires that <literal>x</>
and <literal>y</> both appear somewhere in the document, but
<literal>(x &amp; y) &lt;-&gt; z</> requires <literal>x</>
and <literal>y</> to match at the same place, immediately before
a <literal>z</>. Thus this query behaves differently from
<literal>x &lt;-&gt; z &amp; y &lt;-&gt; z</>, which will match a
document containing two separate sequences <literal>x z</> and
<literal>y z</>. (This specific query is useless as written,
since <literal>x</> and <literal>y</> could not match at the same place;
but with more complex situations such as prefix-match patterns, a query
of this form could be useful.)
</para>
</sect2>
<sect2 id="textsearch-intro-configurations">