Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Supabase 7 min read How-to mediumAdept

PostgREST Aggregate Functions

PostgREST v12 adds on-the-fly aggregate functions (avg, count, max, min, sum) to its API. You apply them by appending the function to a column in the select parameter, and any non-aggregated columns become implicit GROUP BY columns. Aggregates compose with column renaming, filtering, embedded resources and spread relationships, so you can group movies by a related director's country, for example. Because aggregates bypass pagination limits and can be abused for denial of service, the feature is disabled by default. Before enabling it, set per-role query cost ceilings with pg_plan_filter.

Assumes: SQL aggregate functions and GROUP BYPostgREST basics

Related posts

  • PostgREST 11 pre-release

    Supabase mediumAdept

    PostgREST 11's pre-release, available through the Supabase CLI, adds three querying features. A spread operator (`...table(cols)`) flattens embedded related-table columns into the parent object. Ordering by a related table's column is now supported, as are anti-joins via `is.null` filters on embedded resources. Spread and related ordering work only for one-to-one and many-to-one relationships for now. A `not.is.null` filter provides a more flexible alternative to the `!inner` modifier because it can be combined with `or` filters.

  • What is new in PostgREST v11.1?

    Supabase mediumAdept

    PostgREST 11.1 lets impersonated JWT roles (anon, authenticated, service_role) carry Postgres settings via ALTER ROLE ... SET, applied after a NOTIFY pgrst 'reload config'. That enables per-role statement_timeout and pg_plan_filter cost limits, where the cost limit rejects expensive queries at planning time but needs careful tuning. Transaction isolation is now configurable per function or role, alongside bulk JSON inserts that respect column defaults and new any/all filter modifiers. Breaking changes from v10 are small: the Range header is ignored on non-GET requests and Prefer: params=multiple-objects is dropped for RPC, and usage logs show almost no projects relied on either.

  • SQL or NoSQL? Why not use both (with PostgreSQL)?

    Supabase mediumAdept

    PostgreSQL's JSONB columns let an app store nested, variable-length data (here, a daily food, water and exercise log) as a single row instead of spreading it across several normalized tables, while keeping SQL available for analysis. A worked example builds a calendar table with JSONB log fields and inserts a whole day in one statement. It then uses jsonb_array_elements with the -> and ->> operators to flatten the arrays into rows. Because aggregates cannot wrap set-returning functions, the flattened query goes into a CTE before summing daily calories or filtering for specific items.

  • PostgREST 9

    Supabase mediumAdept

    PostgREST 9 adds inner-join resource embedding via the `!inner` keyword, so top-level rows can be filtered by conditions on an embedded table (e.g. messages filtered by users.username). Functions can now accept a single unnamed JSON parameter, which lets webhooks such as Postmark bounce notifications POST raw payloads straight to an rpc endpoint. PostgreSQL 14 compatibility forced a breaking change: per-claim and per-header settings are replaced by `request.jwt.claims` and `request.headers` JSON settings, so custom auth functions must be updated. Supabase's built-in auth helpers already handle this.

  • PostgREST 13

    Supabase mediumAdept

    PostgREST 13 adds spread syntax for to-many relationships so one-to-many and many-to-many joins can be returned as flat JSON arrays, automatic to_tsvector conversion that enables full text search on text and json/jsonb columns (backed by a GIN index on the expression), and a maxAffected limit that throws an error when an update, delete or rpc touches too many rows. For observability, responses now carry Content-Length and a Proxy-Status header with the PostgREST error code. Breaking changes: JWT kid claims are validated against the configured JWKS, and schemas in db-schemas and db-extra-search-path must exist, so schema drops should update the search path in the same transaction.

The quest for software engineering knowledge.

Look:
Layout: