Created
March 11, 2014 15:48
-
-
Save ys/9488574 to your computer and use it in GitHub Desktop.
Schema for will/datascope using free data tier heroku postgres database. The trigger will keep the last 10000 elements
This file contains 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 TABLE stats (id serial primary key, created_at timestamptz default (now()), data json); | |
create index ON stats (created_at); | |
-- Function that will delete old rows to keep the size of the set to 10_000 rows | |
CREATE OR REPLACE FUNCTION prune_stats_table() | |
RETURNS trigger AS | |
$BODY$ | |
BEGIN | |
DELETE FROM stats WHERE id < (SELECT id FROM stats ORDER BY id DESC LIMIT 1 OFFSET 9999); | |
return null; | |
END | |
$BODY$ | |
LANGUAGE plpgsql; | |
-- Trigger pruning before each update... slow but do the job | |
CREATE TRIGGER prune_stats_trigger | |
AFTER INSERT OR UPDATE | |
ON stats | |
EXECUTE PROCEDURE prune_stats_table(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment