From 4e1254bd6236a296e691be43474009f3da2e10de Mon Sep 17 00:00:00 2001 From: Timo Pollmeier Date: Tue, 2 Jul 2024 09:35:46 +0200 Subject: [PATCH] Fix: Also create NVT indexes after rebuild If the nvts, vt_refs and vt_severities tables have been replaced in a VTs rebuild, indexes are now created for the new tables. This addresses the the indexes not existing after a rebuild and the subsequent performance issues. --- src/manage_pg.c | 46 +++++++++++++++++++++++++++---------------- src/manage_sql.h | 3 +++ src/manage_sql_nvts.c | 1 + 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/manage_pg.c b/src/manage_pg.c index 19d3463f7..31d1201c7 100644 --- a/src/manage_pg.c +++ b/src/manage_pg.c @@ -1881,6 +1881,33 @@ create_tables_nvt (const gchar *suffix) suffix); } +/** + * @brief Create NVT related indexes. + * + * @param[in] suffix String to append to table names. + */ +void +create_indexes_nvt () +{ + sql ("SELECT create_index ('nvts_by_creation_time'," + " 'nvts'," + " 'creation_time');"); + sql ("SELECT create_index ('nvts_by_family', 'nvts', 'family');"); + sql ("SELECT create_index ('nvts_by_name', 'nvts', 'name');"); + sql ("SELECT create_index ('nvts_by_modification_time'," + " 'nvts', 'modification_time');"); + sql ("SELECT create_index ('nvts_by_cvss_base'," + " 'nvts', 'cvss_base');"); + sql ("SELECT create_index ('nvts_by_solution_type'," + " 'nvts', 'solution_type');"); + + sql ("SELECT create_index ('vt_refs_by_vt_oid'," + " 'vt_refs', 'vt_oid');"); + + sql ("SELECT create_index ('vt_severities_by_vt_oid'," + " 'vt_severities', 'vt_oid');"); +} + /** * @brief Create all tables. */ @@ -3021,17 +3048,8 @@ create_tables () sql ("SELECT create_index ('nvt_selectors_by_name'," " 'nvt_selectors'," " 'name');"); - sql ("SELECT create_index ('nvts_by_creation_time'," - " 'nvts'," - " 'creation_time');"); - sql ("SELECT create_index ('nvts_by_family', 'nvts', 'family');"); - sql ("SELECT create_index ('nvts_by_name', 'nvts', 'name');"); - sql ("SELECT create_index ('nvts_by_modification_time'," - " 'nvts', 'modification_time');"); - sql ("SELECT create_index ('nvts_by_cvss_base'," - " 'nvts', 'cvss_base');"); - sql ("SELECT create_index ('nvts_by_solution_type'," - " 'nvts', 'solution_type');"); + + create_indexes_nvt (); sql ("SELECT create_index ('permissions_by_name'," " 'permissions', 'name');"); @@ -3063,12 +3081,6 @@ create_tables () " 'tls_certificate_origins'," " 'origin_id, origin_type')"); - sql ("SELECT create_index ('vt_refs_by_vt_oid'," - " 'vt_refs', 'vt_oid');"); - - sql ("SELECT create_index ('vt_severities_by_vt_oid'," - " 'vt_severities', 'vt_oid');"); - /* Previously this included the value column but that can be bigger than 8191, * the maximum size that Postgres can handle. For example, this can happen * for "ports". Mostly value is short, like a CPE for the "App" detail, diff --git a/src/manage_sql.h b/src/manage_sql.h index dfe87caa6..9b6c7d81d 100644 --- a/src/manage_sql.h +++ b/src/manage_sql.h @@ -505,6 +505,9 @@ add_role_permission_resource (const gchar *, const gchar *, const gchar *, void create_view_vulns (); +void +create_indexes_nvt (); + int config_family_entire_and_growing (config_t, const char*); diff --git a/src/manage_sql_nvts.c b/src/manage_sql_nvts.c index ccebce74f..302654bb7 100644 --- a/src/manage_sql_nvts.c +++ b/src/manage_sql_nvts.c @@ -1987,6 +1987,7 @@ update_nvts_from_vts (element_t *get_vts_response, sql ("ALTER TABLE nvts_rebuild RENAME TO nvts;"); create_view_vulns (); + create_indexes_nvt (); } set_nvts_check_time (count_new_vts, count_modified_vts);