PostgreSQL Source Code: contrib/intarray/_int_selfuncs.c Source File

PostgreSQL Source Code git master
_int_selfuncs.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * _int_selfuncs.c
4 * Functions for selectivity estimation of intarray operators
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * contrib/intarray/_int_selfuncs.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "_int.h"
18#include "access/htup_details.h"
19#include "catalog/pg_operator.h"
20#include "catalog/pg_statistic.h"
21#include "catalog/pg_type.h"
22#include "miscadmin.h"
23#include "utils/fmgrprotos.h"
24#include "utils/lsyscache.h"
25#include "utils/selfuncs.h"
26
27 PG_FUNCTION_INFO_V1(_int_overlap_sel);
28 PG_FUNCTION_INFO_V1(_int_contains_sel);
29 PG_FUNCTION_INFO_V1(_int_contained_sel);
30 PG_FUNCTION_INFO_V1(_int_overlap_joinsel);
31 PG_FUNCTION_INFO_V1(_int_contains_joinsel);
32 PG_FUNCTION_INFO_V1(_int_contained_joinsel);
33 PG_FUNCTION_INFO_V1(_int_matchsel);
34
35
36static Selectivity int_query_opr_selec(ITEM *item, Datum *mcelems, float4 *mcefreqs,
37 int nmcelems, float4 minfreq);
38static int compare_val_int4(const void *a, const void *b);
39
40/*
41 * Wrappers around the default array selectivity estimation functions.
42 *
43 * The default array selectivity operators for the @>, && and @< operators
44 * work fine for integer arrays. However, if we tried to just use arraycontsel
45 * and arraycontjoinsel directly as the cost estimator functions for our
46 * operators, they would not work as intended, because they look at the
47 * operator's OID. Our operators behave exactly like the built-in anyarray
48 * versions, but we must tell the cost estimator functions which built-in
49 * operators they correspond to. These wrappers just replace the operator
50 * OID with the corresponding built-in operator's OID, and call the built-in
51 * function.
52 */
53
54Datum
55 _int_overlap_sel(PG_FUNCTION_ARGS)
56{
57 PG_RETURN_DATUM(DirectFunctionCall4(arraycontsel,
58 PG_GETARG_DATUM(0),
59 ObjectIdGetDatum(OID_ARRAY_OVERLAP_OP),
60 PG_GETARG_DATUM(2),
61 PG_GETARG_DATUM(3)));
62}
63
64Datum
65 _int_contains_sel(PG_FUNCTION_ARGS)
66{
67 PG_RETURN_DATUM(DirectFunctionCall4(arraycontsel,
68 PG_GETARG_DATUM(0),
69 ObjectIdGetDatum(OID_ARRAY_CONTAINS_OP),
70 PG_GETARG_DATUM(2),
71 PG_GETARG_DATUM(3)));
72}
73
74Datum
75 _int_contained_sel(PG_FUNCTION_ARGS)
76{
77 PG_RETURN_DATUM(DirectFunctionCall4(arraycontsel,
78 PG_GETARG_DATUM(0),
79 ObjectIdGetDatum(OID_ARRAY_CONTAINED_OP),
80 PG_GETARG_DATUM(2),
81 PG_GETARG_DATUM(3)));
82}
83
84Datum
85 _int_overlap_joinsel(PG_FUNCTION_ARGS)
86{
87 PG_RETURN_DATUM(DirectFunctionCall5(arraycontjoinsel,
88 PG_GETARG_DATUM(0),
89 ObjectIdGetDatum(OID_ARRAY_OVERLAP_OP),
90 PG_GETARG_DATUM(2),
91 PG_GETARG_DATUM(3),
92 PG_GETARG_DATUM(4)));
93}
94
95Datum
96 _int_contains_joinsel(PG_FUNCTION_ARGS)
97{
98 PG_RETURN_DATUM(DirectFunctionCall5(arraycontjoinsel,
99 PG_GETARG_DATUM(0),
100 ObjectIdGetDatum(OID_ARRAY_CONTAINS_OP),
101 PG_GETARG_DATUM(2),
102 PG_GETARG_DATUM(3),
103 PG_GETARG_DATUM(4)));
104}
105
106Datum
107 _int_contained_joinsel(PG_FUNCTION_ARGS)
108{
109 PG_RETURN_DATUM(DirectFunctionCall5(arraycontjoinsel,
110 PG_GETARG_DATUM(0),
111 ObjectIdGetDatum(OID_ARRAY_CONTAINED_OP),
112 PG_GETARG_DATUM(2),
113 PG_GETARG_DATUM(3),
114 PG_GETARG_DATUM(4)));
115}
116
117
118/*
119 * _int_matchsel -- restriction selectivity function for intarray @@ query_int
120 */
121Datum
122 _int_matchsel(PG_FUNCTION_ARGS)
123{
124 PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0);
125
126 List *args = (List *) PG_GETARG_POINTER(2);
127 int varRelid = PG_GETARG_INT32(3);
128 VariableStatData vardata;
129 Node *other;
130 bool varonleft;
131 Selectivity selec;
132 QUERYTYPE *query;
133 Datum *mcelems = NULL;
134 float4 *mcefreqs = NULL;
135 int nmcelems = 0;
136 float4 minfreq = 0.0;
137 float4 nullfrac = 0.0;
138 AttStatsSlot sslot;
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))
146 PG_RETURN_FLOAT8(DEFAULT_EQ_SEL);
147
148 /*
149 * Variable should be int[]. We don't support cases where variable is
150 * query_int.
151 */
152 if (vardata.vartype != INT4ARRAYOID)
153 PG_RETURN_FLOAT8(DEFAULT_EQ_SEL);
154
155 /*
156 * Can't do anything useful if the something is not a constant, either.
157 */
158 if (!IsA(other, Const))
159 {
160 ReleaseVariableStats(vardata);
161 PG_RETURN_FLOAT8(DEFAULT_EQ_SEL);
162 }
163
164 /*
165 * The "@@" operator is strict, so we can cope with NULL right away.
166 */
167 if (((Const *) other)->constisnull)
168 {
169 ReleaseVariableStats(vardata);
170 PG_RETURN_FLOAT8(0.0);
171 }
172
173 /* The caller made sure the const is a query, so get it now */
174 query = DatumGetQueryTypeP(((Const *) other)->constvalue);
175
176 /* Empty query matches nothing */
177 if (query->size == 0)
178 {
179 ReleaseVariableStats(vardata);
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,
201 STATISTIC_KIND_MCELEM, InvalidOid,
202 ATTSTATSSLOT_VALUES | ATTSTATSSLOT_NUMBERS))
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,
227 mcelems, mcefreqs, nmcelems, minfreq);
228
229 /* MCE stats count only non-null rows, so adjust for null rows. */
230 selec *= (1.0 - nullfrac);
231
232 free_attstatsslot(&sslot);
233 ReleaseVariableStats(vardata);
234
235 CLAMP_PROBABILITY(selec);
236
237 PG_RETURN_FLOAT8((float8) selec);
238}
239
240/*
241 * Estimate selectivity of single intquery operator
242 */
243static Selectivity
244 int_query_opr_selec(ITEM *item, Datum *mcelems, float4 *mcefreqs,
245 int nmcelems, float4 minfreq)
246{
247 Selectivity selec;
248
249 /* since this function recurses, it could be driven to stack overflow */
250 check_stack_depth();
251
252 if (item->type == VAL)
253 {
254 Datum *searchres;
255
256 if (mcelems == NULL)
257 return (Selectivity) DEFAULT_EQ_SEL;
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 */
267 selec = mcefreqs[searchres - mcelems];
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 */
278 selec = Min(DEFAULT_EQ_SEL, minfreq / 2);
279 }
280 }
281 else if (item->type == OPR)
282 {
283 /* Current query node is an operator */
284 Selectivity s1,
285 s2;
286
287 s1 = int_query_opr_selec(item - 1, mcelems, mcefreqs, nmcelems,
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,
297 nmcelems, minfreq);
298 selec = s1 * s2;
299 break;
300
301 case (int32) '|':
302 s2 = int_query_opr_selec(item + item->left, mcelems, mcefreqs,
303 nmcelems, minfreq);
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 */
320 CLAMP_PROBABILITY(selec);
321
322 return selec;
323}
324
325/*
326 * Comparison function for binary search in mcelem array.
327 */
328static int
329 compare_val_int4(const void *a, const void *b)
330{
331 int32 key = *(int32 *) a;
332 const Datum *t = (const Datum *) b;
333
334 return key - DatumGetInt32(*t);
335}
#define OPR
Definition: _int.h:163
#define VAL
Definition: _int.h:162
#define GETQUERY(x)
Definition: _int.h:157
#define DatumGetQueryTypeP(X)
Definition: _int.h:168
static int compare_val_int4(const void *a, const void *b)
Definition: _int_selfuncs.c:329
Datum _int_overlap_joinsel(PG_FUNCTION_ARGS)
Definition: _int_selfuncs.c:85
static Selectivity int_query_opr_selec(ITEM *item, Datum *mcelems, float4 *mcefreqs, int nmcelems, float4 minfreq)
Definition: _int_selfuncs.c:244
Datum _int_matchsel(PG_FUNCTION_ARGS)
Definition: _int_selfuncs.c:122
PG_FUNCTION_INFO_V1(_int_overlap_sel)
Datum _int_contains_sel(PG_FUNCTION_ARGS)
Definition: _int_selfuncs.c:65
Datum _int_contained_sel(PG_FUNCTION_ARGS)
Definition: _int_selfuncs.c:75
Datum _int_contains_joinsel(PG_FUNCTION_ARGS)
Definition: _int_selfuncs.c:96
Datum _int_overlap_sel(PG_FUNCTION_ARGS)
Definition: _int_selfuncs.c:55
Datum _int_contained_joinsel(PG_FUNCTION_ARGS)
Definition: _int_selfuncs.c:107
Datum arraycontsel(PG_FUNCTION_ARGS)
Datum arraycontjoinsel(PG_FUNCTION_ARGS)
#define Min(x, y)
Definition: c.h:1003
double float8
Definition: c.h:635
int32_t int32
Definition: c.h:534
float float4
Definition: c.h:634
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
Definition: fmgr.h:688
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Definition: fmgr.h:690
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
Assert(PointerIsAligned(start, uint64))
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
b
int b
Definition: isn.c:74
a
int a
Definition: isn.c:73
void free_attstatsslot(AttStatsSlot *sslot)
Definition: lsyscache.c:3511
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
Definition: lsyscache.c:3401
#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
Definition: pg_statistic.h:135
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:212
#define InvalidOid
Definition: postgres_ext.h:37
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:5180
#define ReleaseVariableStats(vardata)
Definition: selfuncs.h:101
#define CLAMP_PROBABILITY(p)
Definition: selfuncs.h:63
#define DEFAULT_EQ_SEL
Definition: selfuncs.h:34
void check_stack_depth(void)
Definition: stack_depth.c:95
Oid valuetype
Definition: lsyscache.h:53
Datum * values
Definition: lsyscache.h:54
float4 * numbers
Definition: lsyscache.h:57
int nnumbers
Definition: lsyscache.h:58
int nvalues
Definition: lsyscache.h:55
Definition: primnodes.h:324
Definition: _int.h:141
int16 left
Definition: _int.h:143
int32 val
Definition: _int.h:144
int16 type
Definition: _int.h:142
Definition: pg_list.h:54
Definition: nodes.h:135
Definition: _int.h:148
int32 size
Definition: _int.h:150
HeapTuple statsTuple
Definition: selfuncs.h:89

AltStyle によって変換されたページ (->オリジナル) /