{ "(hooks)": { "(hooks)#0": [ {"sql":"\n create table readings (id bigint generated always as identity primary key, org_id integer not null, taken_at timestamptz not null);\n create index on readings (taken_at);\n create index on readings (org_id);\n insert into readings (org_id, taken_at) values (1, '2026-06-30 23:10:00+00');\n ","result":[{"command":"CREATE","rowCount":null,"columns":[],"rows":[]},{"command":"CREATE","rowCount":null,"columns":[],"rows":[]},{"command":"CREATE","rowCount":null,"columns":[],"rows":[]},{"command":"INSERT","rowCount":1,"columns":[],"rows":[]}]}, {"sql":"-- The schema of the functions that keep rollups and refresh time rollups.\ncreate schema if not exists \"rollups\";\n\n-- The table and refresh function of a time rollup of readings_hourly.\ncreate table \"public\".\"readings_hourly\" as select date_trunc('hour', c.\"taken_at\"::timestamptz) as \"bucket\", c.\"org_id\" as \"org_id\", count(*) as \"events\" from \"public\".\"readings\" as c group by 1, 2 with no data;\nalter table \"public\".\"readings_hourly\" add column \"id\" bigint generated always as identity primary key, alter column \"bucket\" set not null, alter column \"org_id\" set not null, alter column \"events\" set not null;\ncreate unique index \"readings_hourly_bucket_key\" on \"public\".\"readings_hourly\" (\"bucket\", \"org_id\");\ncreate index \"readings_hourly_org_id_bucket\" on \"public\".\"readings_hourly\" (\"org_id\", \"bucket\");\ncomment on table \"public\".\"readings_hourly\" is 'protobase time rollup: from \"public\".\"readings\" (taken_at timestamp, org_id integer)';\ncreate or replace function \"rollups\".\"readings_hourly_refresh\"(\"from\" timestamptz) returns void language plpgsql set search_path = pg_catalog, pg_temp set timezone = 'UTC' as $buckets$\n<>\ndeclare\n since \"public\".\"readings\".\"taken_at\"%type := date_trunc('hour', $1);\nbegin\nwith fresh as (\n select date_trunc('hour', c.\"taken_at\"::timestamptz) as \"bucket\", c.\"org_id\" as \"org_id\", count(*) as \"events\"\n from \"public\".\"readings\" as c\n where c.\"taken_at\" >= refresh.since\n group by 1, 2\n), gone as (\n delete from \"public\".\"readings_hourly\" as r where r.\"bucket\" >= date_trunc('hour', $1) and not exists (select from fresh where fresh.\"bucket\" = r.\"bucket\" and fresh.\"org_id\" = r.\"org_id\")\n)\ninsert into \"public\".\"readings_hourly\" (\"bucket\", \"org_id\", \"events\")\nselect \"bucket\", \"org_id\", \"events\" from fresh\non conflict (\"bucket\", \"org_id\") do update set \"events\" = excluded.\"events\";\nend\n$buckets$;\n\n-- The table and refresh function of a time rollup of readings_hourly.\nselect \"rollups\".\"readings_hourly_refresh\"('-infinity');","result":[{"command":"CREATE","rowCount":null,"columns":[],"rows":[]},{"command":"CREATE","rowCount":null,"columns":[],"rows":[]},{"command":"ALTER","rowCount":null,"columns":[],"rows":[]},{"command":"CREATE","rowCount":null,"columns":[],"rows":[]},{"command":"CREATE","rowCount":null,"columns":[],"rows":[]},{"command":"COMMENT","rowCount":null,"columns":[],"rows":[]},{"command":"CREATE","rowCount":null,"columns":[],"rows":[]},{"command":"SELECT","rowCount":1,"columns":[["readings_hourly_refresh",2278]],"rows":[[""]]}]} ] }, "counts a record imported later than late once the rollup is rebuilt from its time": { "(hooks)#0": [ {"sql":"insert into readings (org_id, taken_at) values (1, '2026-07-01 10:15:00+00'), (1, '2026-07-01 10:45:00+00')","result":{"command":"INSERT","rowCount":2,"columns":[],"rows":[]}}, {"sql":"select \"rollups\".\"readings_hourly_refresh\"(now() - $1::interval)","params":["2 hours"],"result":{"command":"SELECT","rowCount":1,"columns":[["readings_hourly_refresh",2278]],"rows":[[""]]}}, {"sql":"select bucket, org_id, events::int from readings_hourly order by bucket","result":{"command":"SELECT","rowCount":1,"columns":[["bucket",1184],["org_id",23],["events",23]],"rows":[["2026-06-30 23:00:00+00","1","1"]]}}, {"sql":"select \"rollups\".\"readings_hourly_refresh\"($1::timestamptz)","params":["2026-07-01T00:00:00.000Z"],"result":{"command":"SELECT","rowCount":1,"columns":[["readings_hourly_refresh",2278]],"rows":[[""]]}}, {"sql":"select bucket, org_id, events::int from readings_hourly order by bucket","result":{"command":"SELECT","rowCount":2,"columns":[["bucket",1184],["org_id",23],["events",23]],"rows":[ ["2026-06-30 23:00:00+00","1","1"], ["2026-07-01 10:00:00+00","1","2"] ]}} ] } }