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