Created
October 11, 2011 09:42
-
-
Save javisantana/1277714 to your computer and use it in GitHub Desktop.
soft quota limit on postgres
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
-- You need to create plpython languaje in the database | |
-- createlang -U postgres plpythonu DATABASE_NAME | |
-- this small trigger prevents an user exceeds a database size. Once the user exceeds the quota | |
-- the database size is checked every time an update or insert are performed until the user deletes some data | |
-- If the server is restarted the quota is checked the fist time user executes some update or insert | |
CREATE OR REPLACE FUNCTION check_quota() RETURNS trigger AS | |
$$ | |
c = SD.get('quota_counter', 0) | |
if c%1000 == 0: | |
s = plpy.execute("SELECT pg_database_size('DATABASE_NAME')")[0]['pg_database_size'] | |
if int(s) > 100000: | |
raise Exception("Quota exceed") | |
SD['quota_counter'] = c + 1 | |
$$ | |
LANGUAGE 'plpythonu' VOLATILE; | |
DROP TRIGGER IF EXISTS quota ON my_epic_table; | |
CREATE TRIGGER quota | |
BEFORE UPDATE OR INSERT ON my_epic_table | |
EXECUTE PROCEDURE check_quota(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment