state_agg() and timeline_agg()

The state_agg aggregate measures the amount of time spent in each distinct value of a state field. It is designed to work with a relatively small number of states and might not perform well on queries where states are mostly distinct across rows.

The timeline_agg aggregate works the same as state_agg, but also tracks when states are entered and exited. It has increased memory usage since it needs to track more data.

warning

Experimental features could have bugs. They might not be backwards compatible, and could be removed in future releases. Use these features at your own risk, and do not use any experimental features in production.

Required arguments

NameTypeDescription
tsTIMESTAMPTZColumn of timestamps
valueTEXT or BIGINTColumn of states

Returns

ColumnTypeDescription
aggStateAgg or TimelineAggAn object storing the total time spent in each state.

Sample usage

This example creates an aggregate over a status column in a devices table, with a timestamp column time.

  1. -- create a state aggregate:
  2. SELECT toolkit_experimental.state_agg(time, status) FROM devices;
  3. -- create a timeline aggregate:
  4. SELECT toolkit_experimental.timeline_agg(time, status) FROM devices;