2014-05-05 23:13:06 +08:00
|
|
|
-- Return the estimated size of user data. Used for quota checking.
|
2014-07-04 16:57:55 +08:00
|
|
|
CREATE OR REPLACE FUNCTION CDB_UserDataSize(schema_name TEXT)
|
2014-05-05 23:13:06 +08:00
|
|
|
RETURNS bigint AS
|
|
|
|
$$
|
|
|
|
-- TODO: double check this query. Maybe use CDB_TableMetadata for lookup ?
|
|
|
|
-- also, it's "table_name" sounds sensible to search_path
|
|
|
|
--
|
|
|
|
-- NOTE: division by 2 is an hack for the_geom_webmercator
|
|
|
|
--
|
2014-07-05 00:50:46 +08:00
|
|
|
SELECT coalesce(int8(sum(pg_total_relation_size(schema_name || '.' || table_name)) / 2), 0)
|
2014-05-05 23:13:06 +08:00
|
|
|
AS quota
|
|
|
|
FROM information_schema.tables
|
2014-07-04 16:57:55 +08:00
|
|
|
WHERE table_catalog = current_database() AND table_schema = schema_name
|
|
|
|
AND table_name != 'spatial_ref_sys'
|
|
|
|
AND table_name != 'cdb_tablemetadata'
|
|
|
|
AND table_type = 'BASE TABLE';
|
|
|
|
$$
|
|
|
|
LANGUAGE 'sql' VOLATILE;
|
|
|
|
|
|
|
|
|
|
|
|
-- Return the estimated size of user data. Used for quota checking.
|
|
|
|
-- Implicit schema version for backwards compatibility
|
|
|
|
CREATE OR REPLACE FUNCTION CDB_UserDataSize()
|
|
|
|
RETURNS bigint AS
|
|
|
|
$$
|
|
|
|
SELECT public.CDB_UserDataSize('public');
|
2014-05-05 23:13:06 +08:00
|
|
|
$$
|
|
|
|
LANGUAGE 'sql' VOLATILE;
|
|
|
|
|
2014-07-04 21:55:08 +08:00
|
|
|
-- Triggers cannot have declared arguments: pbfact float8, qmax int8, schema_name text
|
|
|
|
CREATE OR REPLACE FUNCTION CDB_CheckQuota()
|
2014-05-05 23:13:06 +08:00
|
|
|
RETURNS trigger AS
|
|
|
|
$$
|
|
|
|
DECLARE
|
|
|
|
pbfact float8;
|
|
|
|
qmax int8;
|
2014-07-04 21:55:08 +08:00
|
|
|
schema_name text;
|
2014-05-05 23:13:06 +08:00
|
|
|
dice float8;
|
|
|
|
quota float8;
|
|
|
|
BEGIN
|
2014-07-04 21:55:08 +08:00
|
|
|
IF TG_NARGS = 3 THEN
|
|
|
|
schema_name := TG_ARGV[2];
|
|
|
|
IF cartodb.schema_exists(schema_name) = false THEN
|
|
|
|
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
|
|
|
END IF;
|
|
|
|
ELSE
|
|
|
|
schema_name := 'public';
|
|
|
|
END IF;
|
|
|
|
-- Hack to support old versions of CDB_CheckQuota with 2 params but without schema_name
|
|
|
|
IF TG_NARGS >= 2 AND TG_ARGV[1] <> '-1' THEN
|
|
|
|
qmax := TG_ARGV[1];
|
|
|
|
ELSE
|
|
|
|
BEGIN
|
2014-07-04 22:39:19 +08:00
|
|
|
EXECUTE FORMAT('SELECT %I._CDB_UserQuotaInBytes();', schema_name) INTO qmax;
|
2014-07-04 21:55:08 +08:00
|
|
|
EXCEPTION WHEN undefined_function THEN
|
|
|
|
RAISE EXCEPTION 'Missing "%"._CDB_UserQuotaInBytes()', schema_name;
|
|
|
|
END;
|
|
|
|
END IF;
|
2014-05-05 23:13:06 +08:00
|
|
|
pbfact := TG_ARGV[0];
|
|
|
|
|
2014-07-04 21:55:08 +08:00
|
|
|
dice := random();
|
2014-05-05 23:13:06 +08:00
|
|
|
|
|
|
|
IF dice < pbfact THEN
|
|
|
|
RAISE DEBUG 'Checking quota on table % (dice:%, needed:<%)', TG_RELID::text, dice, pbfact;
|
2014-05-08 23:14:06 +08:00
|
|
|
|
|
|
|
IF qmax = 0 THEN
|
|
|
|
RETURN NEW;
|
|
|
|
END IF;
|
|
|
|
|
2014-07-04 17:48:53 +08:00
|
|
|
SELECT public.CDB_UserDataSize(schema_name) INTO quota;
|
2014-05-05 23:13:06 +08:00
|
|
|
IF quota > qmax THEN
|
2014-07-04 21:55:08 +08:00
|
|
|
RAISE EXCEPTION 'Quota exceeded by %KB', (quota-qmax)/1024;
|
2014-05-05 23:13:06 +08:00
|
|
|
ELSE RAISE DEBUG 'User quota in bytes: % < % (max allowed)', quota, qmax;
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
RETURN NEW;
|
|
|
|
END;
|
|
|
|
$$
|
|
|
|
LANGUAGE 'plpgsql' VOLATILE;
|
2014-05-08 23:14:06 +08:00
|
|
|
|
2014-07-04 17:48:53 +08:00
|
|
|
|
2014-07-04 21:55:08 +08:00
|
|
|
CREATE OR REPLACE FUNCTION CDB_SetUserQuotaInBytes(schema_name text, bytes int8)
|
2014-05-08 23:14:06 +08:00
|
|
|
RETURNS int8 AS
|
|
|
|
$$
|
|
|
|
DECLARE
|
|
|
|
sql text;
|
|
|
|
BEGIN
|
2014-07-04 21:55:08 +08:00
|
|
|
IF cartodb.schema_exists(schema_name::text) = false THEN
|
|
|
|
RAISE EXCEPTION 'Invalid schema name "%"', schema_name::text;
|
2014-07-04 17:48:53 +08:00
|
|
|
END IF;
|
|
|
|
|
2014-07-04 21:55:08 +08:00
|
|
|
sql := 'CREATE OR REPLACE FUNCTION "' || schema_name::text || '"._CDB_UserQuotaInBytes() '
|
2014-05-08 23:14:06 +08:00
|
|
|
|| 'RETURNS int8 AS $X$ SELECT ' || bytes
|
|
|
|
|| '::int8 $X$ LANGUAGE sql IMMUTABLE';
|
|
|
|
EXECUTE sql;
|
|
|
|
|
2014-07-07 16:36:51 +08:00
|
|
|
return bytes;
|
2014-05-08 23:14:06 +08:00
|
|
|
END
|
|
|
|
$$
|
|
|
|
LANGUAGE 'plpgsql' VOLATILE STRICT;
|
2014-07-04 17:48:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION CDB_SetUserQuotaInBytes(bytes int8)
|
|
|
|
RETURNS int8 AS
|
|
|
|
$$
|
2014-07-04 21:55:08 +08:00
|
|
|
BEGIN
|
|
|
|
return public.CDB_SetUserQuotaInBytes('public', bytes);
|
|
|
|
END;
|
2014-07-04 17:48:53 +08:00
|
|
|
$$
|
|
|
|
LANGUAGE 'plpgsql' VOLATILE STRICT;
|