Use PLpgSQL functions for primary key lookups
What does this MR do and why?
This MR adds a feature flag to control whether to use SQL functions when
looking up users
, projects
, namespaces
via their primary key.
The idea is here that by using SQL function we will implicitly use prepared statements, and thus not lock all indexes, which should hep with LWLock saturation.
Related discussion: gitlab-com/gl-infra&1129 (comment 1611749436).
How to set up and validate locally
- Enable the FF
Feature.enable(:use_sql_functions_for_primary_key_lookups)
- Run simple find queries
Project.find(1) Namespace.find(1) User.find(1)
- Instead of a
SELECT FROM table
, you should see a function call query:FROM find_xyz_by_id()
Testing
We have done the following tests:
- Add column
- Open a Rails console, execute
Namespace.find(1)
. - In a separate
psql
console, executealter table namespaces add column test_column integer default 123
. - In the Rails console, confirm
Namespace.find(1)
still works.
- Open a Rails console, execute
- Alter column
- Open a Rails console, execute
Namespace.find(1)
. - In a separate
psql
console, executealter table namespaces alter column test_column type bigint
. - In the Rails console, confirm
Namespace.find(1)
still works.
- Open a Rails console, execute
- Drop column
- Declare
test_column
as ignored column inapp/models/namespace.rb
. - Open a Rails console, execute
Namespace.find(1)
. - In a separate
psql
console, executealter table namespaces drop column test_column
. - In the Rails console, confirm
Namespace.find(1)
still works.
- Declare
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.
Edited by Krasimir Angelov