Skip to content

Commit

Permalink
Fix: Also create NVT indexes after rebuild
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timopollmeier committed Jul 2, 2024
1 parent 95b8ac9 commit 4e1254b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

Check warning on line 1890 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L1890

Added line #L1890 was not covered by tests
{
sql ("SELECT create_index ('nvts_by_creation_time',"

Check warning on line 1892 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L1892

Added line #L1892 was not covered by tests
" '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',"

Check warning on line 1897 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L1895-L1897

Added lines #L1895 - L1897 were not covered by tests
" 'nvts', 'modification_time');");
sql ("SELECT create_index ('nvts_by_cvss_base',"

Check warning on line 1899 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L1899

Added line #L1899 was not covered by tests
" 'nvts', 'cvss_base');");
sql ("SELECT create_index ('nvts_by_solution_type',"

Check warning on line 1901 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L1901

Added line #L1901 was not covered by tests
" 'nvts', 'solution_type');");

sql ("SELECT create_index ('vt_refs_by_vt_oid',"

Check warning on line 1904 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L1904

Added line #L1904 was not covered by tests
" 'vt_refs', 'vt_oid');");

sql ("SELECT create_index ('vt_severities_by_vt_oid',"

Check warning on line 1907 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L1907

Added line #L1907 was not covered by tests
" 'vt_severities', 'vt_oid');");
}

/**
* @brief Create all tables.
*/
Expand Down Expand Up @@ -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 ();

Check warning on line 3052 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3052

Added line #L3052 was not covered by tests

sql ("SELECT create_index ('permissions_by_name',"
" 'permissions', 'name');");
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/manage_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);

Expand Down
1 change: 1 addition & 0 deletions src/manage_sql_nvts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();

Check warning on line 1990 in src/manage_sql_nvts.c

View check run for this annotation

Codecov / codecov/patch

src/manage_sql_nvts.c#L1990

Added line #L1990 was not covered by tests
}

set_nvts_check_time (count_new_vts, count_modified_vts);
Expand Down

0 comments on commit 4e1254b

Please sign in to comment.