PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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/fmgrprotos.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.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, const Datum *mcelem, int nmcelem, const 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

◆ DEFAULT_TS_MATCH_SEL

#define DEFAULT_TS_MATCH_SEL   0.005

Definition at line 32 of file ts_selfuncs.c.

◆ tsquery_opr_selec_no_stats

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

Definition at line 56 of file ts_selfuncs.c.

67{
69
70#ifdef NOT_USED
71 Oid operator = PG_GETARG_OID(1);
72#endif
74 int varRelid = PG_GETARG_INT32(3);
76 Node *other;
77 bool varonleft;
79
80 /*
81 * If expression is not variable = something or something = variable, then
82 * punt and return a default estimate.
83 */
84 if (!get_restriction_variable(root, args, varRelid,
87
88 /*
89 * Can't do anything useful if the something is not a constant, either.
90 */
91 if (!IsA(other, Const))
92 {
95 }
96
97 /*
98 * The "@@" operator is strict, so we can cope with NULL right away
99 */
100 if (((Const *) other)->constisnull)
101 {
103 PG_RETURN_FLOAT8(0.0);
104 }
105
106 /*
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.
110 *
111 * Also check that the Var really is a TSVector, in case this estimator is
112 * mistakenly attached to some other operator.
113 */
114 if (((Const *) other)->consttype == TSQUERYOID &&
115 vardata.vartype == TSVECTOROID)
116 {
117 /* tsvector @@ tsquery or the other way around */
119 }
120 else
121 {
122 /* If we can't see the query structure, must punt */
124 }
125
127
129
131}
132
133
134/*
135 * tsmatchjoinsel -- join selectivity of "@@"
136 *
137 * join selectivity function for tsvector @@ tsquery and tsquery @@ tsvector
138 */
139Datum
141{
142 /* for the moment we just punt */
144}
145
146
147/*
148 * @@ selectivity for tsvector var vs tsquery constant
149 */
150static Selectivity
152{
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;
167
168 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
169
170 /* MCELEM will be an array of TEXT elements for a tsvector column */
171 if (get_attstatsslot(&sslot, vardata->statsTuple,
174 {
175 /*
176 * There is a most-common-elements slot for the tsvector Var, so
177 * use that.
178 */
179 selec = mcelem_tsquery_selec(query, sslot.values, sslot.nvalues,
180 sslot.numbers, sslot.nnumbers);
182 }
183 else
184 {
185 /* No most-common-elements info, so do without */
187 }
188
189 /*
190 * MCE stats count only non-null rows, so adjust for null rows.
191 */
192 selec *= (1.0 - stats->stanullfrac);
193 }
194 else
195 {
196 /* No stats at all, so do without */
198 /* we assume no nulls here, so no stanullfrac correction */
199 }
200
201 return selec;
202}
203
204/*
205 * Extract data from the pg_statistic arrays into useful format.
206 */
207static Selectivity
209 const float4 *numbers, int nnumbers)
210{
214 int i;
215
216 /*
217 * There should be two more Numbers than Values, because the last two
218 * cells are taken for minimal and maximal frequency. Punt if not.
219 *
220 * (Note: the MCELEM statistics slot definition allows for a third extra
221 * number containing the frequency of nulls, but we're not expecting that
222 * to appear for a tsvector column.)
223 */
224 if (nnumbers != nmcelem + 2)
225 return tsquery_opr_selec_no_stats(query);
226
227 /*
228 * Transpose the data into a single array so we can use bsearch().
229 */
231 for (i = 0; i < nmcelem; i++)
232 {
233 /*
234 * The text Datums came from an array, so it cannot be compressed or
235 * stored out-of-line -- it's safe to use VARSIZE_ANY*.
236 */
238 lookup[i].element = (text *) DatumGetPointer(mcelem[i]);
239 lookup[i].frequency = numbers[i];
240 }
241
242 /*
243 * Grab the lowest MCE frequency. compute_tsvector_stats() stored it for
244 * us in the one before the last cell of the Numbers array.
245 */
246 minfreq = numbers[nnumbers - 2];
247
250
251 pfree(lookup);
252
253 return selec;
254}
255
256/*
257 * Traverse the tsquery in preorder, calculating selectivity as:
258 *
259 * selec(left_oper) * selec(right_oper) in AND & PHRASE nodes,
260 *
261 * selec(left_oper) + selec(right_oper) -
262 * selec(left_oper) * selec(right_oper) in OR nodes,
263 *
264 * 1 - select(oper) in NOT nodes
265 *
266 * histogram-based estimation in prefix VAL nodes
267 *
268 * freq[val] in exact VAL nodes, if the value is in MCELEM
269 * min(freq[MCELEM]) / 2 in VAL nodes, if it is not
270 *
271 * The MCELEM array is already sorted (see ts_typanalyze.c), so we can use
272 * binary search for determining freq[MCELEM].
273 *
274 * If we don't have stats for the tsvector, we still use this logic,
275 * except we use default estimates for VAL nodes. This case is signaled
276 * by lookup == NULL.
277 */
278static Selectivity
279tsquery_opr_selec(QueryItem *item, char *operand,
280 TextFreq *lookup, int length, float4 minfreq)
281{
283
284 /* since this function recurses, it could be driven to stack overflow */
286
287 if (item->type == QI_VAL)
288 {
289 QueryOperand *oper = (QueryOperand *) item;
291
292 /*
293 * Prepare the key for bsearch().
294 */
295 key.lexeme = operand + oper->distance;
296 key.length = oper->length;
297
298 if (oper->prefix)
299 {
300 /* Prefix match, ie the query item is lexeme:* */
301 Selectivity matched,
302 allmces;
303 int i,
304 n_matched;
305
306 /*
307 * Our strategy is to scan through the MCELEM list and combine the
308 * frequencies of the ones that match the prefix. We then
309 * extrapolate the fraction of matching MCELEMs to the remaining
310 * rows, assuming that the MCELEMs are representative of the whole
311 * lexeme population in this respect. (Compare
312 * histogram_selectivity().) Note that these are most common
313 * elements not most common values, so they're not mutually
314 * exclusive. We treat occurrences as independent events.
315 *
316 * This is only a good plan if we have a pretty fair number of
317 * MCELEMs available; we set the threshold at 100. If no stats or
318 * insufficient stats, arbitrarily use DEFAULT_TS_MATCH_SEL*4.
319 */
320 if (lookup == NULL || length < 100)
321 return (Selectivity) (DEFAULT_TS_MATCH_SEL * 4);
322
323 matched = allmces = 0;
324 n_matched = 0;
325 for (i = 0; i < length; i++)
326 {
327 TextFreq *t = lookup + i;
329
330 if (tlen >= key.length &&
331 strncmp(key.lexeme, VARDATA_ANY(t->element),
332 key.length) == 0)
333 {
334 matched += t->frequency - matched * t->frequency;
335 n_matched++;
336 }
337 allmces += t->frequency - allmces * t->frequency;
338 }
339
340 /* Clamp to ensure sanity in the face of roundoff error */
341 CLAMP_PROBABILITY(matched);
343
344 selec = matched + (1.0 - allmces) * ((double) n_matched / length);
345
346 /*
347 * In any case, never believe that a prefix match has selectivity
348 * less than we would assign for a non-MCELEM lexeme. This
349 * preserves the property that "word:*" should be estimated to
350 * match at least as many rows as "word" would be.
351 */
353 }
354 else
355 {
356 /* Regular exact lexeme match */
358
359 /* If no stats for the variable, use DEFAULT_TS_MATCH_SEL */
360 if (lookup == NULL)
362
363 searchres = (TextFreq *) bsearch(&key, lookup, length,
364 sizeof(TextFreq),
366
367 if (searchres)
368 {
369 /*
370 * The element is in MCELEM. Return precise selectivity (or
371 * at least as precise as ANALYZE could find out).
372 */
374 }
375 else
376 {
377 /*
378 * The element is not in MCELEM. Estimate its frequency as
379 * half that of the least-frequent MCE. (We know it cannot be
380 * more than minfreq, and it could be a great deal less. Half
381 * seems like a good compromise.) For probably-historical
382 * reasons, clamp to not more than DEFAULT_TS_MATCH_SEL.
383 */
385 }
386 }
387 }
388 else
389 {
390 /* Current TSQuery node is an operator */
392 s2;
393
394 switch (item->qoperator.oper)
395 {
396 case OP_NOT:
397 selec = 1.0 - tsquery_opr_selec(item + 1, operand,
398 lookup, length, minfreq);
399 break;
400
401 case OP_PHRASE:
402 case OP_AND:
403 s1 = tsquery_opr_selec(item + 1, operand,
404 lookup, length, minfreq);
405 s2 = tsquery_opr_selec(item + item->qoperator.left, operand,
406 lookup, length, minfreq);
407 selec = s1 * s2;
408 break;
409
410 case OP_OR:
411 s1 = tsquery_opr_selec(item + 1, operand,
412 lookup, length, minfreq);
413 s2 = tsquery_opr_selec(item + item->qoperator.left, operand,
414 lookup, length, minfreq);
415 selec = s1 + s2 - s1 * s2;
416 break;
417
418 default:
419 elog(ERROR, "unrecognized operator: %d", item->qoperator.oper);
420 selec = 0; /* keep compiler quiet */
421 break;
422 }
423 }
424
425 /* Clamp intermediate results to stay sane despite roundoff error */
427
428 return selec;
429}
430
431/*
432 * bsearch() comparator for a lexeme (non-NULL terminated string with length)
433 * and a TextFreq. Use length, then byte-for-byte comparison, because that's
434 * how ANALYZE code sorted data before storing it in a statistic tuple.
435 * See ts_typanalyze.c for details.
436 */
437static int
438compare_lexeme_textfreq(const void *e1, const void *e2)
439{
440 const LexemeKey *key = (const LexemeKey *) e1;
441 const TextFreq *t = (const TextFreq *) e2;
442 int len1,
443 len2;
444
445 len1 = key->length;
446 len2 = VARSIZE_ANY_EXHDR(t->element);
447
448 /* Compare lengths first, possibly avoiding a strncmp call */
449 if (len1 > len2)
450 return 1;
451 else if (len1 < len2)
452 return -1;
453
454 /* Fall back on byte-for-byte comparison */
455 return strncmp(key->lexeme, VARDATA_ANY(t->element), len1);
456}
#define GETQUERY(x)
Definition _int.h:157
#define Min(x, y)
Definition c.h:1019
#define Max(x, y)
Definition c.h:1013
#define Assert(condition)
Definition c.h:885
double float8
Definition c.h:656
float float4
Definition c.h:655
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_RETURN_FLOAT8(x)
Definition fmgr.h:369
#define PG_GETARG_POINTER(n)
Definition fmgr.h:277
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
int i
Definition isn.c:77
void free_attstatsslot(AttStatsSlot *sslot)
Definition lsyscache.c:3496
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition lsyscache.c:3386
#define ATTSTATSSLOT_NUMBERS
Definition lsyscache.h:44
#define ATTSTATSSLOT_VALUES
Definition lsyscache.h:43
#define GETOPERAND(x)
Definition ltree.h:167
void pfree(void *pointer)
Definition mcxt.c:1616
#define IsA(nodeptr, _type_)
Definition nodes.h:164
double Selectivity
Definition nodes.h:260
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition parse_oper.c:372
FormData_pg_statistic * Form_pg_statistic
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
#define InvalidOid
unsigned int Oid
static int fb(int x)
char * s1
char * s2
tree ctl root
Definition radixtree.h:1857
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
Definition selfuncs.c:5507
#define ReleaseVariableStats(vardata)
Definition selfuncs.h:101
#define CLAMP_PROBABILITY(p)
Definition selfuncs.h:63
void check_stack_depth(void)
Definition stack_depth.c:95
Definition pg_list.h:54
Definition nodes.h:135
uint32 left
Definition ts_type.h:197
int32 size
Definition ts_type.h:221
text * element
Definition ts_selfuncs.c:37
float4 frequency
Definition ts_selfuncs.c:38
Definition zic.c:307
Definition c.h:718
#define tsquery_opr_selec_no_stats(query)
Definition ts_selfuncs.c:56
#define DEFAULT_TS_MATCH_SEL
Definition ts_selfuncs.c:32
Datum tsmatchjoinsel(PG_FUNCTION_ARGS)
static Selectivity tsquery_opr_selec(QueryItem *item, char *operand, TextFreq *lookup, int length, float4 minfreq)
static Selectivity tsquerysel(VariableStatData *vardata, Datum constval)
static int compare_lexeme_textfreq(const void *e1, const void *e2)
static Selectivity mcelem_tsquery_selec(TSQuery query, const Datum *mcelem, int nmcelem, const float4 *numbers, int nnumbers)
static TSQuery DatumGetTSQuery(Datum X)
Definition ts_type.h:249
#define QI_VAL
Definition ts_type.h:149
#define OP_AND
Definition ts_type.h:180
#define OP_PHRASE
Definition ts_type.h:182
#define OP_OR
Definition ts_type.h:181
#define OP_NOT
Definition ts_type.h:179
QueryOperator qoperator
Definition ts_type.h:209
QueryItemType type
Definition ts_type.h:208
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static bool VARATT_IS_EXTERNAL(const void *PTR)
Definition varatt.h:354
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486
static bool VARATT_IS_COMPRESSED(const void *PTR)
Definition varatt.h:347

Function Documentation

◆ compare_lexeme_textfreq()

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

Definition at line 439 of file ts_selfuncs.c.

440{
441 const LexemeKey *key = (const LexemeKey *) e1;
442 const TextFreq *t = (const TextFreq *) e2;
443 int len1,
444 len2;
445
446 len1 = key->length;
447 len2 = VARSIZE_ANY_EXHDR(t->element);
448
449 /* Compare lengths first, possibly avoiding a strncmp call */
450 if (len1 > len2)
451 return 1;
452 else if (len1 < len2)
453 return -1;
454
455 /* Fall back on byte-for-byte comparison */
456 return strncmp(key->lexeme, VARDATA_ANY(t->element), len1);
457}

References TextFreq::element, fb(), VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by tsquery_opr_selec().

◆ mcelem_tsquery_selec()

static Selectivity mcelem_tsquery_selec ( TSQuery  query,
const Datum mcelem,
int  nmcelem,
const float4 numbers,
int  nnumbers 
)
static

Definition at line 209 of file ts_selfuncs.c.

211{
215 int i;
216
217 /*
218 * There should be two more Numbers than Values, because the last two
219 * cells are taken for minimal and maximal frequency. Punt if not.
220 *
221 * (Note: the MCELEM statistics slot definition allows for a third extra
222 * number containing the frequency of nulls, but we're not expecting that
223 * to appear for a tsvector column.)
224 */
225 if (nnumbers != nmcelem + 2)
226 return tsquery_opr_selec_no_stats(query);
227
228 /*
229 * Transpose the data into a single array so we can use bsearch().
230 */
232 for (i = 0; i < nmcelem; i++)
233 {
234 /*
235 * The text Datums came from an array, so it cannot be compressed or
236 * stored out-of-line -- it's safe to use VARSIZE_ANY*.
237 */
239 lookup[i].element = (text *) DatumGetPointer(mcelem[i]);
240 lookup[i].frequency = numbers[i];
241 }
242
243 /*
244 * Grab the lowest MCE frequency. compute_tsvector_stats() stored it for
245 * us in the one before the last cell of the Numbers array.
246 */
247 minfreq = numbers[nnumbers - 2];
248
251
252 pfree(lookup);
253
254 return selec;
255}

References Assert, DatumGetPointer(), fb(), GETOPERAND, GETQUERY, i, palloc_array, pfree(), tsquery_opr_selec(), tsquery_opr_selec_no_stats, VARATT_IS_COMPRESSED(), and VARATT_IS_EXTERNAL().

Referenced by tsquerysel().

◆ tsmatchjoinsel()

Datum tsmatchjoinsel ( PG_FUNCTION_ARGS  )

Definition at line 141 of file ts_selfuncs.c.

142{
143 /* for the moment we just punt */
145}

References DEFAULT_TS_MATCH_SEL, and PG_RETURN_FLOAT8.

◆ tsmatchsel()

Datum tsmatchsel ( PG_FUNCTION_ARGS  )

Definition at line 67 of file ts_selfuncs.c.

68{
70
71#ifdef NOT_USED
72 Oid operator = PG_GETARG_OID(1);
73#endif
75 int varRelid = PG_GETARG_INT32(3);
77 Node *other;
78 bool varonleft;
80
81 /*
82 * If expression is not variable = something or something = variable, then
83 * punt and return a default estimate.
84 */
85 if (!get_restriction_variable(root, args, varRelid,
88
89 /*
90 * Can't do anything useful if the something is not a constant, either.
91 */
92 if (!IsA(other, Const))
93 {
96 }
97
98 /*
99 * The "@@" operator is strict, so we can cope with NULL right away
100 */
101 if (((Const *) other)->constisnull)
102 {
104 PG_RETURN_FLOAT8(0.0);
105 }
106
107 /*
108 * OK, there's a Var and a Const we're dealing with here. We need the
109 * Const to be a TSQuery, else we can't do anything useful. We have to
110 * check this because the Var might be the TSQuery not the TSVector.
111 *
112 * Also check that the Var really is a TSVector, in case this estimator is
113 * mistakenly attached to some other operator.
114 */
115 if (((Const *) other)->consttype == TSQUERYOID &&
116 vardata.vartype == TSVECTOROID)
117 {
118 /* tsvector @@ tsquery or the other way around */
120 }
121 else
122 {
123 /* If we can't see the query structure, must punt */
125 }
126
128
130
132}

References CLAMP_PROBABILITY, DEFAULT_TS_MATCH_SEL, fb(), get_restriction_variable(), IsA, PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_FLOAT8, ReleaseVariableStats, root, and tsquerysel().

◆ tsquery_opr_selec()

static Selectivity tsquery_opr_selec ( QueryItem item,
char operand,
TextFreq lookup,
int  length,
float4  minfreq 
)
static

Definition at line 280 of file ts_selfuncs.c.

282{
284
285 /* since this function recurses, it could be driven to stack overflow */
287
288 if (item->type == QI_VAL)
289 {
290 QueryOperand *oper = (QueryOperand *) item;
292
293 /*
294 * Prepare the key for bsearch().
295 */
296 key.lexeme = operand + oper->distance;
297 key.length = oper->length;
298
299 if (oper->prefix)
300 {
301 /* Prefix match, ie the query item is lexeme:* */
302 Selectivity matched,
303 allmces;
304 int i,
305 n_matched;
306
307 /*
308 * Our strategy is to scan through the MCELEM list and combine the
309 * frequencies of the ones that match the prefix. We then
310 * extrapolate the fraction of matching MCELEMs to the remaining
311 * rows, assuming that the MCELEMs are representative of the whole
312 * lexeme population in this respect. (Compare
313 * histogram_selectivity().) Note that these are most common
314 * elements not most common values, so they're not mutually
315 * exclusive. We treat occurrences as independent events.
316 *
317 * This is only a good plan if we have a pretty fair number of
318 * MCELEMs available; we set the threshold at 100. If no stats or
319 * insufficient stats, arbitrarily use DEFAULT_TS_MATCH_SEL*4.
320 */
321 if (lookup == NULL || length < 100)
322 return (Selectivity) (DEFAULT_TS_MATCH_SEL * 4);
323
324 matched = allmces = 0;
325 n_matched = 0;
326 for (i = 0; i < length; i++)
327 {
328 TextFreq *t = lookup + i;
330
331 if (tlen >= key.length &&
332 strncmp(key.lexeme, VARDATA_ANY(t->element),
333 key.length) == 0)
334 {
335 matched += t->frequency - matched * t->frequency;
336 n_matched++;
337 }
338 allmces += t->frequency - allmces * t->frequency;
339 }
340
341 /* Clamp to ensure sanity in the face of roundoff error */
342 CLAMP_PROBABILITY(matched);
344
345 selec = matched + (1.0 - allmces) * ((double) n_matched / length);
346
347 /*
348 * In any case, never believe that a prefix match has selectivity
349 * less than we would assign for a non-MCELEM lexeme. This
350 * preserves the property that "word:*" should be estimated to
351 * match at least as many rows as "word" would be.
352 */
354 }
355 else
356 {
357 /* Regular exact lexeme match */
359
360 /* If no stats for the variable, use DEFAULT_TS_MATCH_SEL */
361 if (lookup == NULL)
363
364 searchres = (TextFreq *) bsearch(&key, lookup, length,
365 sizeof(TextFreq),
367
368 if (searchres)
369 {
370 /*
371 * The element is in MCELEM. Return precise selectivity (or
372 * at least as precise as ANALYZE could find out).
373 */
375 }
376 else
377 {
378 /*
379 * The element is not in MCELEM. Estimate its frequency as
380 * half that of the least-frequent MCE. (We know it cannot be
381 * more than minfreq, and it could be a great deal less. Half
382 * seems like a good compromise.) For probably-historical
383 * reasons, clamp to not more than DEFAULT_TS_MATCH_SEL.
384 */
386 }
387 }
388 }
389 else
390 {
391 /* Current TSQuery node is an operator */
393 s2;
394
395 switch (item->qoperator.oper)
396 {
397 case OP_NOT:
398 selec = 1.0 - tsquery_opr_selec(item + 1, operand,
399 lookup, length, minfreq);
400 break;
401
402 case OP_PHRASE:
403 case OP_AND:
404 s1 = tsquery_opr_selec(item + 1, operand,
405 lookup, length, minfreq);
406 s2 = tsquery_opr_selec(item + item->qoperator.left, operand,
407 lookup, length, minfreq);
408 selec = s1 * s2;
409 break;
410
411 case OP_OR:
412 s1 = tsquery_opr_selec(item + 1, operand,
413 lookup, length, minfreq);
414 s2 = tsquery_opr_selec(item + item->qoperator.left, operand,
415 lookup, length, minfreq);
416 selec = s1 + s2 - s1 * s2;
417 break;
418
419 default:
420 elog(ERROR, "unrecognized operator: %d", item->qoperator.oper);
421 selec = 0; /* keep compiler quiet */
422 break;
423 }
424 }
425
426 /* Clamp intermediate results to stay sane despite roundoff error */
428
429 return selec;
430}

References check_stack_depth(), CLAMP_PROBABILITY, compare_lexeme_textfreq(), DEFAULT_TS_MATCH_SEL, TextFreq::element, elog, ERROR, fb(), TextFreq::frequency, i, QueryOperator::left, Max, Min, OP_AND, OP_NOT, OP_OR, OP_PHRASE, oper(), QueryOperator::oper, QI_VAL, QueryItem::qoperator, s1, s2, tsquery_opr_selec(), QueryItem::type, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by mcelem_tsquery_selec(), and tsquery_opr_selec().

◆ tsquerysel()

static Selectivity tsquerysel ( VariableStatData vardata,
Datum  constval 
)
static

Definition at line 152 of file ts_selfuncs.c.

153{
155 TSQuery query;
156
157 /* The caller made sure the const is a TSQuery, so get it now */
158 query = DatumGetTSQuery(constval);
159
160 /* Empty query matches nothing */
161 if (query->size == 0)
162 return (Selectivity) 0.0;
163
164 if (HeapTupleIsValid(vardata->statsTuple))
165 {
166 Form_pg_statistic stats;
168
169 stats = (Form_pg_statistic) GETSTRUCT(vardata->statsTuple);
170
171 /* MCELEM will be an array of TEXT elements for a tsvector column */
172 if (get_attstatsslot(&sslot, vardata->statsTuple,
175 {
176 /*
177 * There is a most-common-elements slot for the tsvector Var, so
178 * use that.
179 */
180 selec = mcelem_tsquery_selec(query, sslot.values, sslot.nvalues,
181 sslot.numbers, sslot.nnumbers);
183 }
184 else
185 {
186 /* No most-common-elements info, so do without */
188 }
189
190 /*
191 * MCE stats count only non-null rows, so adjust for null rows.
192 */
193 selec *= (1.0 - stats->stanullfrac);
194 }
195 else
196 {
197 /* No stats at all, so do without */
199 /* we assume no nulls here, so no stanullfrac correction */
200 }
201
202 return selec;
203}

References ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, DatumGetTSQuery(), fb(), free_attstatsslot(), get_attstatsslot(), GETSTRUCT(), HeapTupleIsValid, InvalidOid, mcelem_tsquery_selec(), TSQueryData::size, and tsquery_opr_selec_no_stats.

Referenced by tsmatchsel().