duration_in()

Use this function to report the total duration for a given state in a state aggregate.

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
stateTEXT or BIGINTState to query
aggregateStateAgg or TimelineAggPreviously created aggregate

Returns

ColumnTypeDescription
duration_inINTERVALThe total time spent in the target state. Displayed as days, hh:mm:ss, or a combination of the two.

Sample usage

This example creates a simple test table:

  1. SET timezone TO 'UTC';
  2. CREATE TABLE states(time TIMESTAMPTZ, state TEXT);
  3. INSERT INTO states VALUES
  4. ('1-1-2020 10:00', 'starting'),
  5. ('1-1-2020 10:30', 'running'),
  6. ('1-3-2020 16:00', 'error'),
  7. ('1-3-2020 18:30', 'starting'),
  8. ('1-3-2020 19:30', 'running'),
  9. ('1-5-2020 12:00', 'stopping');

You can query this table for the time spent in the running state, like this:

  1. SELECT toolkit_experimental.duration_in(
  2. 'running',
  3. toolkit_experimental.state_agg(time, state)
  4. ) FROM states;

Which gives the result:

  1. duration_in
  2. ---------------
  3. 3 days 22:00:00

If you prefer to see the result in seconds, EXTRACT the epoch from the returned result.