Counter aggregates can be used in continuous aggregates, even though they are not parallelizable in . For more
information, see the section on parallelism and ordering.
Prerequisites
To follow the steps on this page:- Create a target with Real-time analytics enabled. You need your connection details. This procedure also works for .
Run a counter aggregate query using a delta function
In this procedure, we are using an example table calledexample that contains counter data.
-
Create a table
-
Create a counter aggregate and the delta accessor function
This gives you the change in the counter’s value over the time period using
counter_agg()anddelta(), accounting for any resets. This allows you to search for fifteen minute periods where the counter increased by a larger or smaller amount: -
Use the
time_bucketfunction to produce a series of deltas Produce deltas over fifteen minute increments:
Run a counter aggregate query using an extrapolated delta function
If your series is less regular, the deltas are affected by the number of samples in each fifteen minute period. You can improve this by using theextrapolated_delta function. To do this, you need to provide
bounds that define where to extrapolate to. In this example, we use the time_bucket_range
function, which works in the same way as time_bucket but produces an open ended range of all the times in the
bucket. This example also uses a CTE to do the counter aggregation, which makes it a little easier to understand
what’s going on in each part.
-
Create a hypertable
When you create a using CREATE TABLE … WITH …, the default partitioning column is automatically the first column with a timestamp data type. Also, creates a columnstore policy that automatically converts your data to the , after an interval equal to the value of the chunk_interval, defined through
compress_afterin the policy. This columnar format enables fast scanning and aggregation, optimizing performance for analytical workloads while also saving significant storage space. In the conversion, s are compressed by up to 98%, and organized for efficient, large-scale queries. You can customize this policy later using alter_job. However, to changeafterorcreated_before, the compression settings, or the the policy is acting on, you must remove the columnstore policy and add a new one. You can also manually convert s in a to the . -
Create a counter aggregate and the extrapolated delta function
In this procedure,
Prometheusis used to do the extrapolation. ’s currentextrapolationfunction is built to mimic the Prometheus project’sincreasefunction, which measures the change of a counter extrapolated to the edges of the queried region.
Run a counter aggregate query with a continuous aggregate
Your counter aggregate might be more useful if you make a continuous aggregate out of it.-
Create the continuous aggregate
-
Re-aggregate from the continuous aggregate into a larger bucket size
Use
rollup()to combine counter aggregates:
Parallelism and ordering
The counter reset calculations require a strict ordering of inputs, which means they are not parallelizable in . This is because handles parallelism by issuing rows randomly to workers. However, if your parallelism can guarantee sets of rows that are disjointed in time, the algorithm can be parallelized, as long as it is within a time range, and all rows go to the same worker. This is the case for both continuous aggregates and for distributed hypertables, as long as the partitioning keys are in thegroup by, even though the aggregate itself doesn’t really
make sense otherwise.
For more information about parallelism and ordering, see the developer documentation.