kura_capabilities behaviour (kura v2.31.3)

View Source

Capability 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 ... RETURNING
  • jsonb: JSONB column type with operators (the SQLite backend declares json instead, for its text-based JSON1 support)
  • arrays: array column types
  • advisory_locks: pg_advisory_lock family
  • listen_notify: LISTEN/NOTIFY pubsub
  • select_for_update_skip_locked: SELECT ... FOR UPDATE SKIP LOCKED
  • row_locks: the row locks of kura_query:lock/2: FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE and FOR KEY SHARE. A dialect may take a stronger lock than the one asked for, never a weaker one, so a backend with only FOR UPDATE and FOR SHARE declares it and takes FOR UPDATE for for_no_key_update and FOR SHARE for for_key_share
  • distinct_on: SELECT DISTINCT ON (...), from kura_query:distinct/2
  • cursors: SQL cursors, DECLARE ... CURSOR and FETCH, which kura_stream writes. A backend that reads a result in steps some other way does not declare it
  • partial_indexes: CREATE INDEX ... WHERE
  • concurrent_indexes: CREATE INDEX CONCURRENTLY; without it the migrator compiles concurrently => true to a plain CREATE INDEX for a non-Postgres dialect and refuses it for Postgres
  • window_functions: OVER (PARTITION BY ... ORDER BY ...)
  • full_text_search: the {Field, matches, Query} where-condition
  • vector: pgvector VECTOR(N) columns ({vector, N} type)
  • transactions: BEGIN/COMMIT/ROLLBACK
  • savepoints: SAVEPOINT/RELEASE/ROLLBACK TO
  • prepared_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 queryCapability
{Field, matches, Query}full_text_search
a {over, Fn, Spec} select expressionwindow_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 lockrow_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:

ModelChecked
none: no capability_model/0nothing
1full_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

capability()

-type capability() :: atom().

An open atom set. Backends may define custom capability atoms.

capability_set()

-type capability_set() :: [capability()].

A list of capabilities a backend supports or a consumer requires.

Callbacks

capabilities()

(optional)
-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.

capability_model()

(optional)
-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

check(PoolMod, Capability)

-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.

check_query(PoolMod, Query)

-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.

has(Module, Cap)

-spec has(module(), capability()) -> boolean().

Return true if Module declares Cap.

model(PoolMod)

-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.

require(Module, Required)

-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.

supported(Module)

-spec supported(module()) -> capability_set().

Return the capability set declared by Module.

Returns [] if the module does not export capabilities/0.