Skip to main content
Since 1.15.0 Get a timeline of all states, showing each time a state is entered and exited. If you have multiple state aggregates and need to interpolate the state across interval boundaries, use interpolated_state_timeline.
state_timeline(
    agg StateAgg
) RETURNS (TEXT, TIMESTAMPTZ, TIMESTAMPTZ)

state_int_timeline(
    agg StateAgg
) RETURNS (BIGINT, TIMESTAMPTZ, TIMESTAMPTZ)

Arguments

NameTypeDefaultRequiredDescription
aggStateAgg-The aggregate from which to get a timeline

Returns

ColumnTypeDescription
stateTEXT | BIGINTA state found in the state aggregate
start_timeTIMESTAMPTZThe time when the state started (inclusive)
end_timeTIMESTAMPTZThe time when the state ended (exclusive)

Samples

Get the history of states from a state aggregate.
SELECT state, start_time, end_time
  FROM state_timeline(
    (SELECT state_agg(ts, state) FROM states_test)
  );
Returns:
 state |       start_time       |        end_time
-------+------------------------+------------------------
 START | 2020-01-01 00:00:00+00 | 2020-01-01 00:00:11+00
 OK    | 2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
 ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
 OK    | 2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
 STOP  | 2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00