Skip to content

Add partial index on members to optimize highest access level query

What does this MR do?

Add partial index on memebers (user_id, access_level) WHERE requested_at IS NULL to speed up the query generated by User#current_highest_access_level (called from UpdateHighestRoleWorker).

See #327593 (closed).

Execution plans

We see massive drop in shared buffers used - ~7.40 GiB vs ~32.00 KiB.

Index creation took 37.567 s on postgres.ai.

Database migration

Up

$ bundle exec rails db:migrate:up VERSION=20210415074645
== 20210415074645 IndexMembersOnUserIdAccessLevelRequestedAtIsNull: migrating =
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:members, [:user_id, :access_level], {:where=>"requested_at IS NULL", :name=>"index_members_on_user_id_and_access_level_requested_at_is_null", :algorithm=>:concurrently})
   -> 0.0048s
-- execute("SET statement_timeout TO 0")
   -> 0.0005s
-- add_index(:members, [:user_id, :access_level], {:where=>"requested_at IS NULL", :name=>"index_members_on_user_id_and_access_level_requested_at_is_null", :algorithm=>:concurrently})
   -> 0.0037s
-- execute("RESET ALL")
   -> 0.0005s
== 20210415074645 IndexMembersOnUserIdAccessLevelRequestedAtIsNull: migrated (0.0105s) 

Down

$ bundle exec rails db:migrate:down VERSION=20210415074645
== 20210415074645 IndexMembersOnUserIdAccessLevelRequestedAtIsNull: reverting =
-- transaction_open?()
   -> 0.0000s
-- indexes(:members)
   -> 0.0053s
-- execute("SET statement_timeout TO 0")
   -> 0.0005s
-- remove_index(:members, {:algorithm=>:concurrently, :name=>"index_members_on_user_id_and_access_level_requested_at_is_null"})
   -> 0.0045s
-- execute("RESET ALL")
   -> 0.0006s
== 20210415074645 IndexMembersOnUserIdAccessLevelRequestedAtIsNull: reverted (0.0119s) 

Screenshots (strongly suggested)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team

Related to #327593 (closed)

Edited by Krasimir Angelov

Merge request reports