diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2016-12-21 15:18:25 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2016-12-21 15:18:39 -0500 |
commit | 89fcea1ace40bc025beea2758a80bcd56a319a6f (patch) | |
tree | 5ebfb4979c8eb3c4052eebae98d26017ec72042e /src/backend/utils/adt/tsquery.c | |
parent | 2d1018ca56f5ddaf0bfb5b4d0133283f3e823301 (diff) | |
download | postgresql-89fcea1ace40bc025beea2758a80bcd56a319a6f.tar.gz postgresql-89fcea1ace40bc025beea2758a80bcd56a319a6f.zip |
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
Diffstat (limited to 'src/backend/utils/adt/tsquery.c')
-rw-r--r-- | src/backend/utils/adt/tsquery.c | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/src/backend/utils/adt/tsquery.c b/src/backend/utils/adt/tsquery.c index 3d11a1c2080..f0bd52877f3 100644 --- a/src/backend/utils/adt/tsquery.c +++ b/src/backend/utils/adt/tsquery.c @@ -557,13 +557,11 @@ findoprnd_recurse(QueryItem *ptr, uint32 *pos, int nnodes, bool *needcleanup) curitem->oper == OP_OR || curitem->oper == OP_PHRASE); - if (curitem->oper == OP_PHRASE) - *needcleanup = true; /* push OP_PHRASE down later */ - (*pos)++; /* process RIGHT argument */ findoprnd_recurse(ptr, pos, nnodes, needcleanup); + curitem->left = *pos - tmp; /* set LEFT arg's offset */ /* process LEFT argument */ @@ -574,8 +572,9 @@ findoprnd_recurse(QueryItem *ptr, uint32 *pos, int nnodes, bool *needcleanup) /* - * Fills in the left-fields previously left unfilled. The input - * QueryItems must be in polish (prefix) notation. + * Fill in the left-fields previously left unfilled. + * The input QueryItems must be in polish (prefix) notation. + * Also, set *needcleanup to true if there are any QI_VALSTOP nodes. */ static void findoprnd(QueryItem *ptr, int size, bool *needcleanup) @@ -687,15 +686,17 @@ parse_tsquery(char *buf, memcpy((void *) GETOPERAND(query), (void *) state.op, state.sumlen); pfree(state.op); - /* Set left operand pointers for every operator. */ + /* + * Set left operand pointers for every operator. While we're at it, + * detect whether there are any QI_VALSTOP nodes. + */ findoprnd(ptr, query->size, &needcleanup); /* - * QI_VALSTOP nodes should be cleaned and OP_PHRASE should be pushed - * down + * If there are QI_VALSTOP nodes, delete them and simplify the tree. */ if (needcleanup) - return cleanup_fakeval_and_phrase(query); + query = cleanup_tsquery_stopwords(query); return query; } @@ -1088,6 +1089,9 @@ tsqueryrecv(PG_FUNCTION_ARGS) */ findoprnd(item, size, &needcleanup); + /* Can't have found any QI_VALSTOP nodes */ + Assert(!needcleanup); + /* Copy operands to output struct */ for (i = 0; i < size; i++) { @@ -1105,9 +1109,6 @@ tsqueryrecv(PG_FUNCTION_ARGS) SET_VARSIZE(query, len + datalen); - if (needcleanup) - PG_RETURN_TSQUERY(cleanup_fakeval_and_phrase(query)); - PG_RETURN_TSQUERY(query); } |