Skip to content

Instantly share code, notes, and snippets.

postgres=# select oid, oprname, oprcode, oprrest from pg_operator where oprname = '~~';
 oid | oprname | oprcode | oprrest
 - - - + - - - - -+ - - - - - - + - - - - -
 1207 | ~~ | namelike | likesel
 1209 | ~~ | textlike | likesel
 1211 | ~~ | bpcharlike | likesel
 2016 | ~~ | bytealike | likesel
(4 rows)
postgres=# select oid, proname, prosrc from pg_proc where oid = (select distinct oprrest from pg_operator where oprname = '~~');
oid | proname | prosrc
 - - - + - - - - -+ - - - - -
 1819 | likesel | likesel
(1 row)
if (pstatus == Pattern_Prefix_Exact)
{
/*
* Pattern specifies an exact match, so estimate as for '='
*/
result = var_eq_const(&vardata, eqopr, prefix->constvalue,
false, true, false);
}
selec = histogram_selectivity(&vardata, &opproc, constval, true,
10, 1, &hist_size);
/* If not at least 100 entries, use the heuristic method */
if (hist_size < 100)
{
Selectivity heursel;
Selectivity prefixsel;
*hist_size = sslot.nvalues;
if (sslot.nvalues >= min_hist_size)
{
int nmatch = 0;
int i;
for (i = n_skip; i < sslot.nvalues - n_skip; i++)
{
if (varonleft ?
DatumGetBool(FunctionCall2Coll(opproc,
/* If not at least 100 entries, use the heuristic method */
if (hist_size < 100)
{
Selectivity heursel;
Selectivity prefixsel;
if (pstatus == Pattern_Prefix_Partial)
prefixsel = prefix_selectivity(root, &vardata,
eqopr, ltopr, geopr,
prefix);
static Selectivity
prefix_selectivity(PlannerInfo *root, VariableStatData *vardata,
Oid eqopr, Oid ltopr, Oid geopr,
Const *prefixcon)
{
Selectivity prefixsel;
FmgrInfo opproc;
AttStatsSlot sslot;
Const *greaterstrcon;
Selectivity eq_sel;
/*
* Pull out any fixed prefix implied by the pattern, and estimate the
* fractional selectivity of the remainder of the pattern. Unlike many
* other selectivity estimators, we use the pattern operator's actual
* collation for this step. This is not because we expect the collation
* to make a big difference in the selectivity estimate (it seldom would),
* but because we want to be sure we cache compiled regexps under the
* right cache key, so that they can be re-used at runtime.
*/
patt = (Const *) other;
/*
* Estimate the selectivity of a pattern of the specified type.
* Note that any fixed prefix of the pattern will have been removed already,
* so actually we may be looking at just a fragment of the pattern.
*
* For now, we use a very simplistic approach: fixed characters reduce the
* selectivity a good deal, character ranges reduce it a little,
* wildcards (such as % for LIKE or .* for regex) increase it.
*/
/* In any case, don't believe extremely small or large estimates. */
if (selec < 0.0001)
selec = 0.0001;
else if (selec > 0.9999)
selec = 0.9999;