Skip to content

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

Supabase 12 min read How-to mediumAdept

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

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.

Assumes: basic SQL queriesJSON

Related posts

  • What's New in PostgreSQL 14?

    Render mediumAdept

    PostgreSQL 14 adds jsonb subscripting, which lets queries read, filter and update nested JSONB values with JavaScript-like bracket syntax instead of path operators such as #>. It also adds multirange types, which store several non-overlapping ranges, such as an all-day meeting with a lunch break, as a single value rather than multiple rows. Concurrent connection handling is notably faster: pganalyze's pgbench tests showed roughly 20% higher throughput at 5,000 connections and 50% at 10,000 compared with PostgreSQL 13. Render offered PostgreSQL 14 on release day.

  • Understanding the new JSON capabilities in Postgres 16

    Neon mediumAdept

    Postgres 16 expands SQL/JSON syntax with JSON_ARRAY(), JSON_ARRAYAGG() and the IS JSON predicate, each shown with runnable examples. JSON_ARRAY builds an array from a list of values or from a subquery result. JSON_ARRAYAGG aggregates column values across rows into a single array and skips NULLs. IS JSON tests whether text parses as JSON and can check for a specific type (SCALAR, OBJECT, ARRAY) or for unique keys. Neon now supports Postgres 16, so these features can be tried there.

  • pg_graphql: A GraphQL extension for PostgreSQL

    Supabase mediumAdept

    pg_graphql is an open-source PostgreSQL extension, written in C and SQL, that reflects a GraphQL schema from existing tables and resolves queries entirely inside the database. Supabase built it because its 1 GB free-tier VMs had no memory left for a separate GraphQL process like Graphile or Hasura. Each GraphQL request is transpiled into a single SQL statement that aggregates results as JSON. This avoids N+1 queries, respects row level security, and gives multi-operation requests transactional semantics. Pagination follows the Relay connection spec using keyset pagination, the API is one SQL function (gql.resolve) exposed over HTTP via PostgREST, and a benchmark shows about 2,205 requests/second on a 4-core machine.

  • What's New in pg_clickhouse - JSONB Support, SQL value functions, Streaming, and more

    ClickHouse mediumAdept

    pg_clickhouse, the Postgres foreign data wrapper for querying ClickHouse, now pushes down far more operations to ClickHouse. JSONB accessors (->, ->>, jsonb_extract_path) are translated to ClickHouse sub-column syntax. CURRENT_DATE, CURRENT_TIMESTAMP and clock_timestamp() are translated to ClickHouse now()/now64()/nowInBlock64 calls that use the Postgres session time zone and timestamp precision. Array functions now map to their ClickHouse equivalents. Streaming of HTTP driver results in roughly 50MB batches kept container memory under about 100MiB during a full NYC taxi table scan, versus about 600MiB without it.

  • PostgREST Aggregate Functions

    Supabase mediumAdept

    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.

The quest for software engineering knowledge.

Look:
Layout: