From a067cc7da1d26ba0f76779760a1df723f038bb16 Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Wed, 27 Apr 2016 15:06:09 +0200 Subject: [PATCH] Generate stats used to identify category columns in overviews if needed This only generates the stats if no stats are available for a table. This doesn't warrant that the stats are up to date or accurate. --- scripts-available/CDB_Overviews.sql | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/scripts-available/CDB_Overviews.sql b/scripts-available/CDB_Overviews.sql index 01ce9d7..fd4eeb4 100644 --- a/scripts-available/CDB_Overviews.sql +++ b/scripts-available/CDB_Overviews.sql @@ -557,10 +557,20 @@ AS $$ DECLARE schema_name TEXT; table_name TEXT; + available BOOLEAN; categorical BOOLEAN; BEGIN SELECT * FROM _cdb_split_table_name(reloid) INTO schema_name, table_name; - SELECT n_distinct IS NOT NULL AND n_distinct > 0 AND n_distinct <= 20 + SELECT n_distinct IS NOT NULL + FROM pg_stats + WHERE pg_stats.schemaname = schema_name + AND pg_stats.tablename = table_name + AND pg_stats.attname = col_name + INTO available; + IF available IS NULL OR NOT available THEN + EXECUTE Format('ANALYZE %s;', reloid); + END IF; + SELECT n_distinct > 0 AND n_distinct <= 20 FROM pg_stats WHERE pg_stats.schemaname = schema_name AND pg_stats.tablename = table_name @@ -568,7 +578,7 @@ BEGIN INTO categorical; RETURN categorical; END; -$$ LANGUAGE PLPGSQL STABLE; +$$ LANGUAGE PLPGSQL VOLATILE; CREATE OR REPLACE FUNCTION _cdb_mode_of_array(anyarray) RETURNS anyelement AS