Skip to content

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

Supabase 4 min read How-to mediumAdept

Simplifying Time-Based Queries with Range Columns

Postgres range types such as tstzrange replace separate start_at/end_at columns with a single range value, which makes time-window queries and overlap prevention simpler. The && operator finds overlapping ranges directly, and an exclusion constraint using GiST rejects overlapping reservations at the database level. With the btree_gist extension, the constraint can be scoped per resource, for example `exclude using gist (table_id with =, duration with &&)`. That combination yields an overlap-proof reservations table without application-side checks.

Related posts

  • How and when to use btree_gist

    Neon mediumAdept

    Postgres's btree_gist extension lets a single GiST index combine one-dimensional B-Tree-style columns (like dates) with spatial PostGIS geometries, so queries filtering on both space and time can use one index scan. Using 38.6 million UK police crime records loaded via xsv and COPY, then transformed from WGS84 to British National Grid, a count of crimes within 1km of Stratford during the 2012 Olympics takes about 8 seconds unindexed. A GiST spatial index cuts that to about 32ms via a bitmap scan. A combined gist(location, month) index brings it to about 8ms with a plain Index Scan, roughly 1000x faster than no index.

  • Handling Time-Variant DAGs with Constraints in Postgres

    Neon mediumAdept

    Vehicle attachments in a fleet (trucks pulling trailers) are modeled as a time-variant DAG in a single Postgres table of edges with validity intervals and a source-authority priority. Deferred constraint triggers run PL/pgSQL checks at commit to reject cycles and overlapping incoming edges per node, so multi-statement batches can proceed before validation. An add_edge() helper resolves conflicts by truncating, splitting or deleting overlapping intervals according to priority. Recursive CTEs with temporal filters then answer point-in-time snapshots and downstream traversal queries; full code is open source.

  • The side quest that freed 10% of our database

    incident.io mediumAdept

    A single lateral-join query answering "who's on call for this rotation layer right now" consumed 34% of incident.io's Postgres replica CPU. It was using a GiST exclusion-constraint index that lacked organisation_id and start_at, so every lookup read a layer's full history. Adding a five-column btree index did nothing on its own, because the planner assumed the four ID columns were independent and kept choosing the GiST index. Rewriting the predicate as a row-value comparison with a matching ORDER BY, which only btree indexes can serve, forced the new index and cut per-call time from 44ms to 0.26ms, buffers by 115x and CPU share to 0.3%.

  • Postgres Changes gets AND filters, new operators, and column selection

    Supabase mediumAdept

    Supabase Realtime's Postgres Changes subscriptions now support AND-composed filters across multiple columns, new operators (like, ilike, is, match/imatch for POSIX regex, isdistinct, and not. negation), a typed filter builder, and a select option that returns only chosen columns plus the primary key. Server-side filtering and trimmed payloads cut billable messages and egress, since each delivered change counts per subscriber. Column selection needs supabase-js 2.109.0+, RLS still applies, DELETE events can't be column-filtered because they carry only the primary key, and OR composition, JSON/array containment, full-text and range operators remain unsupported.

  • Dynamic Table Partitioning in Postgres

    Supabase mediumAdept

    Postgres range partitioning can be retrofitted onto an existing "large table" (here, 500 million chat messages) without blocking queries by creating standalone daily child tables, bulk-loading them, adding a CHECK constraint that matches the partition range, and only then running ATTACH PARTITION so Postgres skips the validation scan and its blocking lock. The walkthrough builds a simulated chat app with pl/pgSQL functions that create partitions dynamically. It also covers why large tables hurt (btree depth, vacuum, archiving), the requirement that the partition key be part of composite primary and foreign keys, and ORM support gaps such as Django's lack of composite keys.

The quest for software engineering knowledge.

Look:
Layout: