gopgext/selectivity
import "gopgext/selectivity"
Package selectivity writes the selectivity estimators of operators in Go: the RESTRICT function (the fraction of a table’s rows “column op value” matches) and the JOIN function (the fraction of the pairs of rows “a op b” matches), which the planner uses to choose plans.
The extension registers estimators under the names of SQL functions (in main or a package initializer):
selectivity.RegisterRestriction("shape_overlaps_sel", func(r *selectivity.Restriction) float64 {
if r.Value == nil || r.Column == nil {
return 0.01 // not "column op constant": a default
}
...
})
and its sql/ files declare those functions, with this package’s C symbols, and use them in operators:
CREATE FUNCTION shape_overlaps_sel(internal, oid, internal, integer) RETURNS float8
AS 'MODULE_PATHNAME', 'gopgext_restriction_selectivity' LANGUAGE C STABLE STRICT;
CREATE FUNCTION shape_overlaps_joinsel(internal, oid, internal, smallint, internal) RETURNS float8
AS 'MODULE_PATHNAME', 'gopgext_join_selectivity' LANGUAGE C STABLE STRICT;
CREATE OPERATOR && (..., RESTRICT = shape_overlaps_sel, JOIN = shape_overlaps_joinsel);
Estimators get the column’s statistics, from ANALYZE: the fraction of NULLs, the number of distinct values, and, for types with the default “=” and “<” operators, the most common values with their frequencies and a histogram of the others. Values (the constant, statistics) are pg.AnyElement values, valid during the call. Results are clamped to [0, 1]; a NULL constant never matches (0, without calling the estimator).
Index#
func RegisterJoin(name string, f func(*Join) float64)func RegisterRestriction(name string, f func(*Restriction) float64)- type Column
- type Join
- type Restriction
Functions#
func RegisterJoin#
func RegisterJoin(name string, f func(*Join) float64)
RegisterJoin registers the estimator of SQL function name, a JOIN function with symbol gopgext_join_selectivity.
func RegisterRestriction#
func RegisterRestriction(name string, f func(*Restriction) float64)
RegisterRestriction registers the estimator of SQL function name, a RESTRICT function with symbol gopgext_restriction_selectivity.
Types#
type Column#
type Column struct {
Type pg.Oid
Rows float64 // estimated number of rows of its table (0: unknown)
// Statistics (HasStats false: not analyzed yet).
HasStats bool
NullFrac float64 // fraction of NULLs
// Distinct is the number of distinct non-NULL values: > 0 a count, < 0
// minus a fraction of the rows (-1: all distinct), 0 unknown.
Distinct float64
Width int // average width in bytes
// Most common values and their frequencies (fractions of all rows), and
// histogram bounds of the remaining values (sorted, equal-frequency
// buckets). Empty if the type has no default btree operator class, or
// the user can't read the column.
MCV []pg.AnyElement
MCVFreqs []float64
Histogram []pg.AnyElement
}
Column is a column (or an expression with statistics, e.g. of an expression index) a clause compares.
type Join#
type Join struct {
Operator pg.Oid
Left, Right *Column // nil: not a column
JoinType int // JOIN_INNER (0), JOIN_LEFT, ... (nodes.h)
}
Join is “a op b” with a and b of different tables.
type Restriction#
type Restriction struct {
Operator pg.Oid
Column *Column // nil: no column (e.g. an expression without statistics)
Value *pg.AnyElement // nil: not a constant (a parameter, another column...)
ColumnOnLeft bool
}
Restriction is “column op value” (or “value op column”: ColumnOnLeft).