Skip to content

Instantly share code, notes, and snippets.

@Harkishen-Singh
Created October 31, 2022 10:53
Show Gist options
  • Save Harkishen-Singh/d6167aab46762540f02ee2c8952f4d17 to your computer and use it in GitHub Desktop.
Save Harkishen-Singh/d6167aab46762540f02ee2c8952f4d17 to your computer and use it in GitHub Desktop.
This Gist helps to generate Prometheus Counter data behaviour on TimescaleDB/PostgresQL

Generate counter metric data

This example was done on [email protected].

Create function

create or replace function generate_counter_data(_from timestamptz, _till timestamptz, _step interval, _insert_table_name TEXT) returns void as
$$
    declare
        r record;
        counter integer := 0;
    begin
        for r in
            select generate_series as ts from generate_series(
                    _from, _till, _step
            )
        loop
            EXECUTE FORMAT ('insert into %I values ($1::timestamptz, $2::bigint)', _insert_table_name) USING r.ts, counter;
            counter := counter + 1;
        end loop;
    end;
$$
language plpgsql;

Usage example

create table test_table ( time timestamptz not null, value bigint not null );

-- Usage example.
select generate_counter_data('2022-06-01 00:00:00'::timestamptz, '2022-10-20 00:00:00'::timestamptz, interval '30 seconds', 'test_table');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment