Skip to content

Instantly share code, notes, and snippets.

@javisantana
Created October 11, 2011 09:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save javisantana/1277714 to your computer and use it in GitHub Desktop.
Save javisantana/1277714 to your computer and use it in GitHub Desktop.
soft quota limit on postgres
-- 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