kura_capabilities behaviour (kura v2.31.3)
View SourceCapability flags for kura backends and pool implementations.
A module that participates in kura's backend story (today: the pool
implementation; in 2.0 likely a dedicated backend aggregator) declares
the capability set it supports by implementing the optional
capabilities/0 callback. Consumers ask kura whether the running
backend supports the features they need.
This makes "we don't support that on this backend" a boot error instead of a 3am pager. It is also the discipline that keeps the future multi-backend story honest: a non-SQL backend (Mnesia, Riak) will declare a small capability set, SQL-needing consumers will refuse to start, and there will be no runtime surprises.
Standard capabilities
The capability atom set is open: backends may declare custom flags and consumers may match against them. The standard atoms kura itself cares about:
returning:INSERT/UPDATE/DELETE ... RETURNINGjsonb:JSONBcolumn type with operators (the SQLite backend declaresjsoninstead, for its text-based JSON1 support)arrays: array column typesadvisory_locks:pg_advisory_lockfamilylisten_notify:LISTEN/NOTIFYpubsubselect_for_update_skip_locked:SELECT ... FOR UPDATE SKIP LOCKEDrow_locks: the row locks ofkura_query:lock/2:FOR UPDATE,FOR NO KEY UPDATE,FOR SHAREandFOR KEY SHARE. A dialect may take a stronger lock than the one asked for, never a weaker one, so a backend with onlyFOR UPDATEandFOR SHAREdeclares it and takesFOR UPDATEforfor_no_key_updateandFOR SHAREforfor_key_sharedistinct_on:SELECT DISTINCT ON (...), fromkura_query:distinct/2cursors: SQL cursors,DECLARE ... CURSORandFETCH, whichkura_streamwrites. A backend that reads a result in steps some other way does not declare itpartial_indexes:CREATE INDEX ... WHEREconcurrent_indexes:CREATE INDEX CONCURRENTLY; without it the migrator compilesconcurrently => trueto a plainCREATE INDEXfor a non-Postgres dialect and refuses it for Postgreswindow_functions:OVER (PARTITION BY ... ORDER BY ...)full_text_search: the{Field, matches, Query}where-conditionvector: pgvectorVECTOR(N)columns ({vector, N}type)transactions:BEGIN/COMMIT/ROLLBACKsavepoints:SAVEPOINT/RELEASE/ROLLBACK TOprepared_statements: server-side prepared statements
A module that does not implement capabilities/0 is treated as having
the empty capability set. Anything it does support is opaque to kura.
Queries
A query is checked against the repo's pool module when it is compiled, so
an operation the backend does not declare raises
{kura, {unsupported_capability, Capability, PoolModule}} before any SQL
is built, instead of failing in the database:
| In the query | Capability |
|---|---|
{Field, matches, Query} | full_text_search |
a {over, Fn, Spec} select expression | window_functions |
a {distance, Field, Metric, Vector} | vector |
kura_query:lock(Q, {Strength, skip_locked}) | select_for_update_skip_locked |
kura_query:lock(Q, Lock), any lock | row_locks |
kura_query:distinct(Q, Fields) | distinct_on |
CTEs, combinations and subqueries are checked too. kura_stream:stream/4
raises the same error for cursors.
The pool module is the pool_module of the repo's settings
(kura_repo:settings/1), which a started pool always has. A repo whose
config names none and whose pool kura has not started, such as one a unit
test gives only a dialect to compile SQL, is not checked.
Capability model
kura 2.30 checked nothing, so a list written for it says what a backend
has and not what it lacks: it may leave out something the backend runs. A
backend therefore says which revision of the checks its list was written
against, with the optional capability_model/0 callback, and only then is
it checked:
| Model | Checked |
|---|---|
none: no capability_model/0 | nothing |
| 1 | full_text_search, window_functions, vector, select_for_update_skip_locked, row_locks, distinct_on, cursors |
A model number never changes its meaning. When kura starts checking another
capability, that is a new model, and a backend on an earlier one is not
checked for it. A value that is not a model of this kura is refused when
the repo's pool is started, with
{error, {invalid_capability_model, PoolModule, Value}}, and raises
{kura, {invalid_capability_model, PoolModule, Value}} from a check.
kura 3.0 makes the latest model mandatory: a pool module without
capability_model/0, or on an earlier model, is checked as if it declared
the latest.
Example
A consumer's supervisor refuses to start if PG-only features are missing:
init(_) ->
case kura_capabilities:require(kura_pool_minato, [returning, listen_notify]) of
ok -> {ok, ...};
{error, _} = Err -> Err
end.A backend declares its set:
-module(my_pool).
-behaviour(kura_pool).
capabilities() ->
[returning, jsonb, arrays, advisory_locks, listen_notify,
select_for_update_skip_locked, row_locks, distinct_on, cursors,
partial_indexes, window_functions, full_text_search, transactions,
savepoints, prepared_statements].
capability_model() -> 1.
Summary
Types
An open atom set. Backends may define custom capability atoms.
A list of capabilities a backend supports or a consumer requires.
Callbacks
Optional callback. Return the set of capabilities this module supports.
Optional callback. Return the revision of the capability checks the list
from capabilities/0 was written against: 1. See the moduledoc. A
module without it is not checked.
Functions
Check one capability as check_query/2 checks those a query needs: raises
{kura, {unsupported_capability, Capability, PoolMod}} when PoolMod
declares a capability model, that model checks Capability, and its list
leaves it out. has/2 and require/2 answer for any capability, checked
or not.
Check that PoolMod declares every capability Query needs. See the
moduledoc for what needs which. Raises
{kura, {unsupported_capability, Capability, PoolMod}} for the first one
missing. A module without capability_model/0 passes.
Return true if Module declares Cap.
The capability model PoolMod declares, or unchecked when it has no
capability_model/0. Raises
{kura, {invalid_capability_model, PoolMod, Value}} for a value that is
not a model of this kura.
Return ok if Module declares every capability in Required.
Return the capability set declared by Module.
Types
-type capability() :: atom().
An open atom set. Backends may define custom capability atoms.
-type capability_set() :: [capability()].
A list of capabilities a backend supports or a consumer requires.
Callbacks
-callback capabilities() -> capability_set().
Optional callback. Return the set of capabilities this module supports.
Modules that do not implement this callback are treated as supporting nothing.
-callback capability_model() -> pos_integer().
Optional callback. Return the revision of the capability checks the list
from capabilities/0 was written against: 1. See the moduledoc. A
module without it is not checked.
Functions
-spec check(module(), capability()) -> ok.
Check one capability as check_query/2 checks those a query needs: raises
{kura, {unsupported_capability, Capability, PoolMod}} when PoolMod
declares a capability model, that model checks Capability, and its list
leaves it out. has/2 and require/2 answer for any capability, checked
or not.
-spec check_query(module(), #kura_query{from :: atom() | module() | binary() | undefined, select :: [atom() | term()] | {exprs, [term()]}, wheres :: [term()], joins :: [term()], order_bys :: [term()], group_bys :: [atom()], havings :: [term()], limit :: non_neg_integer() | undefined, offset :: non_neg_integer() | undefined, distinct :: boolean() | [atom()], lock :: kura_query:lock() | binary() | undefined, prefix :: binary() | undefined, preloads :: [atom() | {atom(), list() | map()}], ctes :: [{binary(), #kura_query{}}], combinations :: [{union | union_all | intersect | except, #kura_query{}}], include_deleted :: boolean()}) -> ok.
Check that PoolMod declares every capability Query needs. See the
moduledoc for what needs which. Raises
{kura, {unsupported_capability, Capability, PoolMod}} for the first one
missing. A module without capability_model/0 passes.
The query is only read when the module lacks one of the capabilities its model checks.
-spec has(module(), capability()) -> boolean().
Return true if Module declares Cap.
-spec model(module()) -> unchecked | 1.
The capability model PoolMod declares, or unchecked when it has no
capability_model/0. Raises
{kura, {invalid_capability_model, PoolMod, Value}} for a value that is
not a model of this kura.
-spec require(module(), capability_set()) -> ok | {error, {missing_capabilities, capability_set()}}.
Return ok if Module declares every capability in Required.
Otherwise return {error, {missing_capabilities, Missing}} listing
the capabilities that the module did not declare. Intended to be
called from a supervisor init so the application refuses to start
on a backend that lacks required features.
-spec supported(module()) -> capability_set().
Return the capability set declared by Module.
Returns [] if the module does not export capabilities/0.