PgRest.Catalog (PgRest v0.2.0)

Copy Markdown View Source

Reads metadata about registered tables from the PostgreSQL catalogs.

Only tables registered as resources are ever loaded, so the cache key space is bounded. Results are cached lazily in :persistent_term under {PgRest.Catalog, repo, kind, {schema, table}}; the server facts (facts/1) under {PgRest.Catalog, repo, :facts, :server}. PgRest.Planner.Columns memoizes its per-resource info under the same {PgRest.Catalog, repo, :info, _} namespace, so forget/1 clears both without the catalog depending on the planner.

Every load runs in its own short transaction that first sets a local empty search_path, so names of non-pg_catalog objects come back schema-qualified (test.status_enum). Because that setting is transaction-local, a load refuses to run inside an open transaction: it would leak into the caller's request. Resolve catalog data before opening the request transaction.

The catalog never leaves PgRest: type text is emitted into SQL only through PgRest.SQL.Builder.param/4.

Summary

Functions

Maps each column of schema.table to its pg_type.typcategory letter ("S" string, "E" enum, "N" numeric, ...). A domain carries its base type's category. Loaded together with column_types/3; %{} when the catalog is unreadable.

Maps each column of schema.table that has a default expression to that expression's text (pg_get_expr). An identity BY DEFAULT column maps to nextval(<sequence>), a domain default applies when the column has none, and generated columns have no default (PostgREST's SchemaCache).

Maps each column of schema.table to its format_type(atttypid, atttypmod) text.

The server facts of repo (PgRest.Catalog.Facts): the pg_timezone_names, the server_version_num and whether the login role (session_user, never current_user) is a superuser or BYPASSRLS, and the role settings RLS mode applies (PostgREST's queryRoleSettings). Two queries, cached under {PgRest.Catalog, repo, :facts, :server} until forget/1.

Clears every cached catalog entry for repo, and the info memoized from it (for tests).

Types

table_key()

@type table_key() :: {String.t(), String.t()}

Functions

column_categories(repo, schema, table)

@spec column_categories(module(), String.t(), String.t()) :: %{
  required(String.t()) => String.t()
}

Maps each column of schema.table to its pg_type.typcategory letter ("S" string, "E" enum, "N" numeric, ...). A domain carries its base type's category. Loaded together with column_types/3; %{} when the catalog is unreadable.

column_defaults(repo, schema, table)

@spec column_defaults(module(), String.t(), String.t()) :: %{
  required(String.t()) => String.t()
}

Maps each column of schema.table that has a default expression to that expression's text (pg_get_expr). An identity BY DEFAULT column maps to nextval(<sequence>), a domain default applies when the column has none, and generated columns have no default (PostgREST's SchemaCache).

Loaded with an empty search_path, so a default such as test.next_code() is schema qualified and stays correct when spliced into a statement that runs under another search_path. %{} when the catalog is unreadable. Raises ArgumentError inside an open transaction.

column_types(repo, schema, table)

@spec column_types(module(), String.t(), String.t()) :: %{
  required(String.t()) => String.t()
}

Maps each column of schema.table to its format_type(atttypid, atttypmod) text.

Returns %{} when the catalog is permanently unreadable (no privilege on pg_attribute, missing object; cached, with a warning once per repo) and the caller falls back to the Ecto type mapping. Transient failures (timeouts, failover) are re-raised and never cached. Raises ArgumentError inside an open transaction.

facts(repo)

@spec facts(module()) :: PgRest.Catalog.Facts.t()

The server facts of repo (PgRest.Catalog.Facts): the pg_timezone_names, the server_version_num and whether the login role (session_user, never current_user) is a superuser or BYPASSRLS, and the role settings RLS mode applies (PostgREST's queryRoleSettings). Two queries, cached under {PgRest.Catalog, repo, :facts, :server} until forget/1.

PgRest.Plug warms it before opening the request transaction, so the query never runs after a request switched roles. A failure raises and is not cached.

forget(repo)

@spec forget(module()) :: :ok

Clears every cached catalog entry for repo, and the info memoized from it (for tests).