PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ts_selfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "nodes/nodes.h"
#include "tsearch/ts_type.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
#include "utils/syscache.h"
Include dependency graph for ts_selfuncs.c:

Go to the source code of this file.

Data Structures

struct  TextFreq
 
struct  LexemeKey
 

Macros

#define DEFAULT_TS_MATCH_SEL   0.005
 
#define tsquery_opr_selec_no_stats(query)   tsquery_opr_selec(GETQUERY(query), GETOPERAND(query), NULL, 0, 0)
 

Functions

static Selectivity tsquerysel (VariableStatData *vardata, Datum constval)
 
static Selectivity mcelem_tsquery_selec (TSQuery query, Datum *mcelem, int nmcelem, float4 *numbers, int nnumbers)
 
static Selectivity tsquery_opr_selec (QueryItem *item, char *operand, TextFreq *lookup, int length, float4 minfreq)
 
static int compare_lexeme_textfreq (const void *e1, const void *e2)
 
Datum tsmatchsel (PG_FUNCTION_ARGS)
 
Datum tsmatchjoinsel (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

#define DEFAULT_TS_MATCH_SEL   0.005

Definition at line 33 of file ts_selfuncs.c.

Referenced by tsmatchjoinsel(), tsmatchsel(), and tsquery_opr_selec().

#define tsquery_opr_selec_no_stats (   query)    tsquery_opr_selec(GETQUERY(query), GETOPERAND(query), NULL, 0, 0)

Definition at line 57 of file ts_selfuncs.c.

Referenced by mcelem_tsquery_selec(), and tsquerysel().

Function Documentation

static int compare_lexeme_textfreq ( const void *  e1,
const void *  e2 
)
static

Definition at line 441 of file ts_selfuncs.c.

References TextFreq::element, LexemeKey::length, LexemeKey::lexeme, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by tsquery_opr_selec().

442 {
443  const LexemeKey *key = (const LexemeKey *) e1;
444  const TextFreq *t = (const TextFreq *) e2;
445  int len1,
446  len2;
447 
448  len1 = key->length;
449  len2 = VARSIZE_ANY_EXHDR(t->element);
450 
451  /* Compare lengths first, possibly avoiding a strncmp call */
452  if (len1 > len2)
453  return 1;
454  else if (len1 < len2)
455  return -1;
456 
457  /* Fall back on byte-for-byte comparison */
458  return strncmp(key->lexeme, VARDATA_ANY(t->element), len1);
459 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
char * lexeme
Definition: ts_selfuncs.c:45
int length
Definition: ts_selfuncs.c:46
text * element
Definition: ts_selfuncs.c:38
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
static Selectivity mcelem_tsquery_selec ( TSQuery  query,
Datum mcelem,
int  nmcelem,
float4 numbers,
int  nnumbers 
)
static

Definition at line 214 of file ts_selfuncs.c.

References Assert, DatumGetPointer, TextFreq::element, TextFreq::frequency, GETOPERAND, GETQUERY, i, palloc(), pfree(), tsquery_opr_selec(), tsquery_opr_selec_no_stats, VARATT_IS_COMPRESSED, and VARATT_IS_EXTERNAL.

Referenced by tsquerysel().

216 {
217  float4 minfreq;
218  TextFreq *lookup;
219  Selectivity selec;
220  int i;
221 
222  /*
223  * There should be two more Numbers than Values, because the last two
224  * cells are taken for minimal and maximal frequency. Punt if not.
225  *
226  * (Note: the MCELEM statistics slot definition allows for a third extra
227  * number containing the frequency of nulls, but we're not expecting that
228  * to appear for a tsvector column.)
229  */
230  if (nnumbers != nmcelem + 2)
231  return tsquery_opr_selec_no_stats(query);
232 
233  /*
234  * Transpose the data into a single array so we can use bsearch().
235  */
236  lookup = (TextFreq *) palloc(sizeof(TextFreq) * nmcelem);
237  for (i = 0; i < nmcelem; i++)
238  {
239  /*
240  * The text Datums came from an array, so it cannot be compressed or
241  * stored out-of-line -- it's safe to use VARSIZE_ANY*.
242  */
243  Assert(!VARATT_IS_COMPRESSED(mcelem[i]) && !VARATT_IS_EXTERNAL(mcelem[i]));
244  lookup[i].element = (text *) DatumGetPointer(mcelem[i]);
245  lookup[i].frequency = numbers[i];
246  }
247 
248  /*
249  * Grab the lowest frequency. compute_tsvector_stats() stored it for us in
250  * the one before the last cell of the Numbers array. See ts_typanalyze.c
251  */
252  minfreq = numbers[nnumbers - 2];
253 
254  selec = tsquery_opr_selec(GETQUERY(query), GETOPERAND(query), lookup,
255  nmcelem, minfreq);
256 
257  pfree(lookup);
258 
259  return selec;
260 }
static Selectivity tsquery_opr_selec(QueryItem *item, char *operand, TextFreq *lookup, int length, float4 minfreq)
Definition: ts_selfuncs.c:285
#define VARATT_IS_COMPRESSED(PTR)
Definition: postgres.h:315
double Selectivity
Definition: nodes.h:632
#define GETQUERY(x)
Definition: _int.h:142
#define GETOPERAND(x)
Definition: ltree.h:118
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:316
void pfree(void *pointer)
Definition: mcxt.c:992
float4 frequency
Definition: ts_selfuncs.c:39
Definition: zic.c:287
float float4
Definition: c.h:377
text * element
Definition: ts_selfuncs.c:38
#define Assert(condition)
Definition: c.h:671
#define DatumGetPointer(X)
Definition: postgres.h:557
void * palloc(Size size)
Definition: mcxt.c:891
int i
Definition: c.h:435
#define tsquery_opr_selec_no_stats(query)
Definition: ts_selfuncs.c:57
Datum tsmatchjoinsel ( PG_FUNCTION_ARGS  )

Definition at line 140 of file ts_selfuncs.c.

References DEFAULT_TS_MATCH_SEL, and PG_RETURN_FLOAT8.

141 {
142  /* for the moment we just punt */
144 }
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:310
#define DEFAULT_TS_MATCH_SEL
Definition: ts_selfuncs.c:33
Datum tsmatchsel ( PG_FUNCTION_ARGS  )

Definition at line 68 of file ts_selfuncs.c.

References generate_unaccent_rules::args, Assert, CLAMP_PROBABILITY, DEFAULT_TS_MATCH_SEL, get_restriction_variable(), IsA, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_FLOAT8, ReleaseVariableStats, TSQUERYOID, tsquerysel(), TSVECTOROID, and VariableStatData::vartype.

69 {
71 
72 #ifdef NOT_USED
73  Oid operator = PG_GETARG_OID(1);
74 #endif
75  List *args = (List *) PG_GETARG_POINTER(2);
76  int varRelid = PG_GETARG_INT32(3);
77  VariableStatData vardata;
78  Node *other;
79  bool varonleft;
80  Selectivity selec;
81 
82  /*
83  * If expression is not variable = something or something = variable, then
84  * punt and return a default estimate.
85  */
86  if (!get_restriction_variable(root, args, varRelid,
87  &vardata, &other, &varonleft))
89 
90  /*
91  * Can't do anything useful if the something is not a constant, either.
92  */
93  if (!IsA(other, Const))
94  {
95  ReleaseVariableStats(vardata);
97  }
98 
99  /*
100  * The "@@" operator is strict, so we can cope with NULL right away
101  */
102  if (((Const *) other)->constisnull)
103  {
104  ReleaseVariableStats(vardata);
105  PG_RETURN_FLOAT8(0.0);
106  }
107 
108  /*
109  * OK, there's a Var and a Const we're dealing with here. We need the
110  * Const to be a TSQuery, else we can't do anything useful. We have to
111  * check this because the Var might be the TSQuery not the TSVector.
112  */
113  if (((Const *) other)->consttype == TSQUERYOID)
114  {
115  /* tsvector @@ tsquery or the other way around */
116  Assert(vardata.vartype == TSVECTOROID);
117 
118  selec = tsquerysel(&vardata, ((Const *) other)->constvalue);
119  }
120  else
121  {
122  /* If we can't see the query structure, must punt */
123  selec = DEFAULT_TS_MATCH_SEL;
124  }
125 
126  ReleaseVariableStats(vardata);
127 
128  CLAMP_PROBABILITY(selec);
129 
130  PG_RETURN_FLOAT8((float8) selec);
131 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:225
#define TSQUERYOID
Definition: pg_type.h:609
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition: selfuncs.c:4272
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:310
static Selectivity tsquerysel(VariableStatData *vardata, Datum constval)
Definition: ts_selfuncs.c:151
Definition: nodes.h:509
#define DEFAULT_TS_MATCH_SEL
Definition: ts_selfuncs.c:33
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
double Selectivity
Definition: nodes.h:632
unsigned int Oid
Definition: postgres_ext.h:31
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
double float8
Definition: c.h:378
#define PG_GETARG_OID(n)
Definition: fmgr.h:231
#define Assert(condition)
Definition: c.h:671
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:80
#define TSVECTOROID
Definition: pg_type.h:603
Definition: pg_list.h:45
static Selectivity tsquery_opr_selec ( QueryItem item,
char *  operand,
TextFreq lookup,
int  length,
float4  minfreq 
)
static

Definition at line 285 of file ts_selfuncs.c.

References check_stack_depth(), CLAMP_PROBABILITY, compare_lexeme_textfreq(), DEFAULT_TS_MATCH_SEL, QueryOperand::distance, TextFreq::element, elog, ERROR, TextFreq::frequency, i, QueryOperator::left, LexemeKey::length, QueryOperand::length, length(), LexemeKey::lexeme, Max, Min, NULL, OP_AND, OP_NOT, OP_OR, OP_PHRASE, QueryOperator::oper, oper(), QueryOperand::prefix, QI_VAL, QueryItem::qoperator, s1, s2, QueryItem::type, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by mcelem_tsquery_selec().

287 {
288  Selectivity selec;
289 
290  /* since this function recurses, it could be driven to stack overflow */
292 
293  if (item->type == QI_VAL)
294  {
295  QueryOperand *oper = (QueryOperand *) item;
296  LexemeKey key;
297 
298  /*
299  * Prepare the key for bsearch().
300  */
301  key.lexeme = operand + oper->distance;
302  key.length = oper->length;
303 
304  if (oper->prefix)
305  {
306  /* Prefix match, ie the query item is lexeme:* */
307  Selectivity matched,
308  allmces;
309  int i,
310  n_matched;
311 
312  /*
313  * Our strategy is to scan through the MCELEM list and combine the
314  * frequencies of the ones that match the prefix. We then
315  * extrapolate the fraction of matching MCELEMs to the remaining
316  * rows, assuming that the MCELEMs are representative of the whole
317  * lexeme population in this respect. (Compare
318  * histogram_selectivity().) Note that these are most common
319  * elements not most common values, so they're not mutually
320  * exclusive. We treat occurrences as independent events.
321  *
322  * This is only a good plan if we have a pretty fair number of
323  * MCELEMs available; we set the threshold at 100. If no stats or
324  * insufficient stats, arbitrarily use DEFAULT_TS_MATCH_SEL*4.
325  */
326  if (lookup == NULL || length < 100)
327  return (Selectivity) (DEFAULT_TS_MATCH_SEL * 4);
328 
329  matched = allmces = 0;
330  n_matched = 0;
331  for (i = 0; i < length; i++)
332  {
333  TextFreq *t = lookup + i;
334  int tlen = VARSIZE_ANY_EXHDR(t->element);
335 
336  if (tlen >= key.length &&
337  strncmp(key.lexeme, VARDATA_ANY(t->element),
338  key.length) == 0)
339  {
340  matched += t->frequency - matched * t->frequency;
341  n_matched++;
342  }
343  allmces += t->frequency - allmces * t->frequency;
344  }
345 
346  /* Clamp to ensure sanity in the face of roundoff error */
347  CLAMP_PROBABILITY(matched);
348  CLAMP_PROBABILITY(allmces);
349 
350  selec = matched + (1.0 - allmces) * ((double) n_matched / length);
351 
352  /*
353  * In any case, never believe that a prefix match has selectivity
354  * less than we would assign for a non-MCELEM lexeme. This
355  * preserves the property that "word:*" should be estimated to
356  * match at least as many rows as "word" would be.
357  */
358  selec = Max(Min(DEFAULT_TS_MATCH_SEL, minfreq / 2), selec);
359  }
360  else
361  {
362  /* Regular exact lexeme match */
363  TextFreq *searchres;
364 
365  /* If no stats for the variable, use DEFAULT_TS_MATCH_SEL */
366  if (lookup == NULL)
368 
369  searchres = (TextFreq *) bsearch(&key, lookup, length,
370  sizeof(TextFreq),
372 
373  if (searchres)
374  {
375  /*
376  * The element is in MCELEM. Return precise selectivity (or
377  * at least as precise as ANALYZE could find out).
378  */
379  selec = searchres->frequency;
380  }
381  else
382  {
383  /*
384  * The element is not in MCELEM. Punt, but assume that the
385  * selectivity cannot be more than minfreq / 2.
386  */
387  selec = Min(DEFAULT_TS_MATCH_SEL, minfreq / 2);
388  }
389  }
390  }
391  else
392  {
393  /* Current TSQuery node is an operator */
394  Selectivity s1,
395  s2;
396 
397  switch (item->qoperator.oper)
398  {
399  case OP_NOT:
400  selec = 1.0 - tsquery_opr_selec(item + 1, operand,
401  lookup, length, minfreq);
402  break;
403 
404  case OP_PHRASE:
405  case OP_AND:
406  s1 = tsquery_opr_selec(item + 1, operand,
407  lookup, length, minfreq);
408  s2 = tsquery_opr_selec(item + item->qoperator.left, operand,
409  lookup, length, minfreq);
410  selec = s1 * s2;
411  break;
412 
413  case OP_OR:
414  s1 = tsquery_opr_selec(item + 1, operand,
415  lookup, length, minfreq);
416  s2 = tsquery_opr_selec(item + item->qoperator.left, operand,
417  lookup, length, minfreq);
418  selec = s1 + s2 - s1 * s2;
419  break;
420 
421  default:
422  elog(ERROR, "unrecognized operator: %d", item->qoperator.oper);
423  selec = 0; /* keep compiler quiet */
424  break;
425  }
426  }
427 
428  /* Clamp intermediate results to stay sane despite roundoff error */
429  CLAMP_PROBABILITY(selec);
430 
431  return selec;
432 }
QueryOperator qoperator
Definition: ts_type.h:196
static Selectivity tsquery_opr_selec(QueryItem *item, char *operand, TextFreq *lookup, int length, float4 minfreq)
Definition: ts_selfuncs.c:285
int length(const List *list)
Definition: list.c:1271
#define VARDATA_ANY(PTR)
Definition: postgres.h:349
char * lexeme
Definition: ts_selfuncs.c:45
#define Min(x, y)
Definition: c.h:802
#define DEFAULT_TS_MATCH_SEL
Definition: ts_selfuncs.c:33
#define QI_VAL
Definition: ts_type.h:134
double Selectivity
Definition: nodes.h:632
uint32 distance
Definition: ts_type.h:158
#define OP_OR
Definition: ts_type.h:168
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:57
static int compare_lexeme_textfreq(const void *e1, const void *e2)
Definition: ts_selfuncs.c:441
#define OP_AND
Definition: ts_type.h:167
#define ERROR
Definition: elog.h:43
char * s1
float4 frequency
Definition: ts_selfuncs.c:39
void check_stack_depth(void)
Definition: postgres.c:3096
int length
Definition: ts_selfuncs.c:46
QueryItemType type
Definition: ts_type.h:195
char * s2
text * element
Definition: ts_selfuncs.c:38
#define Max(x, y)
Definition: c.h:796
#define NULL
Definition: c.h:226
#define OP_PHRASE
Definition: ts_type.h:169
uint32 length
Definition: ts_type.h:158
uint32 left
Definition: ts_type.h:184
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:342
int i
bool prefix
Definition: ts_type.h:150
#define elog
Definition: elog.h:219
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:375
#define OP_NOT
Definition: ts_type.h:166
static Selectivity tsquerysel ( VariableStatData vardata,
Datum  constval 
)
static

Definition at line 151 of file ts_selfuncs.c.

References DatumGetTSQuery, free_attstatsslot(), get_attstatsslot(), GETSTRUCT, HeapTupleIsValid, InvalidOid, mcelem_tsquery_selec(), NULL, TSQueryData::size, STATISTIC_KIND_MCELEM, VariableStatData::statsTuple, TEXTOID, tsquery_opr_selec_no_stats, and values.

Referenced by tsmatchsel().

152 {
153  Selectivity selec;
154  TSQuery query;
155 
156  /* The caller made sure the const is a TSQuery, so get it now */
157  query = DatumGetTSQuery(constval);
158 
159  /* Empty query matches nothing */
160  if (query->size == 0)
161  return (Selectivity) 0.0;
162 
163  if (HeapTupleIsValid(vardata->statsTuple))
164  {
165  Form_pg_statistic stats;
166  Datum *values;
167  int nvalues;
168  float4 *numbers;
169  int nnumbers;
170 
171  stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
172 
173  /* MCELEM will be an array of TEXT elements for a tsvector column */
174  if (get_attstatsslot(vardata->statsTuple,
175  TEXTOID, -1,
177  NULL,
178  &values, &nvalues,
179  &numbers, &nnumbers))
180  {
181  /*
182  * There is a most-common-elements slot for the tsvector Var, so
183  * use that.
184  */
185  selec = mcelem_tsquery_selec(query, values, nvalues,
186  numbers, nnumbers);
187  free_attstatsslot(TEXTOID, values, nvalues, numbers, nnumbers);
188  }
189  else
190  {
191  /* No most-common-elements info, so do without */
192  selec = tsquery_opr_selec_no_stats(query);
193  }
194 
195  /*
196  * MCE stats count only non-null rows, so adjust for null rows.
197  */
198  selec *= (1.0 - stats->stanullfrac);
199  }
200  else
201  {
202  /* No stats at all, so do without */
203  selec = tsquery_opr_selec_no_stats(query);
204  /* we assume no nulls here, so no stanullfrac correction */
205  }
206 
207  return selec;
208 }
#define DatumGetTSQuery(X)
Definition: ts_type.h:235
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
HeapTuple statsTuple
Definition: selfuncs.h:71
#define TEXTOID
Definition: pg_type.h:324
double Selectivity
Definition: nodes.h:632
bool get_attstatsslot(HeapTuple statstuple, Oid atttype, int32 atttypmod, int reqkind, Oid reqop, Oid *actualop, Datum **values, int *nvalues, float4 **numbers, int *nnumbers)
Definition: lsyscache.c:2854
FormData_pg_statistic * Form_pg_statistic
Definition: pg_statistic.h:129
float float4
Definition: c.h:377
uintptr_t Datum
Definition: postgres.h:374
static Selectivity mcelem_tsquery_selec(TSQuery query, Datum *mcelem, int nmcelem, float4 *numbers, int nnumbers)
Definition: ts_selfuncs.c:214
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int32 size
Definition: ts_type.h:208
#define STATISTIC_KIND_MCELEM
Definition: pg_statistic.h:257
#define tsquery_opr_selec_no_stats(query)
Definition: ts_selfuncs.c:57
void free_attstatsslot(Oid atttype, Datum *values, int nvalues, float4 *numbers, int nnumbers)
Definition: lsyscache.c:2978