Skip to main content
Since 1.15.0 Calculate the integral, or the area under the curve formed by the data points. Equal to average multiplied by the elapsed time.

Arguments

NameTypeDefaultRequiredDescription
twsTimeWeightSummary-The input TimeWeightSummary from a time_weight() call
unitTEXTsecondThe unit of time to express the integral in. Can be microsecond, millisecond, second, minute, hour, or any alias for those units supported by Postgres

Returns

ColumnTypeDescription
integralDOUBLE PRECISIONThe time-weighted integral

Samples

Create a table to track irregularly sampled storage usage in bytes, and get the total storage used in byte-hours. Use the ‘last observation carried forward’ interpolation method:
-- Create a table to track irregularly sampled storage usage
CREATE TABLE user_storage_usage(ts TIMESTAMP, storage_bytes BIGINT);
INSERT INTO user_storage_usage(ts, storage_bytes) VALUES
    ('01-01-2022 00:00', 0),
    ('01-01-2022 00:30', 100),
    ('01-01-2022 03:00', 300),
    ('01-01-2022 03:10', 1000),
    ('01-01-2022 03:25', 817);

-- Get the total byte-hours used
SELECT
    integral(time_weight('LOCF', ts, storage_bytes), 'hours')
FROM
    user_storage_usage;