From 59c8d4e072cc9523cd79f61599748cfe309841ad Mon Sep 17 00:00:00 2001 From: Radko Krkos Date: Thu, 3 Jan 2019 16:54:26 +0100 Subject: [PATCH] Fixed enumeration precalculation for non-arrays. Enumeration tables used to speed up enum generation for filtering were incorrect as multiple NULL values could have been inserted increasing the table size over time. This affected the enumerations for non-array parameters only. As the 'any value' and 'without value' options are added automatically, we can disable support for NULLs, fixing the growing tables. (Redmine issue: #4367) --- lib/mentat/services/eventstorage.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mentat/services/eventstorage.py b/lib/mentat/services/eventstorage.py index 67138d0..6db0bed 100644 --- a/lib/mentat/services/eventstorage.py +++ b/lib/mentat/services/eventstorage.py @@ -748,7 +748,7 @@ class EventStorageService: for column_name in ENUM_TABLES: create_table_sqls.append( psycopg2.sql.SQL( - "CREATE TABLE IF NOT EXISTS {}(data text UNIQUE, last_seen TIMESTAMP WITHOUT TIME ZONE)" + "CREATE TABLE IF NOT EXISTS {} (data text UNIQUE NOT NULL, last_seen TIMESTAMP WITHOUT TIME ZONE NOT NULL)" ).format( psycopg2.sql.Identifier( "enum_{}".format(column_name) @@ -1056,12 +1056,12 @@ class EventStorageService: enum_table = "enum_{}".format(column) try: # Build and execute query for updating enumeration table. - enum_query = psycopg2.sql.SQL("INSERT INTO {} (").format(psycopg2.sql.Identifier(enum_table)) + enum_query = psycopg2.sql.SQL("INSERT INTO {} (SELECT * FROM (").format(psycopg2.sql.Identifier(enum_table)) if column not in ('cesnet_eventclass', 'cesnet_eventseverity'): enum_query += psycopg2.sql.SQL("SELECT unnest({})").format(psycopg2.sql.Identifier(column)) else: enum_query += psycopg2.sql.SQL("SELECT {}").format(psycopg2.sql.Identifier(column)) - enum_query += psycopg2.sql.SQL(' AS data, max(cesnet_storagetime) AS last_seen FROM events WHERE cesnet_storagetime >= COALESCE((SELECT max(last_seen) FROM {}), (SELECT min(cesnet_storagetime) FROM events)) GROUP BY data) ON CONFLICT (data) DO UPDATE SET last_seen = excluded.last_seen').format(psycopg2.sql.Identifier(enum_table)) + enum_query += psycopg2.sql.SQL(' AS data, max(cesnet_storagetime) AS last_seen FROM events WHERE cesnet_storagetime >= COALESCE((SELECT max(last_seen) FROM {}), (SELECT min(cesnet_storagetime) FROM events)) GROUP BY data) AS enum WHERE data IS NOT NULL) ON CONFLICT (data) DO UPDATE SET last_seen = excluded.last_seen').format(psycopg2.sql.Identifier(enum_table)) self.cursor.execute(enum_query) self.commit() -- 2.14.5