timescaledb_experimental.time_bucket_ng()

The 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.

deprecation

This section describes a feature that is deprecated on TimescaleDB. We strongly recommend that you do not use this feature in a production environment. If you need more information, contact the support team.

warning

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.

Required arguments

NameTypeDescription
bucket_widthINTERVALA PostgreSQL time interval for how long each bucket is
tsDATE, TIMESTAMP or TIMESTAMPTZThe timestamp to bucket

Optional arguments

NameTypeDescription
originShould be the same as tsBuckets are aligned relative to this timestamp
timezoneTEXTThe 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 as ts.

Sample usage

In this example, time_bucket_ng() is used to create bucket data in three month intervals:

  1. SELECT timescaledb_experimental.time_bucket_ng('3 month', date '2021-08-01');
  2. time_bucket_ng
  3. ----------------
  4. 2021-07-01
  5. (1 row)

This example uses time_bucket_ng() to bucket data in one year intervals:

  1. SELECT timescaledb_experimental.time_bucket_ng('1 year', date '2021-08-01');
  2. time_bucket_ng
  3. ----------------
  4. 2021-01-01
  5. (1 row)

To split time into buckets, 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:

  1. SELECT timescaledb_experimental.time_bucket_ng('100 years', timestamp '1988-05-08');
  2. ERROR: origin must be before the given date

Going back in time from 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:

  1. -- working with timestamps before 2000-01-01
  2. SELECT timescaledb_experimental.time_bucket_ng('100 years', timestamp '1988-05-08', origin => '1900-01-01');
  3. time_bucket_ng
  4. ---------------------
  5. 1900-01-01 00:00:00
  6. -- unlike the default origin, which is Saturday, 2000-01-03 is Monday
  7. SELECT timescaledb_experimental.time_bucket_ng('1 week', timestamp '2021-08-26', origin => '2000-01-03');
  8. time_bucket_ng
  9. ---------------------
  10. 2021-08-23 00:00:00

This example shows how time_bucket_ng() is used to bucket data by months in a specified timezone:

  1. -- note that timestamptz is displayed differently depending on the session parameters
  2. SET TIME ZONE 'Europe/Moscow';
  3. SET
  4. SELECT timescaledb_experimental.time_bucket_ng('1 month', timestamptz '2001-02-03 12:34:56 MSK', timezone => 'Europe/Moscow');
  5. time_bucket_ng
  6. ------------------------
  7. 2001-02-01 00:00:00+03

You can use time_bucket_ng() with continuous aggregates. This example tracks the temperature in Moscow over seven day intervals:

  1. CREATE TABLE conditions(
  2. day DATE NOT NULL,
  3. city text NOT NULL,
  4. temperature INT NOT NULL);
  5. SELECT create_hypertable(
  6. 'conditions', 'day',
  7. chunk_time_interval => INTERVAL '1 day'
  8. );
  9. INSERT INTO conditions (day, city, temperature) VALUES
  10. ('2021-06-14', 'Moscow', 26),
  11. ('2021-06-15', 'Moscow', 22),
  12. ('2021-06-16', 'Moscow', 24),
  13. ('2021-06-17', 'Moscow', 24),
  14. ('2021-06-18', 'Moscow', 27),
  15. ('2021-06-19', 'Moscow', 28),
  16. ('2021-06-20', 'Moscow', 30),
  17. ('2021-06-21', 'Moscow', 31),
  18. ('2021-06-22', 'Moscow', 34),
  19. ('2021-06-23', 'Moscow', 34),
  20. ('2021-06-24', 'Moscow', 34),
  21. ('2021-06-25', 'Moscow', 32),
  22. ('2021-06-26', 'Moscow', 32),
  23. ('2021-06-27', 'Moscow', 31);
  24. CREATE MATERIALIZED VIEW conditions_summary_weekly
  25. WITH (timescaledb.continuous) AS
  26. SELECT city,
  27. timescaledb_experimental.time_bucket_ng('7 days', day) AS bucket,
  28. MIN(temperature),
  29. MAX(temperature)
  30. FROM conditions
  31. GROUP BY city, bucket;
  32. SELECT to_char(bucket, 'YYYY-MM-DD'), city, min, max
  33. FROM conditions_summary_weekly
  34. ORDER BY bucket;
  35. to_char | city | min | max
  36. ------------+--------+-----+-----
  37. 2021-06-12 | Moscow | 22 | 27
  38. 2021-06-19 | Moscow | 28 | 34
  39. 2021-06-26 | Moscow | 31 | 32
  40. (3 rows)

For more information, see the continuous aggregates documentation.

important

While time_bucket_ng() supports months and timezones, continuous aggregates cannot always be used with monthly buckets or buckets with timezones.

This table shows which time_bucket_ng() functions can be used in a continuous aggregate:

FunctionAvailable in continuous aggregateTimescaleDB version
Buckets by seconds, minutes, hours, days, and weeks2.4.0 and later
Buckets by months and years2.6.0 or later
Timezones support2.6.0 or later
Specify custom origin2.7.0 or later