question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add time bucketing function `time_bucket(<interval>,<timestamp>)` || `date_bin(<interval>,<timestamp>)`

See original GitHub issue

Use case: Right now one can only use date_trunc() to easily define time buckets. date_trunc() only supports predefine time intervals like 1 minute, 1 hour, etc. . In time-series use cases it is often necessary to define different time bucket sizes like e.g. ‘5 minutes’ or ‘20 minutes’

a workaround for this is the - error prone - integer division on the timestamp e.g.

SELECT 
(ts / ( 300 * 1000)) * 300 * 1000 AS "bucket",
avg(metric)
FROM data
GROUP BY bucket
ORDER BY bucket DESC LIMIT 10;

Feature description Have the ability to pass other time bucket sizes / intervals to date_trunc(), e.g. date_trunc('5 minutes',ts) or have a separate time_bucket(INTERVAL,TIMESTAMP) function that takes an interval e.g. '5 minutes' and a timestamp as input and returns the corresponding bucket (i.e. a timestamp that has the starting value of the bucket)

SELECT
time_bucket('5 minutes', ts) AS "bucket",
avg(metric)
FROM data
GROUP BY bucket
ORDER BY bucket DESC LIMIT 10;

Timescale time_bucket() function TS docs GH implementation

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
proddatacommented, Jul 19, 2021

PostgreSQL 14 will implement date_bin(stride, source, origin) which is quite similar to the above mentioned time_bucket() definition. To improve compatibility with Postgres Wire Protocol, I think this should be the preferred solution. However the origin parameter should be optional, for usability reasons.

default origin is unix 0, Thursday, 01.01.1970 - should be there an option to provide custom origin for easier bucketing by weeks? with origin = unix ts 0, time_bucket(‘1 week’::INTERVAL, ‘1969-01-09T00:00:00Z’::timestamp without time zone) returns -31449600000 which is Thursday, 02.01.1970.

This doesn’t necessarily have to do with the default origin, but I would expect time_bucket('1 week',<timestamp>) to return a Monday 00:00:00.000Z. I think this could be achieved by e.g. setting the default origin to 2001-01-01 or any other year that starts with a Monday.

To follow the new date_bin function, it might be good to be able to specify a origin.

@BaurzhanSakhariev

1reaction
mfusseneggercommented, Jul 19, 2021

should the null handling be the same as date_trunc?

select DATE_TRUNC('day', null) - returns null select DATE_TRUNC(null, CURRENT_TIMESTAMP) - returns null select DATE_TRUNC(null, null) - returns null

yes

Read more comments on GitHub >

github_iconTop Results From Across the Web

time_bucket - Timescale Docs
Buckets are aligned relative to this timestamp. Defaults to midnight on January 3, 2000, for buckets that don't include a month or year...
Read more >
TimescaleDB time_bucket() function giving unexpected results ...
I have created a hypertable water_meter to store the sensor data. It contains following data ordered by timestamp in ascending order
Read more >
TIME_BUCKET - SingleStore Documentation
A function that normalizes time to the nearest bucket start time. Syntax. TIME_BUCKET (bucket_width [, time [,origin]]). Arguments.
Read more >
Support for month, year and time zones in time_bucket #414
I would like to know how to get month bucket and even yearly buckets? ... FUNCTION tools.time_bucket ( bucket_width interval, ts timestamp, ...
Read more >
Bucketizing date and time data - SQLPerformance.com
The timeline is then divided into discrete 2-month intervals starting with the origin point. The input timestamp is the DATETIME2 value May 10...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found