← Back to PostgreSQL, page 4

postgresql-0061 · Medium mid

Bucket timestamps into 15-minute intervals with date_bin and fill empty buckets.

date_bin(stride, ts, origin) (PostgreSQL 14+) rounds a timestamp down to the start of its bucket for any interval, such as 15 minutes, which date_trunc cannot do. Grouping by the bucket counts events per interval, and joining to generate_series over the same buckets shows intervals with no events as zero.

Full answer

Setup for the example:

sql
CREATE TABLE requests (at timestamp, path text);
INSERT INTO requests VALUES
  ('2026-12-04 09:02', '/'), ('2026-12-04 09:14', '/pay'), ('2026-12-04 09:16', '/'),
  ('2026-12-04 09:58', '/'), ('2026-12-04 09:59', '/pay');
sql
SELECT date_bin('15 minutes', at, timestamp '2026-12-04 00:00') AS bucket, count(*) FROM requests GROUP BY 1 ORDER BY 1;

Output:

text
        bucket       | count
---------------------+-------
 2026-12-04 09:00:00 | 2
 2026-12-04 09:15:00 | 1
 2026-12-04 09:45:00 | 2
(3 rows)
sql
SELECT g AS bucket, count(r.at) AS requests FROM generate_series(timestamp '2026-12-04 09:00', timestamp '2026-12-04 09:45', interval '15 minutes') g LEFT JOIN requests r ON date_bin('15 minutes', r.at, timestamp '2026-12-04 00:00') = g GROUP BY g ORDER BY g;

PostgreSQL returns:

text
        bucket       | requests
---------------------+----------
 2026-12-04 09:00:00 | 2
 2026-12-04 09:15:00 | 1
 2026-12-04 09:30:00 | 0
 2026-12-04 09:45:00 | 2
(4 rows)

The first query shows three non-empty buckets: two requests at 09:00, one at 09:15 and two at 09:45. The 09:30 bucket is missing because nothing happened then. The second query generates all four buckets and counts matching requests with count(r.at), which ignores the NULLs from the outer join, so 09:30 shows 0. The origin fixes the bucket boundaries; an origin of 00:07 would produce buckets starting at :07, :22 and so on.

sql
CREATE TABLE requests (at timestamp, path text);
INSERT INTO requests VALUES
  ('2026-12-04 09:02', '/'), ('2026-12-04 09:14', '/pay'), ('2026-12-04 09:16', '/'),
  ('2026-12-04 09:58', '/'), ('2026-12-04 09:59', '/pay');

SELECT date_bin('15 minutes', at, timestamp '2026-12-04 00:00') AS bucket, count(*) FROM requests GROUP BY 1 ORDER BY 1;

SELECT g AS bucket, count(r.at) AS requests FROM generate_series(timestamp '2026-12-04 09:00', timestamp '2026-12-04 09:45', interval '15 minutes') g LEFT JOIN requests r ON date_bin('15 minutes', r.at, timestamp '2026-12-04 00:00') = g GROUP BY g ORDER BY g;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • date_bin buckets by any interval
  • Origin sets boundaries
  • generate_series fills gaps
  • count(col) ignores outer-join NULLs
  • PostgreSQL 14+

Common mistakes

  • Using count(*) after a LEFT JOIN, counting empty buckets as 1
  • Bucketing with string formatting

Follow-ups

  • How would you bucket in the user's time zone?
  • How does date_bin handle timestamptz?

Tips

  • Explain count(r.at)