Skip to content

Use PLpgSQL functions for primary key lookups

Adam Hegyi requested to merge ah-use-sql-functions-for-primary-key-lookups into master

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

  1. Enable the FF
    Feature.enable(:use_sql_functions_for_primary_key_lookups)
  2. Run simple find queries
    Project.find(1)
    Namespace.find(1)
    User.find(1)
  3. 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
    1. Open a Rails console, execute Namespace.find(1).
    2. In a separate psql console, execute alter table namespaces add column test_column integer default 123.
    3. In the Rails console, confirm Namespace.find(1) still works.
  • Alter column
    1. Open a Rails console, execute Namespace.find(1).
    2. In a separate psql console, execute alter table namespaces alter column test_column type bigint.
    3. In the Rails console, confirm Namespace.find(1) still works.
  • Drop column
    1. Declare test_column as ignored column in app/models/namespace.rb.
    2. Open a Rails console, execute Namespace.find(1).
    3. In a separate psql console, execute alter table namespaces drop column test_column.
    4. In the Rails console, confirm Namespace.find(1) still works.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Krasimir Angelov

Merge request reports