PostgreSQL Source Code git master
Loading...
Searching...
No Matches
ts_selfuncs.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * ts_selfuncs.c
4 * Selectivity estimation functions for text search operators.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 *
8 *
9 * IDENTIFICATION
10 * src/backend/tsearch/ts_selfuncs.c
11 *
12 *-------------------------------------------------------------------------
13 */
14#include "postgres.h"
15
16#include "access/htup_details.h"
18#include "catalog/pg_type.h"
19#include "miscadmin.h"
20#include "nodes/nodes.h"
21#include "tsearch/ts_type.h"
22#include "utils/fmgrprotos.h"
23#include "utils/lsyscache.h"
24#include "utils/selfuncs.h"
25
26
27/*
28 * The default text search selectivity is chosen to be small enough to
29 * encourage indexscans for typical table densities. See selfuncs.h and
30 * DEFAULT_EQ_SEL for details.
31 */
32#define DEFAULT_TS_MATCH_SEL 0.005
33
34/* lookup table type for binary searching through MCELEMs */
35typedef struct
36{
39} TextFreq;
40
41/* type of keys for bsearch'ing through an array of TextFreqs */
42typedef struct
43{
44 char *lexeme;
45 int length;
46} LexemeKey;
47
50 const Datum *mcelem, int nmcelem,
51 const float4 *numbers, int nnumbers);
52static Selectivity tsquery_opr_selec(QueryItem *item, char *operand,
53 TextFreq *lookup, int length, float4 minfreq);
54static int compare_lexeme_textfreq(const void *e1, const void *e2);
55
56#define tsquery_opr_selec_no_stats(query) \
57 tsquery_opr_selec(GETQUERY(query), GETOPERAND(query), NULL, 0, 0)
58
59
60/*
61 * tsmatchsel -- Selectivity of "@@"
62 *
63 * restriction selectivity function for tsvector @@ tsquery and
64 * tsquery @@ tsvector
65 */
68{
70
71#ifdef NOT_USED
72 Oid operator = PG_GETARG_OID(1);
73#endif
74 List *args = (List *) PG_GETARG_POINTER(2);
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}
133
134
135/*
136 * tsmatchjoinsel -- join selectivity of "@@"
137 *
138 * join selectivity function for tsvector @@ tsquery and tsquery @@ tsvector
139 */
140Datum
142{
143 /* for the moment we just punt */
145}
146
147
148/*
149 * @@ selectivity for tsvector var vs tsquery constant
150 */
151static Selectivity
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}
204
205/*
206 * Extract data from the pg_statistic arrays into useful format.
207 */
208static Selectivity
210 const float4 *numbers, int nnumbers)
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}
256
257/*
258 * Traverse the tsquery in preorder, calculating selectivity as:
259 *
260 * selec(left_oper) * selec(right_oper) in AND & PHRASE nodes,
261 *
262 * selec(left_oper) + selec(right_oper) -
263 * selec(left_oper) * selec(right_oper) in OR nodes,
264 *
265 * 1 - select(oper) in NOT nodes
266 *
267 * histogram-based estimation in prefix VAL nodes
268 *
269 * freq[val] in exact VAL nodes, if the value is in MCELEM
270 * min(freq[MCELEM]) / 2 in VAL nodes, if it is not
271 *
272 * The MCELEM array is already sorted (see ts_typanalyze.c), so we can use
273 * binary search for determining freq[MCELEM].
274 *
275 * If we don't have stats for the tsvector, we still use this logic,
276 * except we use default estimates for VAL nodes. This case is signaled
277 * by lookup == NULL.
278 */
279static Selectivity
280tsquery_opr_selec(QueryItem *item, char *operand,
281 TextFreq *lookup, int length, float4 minfreq)
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;
291 LexemeKey key;
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}
431
432/*
433 * bsearch() comparator for a lexeme (non-NULL terminated string with length)
434 * and a TextFreq. Use length, then byte-for-byte comparison, because that's
435 * how ANALYZE code sorted data before storing it in a statistic tuple.
436 * See ts_typanalyze.c for details.
437 */
438static int
439compare_lexeme_textfreq(const void *e1, const void *e2)
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}
#define GETQUERY(x)
Definition _int.h:157
#define Min(x, y)
Definition c.h:997
#define Max(x, y)
Definition c.h:991
#define Assert(condition)
Definition c.h:873
double float8
Definition c.h:644
float float4
Definition c.h:643
#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:3494
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition lsyscache.c:3384
#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:371
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
char * lexeme
Definition ts_selfuncs.c:44
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:706
#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)
Datum tsmatchsel(PG_FUNCTION_ARGS)
Definition ts_selfuncs.c:67
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