PostgreSQL Source Code git master
Loading...
Searching...
No Matches
_int_selfuncs.c File Reference
#include "postgres.h"
#include "_int.h"
#include "access/htup_details.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_statistic.h"
#include "catalog/pg_type.h"
#include "miscadmin.h"
#include "utils/fmgrprotos.h"
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"
Include dependency graph for _int_selfuncs.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (_int_overlap_sel)
 
 PG_FUNCTION_INFO_V1 (_int_contains_sel)
 
 PG_FUNCTION_INFO_V1 (_int_contained_sel)
 
 PG_FUNCTION_INFO_V1 (_int_overlap_joinsel)
 
 PG_FUNCTION_INFO_V1 (_int_contains_joinsel)
 
 PG_FUNCTION_INFO_V1 (_int_contained_joinsel)
 
 PG_FUNCTION_INFO_V1 (_int_matchsel)
 
static Selectivity int_query_opr_selec (ITEM *item, Datum *mcelems, float4 *mcefreqs, int nmcelems, float4 minfreq)
 
static int compare_val_int4 (const void *a, const void *b)
 
Datum _int_overlap_sel (PG_FUNCTION_ARGS)
 
Datum _int_contains_sel (PG_FUNCTION_ARGS)
 
Datum _int_contained_sel (PG_FUNCTION_ARGS)
 
Datum _int_overlap_joinsel (PG_FUNCTION_ARGS)
 
Datum _int_contains_joinsel (PG_FUNCTION_ARGS)
 
Datum _int_contained_joinsel (PG_FUNCTION_ARGS)
 
Datum _int_matchsel (PG_FUNCTION_ARGS)
 

Function Documentation

◆ _int_contained_joinsel()

Datum _int_contained_joinsel ( PG_FUNCTION_ARGS  )

Definition at line 107 of file _int_selfuncs.c.

108{
114 PG_GETARG_DATUM(4)));
115}
Datum arraycontjoinsel(PG_FUNCTION_ARGS)
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_RETURN_DATUM(x)
Definition fmgr.h:354
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition fmgr.h:692
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
static int fb(int x)

References arraycontjoinsel(), DirectFunctionCall5, fb(), ObjectIdGetDatum(), PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ _int_contained_sel()

Datum _int_contained_sel ( PG_FUNCTION_ARGS  )

Definition at line 75 of file _int_selfuncs.c.

76{
81 PG_GETARG_DATUM(3)));
82}
Datum arraycontsel(PG_FUNCTION_ARGS)
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition fmgr.h:690

References arraycontsel(), DirectFunctionCall4, fb(), ObjectIdGetDatum(), PG_GETARG_DATUM, and PG_RETURN_DATUM.

◆ _int_contains_joinsel()

◆ _int_contains_sel()

◆ _int_matchsel()

Datum _int_matchsel ( PG_FUNCTION_ARGS  )

Definition at line 122 of file _int_selfuncs.c.

123{
125
127 int varRelid = PG_GETARG_INT32(3);
129 Node *other;
130 bool varonleft;
132 QUERYTYPE *query;
133 Datum *mcelems = NULL;
135 int nmcelems = 0;
136 float4 minfreq = 0.0;
137 float4 nullfrac = 0.0;
139
140 /*
141 * If expression is not "variable @@ something" or "something @@ variable"
142 * then punt and return a default estimate.
143 */
144 if (!get_restriction_variable(root, args, varRelid,
145 &vardata, &other, &varonleft))
147
148 /*
149 * Variable should be int[]. We don't support cases where variable is
150 * query_int.
151 */
152 if (vardata.vartype != INT4ARRAYOID)
154
155 /*
156 * Can't do anything useful if the something is not a constant, either.
157 */
158 if (!IsA(other, Const))
159 {
162 }
163
164 /*
165 * The "@@" operator is strict, so we can cope with NULL right away.
166 */
167 if (((Const *) other)->constisnull)
168 {
170 PG_RETURN_FLOAT8(0.0);
171 }
172
173 /* The caller made sure the const is a query, so get it now */
175
176 /* Empty query matches nothing */
177 if (query->size == 0)
178 {
180 PG_RETURN_FLOAT8(0.0);
181 }
182
183 /*
184 * Get the statistics for the intarray column.
185 *
186 * We're interested in the Most-Common-Elements list, and the NULL
187 * fraction.
188 */
189 if (HeapTupleIsValid(vardata.statsTuple))
190 {
191 Form_pg_statistic stats;
192
193 stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple);
194 nullfrac = stats->stanullfrac;
195
196 /*
197 * For an int4 array, the default array type analyze function will
198 * collect a Most Common Elements list, which is an array of int4s.
199 */
200 if (get_attstatsslot(&sslot, vardata.statsTuple,
203 {
204 Assert(sslot.valuetype == INT4OID);
205
206 /*
207 * There should be three more Numbers than Values, because the
208 * last three (for intarray) cells are taken for minimal, maximal
209 * and nulls frequency. Punt if not.
210 */
211 if (sslot.nnumbers == sslot.nvalues + 3)
212 {
213 /* Grab the minimal MCE frequency. */
214 minfreq = sslot.numbers[sslot.nvalues];
215
216 mcelems = sslot.values;
217 mcefreqs = sslot.numbers;
218 nmcelems = sslot.nvalues;
219 }
220 }
221 }
222 else
223 memset(&sslot, 0, sizeof(sslot));
224
225 /* Process the logical expression in the query, using the stats */
226 selec = int_query_opr_selec(GETQUERY(query) + query->size - 1,
228
229 /* MCE stats count only non-null rows, so adjust for null rows. */
230 selec *= (1.0 - nullfrac);
231
234
236
238}
#define GETQUERY(x)
Definition _int.h:157
#define DatumGetQueryTypeP(X)
Definition _int.h:168
static Selectivity int_query_opr_selec(ITEM *item, Datum *mcelems, float4 *mcefreqs, int nmcelems, float4 minfreq)
#define Assert(condition)
Definition c.h:873
double float8
Definition c.h:644
float float4
Definition c.h:643
#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 HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
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 IsA(nodeptr, _type_)
Definition nodes.h:164
double Selectivity
Definition nodes.h:260
FormData_pg_statistic * Form_pg_statistic
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
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
#define DEFAULT_EQ_SEL
Definition selfuncs.h:34
Definition pg_list.h:54
Definition nodes.h:135
int32 size
Definition _int.h:150

References Assert, ATTSTATSSLOT_NUMBERS, ATTSTATSSLOT_VALUES, CLAMP_PROBABILITY, DatumGetQueryTypeP, DEFAULT_EQ_SEL, fb(), free_attstatsslot(), get_attstatsslot(), get_restriction_variable(), GETQUERY, GETSTRUCT(), HeapTupleIsValid, int_query_opr_selec(), InvalidOid, IsA, PG_GETARG_INT32, PG_GETARG_POINTER, PG_RETURN_FLOAT8, ReleaseVariableStats, root, and QUERYTYPE::size.

◆ _int_overlap_joinsel()

◆ _int_overlap_sel()

◆ compare_val_int4()

static int compare_val_int4 ( const void a,
const void b 
)
static

Definition at line 329 of file _int_selfuncs.c.

330{
331 int32 key = *(const int32 *) a;
332 int32 value = DatumGetInt32(*(const Datum *) b);
333
334 if (key < value)
335 return -1;
336 else if (key > value)
337 return 1;
338 else
339 return 0;
340}
int32_t int32
Definition c.h:542
static struct @172 value
int b
Definition isn.c:74
int a
Definition isn.c:73
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212

References a, b, DatumGetInt32(), and value.

Referenced by int_query_opr_selec().

◆ int_query_opr_selec()

static Selectivity int_query_opr_selec ( ITEM item,
Datum mcelems,
float4 mcefreqs,
int  nmcelems,
float4  minfreq 
)
static

Definition at line 244 of file _int_selfuncs.c.

246{
248
249 /* since this function recurses, it could be driven to stack overflow */
251
252 if (item->type == VAL)
253 {
255
256 if (mcelems == NULL)
258
259 searchres = (Datum *) bsearch(&item->val, mcelems, nmcelems,
260 sizeof(Datum), compare_val_int4);
261 if (searchres)
262 {
263 /*
264 * The element is in MCELEM. Return precise selectivity (or at
265 * least as precise as ANALYZE could find out).
266 */
268 }
269 else
270 {
271 /*
272 * The element is not in MCELEM. Estimate its frequency as half
273 * that of the least-frequent MCE. (We know it cannot be more
274 * than minfreq, and it could be a great deal less. Half seems
275 * like a good compromise.) For probably-historical reasons,
276 * clamp to not more than DEFAULT_EQ_SEL.
277 */
279 }
280 }
281 else if (item->type == OPR)
282 {
283 /* Current query node is an operator */
285 s2;
286
288 minfreq);
289 switch (item->val)
290 {
291 case (int32) '!':
292 selec = 1.0 - s1;
293 break;
294
295 case (int32) '&':
296 s2 = int_query_opr_selec(item + item->left, mcelems, mcefreqs,
298 selec = s1 * s2;
299 break;
300
301 case (int32) '|':
302 s2 = int_query_opr_selec(item + item->left, mcelems, mcefreqs,
304 selec = s1 + s2 - s1 * s2;
305 break;
306
307 default:
308 elog(ERROR, "unrecognized operator: %d", item->val);
309 selec = 0; /* keep compiler quiet */
310 break;
311 }
312 }
313 else
314 {
315 elog(ERROR, "unrecognized int query item type: %u", item->type);
316 selec = 0; /* keep compiler quiet */
317 }
318
319 /* Clamp intermediate results to stay sane despite roundoff error */
321
322 return selec;
323}
#define OPR
Definition _int.h:163
#define VAL
Definition _int.h:162
static int compare_val_int4(const void *a, const void *b)
#define Min(x, y)
Definition c.h:997
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
char * s1
char * s2
void check_stack_depth(void)
Definition stack_depth.c:95
int16 left
Definition _int.h:143
int32 val
Definition _int.h:144
int16 type
Definition _int.h:142

References check_stack_depth(), CLAMP_PROBABILITY, compare_val_int4(), DEFAULT_EQ_SEL, elog, ERROR, fb(), int_query_opr_selec(), ITEM::left, Min, OPR, s1, s2, ITEM::type, ITEM::val, and VAL.

Referenced by _int_matchsel(), and int_query_opr_selec().

◆ PG_FUNCTION_INFO_V1() [1/7]

PG_FUNCTION_INFO_V1 ( _int_contained_joinsel  )

◆ PG_FUNCTION_INFO_V1() [2/7]

PG_FUNCTION_INFO_V1 ( _int_contained_sel  )

◆ PG_FUNCTION_INFO_V1() [3/7]

PG_FUNCTION_INFO_V1 ( _int_contains_joinsel  )

◆ PG_FUNCTION_INFO_V1() [4/7]

PG_FUNCTION_INFO_V1 ( _int_contains_sel  )

◆ PG_FUNCTION_INFO_V1() [5/7]

PG_FUNCTION_INFO_V1 ( _int_matchsel  )

◆ PG_FUNCTION_INFO_V1() [6/7]

PG_FUNCTION_INFO_V1 ( _int_overlap_joinsel  )

◆ PG_FUNCTION_INFO_V1() [7/7]

PG_FUNCTION_INFO_V1 ( _int_overlap_sel  )