Referencia · §8.17

Index access methods

index_method, §M14.12 V4b

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

An index is the built-in B-tree (an entry a row: the indexed values, then the primary key, holding the primary key) or an IndexMethod registered through Registry::add_index_method (process-wide, as storages): CREATE INDEX [name] ON t [USING method] (col [opclass], …) [WITH (name = value, …)] names it (USING btree, or none, the B-tree; an unknown method 42704; the B-tree takes no operator class, 42704, and no option, 22023; a method’s index is never UNIQUE nor in a taught ordering, 0A000). The index record (table record version 14) keeps the method, the operator classes and the options as the method’s define left them — it checks the columns’ types, refuses what it does not know and fills in defaults — and ALTER … TYPE of an indexed column asks it again.

A method keeps everything under its index’s prefix, so DROP INDEX, DROP TABLE and deleting the table’s data need nothing of it. dml::IndexKeys tells it every change in the writing statement’s transaction (write(txn, t, i, old, new): the row as it was and as it is, primary key bytes and values; None for an insert’s old and a delete’s new) — after the storage took the row, so a refused row never reaches it; a rollback undoes its writes with the rest. build makes it over the rows a table has (CREATE INDEX, and after a rewrite of the table’s rows: ALTER … TYPE, a key encoding upgrade; index_method::rows reads them), and check checks it for the integrity check against the table’s rows. The optimizer’s key ranges take only plain B-trees (Index::ordered); a method’s own search is an access path of its own (Access::Method: the condition it serves, the order it ranks by, or both).

Two hooks let a method serve a filter’s condition and decide what an operator means on its column (§M14.21 T3, the text index’s, §8.19). operand(ix, op): a value the binder appends to operator op‘s call when its left side is a table column the index is on (ScopeCol::origin, Binder::index_operand) — the text index’s analyzer for @@, so the operator means the same through the index and on the rows; the first index that gives one wins. serves(ix, function) and rows_where(txn, t, i, function, args): a conjunct of a scan’s filter f(col, args…) with col the index’s first column and the arguments fixed for the scan (optimizer::fixed) — and the operand the index gives, so a statement bound before the index existed is not served — becomes Access::Method with served: Served { function, args, cond } (optimizer::method_filters: a full scan, or a key range estimated past METHOD_OVER_RANGE rows, 1,000 — without statistics, any key range but an equality on every column of its index’s key, which stays: a condition with a parameter has no estimate either, and a memory store’s subject = $1 must not become every subject’s matches, §8.28), the conjunct out of the filter — under row-level security a statement’s conjunct above the barrier too, as it may narrow a key range, staying there (Served::above: where the index cannot tell the rows they are read without it, so it is never evaluated below the policy); exec::method_rows asks for the primary keys and reads their rows (none when the statement reads no column and nothing else is checked: the method is exact; a lossy one says it is not — exact(ix, function) false, the JSON containment index’s hashes, §8.31 — and the conjunct then stays in the filter, checked on each row found, PostgreSQL’s recheck) — in key order, a block at a time (8, then twice the last up to 256, four times past it, up to 16,384, what is left joining a block the next would end with: a limit above stops it early), by a merge with the table’s scan (exec::rows_at, TableStorage::rows_at; the row storage’s exec::entries_at, PLAN.md §M14.21 T5b): where the keys lie far apart they are looked up a group of GET_MANY at a time (their descents’ cache misses overlapped), where they lie close the scan reads on through the entries between, passing the unwanted by their keys — chosen by the share wanted of the entries passed, measured as it goes (a probe of 16 entries where it is not yet known and every 1,024th lookup, each chunk read on) and kept across the statement’s blocks, reading on above one in SEEK_ENTRIES (16, what a lookup costs in entries read); past 256 keys a run each on the statement’s workers over the transaction’s shared reads — the filter (the recheck with the rest of the conditions) checked on the workers too where it may run on any thread (vector::parallel_safe_in, no enclosing row), so they give back only the rows that pass (issue #10). Where rows_where says None every row is read with cond checked. EXPLAIN: Index Scan using ix on t (col f arg). selectivity(txn, t, i, function, args): the share of the table’s rows such a conjunct holds for, where the index tells it at less cost than the statistics’ kept sample (§8.8), which it is asked before; by default None. The text index’s @@: each word’s documents over the index’s, the words as independent — all of them the product, any of them one less the product of their misses, a phrase as all of its words, an excluded one its miss (query::share, PostgreSQL’s tsquery_opr_selec over exact counts where PostgreSQL has a sample’s most common words); a prefix or a fuzzy word leaves it to the sample.

Two more let a method compute a function of such a condition and rank the rows it finds (§M14.21 T5, BM25’s). function(t, i, name, arg, parts): name(arg) with arg an operator’s call whose left side is a column the index is on (Binder::operator_origin, Binder::index_function) bound as the method says — an expression and its type in place of the call (score(body @@ q): §8.19); none, and the function is bound as any other. parts, for a partitioned table under row-level security, is the partitions the statement may read (a text[], NULL where the policies rule one out), for what the function reads of the index as a whole (BM25’s statistics; §9, “Retrieval under it”).

One more ranks a table’s rows for ORDER BY f(col, args…) LIMIT k [OFFSET o] — HNSW’s nearest rows (§8.18) and the text index’s first by BM25 (§8.19) through one hook, one access path and one optimizer pass (PLAN.md §M14.21 T5a). ranks(ix, function, desc, nulls_first): the orders it ranks by; rank_cost(ix, RankCost { function, served, rows, k, share }): what ranking costs in rows read (HNSW’s ef · (3 + 2m / 10); the text index’s k over the share passing, never more than the rows its condition finds), None where it cannot; rows(txn, t, i): the rows it holds, for a table not analyzed. The optimizer’s ranked (its last pass) finds f the first sort key of a Limit over a Sort (through a projection), the column f‘s first argument (or either of a distance’s two) and the rest fixed; under the sort, a scan — of each partition under an Append, each choosing for itself — under row-level security perhaps, whose policy and statement filter above its barrier become the rank’s checks, in that order. The index is the scan’s own where its condition is served by one that ranks (the text index), else one on the column that ranks by f, the one with the most of its other columns held equal by the conditions (a partitioned HNSW’s partition). A scan through a B-tree’s range of the filter or another method’s condition keeps it as the rank’s within and always ranks: its rows are ranked among, or checked where they turn out many. Otherwise it ranks where rank_cost is at most the rows the scan reads (the table’s, the range’s or the condition’s estimate), with share the filter’s and the checks’ selectivity. The scan becomes Access::Method with rank: MethodRank { function, args, desc, count, within, share, checks, partition, value } (count the limit plus the offset); the sort and the limit stay above. Where the method gives each row’s value of f with it — values(ix, f), the very number f computes of the row (the text index’s BM25; by default none) — and a projection under the sort computes f (of one table’s scan, not each partition’s), value is the call: the scan’s rows carry its value one more column past the table’s (Plan::width), and the projection’s call, wherever it is in its expressions, reads that column instead (PLAN.md §M14.21 T5c). Where the index cannot rank after all, the scan computes the call on each row it reads (prune reads the columns it takes).

exec::method_rows evaluates them and calls rank(txn, t, i, RankAsk { rank, served, share, part, among, values }, wanted, keep): keep the join’s key filters, then within’s condition (unless the row came from among), the filter and the checks — None where nothing checks; and among (exec::among) a callback the method calls with the most rows it takes, which reads the B-tree range’s primary keys or asks the other method’s rows_where, None past that many. The range’s entries are read with their keys left escaped (only the values, the primary keys, are wanted); the filter’s conjuncts that read only an integer primary key (exec::early_conditions: nothing volatile, no subquery) are checked on each entry’s key first, a false one dropping it before its vector is compared — up to sixteen times the most entries read so. The method gives at least k rows that pass, in any order — an exact one every row tied with the k-th passing — or None where it cannot tell them; the scan then reads within’s rows (or every row) with the filter, the sort and the limit above taking the first. EXPLAIN: Index Scan using ix on t (col f arg) first k by f(col) [DESC] [with values] for a condition ranked, (k nearest col to q [in c = …] [among ix: …]) for HNSW’s — among docs_body: body @@ 'fox' where the text index’s matches are what it ranks.

crates/sql/tests/index_method.rs registers a method that mirrors a column and runs writes, rollbacks, a rewrite, the integrity check (a stray entry found), the refusals and a drop through it; 06_indexes.slt the syntax.