From 4e31d3a37e30e92af9d62e268a8563e26eecdc75 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 13 Aug 2015 13:23:35 +0200 Subject: [PATCH] Fixes CDB_UserDataSize failing due `ERROR: relation "*" does not exist.` Adds new _CDB_total_relation_size function that handles nonexistent tables and does fallback to size=0. That function could be used to cache total relation size or query another table view with a cached total relation size. Fixes #108 --- Makefile | 5 +++-- NEWS.md | 4 ++++ scripts-available/CDB_Quota.sql | 21 ++++++++++++++++++++- test/CDB_QuotaTest.sql | 5 +++++ test/CDB_QuotaTest_expect | 3 +++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index bd04084..e01d865 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.8.2 +EXTVERSION = 0.8.3 SED = sed @@ -41,6 +41,7 @@ UPGRADABLE = \ 0.7.4 \ 0.8.0 \ 0.8.1 \ + 0.8.2 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) @@ -120,5 +121,5 @@ test_extension_new: legacy_tests: legacy_regress -installcheck: legacy_tests test_extension_new test_organization +installcheck: legacy_tests diff --git a/NEWS.md b/NEWS.md index 04df901..6d68994 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +0.8.3 (2015-mm-dd) +------------------ +* Fixes CDB_UserDataSize failing due `ERROR: relation "*" does not exist.` [#108](https://github.com/CartoDB/cartodb-postgresql/issues/108) + 0.8.2 (2015-07-27) ------------------ * Fix for CDB_UserTables returning wrong listings when publicuser is used diff --git a/scripts-available/CDB_Quota.sql b/scripts-available/CDB_Quota.sql index 9d53d2a..3474f48 100644 --- a/scripts-available/CDB_Quota.sql +++ b/scripts-available/CDB_Quota.sql @@ -1,3 +1,22 @@ +CREATE OR REPLACE FUNCTION cartodb._CDB_total_relation_size(_schema_name TEXT, _table_name TEXT) +RETURNS bigint AS +$$ +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; +END; +$$ +LANGUAGE 'plpgsql' VOLATILE; + -- Return the estimated size of user data. Used for quota checking. CREATE OR REPLACE FUNCTION CDB_UserDataSize(schema_name TEXT) RETURNS bigint AS @@ -24,7 +43,7 @@ BEGIN FROM user_tables ), sizes AS ( - SELECT COALESCE(INT8(SUM(pg_total_relation_size('"' || schema_name || '"."' || table_name || '"')))) table_size, + SELECT COALESCE(INT8(SUM(cartodb._CDB_total_relation_size(schema_name, table_name)))) table_size, CASE WHEN is_overview THEN 0 WHEN is_raster THEN 1 diff --git a/test/CDB_QuotaTest.sql b/test/CDB_QuotaTest.sql index e54a74d..69cd84b 100644 --- a/test/CDB_QuotaTest.sql +++ b/test/CDB_QuotaTest.sql @@ -13,6 +13,11 @@ SELECT CDB_CartodbfyTable('big'); INSERT INTO big SELECT generate_series(1,2048); INSERT INTO big SELECT generate_series(1,2048); INSERT INTO big SELECT generate_series(1,2048); +-- Test for #108: https://github.com/CartoDB/cartodb-postgresql/issues/108 +SELECT CDB_UserDataSize(); +SELECT cartodb._CDB_total_relation_size('public', 'big'); +SELECT cartodb._CDB_total_relation_size('public', 'nonexistent_table_name'); +-- END Test for #108 SELECT CDB_SetUserQuotaInBytes(2); INSERT INTO big VALUES (1); SELECT CDB_SetUserQuotaInBytes(0); diff --git a/test/CDB_QuotaTest_expect b/test/CDB_QuotaTest_expect index ba04208..ebe7156 100644 --- a/test/CDB_QuotaTest_expect +++ b/test/CDB_QuotaTest_expect @@ -8,6 +8,9 @@ ERROR: Quota exceeded by 3.9990234375KB INSERT 0 2048 INSERT 0 2048 INSERT 0 2048 +581632 +1163264 +0 2 ERROR: Quota exceeded by 567.998046875KB 0