time_bucket_ng() function is an experimental version of the
time_bucket() function. It introduced some new capabilities,
such as monthly buckets and timezone support. Those features are now part of the
regular time_bucket() function.
The
time_bucket() and time_bucket_ng() functions are similar, but not
completely compatible. There are two main differences.Firstly, time_bucket_ng() doesn’t work with timestamps prior to origin,
while time_bucket() does.Secondly, the default origin values differ. time_bucket() uses an origin
date of January 3, 2000, for buckets shorter than a month. time_bucket_ng()
uses an origin date of January 1, 2000, for all bucket sizes.Samples
In this example,time_bucket_ng() is used to create bucket data in three month
intervals:
time_bucket_ng() to bucket data in one year intervals:
time_bucket_ng() uses a starting point in time
called origin. The default origin is 2000-01-01. time_bucket_ng cannot use
timestamps earlier than origin:
origin isn’t usually possible, especially when you
consider timezones and daylight savings time (DST). Note also that there is no
reasonable way to split time in variable-sized buckets (such as months) from an
arbitrary origin, so origin defaults to the first day of the month.
To bypass named limitations, you can override the default origin:
time_bucket_ng() is used to bucket data
by months in a specified timezone:
time_bucket_ng() with continuous aggregates. This example tracks
the temperature in Moscow over seven day intervals:
The
by_range dimension builder is an addition to
2.13. For simpler cases, like this one, you can also create the
using the old syntax:While
time_bucket_ng() supports months and timezones,
continuous aggregates cannot always be used with monthly
buckets or buckets with timezones.time_bucket_ng() functions can be used in a continuous aggregate:
| Function | Available in continuous aggregate | version |
|---|---|---|
| Buckets by seconds, minutes, hours, days, and weeks | ✅ | 2.4.0 - 2.14.2 |
| Buckets by months and years | ✅ | 2.6.0 - 2.14.2 |
| Timezones support | ✅ | 2.6.0 - 2.14.2 |
| Specify custom origin | ✅ | 2.7.0 - 2.14.2 |
Arguments
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
bucket_width | INTERVAL | - | ✔ | A time interval for how long each bucket is |
ts | DATE, TIMESTAMP or TIMESTAMPTZ | - | ✔ | The timestamp to bucket |
origin | Should be the same as ts | - | ❌ | Buckets are aligned relative to this timestamp |
timezone | TEXT | - | ❌ | The name of the timezone. The argument can be specified only if the type of ts is TIMESTAMPTZ |
For backward compatibility with
time_bucket() the timezone argument is
optional. However, it is required for time buckets that are less than 24 hours.If you call the TIMESTAMPTZ-version of the function without the timezone
argument, the timezone defaults to the session’s timezone and so the function
can’t be used with continuous aggregates. Best practice is to use
time_bucket_ng(interval, timestamptz, text) and specify the timezone.Returns
The function returns the bucket’s start time. The return value type is the same asts.