The question is pretty simple. Is there any way to dynamically limit the maximum number of rows of a Postgres database?
I didn't thought it was possible, until I saw that Heroku does it. After some research, I found out a way with triggers:
CREATE OR REPLACE FUNCTION check_number_of_row()
RETURNS TRIGGER AS
$body$
BEGIN
-- replace 100 by the number of rows you want
IF (SELECT count(*) FROM your_table) > 100
THEN
RAISE EXCEPTION 'INSERT statement exceeding maximum number of rows for
this table'
END IF;
END;
$body$
LANGUAGE plpgsql;
CREATE TRIGGER tr_check_number_of_row
BEFORE INSERT ON your_table
FOR EACH ROW EXECUTE PROCEDURE check_number_of_row();
But this isn't quite what I need. It limits the rows of a table to a STATIC maximal number, so I would have to loop through all the tables, and it looks like a pretty messy option. And I haven't even thought on a way to set this max number dynamically.
I thought about doing daily/weekly cron job to check all the databases, count the total number of rows and see if it's within the limit, but if there is a more efficient way, I'll take it.