Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexes and trigger changes to improve performance #5111

Merged
merged 4 commits into from
Sep 17, 2024

Commits on Sep 17, 2024

  1. perf(db): Add indexes on foreign key to improve delete performance

    This adds indexes for a few categories:
    
    1. Foreign keys on the session table. These are set to `null` when the
       referenced row is deleted. These indexes will help to more
       efficiently set these `null` values in the case where a target is
       deleted, or and auth token is deleted.
    2. Foreign keys from other tables to the session table. These are either
       set to `null` or cascade deleted when a session is deleted. These
       indexes help with these update/deletes when a session is deleted.
    3. A multi-column index on session_state. This helps with the query that
       is used to delete all terminated sessions that have been terminated
       for over an hour.
    tmessi committed Sep 17, 2024
    Configuration menu
    Copy the full SHA
    993172a View commit details
    Browse the repository at this point in the history
  2. perf(db): Reorder index columns for primary keys

    When processing a controller API request, system uses a query to fetch
    the grants for the requesting user. This query requires pulling together
    information from several tables, and is currently performing many
    sequential scans to do so. For a number of the tables involved, there
    are indexes from multi-column primary keys, however, due to the order of
    the columns in the index, the postgres planner would need to do a full
    scan of the index, which can be less efficient than a sequential scan,
    so instead it will perform a sequential scan of the table.
    
    This recreates the primary keys while swapping the order of the columns
    in the primary key definition, and thus the order in the index. By doing
    so, the planner will not need to perform a full index scan, and will be
    more likely to use the index when executing the grants query.
    tmessi committed Sep 17, 2024
    Configuration menu
    Copy the full SHA
    b5703b3 View commit details
    Browse the repository at this point in the history
  3. perf(db): Use statement trigger for marking deleted sessions

    When sessions are deleted, a trigger is used to insert records into the
    session_deleted table. This table is utilized by the sessions list
    endpoint when using a refresh token to inform a client of the sessions
    that have been deleted since the last request. We delete sessions in
    bulk via a controller job to delete sessions that have been terminated
    over an hour ago, which results in the trigger running a large number of
    separate insert statements while processing the delete statement.
    
    This changes the trigger to run once for the delete statement, instead
    of for each row, resulting in a single bulk insert statement to the
    session_deleted table.
    
    This new trigger function also avoids the use of `on conflict`. When
    testing this function, while the single statement was still faster than
    running multiple inserts, the `on conflict` still added significant
    overhead, even when there were no conflicts. It should be safe to
    perform the insert without the `on conflict`, since the same ID should
    never be deleted more than once if it is successfully deleted.
    tmessi committed Sep 17, 2024
    Configuration menu
    Copy the full SHA
    3321c22 View commit details
    Browse the repository at this point in the history
  4. refact(db): Replace get_deleted_tables function with a view

    This function was returning the set of deletion tables. A view seems
    better suited for this task since it would allow for applying additional
    filters of the result set. This was particularly necessary to easily
    make changes to some sqltests due to switching the delete trigger for
    the session table.
    tmessi committed Sep 17, 2024
    Configuration menu
    Copy the full SHA
    139e5b9 View commit details
    Browse the repository at this point in the history