Applies a policy to one scoped source. Authorization.check/5 runs first; the query
callback can only narrow the base query. Columns are physical database column names.
Differences from PostgREST
Query policies are an application extension to database privileges/RLS. Stars expand
to permitted columns; an explicit hidden-column reference is 42501. Counts and aggregates
require explicit grants and permission limits cap their input rows, including totals.
PostgREST's db-max-rows limits output rows and does not cap exact totals.
With a permission cap, all count modes return the exact capped total instead of
a planner estimate, so an estimate cannot disclose rows outside the visible set.
RPCs with result_scope: :none discard row predicates only; hook denials, columns,
count/aggregate grants and permission limits still apply.
Hook narrowing needs a primary or unique key for source intersection; otherwise 42501.
PostgREST can apply RLS to keyless tables.
Summary
Functions
Checks Ecto field names against a policy's physical column allowlist.
Whether an Ecto field belongs to a physical-column grant.
Intersects grants, retaining the left flags; columns: :left retains a read grant.
Explicit EXPLAIN admission; the plan still explains the original authorized statement.
Intersects physical-column grants.
The smaller non-nil permission limit.
Whether every physical source name belongs to a column grant.
Authorizes and narrows one path/operation. Options :authorize? and :rows?
default to true; disabling the bare check never disables query narrowing.
With rows?: false, the hook still decides admission, columns, limits and grants,
but its row predicate is discarded without requiring a key.
An unrestricted policy over a source (nil for a non-resource result).
Types
@type policy() :: %{ query: Ecto.Query.t() | nil, columns: :all | MapSet.t(String.t()), limit: pos_integer() | nil, aggregates?: boolean(), count?: boolean() }
Functions
Checks Ecto field names against a policy's physical column allowlist.
Whether an Ecto field belongs to a physical-column grant.
Intersects grants, retaining the left flags; columns: :left retains a read grant.
@spec explain_granted?(module(), Plug.Conn.t(), module(), Ecto.Query.t(), map()) :: boolean()
Explicit EXPLAIN admission; the plan still explains the original authorized statement.
Intersects physical-column grants.
@spec min_limit(non_neg_integer() | nil, non_neg_integer() | nil) :: non_neg_integer() | nil
The smaller non-nil permission limit.
Whether every physical source name belongs to a column grant.
@spec source( Plug.Conn.t(), module() | nil, module(), PgRest.Authorization.operation(), Ecto.Query.t(), map(), keyword() ) :: {:ok, policy()} | {:error, :denied}
Authorizes and narrows one path/operation. Options :authorize? and :rows?
default to true; disabling the bare check never disables query narrowing.
With rows?: false, the hook still decides admission, columns, limits and grants,
but its row predicate is discarded without requiring a key.
@spec unrestricted(Ecto.Query.t() | nil) :: policy()
An unrestricted policy over a source (nil for a non-resource result).