Skip to content

Primary key for elasticsearch_indexed_namespaces

What does this MR do?

Related to #270090 (closed)

Promotes the unique index from elasticsearch_indexed_namespaces into a primary key. On GitLab.com production, the table has around 10k rows. It is also in-use in self-managed installations, but estimated to be significantly smaller than in the GitLab.com database.

Since the table is small enough, it seems relatively safe to transactionally promote the existing index inline, rather than add the complexity of creating a new NOT NULL replacement column, migrating data, etc.

For example in database-lab, the null check + adding primary key is quite fast:

ALTER TABLE elasticsearch_indexed_namespaces ALTER COLUMN namespace_id SET NOT NULL, ADD CONSTRAINT elasticsearch_indexed_namespaces_pkey PRIMARY KEY USING INDEX index_elasticsearch_indexed_namespaces_on_namespace_id;
Time: 163.284 ms

The DELETE is supported by the index, if there are any existing rows where namespace_id IS NULL: https://explain.depesz.com/s/v27r

Migration up
== 20201201192112 AddPrimaryKeyToElasticSearchIndexedNamespaces: migrating ====
-- execute("LOCK TABLE elasticsearch_indexed_namespaces IN ACCESS EXCLUSIVE MODE;\n\nDELETE FROM elasticsearch_indexed_namespaces\nWHERE namespace_id IS NULL;\n\nALTER TABLE elasticsearch_indexed_namespaces\nADD CONSTRAINT elasticsearch_indexed_namespaces_pkey PRIMARY KEY USING INDEX index_elasticsearch_indexed_namespaces_on_namespace_id;\n")
   -> 0.0012s
== 20201201192112 AddPrimaryKeyToElasticSearchIndexedNamespaces: migrated (0.0043s)
Migration down
rails db:migrate:down VERSION=20201201192112
== 20201201192112 AddPrimaryKeyToElasticSearchIndexedNamespaces: reverting ====
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:elasticsearch_indexed_namespaces, :namespace_id, {:unique=>true, :name=>"index_elasticsearch_indexed_namespaces_on_namespace_id", :algorithm=>:concurrently})
   -> 0.0023s
-- execute("SET statement_timeout TO 0")
   -> 0.0002s
-- add_index(:elasticsearch_indexed_namespaces, :namespace_id, {:unique=>true, :name=>"index_elasticsearch_indexed_namespaces_on_namespace_id", :algorithm=>:concurrently})
   -> 0.0030s
-- execute("RESET ALL")
   -> 0.0002s
-- execute("ALTER TABLE elasticsearch_indexed_namespaces\nDROP CONSTRAINT elasticsearch_indexed_namespaces_pkey,\nALTER COLUMN namespace_id DROP NOT NULL\n")
   -> 0.0006s
== 20201201192112 AddPrimaryKeyToElasticSearchIndexedNamespaces: reverted (0.0099s)

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
Edited by Patrick Bair

Merge request reports

Loading