---
title: "Simplifying Time-Based Queries with Range Columns"
description: "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."
url: "https://swe.quest/articles/simplifying-time-based-queries-with-range-columns-42d3f987b6918835"
canonical: "https://supabase.com/blog/range-columns"
source: "Supabase"
published: "2024-07-11"
difficulty: "medium"
kind: "How-to"
reading_minutes: 4
tags: ["postgres","sql","databases"]
---

# Simplifying Time-Based Queries with Range Columns

A post on [Supabase](https://swe.quest/sources/supabase.md), published 2024-07-11, 4 min read, How-to. Original: [https://supabase.com/blog/range-columns](https://supabase.com/blog/range-columns)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- Postgres table constraints ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))

## Keywords

PostgreSQL, tstzrange, exclusion constraints, GiST, btree\_gist, Supabase

## Related posts

- [How and when to use btree\_gist](https://swe.quest/articles/how-and-when-to-use-btree-gist-a2bcb15ac54b481b.md): Neon · 2024-07-08 · Medium · 10 min read · PostgreSQL, Maps & geospatial, Performance engineering, SQL
  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.
- [Handling Time-Variant DAGs with Constraints in Postgres](https://swe.quest/articles/handling-time-variant-dags-with-constraints-in-postgres-1fba8d43f4c83cc3.md): Neon · 2025-10-20 · Medium · 7 min read · PostgreSQL, SQL, Data structures, Databases
  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.
- [The side quest that freed 10% of our database](https://swe.quest/articles/the-side-quest-that-freed-10-of-our-database-649662623bb94a1e.md): incident.io · 2026-11-24 · Medium · 13 min read · PostgreSQL, Query engines, Performance engineering, SQL
  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.
- [Postgres Changes gets AND filters, new operators, and column selection](https://swe.quest/articles/postgres-changes-gets-and-filters-new-operators-and-column-selection-74cc67b661b6c154.md): Supabase · 2026-08-05 · Medium · 3 min read · Real-time systems, PostgreSQL, API design
  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.
- [Dynamic Table Partitioning in Postgres](https://swe.quest/articles/dynamic-table-partitioning-in-postgres-13de90b4fe06270e.md): Supabase · 2023-10-03 · Medium · 20 min read · PostgreSQL, Schema migrations, SQL, Scaling
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
