Created
April 6, 2018 16:44
-
-
Save martyndavies/e61ce1be6a2620ee1e62c5114758b997 to your computer and use it in GitHub Desktop.
Example of a Postgres rollup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION compute_5min_rollups(start_time TIMESTAMP, end_time TIMESTAMP) | |
RETURNS void LANGUAGE PLPGSQL AS $function$ | |
BEGIN | |
EXECUTE $$ | |
INSERT INTO rollups_5min | |
SELECT | |
date_trunc('seconds', (timestamp - TIMESTAMP 'epoch') / 300) * 300 + TIMESTAMP 'epoch' AS minute, | |
app_id, | |
timestamp, | |
count(*) AS query_count, | |
hll_add_agg(hll_hash_bigint(user_id)) AS user_count, | |
topn_add_agg(query) AS top_queries | |
FROM queries | |
WHERE timestamp >= $1 AND timestamp <= $2 | |
GROUP BY app_id, minute; $$ | |
USING start_time, end_time; | |
END; | |
$function$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment