mirror of
https://github.com/postgres/postgres.git
synced 2025-07-26 01:22:12 +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:
@ -113,8 +113,8 @@ extern text *generateHeadline(HeadlineParsedText *prs);
|
||||
* struct ExecPhraseData is passed to a TSExecuteCallback function if we need
|
||||
* lexeme position data (because of a phrase-match operator in the tsquery).
|
||||
* The callback should fill in position data when it returns true (success).
|
||||
* If it cannot return position data, it may ignore its "data" argument, but
|
||||
* then the caller of TS_execute() must pass the TS_EXEC_PHRASE_AS_AND flag
|
||||
* If it cannot return position data, it may leave "data" unchanged, but
|
||||
* then the caller of TS_execute() must pass the TS_EXEC_PHRASE_NO_POS flag
|
||||
* and must arrange for a later recheck with position data available.
|
||||
*
|
||||
* The reported lexeme positions must be sorted and unique. Callers must only
|
||||
@ -123,13 +123,21 @@ extern text *generateHeadline(HeadlineParsedText *prs);
|
||||
* portion of a tsvector value. If "allocated" is true then the pos array
|
||||
* is palloc'd workspace and caller may free it when done.
|
||||
*
|
||||
* "negate" means that the pos array contains positions where the query does
|
||||
* not match, rather than positions where it does. "width" is positive when
|
||||
* the match is wider than one lexeme. Neither of these fields normally need
|
||||
* to be touched by TSExecuteCallback functions; they are used for
|
||||
* phrase-search processing within TS_execute.
|
||||
*
|
||||
* All fields of the ExecPhraseData struct are initially zeroed by caller.
|
||||
*/
|
||||
typedef struct ExecPhraseData
|
||||
{
|
||||
int npos; /* number of positions reported */
|
||||
bool allocated; /* pos points to palloc'd data? */
|
||||
bool negate; /* positions are where query is NOT matched */
|
||||
WordEntryPos *pos; /* ordered, non-duplicate lexeme positions */
|
||||
int width; /* width of match in lexemes, less 1 */
|
||||
} ExecPhraseData;
|
||||
|
||||
/*
|
||||
@ -139,7 +147,9 @@ typedef struct ExecPhraseData
|
||||
* val: lexeme to test for presence of
|
||||
* data: to be filled with lexeme positions; NULL if position data not needed
|
||||
*
|
||||
* Return TRUE if lexeme is present in data, else FALSE
|
||||
* Return TRUE if lexeme is present in data, else FALSE. If data is not
|
||||
* NULL, it should be filled with lexeme positions, but function can leave
|
||||
* it as zeroes if position data is not available.
|
||||
*/
|
||||
typedef bool (*TSExecuteCallback) (void *arg, QueryOperand *val,
|
||||
ExecPhraseData *data);
|
||||
@ -151,15 +161,18 @@ typedef bool (*TSExecuteCallback) (void *arg, QueryOperand *val,
|
||||
/*
|
||||
* If TS_EXEC_CALC_NOT is not set, then NOT expressions are automatically
|
||||
* evaluated to be true. Useful in cases where NOT cannot be accurately
|
||||
* computed (GiST) or it isn't important (ranking).
|
||||
* computed (GiST) or it isn't important (ranking). From TS_execute's
|
||||
* perspective, !CALC_NOT means that the TSExecuteCallback function might
|
||||
* return false-positive indications of a lexeme's presence.
|
||||
*/
|
||||
#define TS_EXEC_CALC_NOT (0x01)
|
||||
/*
|
||||
* Treat OP_PHRASE as OP_AND. Used when positional information is not
|
||||
* accessible, like in consistent methods of GIN/GiST indexes; rechecking
|
||||
* must occur later.
|
||||
* If TS_EXEC_PHRASE_NO_POS is set, allow OP_PHRASE to be executed lossily
|
||||
* in the absence of position information: a TRUE result indicates that the
|
||||
* phrase might be present. Without this flag, OP_PHRASE always returns
|
||||
* false if lexeme position information is not available.
|
||||
*/
|
||||
#define TS_EXEC_PHRASE_AS_AND (0x02)
|
||||
#define TS_EXEC_PHRASE_NO_POS (0x02)
|
||||
|
||||
extern bool TS_execute(QueryItem *curitem, void *arg, uint32 flags,
|
||||
TSExecuteCallback chkcond);
|
||||
@ -228,7 +241,7 @@ extern Datum gin_tsquery_consistent_oldsig(PG_FUNCTION_ARGS);
|
||||
* TSQuery Utilities
|
||||
*/
|
||||
extern QueryItem *clean_NOT(QueryItem *ptr, int32 *len);
|
||||
extern TSQuery cleanup_fakeval_and_phrase(TSQuery in);
|
||||
extern TSQuery cleanup_tsquery_stopwords(TSQuery in);
|
||||
|
||||
typedef struct QTNode
|
||||
{
|
||||
|
Reference in New Issue
Block a user