1/*-------------------------------------------------------------------------
4 * Functions for selectivity estimation of intarray operators
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * contrib/intarray/_int_selfuncs.c
13 *-------------------------------------------------------------------------
23#include "utils/fmgrprotos.h"
37 int nmcelems,
float4 minfreq);
41 * Wrappers around the default array selectivity estimation functions.
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
119 * _int_matchsel -- restriction selectivity function for intarray @@ query_int
133 Datum *mcelems = NULL;
141 * If expression is not "variable @@ something" or "something @@ variable"
142 * then punt and return a default estimate.
145 &vardata, &other, &varonleft))
149 * Variable should be int[]. We don't support cases where variable is
152 if (vardata.
vartype != INT4ARRAYOID)
156 * Can't do anything useful if the something is not a constant, either.
165 * The "@@" operator is strict, so we can cope with NULL right away.
167 if (((
Const *) other)->constisnull)
173 /* The caller made sure the const is a query, so get it now */
176 /* Empty query matches nothing */
177 if (query->
size == 0)
184 * Get the statistics for the intarray column.
186 * We're interested in the Most-Common-Elements list, and the NULL
194 nullfrac = stats->stanullfrac;
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.
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.
213 /* Grab the minimal MCE frequency. */
223 memset(&sslot, 0,
sizeof(sslot));
225 /* Process the logical expression in the query, using the stats */
227 mcelems, mcefreqs, nmcelems, minfreq);
229 /* MCE stats count only non-null rows, so adjust for null rows. */
230 selec *= (1.0 - nullfrac);
241 * Estimate selectivity of single intquery operator
245 int nmcelems,
float4 minfreq)
249 /* since this function recurses, it could be driven to stack overflow */
259 searchres = (
Datum *) bsearch(&item->
val, mcelems, nmcelems,
264 * The element is in MCELEM. Return precise selectivity (or at
265 * least as precise as ANALYZE could find out).
267 selec = mcefreqs[searchres - mcelems];
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.
283 /* Current query node is an operator */
309 selec = 0;
/* keep compiler quiet */
315 elog(
ERROR,
"unrecognized int query item type: %u", item->
type);
316 selec = 0;
/* keep compiler quiet */
319 /* Clamp intermediate results to stay sane despite roundoff error */
326 * Comparison function for binary search in mcelem array.
#define DatumGetQueryTypeP(X)
static int compare_val_int4(const void *a, const void *b)
Datum _int_overlap_joinsel(PG_FUNCTION_ARGS)
static Selectivity int_query_opr_selec(ITEM *item, Datum *mcelems, float4 *mcefreqs, int nmcelems, float4 minfreq)
Datum _int_matchsel(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(_int_overlap_sel)
Datum _int_contains_sel(PG_FUNCTION_ARGS)
Datum _int_contained_sel(PG_FUNCTION_ARGS)
Datum _int_contains_joinsel(PG_FUNCTION_ARGS)
Datum _int_overlap_sel(PG_FUNCTION_ARGS)
Datum _int_contained_joinsel(PG_FUNCTION_ARGS)
Datum arraycontsel(PG_FUNCTION_ARGS)
Datum arraycontjoinsel(PG_FUNCTION_ARGS)
#define PG_RETURN_FLOAT8(x)
#define DirectFunctionCall4(func, arg1, arg2, arg3, arg4)
#define PG_GETARG_POINTER(n)
#define PG_GETARG_DATUM(n)
#define PG_GETARG_INT32(n)
#define PG_RETURN_DATUM(x)
#define DirectFunctionCall5(func, arg1, arg2, arg3, arg4, arg5)
Assert(PointerIsAligned(start, uint64))
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
void free_attstatsslot(AttStatsSlot *sslot)
bool get_attstatsslot(AttStatsSlot *sslot, HeapTuple statstuple, int reqkind, Oid reqop, int flags)
#define ATTSTATSSLOT_NUMBERS
#define ATTSTATSSLOT_VALUES
#define IsA(nodeptr, _type_)
FormData_pg_statistic * Form_pg_statistic
static Datum ObjectIdGetDatum(Oid X)
static int32 DatumGetInt32(Datum X)
bool get_restriction_variable(PlannerInfo *root, List *args, int varRelid, VariableStatData *vardata, Node **other, bool *varonleft)
#define ReleaseVariableStats(vardata)
#define CLAMP_PROBABILITY(p)
void check_stack_depth(void)