Using exception with pg_total_relation_size for better performance

IF EXISTS is too slow, one order of magnitude, than using exception
handling. In combination with thousands of tables to check total
relation size that's a problem.

Exception handles specifically undefined_table as it was the original
issue but also any other exception to guarantee a size is always
returned and no error is raised.
This commit is contained in:
Raul Ochoa 2015-08-17 10:17:07 +02:00
parent 219d876973
commit 6bc91c7125

View File

@ -1,18 +1,15 @@
CREATE OR REPLACE FUNCTION cartodb._CDB_total_relation_size(_schema_name TEXT, _table_name TEXT)
RETURNS bigint AS
$$
DECLARE relation_size bigint := 0;
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_catalog = current_database()
AND table_schema = _schema_name
AND table_name = _table_name
)
THEN
RETURN pg_total_relation_size(format('"%s"."%s"', _schema_name, _table_name));
ELSE
RETURN 0;
END IF;
BEGIN
SELECT pg_total_relation_size(format('"%s"."%s"', _schema_name, _table_name)) INTO relation_size;
EXCEPTION
WHEN undefined_table OR OTHERS THEN
RAISE NOTICE 'caught undefined_table: "%"."%"', _schema_name, _table_name;
END;
RETURN relation_size;
END;
$$
LANGUAGE 'plpgsql' VOLATILE;