From 6bc91c71258d8be8f5a4e18f4f692e532b05b5ff Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 17 Aug 2015 10:17:07 +0200 Subject: [PATCH 1/2] 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. --- scripts-available/CDB_Quota.sql | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/scripts-available/CDB_Quota.sql b/scripts-available/CDB_Quota.sql index 3474f48..8e6a872 100644 --- a/scripts-available/CDB_Quota.sql +++ b/scripts-available/CDB_Quota.sql @@ -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; From 50169e58d5a21657e4064b6b6329f53e7b34576c Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 17 Aug 2015 10:25:31 +0200 Subject: [PATCH 2/2] Raise better notice on _CDB_total_relation_size errors --- scripts-available/CDB_Quota.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts-available/CDB_Quota.sql b/scripts-available/CDB_Quota.sql index 8e6a872..77f9df5 100644 --- a/scripts-available/CDB_Quota.sql +++ b/scripts-available/CDB_Quota.sql @@ -7,7 +7,7 @@ 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; + RAISE NOTICE 'cartodb._CDB_total_relation_size(''%'', ''%'') caught error: % (%)', _schema_name, _table_name, SQLERRM, SQLSTATE; END; RETURN relation_size; END;