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/tsginidx.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/tsginidx.c')
-rw-r--r-- | src/backend/utils/adt/tsginidx.c | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/backend/utils/adt/tsginidx.c b/src/backend/utils/adt/tsginidx.c index efc111e379c..3e0a44459ac 100644 --- a/src/backend/utils/adt/tsginidx.c +++ b/src/backend/utils/adt/tsginidx.c @@ -212,7 +212,7 @@ checkcondition_gin(void *checkval, QueryOperand *val, ExecPhraseData *data) * Evaluate tsquery boolean expression using ternary logic. */ static GinTernaryValue -TS_execute_ternary(GinChkVal *gcv, QueryItem *curitem) +TS_execute_ternary(GinChkVal *gcv, QueryItem *curitem, bool in_phrase) { GinTernaryValue val1, val2, @@ -230,7 +230,10 @@ TS_execute_ternary(GinChkVal *gcv, QueryItem *curitem) switch (curitem->qoperator.oper) { case OP_NOT: - result = TS_execute_ternary(gcv, curitem + 1); + /* In phrase search, always return MAYBE since we lack positions */ + if (in_phrase) + return GIN_MAYBE; + result = TS_execute_ternary(gcv, curitem + 1, in_phrase); if (result == GIN_MAYBE) return result; return !result; @@ -238,17 +241,21 @@ TS_execute_ternary(GinChkVal *gcv, QueryItem *curitem) case OP_PHRASE: /* - * GIN doesn't contain any information about positions, treat + * GIN doesn't contain any information about positions, so treat * OP_PHRASE as OP_AND with recheck requirement */ - *gcv->need_recheck = true; + *(gcv->need_recheck) = true; + /* Pass down in_phrase == true in case there's a NOT below */ + in_phrase = true; + /* FALL THRU */ case OP_AND: - val1 = TS_execute_ternary(gcv, curitem + curitem->qoperator.left); + val1 = TS_execute_ternary(gcv, curitem + curitem->qoperator.left, + in_phrase); if (val1 == GIN_FALSE) return GIN_FALSE; - val2 = TS_execute_ternary(gcv, curitem + 1); + val2 = TS_execute_ternary(gcv, curitem + 1, in_phrase); if (val2 == GIN_FALSE) return GIN_FALSE; if (val1 == GIN_TRUE && val2 == GIN_TRUE) @@ -257,10 +264,11 @@ TS_execute_ternary(GinChkVal *gcv, QueryItem *curitem) return GIN_MAYBE; case OP_OR: - val1 = TS_execute_ternary(gcv, curitem + curitem->qoperator.left); + val1 = TS_execute_ternary(gcv, curitem + curitem->qoperator.left, + in_phrase); if (val1 == GIN_TRUE) return GIN_TRUE; - val2 = TS_execute_ternary(gcv, curitem + 1); + val2 = TS_execute_ternary(gcv, curitem + 1, in_phrase); if (val2 == GIN_TRUE) return GIN_TRUE; if (val1 == GIN_FALSE && val2 == GIN_FALSE) @@ -307,7 +315,7 @@ gin_tsquery_consistent(PG_FUNCTION_ARGS) res = TS_execute(GETQUERY(query), &gcv, - TS_EXEC_CALC_NOT | TS_EXEC_PHRASE_AS_AND, + TS_EXEC_CALC_NOT | TS_EXEC_PHRASE_NO_POS, checkcondition_gin); } @@ -343,7 +351,7 @@ gin_tsquery_triconsistent(PG_FUNCTION_ARGS) gcv.map_item_operand = (int *) (extra_data[0]); gcv.need_recheck = &recheck; - res = TS_execute_ternary(&gcv, GETQUERY(query)); + res = TS_execute_ternary(&gcv, GETQUERY(query), false); if (res == GIN_TRUE && recheck) res = GIN_MAYBE; |