1 /*-------------------------------------------------------------------------
4 * Selectivity estimation functions for text search operators.
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
10 * $PostgreSQL: pgsql/src/backend/tsearch/ts_selfuncs.c,v 1.7.6.1 2010/07/31 03:27:48 tgl Exp $
12 *-------------------------------------------------------------------------
16 #include "catalog/pg_statistic.h"
17 #include "catalog/pg_type.h"
18 #include "miscadmin.h"
19 #include "nodes/nodes.h"
20 #include "tsearch/ts_type.h"
21 #include "utils/lsyscache.h"
22 #include "utils/selfuncs.h"
23 #include "utils/syscache.h"
27 * The default text search selectivity is chosen to be small enough to
28 * encourage indexscans for typical table densities. See selfuncs.h and
29 * DEFAULT_EQ_SEL for details.
31 #define DEFAULT_TS_MATCH_SEL 0.005
33 /* lookup table type for binary searching through MCELEMs */
40 /* type of keys for bsearch'ing through an array of TextFreqs */
47 static Selectivity tsquerysel(VariableStatData *vardata, Datum constval);
48 static Selectivity mcelem_tsquery_selec(TSQuery query,
49 Datum *mcelem, int nmcelem,
50 float4 *numbers, int nnumbers);
51 static Selectivity tsquery_opr_selec(QueryItem *item, char *operand,
52 TextFreq *lookup, int length, float4 minfreq);
53 static int compare_lexeme_textfreq(const void *e1, const void *e2);
55 #define tsquery_opr_selec_no_stats(query) \
56 tsquery_opr_selec(GETQUERY(query), GETOPERAND(query), NULL, 0, 0)
60 * tsmatchsel -- Selectivity of "@@"
62 * restriction selectivity function for tsvector @@ tsquery and
66 tsmatchsel(PG_FUNCTION_ARGS)
68 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
71 Oid operator = PG_GETARG_OID(1);
73 List *args = (List *) PG_GETARG_POINTER(2);
74 int varRelid = PG_GETARG_INT32(3);
75 VariableStatData vardata;
81 * If expression is not variable = something or something = variable, then
82 * punt and return a default estimate.
84 if (!get_restriction_variable(root, args, varRelid,
85 &vardata, &other, &varonleft))
86 PG_RETURN_FLOAT8(DEFAULT_TS_MATCH_SEL);
89 * Can't do anything useful if the something is not a constant, either.
91 if (!IsA(other, Const))
93 ReleaseVariableStats(vardata);
94 PG_RETURN_FLOAT8(DEFAULT_TS_MATCH_SEL);
98 * The "@@" operator is strict, so we can cope with NULL right away
100 if (((Const *) other)->constisnull)
102 ReleaseVariableStats(vardata);
103 PG_RETURN_FLOAT8(0.0);
107 * OK, there's a Var and a Const we're dealing with here. We need the
108 * Const to be a TSQuery, else we can't do anything useful. We have to
109 * check this because the Var might be the TSQuery not the TSVector.
111 if (((Const *) other)->consttype == TSQUERYOID)
113 /* tsvector @@ tsquery or the other way around */
114 Assert(vardata.vartype == TSVECTOROID);
116 selec = tsquerysel(&vardata, ((Const *) other)->constvalue);
120 /* If we can't see the query structure, must punt */
121 selec = DEFAULT_TS_MATCH_SEL;
124 ReleaseVariableStats(vardata);
126 CLAMP_PROBABILITY(selec);
128 PG_RETURN_FLOAT8((float8) selec);
133 * tsmatchjoinsel -- join selectivity of "@@"
135 * join selectivity function for tsvector @@ tsquery and tsquery @@ tsvector
138 tsmatchjoinsel(PG_FUNCTION_ARGS)
140 /* for the moment we just punt */
141 PG_RETURN_FLOAT8(DEFAULT_TS_MATCH_SEL);
146 * @@ selectivity for tsvector var vs tsquery constant
149 tsquerysel(VariableStatData *vardata, Datum constval)
154 /* The caller made sure the const is a TSQuery, so get it now */
155 query = DatumGetTSQuery(constval);
157 /* Empty query matches nothing */
158 if (query->size == 0)
159 return (Selectivity) 0.0;
161 if (HeapTupleIsValid(vardata->statsTuple))
163 Form_pg_statistic stats;
169 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
171 /* MCELEM will be an array of TEXT elements for a tsvector column */
172 if (get_attstatsslot(vardata->statsTuple,
174 STATISTIC_KIND_MCELEM, InvalidOid,
177 &numbers, &nnumbers))
180 * There is a most-common-elements slot for the tsvector Var, so
183 selec = mcelem_tsquery_selec(query, values, nvalues,
185 free_attstatsslot(TEXTOID, values, nvalues, numbers, nnumbers);
189 /* No most-common-elements info, so do without */
190 selec = tsquery_opr_selec_no_stats(query);
195 /* No stats at all, so do without */
196 selec = tsquery_opr_selec_no_stats(query);
203 * Extract data from the pg_statistic arrays into useful format.
206 mcelem_tsquery_selec(TSQuery query, Datum *mcelem, int nmcelem,
207 float4 *numbers, int nnumbers)
215 * There should be two more Numbers than Values, because the last two
216 * cells are taken for minimal and maximal frequency. Punt if not.
218 if (nnumbers != nmcelem + 2)
219 return tsquery_opr_selec_no_stats(query);
222 * Transpose the data into a single array so we can use bsearch().
224 lookup = (TextFreq *) palloc(sizeof(TextFreq) * nmcelem);
225 for (i = 0; i < nmcelem; i++)
228 * The text Datums came from an array, so it cannot be compressed or
229 * stored out-of-line -- it's safe to use VARSIZE_ANY*.
231 Assert(!VARATT_IS_COMPRESSED(mcelem[i]) && !VARATT_IS_EXTERNAL(mcelem[i]));
232 lookup[i].element = (text *) DatumGetPointer(mcelem[i]);
233 lookup[i].frequency = numbers[i];
237 * Grab the lowest frequency. compute_tsvector_stats() stored it for us in
238 * the one before the last cell of the Numbers array. See ts_typanalyze.c
240 minfreq = numbers[nnumbers - 2];
242 selec = tsquery_opr_selec(GETQUERY(query), GETOPERAND(query), lookup,
251 * Traverse the tsquery in preorder, calculating selectivity as:
253 * selec(left_oper) * selec(right_oper) in AND nodes,
255 * selec(left_oper) + selec(right_oper) -
256 * selec(left_oper) * selec(right_oper) in OR nodes,
258 * 1 - select(oper) in NOT nodes
260 * freq[val] in VAL nodes, if the value is in MCELEM
261 * min(freq[MCELEM]) / 2 in VAL nodes, if it is not
263 * The MCELEM array is already sorted (see ts_typanalyze.c), so we can use
264 * binary search for determining freq[MCELEM].
266 * If we don't have stats for the tsvector, we still use this logic,
267 * except we always use DEFAULT_TS_MATCH_SEL for VAL nodes. This case
268 * is signaled by lookup == NULL.
271 tsquery_opr_selec(QueryItem *item, char *operand,
272 TextFreq *lookup, int length, float4 minfreq)
280 /* since this function recurses, it could be driven to stack overflow */
283 if (item->type == QI_VAL)
285 QueryOperand *oper = (QueryOperand *) item;
287 /* If no stats for the variable, use DEFAULT_TS_MATCH_SEL */
289 return (Selectivity) DEFAULT_TS_MATCH_SEL;
292 * Prepare the key for bsearch().
294 key.lexeme = operand + oper->distance;
295 key.length = oper->length;
297 searchres = (TextFreq *) bsearch(&key, lookup, length,
299 compare_lexeme_textfreq);
304 * The element is in MCELEM. Return precise selectivity (or at
305 * least as precise as ANALYZE could find out).
307 return (Selectivity) searchres->frequency;
312 * The element is not in MCELEM. Punt, but assume that the
313 * selectivity cannot be more than minfreq / 2.
315 return (Selectivity) Min(DEFAULT_TS_MATCH_SEL, minfreq / 2);
319 /* Current TSQuery node is an operator */
320 switch (item->qoperator.oper)
323 selec = 1.0 - tsquery_opr_selec(item + 1, operand,
324 lookup, length, minfreq);
328 s1 = tsquery_opr_selec(item + 1, operand,
329 lookup, length, minfreq);
330 s2 = tsquery_opr_selec(item + item->qoperator.left, operand,
331 lookup, length, minfreq);
336 s1 = tsquery_opr_selec(item + 1, operand,
337 lookup, length, minfreq);
338 s2 = tsquery_opr_selec(item + item->qoperator.left, operand,
339 lookup, length, minfreq);
340 selec = s1 + s2 - s1 * s2;
344 elog(ERROR, "unrecognized operator: %d", item->qoperator.oper);
345 selec = 0; /* keep compiler quiet */
349 /* Clamp intermediate results to stay sane despite roundoff error */
350 CLAMP_PROBABILITY(selec);
356 * bsearch() comparator for a lexeme (non-NULL terminated string with length)
357 * and a TextFreq. Use length, then byte-for-byte comparison, because that's
358 * how ANALYZE code sorted data before storing it in a statistic tuple.
359 * See ts_typanalyze.c for details.
362 compare_lexeme_textfreq(const void *e1, const void *e2)
364 const LexemeKey *key = (const LexemeKey *) e1;
365 const TextFreq *t = (const TextFreq *) e2;
370 len2 = VARSIZE_ANY_EXHDR(t->element);
372 /* Compare lengths first, possibly avoiding a strncmp call */
375 else if (len1 < len2)
378 /* Fall back on byte-for-byte comparison */
379 return strncmp(key->lexeme, VARDATA_ANY(t->element), len1);