惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
博客园_首页
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
B
Blog RSS Feed
D
Docker
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
V
V2EX
量子位
雷峰网
雷峰网
月光博客
月光博客
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog

Supabase Blog

AI Agents Know About Supabase. They Don't Always Use It Right. Custom OIDC Providers for Supabase Auth 100,000 GitHub stars Supabase docs over SSH Navigating Regional Network Blocks Supabase Joins the Stripe Projects Developer Preview Log Drains: Now available on Pro Supabase Storage: major performance, security, and reliability updates Supabase incident on February 12, 2026 Hydra joins Supabase X / Twitter OAuth 2.0 is now available for Supabase Auth BKND joins Supabase Supabase is now an official Claude connector Supabase PrivateLink is now available Introducing: Postgres Best Practices When to use Read Replicas vs. bigger compute Introducing TRAE SOLO integration with Supabase Supabase Security Retro: 2025 Sync Stripe Data to Your Supabase Database in One Click Building ChatGPT Apps with Supabase Edge Functions and mcp-use Own Your Observability: Supabase Metrics API Introducing iceberg-js: A JavaScript Client for Apache Iceberg Introducing Supabase for Platforms Adding Async Streaming to Postgres Foreign Data Wrappers Build "Sign in with Your App" using Supabase Auth Introducing Seven New Email Templates for Supabase Auth The new Supabase power for Kiro Introducing Supabase ETL Introducing Analytics Buckets Introducing Vector Buckets
Simplifying Time-Based Queries with Range Columns
Tyler Shukert · 2024-07-11 · via Supabase Blog

Simplifying Time-Based Queries with Range Columns

When working on applications such as a reservation app or calendar app, you need to store the start time and end time of an event. You may also need to query events occurring in a specific time frame or ensure that certain events do not overlap. If you have a table with two separate columns start_at and end_at to hold the beginning and end of an event, it might be hard to perform advanced queries or add constraints to prevent overlaps. This article will show how range-type columns could provide helpful query functionalities and advanced constraints to avoid overlapping.

Traditionally, when dealing with events or periods, developers often use two separate columns to represent the start and end of a range. For example:


_10

create table reservations (

_10

id serial primary key,

_10

title text,

_10

start_at timestamptz,

_10

end_at timestamptz

_10

);


While this approach works, it has a few drawbacks:

  1. Querying Complexity: Writing queries to find overlapping events or events within a specific period becomes complex and error-prone.
  2. Data Integrity: Ensuring that reservations do not overlap is difficult.

Range types are data types in Postgres that hold the beginning and end of a range of a base type. The range of int4 is int4range, the range of timestamptz is tstzrange, and the range of date is daterange. Each range has a start value, an end value, and either square brackets [] or parenthesis () surrounding them. A bracket means the end is inclusive, and a parenthesis means the end is exclusive. An int4range of [2,5) represents a range of integers from 2 including it to 5 excluding it, so 2, 3, and 4.

Querying range columns#

Using these range values, we can create a reservation table like the following:


_10

create table reservations (

_10

id serial primary key,

_10

title text,

_10

duration tstzrange

_10

);


Using tstzrange instead of two timestamptz columns have a few advantages. First, it allows us to easily query reservations that overlap with a provided range using the && operator. Look at the following select query:


_10

select *

_10

from reservations

_10

where duration && '[2024-07-04 16:00, 2024-07-04 19:00)';


This query returns rows where the duration overlaps with [2024-07-04 16:00, 2024-07-04 19:00). For example, a row with [2024-07-04 18:00, 2024-07-04 21:00) will be returned, but a row with [2024-07-04 20:00, 2024-07-04 22:00) will not be returned. The overlaps operator can be used when finding reservations or events in a given period.

Postgres provides more range-specific operators. The official Postgres documentation provides a complete list of range operators.

Adding constraints on range columns#

When working on a reservations app, you might want to ensure there are no overlapping reservations. Range columns make it easy to add such constraints. The following SQL statement adds an exclude constraint that prevents new inserts/ updates from overlapping on any of the existing reservations.


_10

alter table reservations

_10

add constraint exclude_duration exclude

_10

using gist (duration with &&)


With the above constraint, the second insert on the following SQL statements fails because the duration overlaps with the first insert.


_10

-- Add a first reservation

_10

insert into reservations (title, duration)

_10

values ('Tyler Dinner', '[2024-07-04 18:00, 2024-07-04 21:00)');

_10

_10

-- The following insert fails because the duration overlaps with the above

_10

insert into reservations (title, duration)

_10

values ('Thor Dinner', '[2024-07-04 20:00, 2024-07-04 22:00)');


Now, the exclusion constraint prevents any reservations from overlapping, but in the real world, a single reservations table typically holds reservations for different restaurants and tables within a restaurant, and just because a single reservation was made at a restaurant, it does not mean the entire restaurant is booked. Postgres can create such constraints where an insert or an update is disallowed only if a specific other column matches and the range overlaps.

Let’s say we had a table_id column in our reservations table. This table_id could represent a single table in various restaurants this database holds.


_10

create table reservations (

_10

id serial primary key,

_10

title text,

_10

table_id int4,

_10

duration tstzrange

_10

);


With a table_id column in place, we can add a constraint to ensure that reservations on the same table do not overlap. The constraint requires the btree_gist extension.


_10

-- Enable the btree_gist index required for the constraint.

_10

create extension btree_gist

_10

_10

-- Add a constraint to prevent overlaps with the same table_id

_10

alter table reservations

_10

add constraint exclude_duration

_10

exclude using gist (table_id WITH =, duration WITH &&);


With this simple constraint, no two reservations will overlap with each other with the same table_id. If we run the following inserts, the second insert will fail because it is trying to book the same table as the first insert while the duration overlaps.


_11

-- Add a first reservation

_11

insert into reservations (title, table_id, duration)

_11

values ('Tyler Dinner', 1, '[2024-07-04 18:00, 2024-07-04 21:00)');

_11

_11

-- Insert fails, because table 1 is taken from 18:00 - 21:00

_11

insert into reservations (title, table_id, duration)

_11

values ('Thor Dinner', 1, '[2024-07-04 20:00, 2024-07-04 22:00)');

_11

_11

-- Insert succeeds because table 2 is not taken by anyone

_11

insert into reservations (title, table_id, duration)

_11

values ('Thor Dinner', 2, '[2024-07-04 20:00, 2024-07-04 22:00)');


And that is how to create an air-tight table that holds reservations.

Postgres's range columns offer a solution for handling range data in applications like reservation systems. They simplify queries with specific operators such as && and improve data integrity by enabling constraints to prevent overlaps. Range columns provide an alternative to traditional two-column approaches for representing periods. By leveraging these features, developers can create more sophisticated and reliable applications with less code.