From bf622ae5a6767591ac0e431ab7d0c067d0608a53 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 28 Jan 2015 16:54:50 +0000 Subject: [PATCH 1/5] Optimize CDB_UserDataSize (on behalf of @javisantana) #65 --- scripts-available/CDB_Quota.sql | 69 +++++++++++++-------------------- 1 file changed, 28 insertions(+), 41 deletions(-) diff --git a/scripts-available/CDB_Quota.sql b/scripts-available/CDB_Quota.sql index 2350f48..1804084 100644 --- a/scripts-available/CDB_Quota.sql +++ b/scripts-available/CDB_Quota.sql @@ -3,50 +3,37 @@ CREATE OR REPLACE FUNCTION CDB_UserDataSize(schema_name TEXT) RETURNS bigint AS $$ DECLARE - quota_vector INT8; - quota_raster INT8; + total_size INT8; BEGIN - -- TODO: double check queries. Maybe use CDB_TableMetadata for lookup? - -- Also, "table_name" sounds sensible to search_path - - -- Division by 2 is for not counting the_geom_webmercator - SELECT COALESCE(INT8(SUM(pg_total_relation_size('"' || schema_name || '"."' || table_name || '"')) / 2), 0) INTO quota_vector - FROM information_schema.tables - 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' - -- exclude raster overview tables - AND table_name NOT IN ( - SELECT o_table_name FROM raster_overviews + WITH raster_tables AS ( + SELECT o_table_name, r_table_name FROM raster_overviews WHERE o_table_schema = schema_name AND o_table_catalog = current_database() - ) - -- exclude raster "main" tables - AND table_name NOT IN ( - SELECT r_table_name FROM raster_overviews - WHERE r_table_name = table_name - AND o_table_schema = schema_name AND o_table_catalog = current_database() - ); + ), + user_tables AS ( + SELECT table_name FROM information_schema.tables + 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' + ), + table_cat AS ( + SELECT + table_name, + EXISTS(select * from raster_tables where o_table_name = table_name) AS is_overview, + EXISTS(SELECT * FROM raster_tables WHERE r_table_name = table_name) AS is_raster + FROM user_tables + ), + sizes AS ( + SELECT COALESCE(INT8(SUM(pg_total_relation_size('"' || schema_name || '"."' || table_name || '"')))) table_size, + CASE + WHEN is_overview THEN 0 + WHEN is_raster THEN 1 + ELSE 0.5 + END AS multiplier FROM table_cat GROUP BY is_overview, is_raster + ) + SELECT sum(table_size*multiplier)::int8 INTO total_size FROM sizes; - SELECT COALESCE(INT8(SUM(pg_total_relation_size('"' || schema_name || '"."' || table_name || '"'))), 0) INTO quota_raster - FROM information_schema.tables - 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' - -- exclude raster overview tables - AND table_name NOT IN ( - SELECT o_table_name FROM raster_overviews - WHERE o_table_schema = schema_name AND o_table_catalog = current_database() - ) - -- filter to raster "main" tables - AND table_name IN ( - SELECT r_table_name FROM raster_overviews - WHERE r_table_name = table_name - AND o_table_schema = schema_name AND o_table_catalog = current_database() - ); - - RETURN quota_vector + quota_raster; + RETURN total_size; END; $$ LANGUAGE 'plpgsql' VOLATILE; From a4e42571cdad14b76e116f7ecbca9c4de4c8b752 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 28 Jan 2015 17:10:04 +0000 Subject: [PATCH 2/5] Add a comment to CDB_UserDataSize (from PR) #65 --- 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 1804084..7971445 100644 --- a/scripts-available/CDB_Quota.sql +++ b/scripts-available/CDB_Quota.sql @@ -28,7 +28,7 @@ BEGIN CASE WHEN is_overview THEN 0 WHEN is_raster THEN 1 - ELSE 0.5 + ELSE 0.5 -- Division by 2 is for not counting the_geom_webmercator END AS multiplier FROM table_cat GROUP BY is_overview, is_raster ) SELECT sum(table_size*multiplier)::int8 INTO total_size FROM sizes; From 73232b88025606f696a5719081c42fda690daf24 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Thu, 29 Jan 2015 14:02:38 +0000 Subject: [PATCH 3/5] Increase version number to 0.5.2 #65 --- Makefile | 2 +- NEWS.md | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 27feddc..16e8bac 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.5.1 +EXTVERSION = 0.5.2 SED = sed diff --git a/NEWS.md b/NEWS.md index dda02fa..e29edcd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +0.5.2 (2015-01-29) +------------------ +* Improvement: make CDB_UserDataSize functions much faster. + 0.5.1 (2014-11-21) ------------------ * Bugfix: Quota check and some organization permissions functions were not properly escaping table name. From 693b147ef16f1ac633731bf117be7a866a0d7738 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Thu, 29 Jan 2015 15:46:08 +0000 Subject: [PATCH 4/5] Fix corner case (no tables) in CDB_UserDataSize #65 --- scripts-available/CDB_Quota.sql | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts-available/CDB_Quota.sql b/scripts-available/CDB_Quota.sql index 7971445..9d53d2a 100644 --- a/scripts-available/CDB_Quota.sql +++ b/scripts-available/CDB_Quota.sql @@ -33,7 +33,11 @@ BEGIN ) SELECT sum(table_size*multiplier)::int8 INTO total_size FROM sizes; - RETURN total_size; + IF total_size IS NOT NULL THEN + RETURN total_size; + ELSE + RETURN 0; + END IF; END; $$ LANGUAGE 'plpgsql' VOLATILE; From f0bf8a85a5d74a50a4188d01a00e7d0bb1282776 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Thu, 29 Jan 2015 16:37:59 +0000 Subject: [PATCH 5/5] Fix Makefile versioning stuff #65 --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 16e8bac..f36c5c3 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ UPGRADABLE = \ 0.4.0 \ 0.4.1 \ 0.5.0 \ + 0.5.1 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END)