Index: src/backend/utils/adt/selfuncs.c =================================================================== RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/selfuncs.c,v retrieving revision 1.187 diff -r1.187 selfuncs.c 1309a1310,1433 > * contstatsel - Selectivity of containment for any data types. > */ > Datum > contstatsel(PG_FUNCTION_ARGS) > { > PlannerInfo *root = (PlannerInfo *) PG_GETARG_POINTER(0); > Oid operator = PG_GETARG_OID(1); > List *args = (List *) PG_GETARG_POINTER(2); > int varRelid = PG_GETARG_INT32(3); > VariableStatData vardata; > Node *other; > bool varonleft; > Datum *values; > int nvalues; > double selec = 0.0; > > /* > * If expression is not variable = something or something = variable, > * then punt and return a default estimate. > */ > if (!get_restriction_variable(root, args, varRelid, > &vardata, &other, &varonleft)) > PG_RETURN_FLOAT8(0.001); > > /* > * If the something is a NULL constant, assume operator is strict and > * return zero, ie, operator will never return TRUE. > */ > if (IsA(other, Const) && > ((Const *) other)->constisnull) > { > ReleaseVariableStats(vardata); > PG_RETURN_FLOAT8(0.0); > } > > if (HeapTupleIsValid(vardata.statsTuple)) > { > Form_pg_statistic stats; > > stats = (Form_pg_statistic) GETSTRUCT(vardata.statsTuple); > > if (IsA(other, Const)) > { > /* Variable is being compared to a known non-null constant */ > Datum constval = ((Const *) other)->constvalue; > bool match = false; > int i; > > elog(INFO, "Checking histogram"); > > /* > * Is the constant "=" to any of the column's most common > * values? (Although the given operator may not really be > * "=", we will assume that seeing whether it returns TRUE is > * an appropriate test. If you don't like this, maybe you > * shouldn't be using eqsel for your operator...) > */ > if (get_attstatsslot(vardata.statsTuple, > vardata.atttype, vardata.atttypmod, > STATISTIC_KIND_HISTOGRAM, InvalidOid, > &values, &nvalues, > NULL, NULL)) > { > FmgrInfo contproc; > > fmgr_info(get_opcode(operator), &contproc); > > elog(INFO, "Found %d values", nvalues); > > for (i = 0; i < nvalues; i++) > { > /* be careful to apply operator right way 'round */ > if (varonleft) > match = DatumGetBool(FunctionCall2(&contproc, > values[i], > constval)); > else > match = DatumGetBool(FunctionCall2(&contproc, > constval, > values[i])); > if (match) > selec++; > } > > if (selec > 0.0 && nvalues > 0) > { > selec /= nvalues; > } > > elog(INFO, "Computed selectivity: %04.3f", selec); > > } > else > { > elog(INFO, "No histogram info"); > > /* no most-common-value info available */ > values = NULL; > i = nvalues = 0; > } > > if (!selec) > selec = 0.001; > > free_attstatsslot(vardata.atttype, values, nvalues, > NULL, 0); > } > else > selec = 0.001; > } > else > selec = 0.001; > > ReleaseVariableStats(vardata); > > /* result should be in range, but make sure... */ > CLAMP_PROBABILITY(selec); > > elog(INFO, "Returned selectivity: %04.3f", selec); > > PG_RETURN_FLOAT8((float8) selec); > } > > /* Index: src/include/catalog/pg_proc.h =================================================================== RCS file: /projects/cvsroot/pgsql/src/include/catalog/pg_proc.h,v retrieving revision 1.380 diff -r1.380 pg_proc.h 3752a3753,3755 > DATA(insert OID = 2600 ( contstatsel PGNSP PGUID 12 f f t f s 4 701 "2281 26 2281 23" _null_ _null_ _null_ contstatsel - _null_ )); > DESCR("enhanced restriction selectivity for containment comparison operators"); > Index: src/include/utils/selfuncs.h =================================================================== RCS file: /projects/cvsroot/pgsql/src/include/utils/selfuncs.h,v retrieving revision 1.23 diff -r1.23 selfuncs.h 97a98,99 > extern Datum contstatsel(PG_FUNCTION_ARGS); >