Skip to content

User emails dependent destroy to foreign key with cascade delete

Drew Blessing requested to merge dblessing-emails-cascade-delete into master

What does this MR do?

Cleanup/ prep work for https://gitlab.com/gitlab-org/gitlab/-/issues/218788

The user emails relationship still relied on the Rails dependent destroy mechanism. This change moves that dependent destroy to a proper foreign key with cascading delete.

Database

Migration

== 20200819192143 AddEmailsUserIdForeignKey: migrating ========================
-- add_foreign_key(:emails, :users, {:on_delete=>:cascade, :validate=>false, :name=>"fk_emails_user_id"})
   -> 0.0022s
== 20200819192143 AddEmailsUserIdForeignKey: migrated (0.0039s) ===============

== 20200819202048 RemoveOrphanedEmails: migrating =============================
-- execute("DELETE FROM emails\n  WHERE not exists (\n    SELECT 1 FROM users WHERE users.id = emails.user_id\n  );\n")
   -> 0.0044s
-- execute("DELETE FROM emails WHERE user_id IS NULL;")
   -> 0.0003s
== 20200819202048 RemoveOrphanedEmails: migrated (0.0048s) ====================

== 20200819202222 ValidateEmailsUserIdForeignKey: migrating ===================
-- foreign_keys(:emails)
   -> 0.0036s
-- execute("ALTER TABLE emails VALIDATE CONSTRAINT fk_emails_user_id;")
   -> 0.0006s
== 20200819202222 ValidateEmailsUserIdForeignKey: migrated (0.0045s) ==========

Rollback

== 20200819202222 ValidateEmailsUserIdForeignKey: reverting ===================
== 20200819202222 ValidateEmailsUserIdForeignKey: reverted (0.0000s) ==========

== 20200819202048 RemoveOrphanedEmails: reverting =============================
== 20200819202048 RemoveOrphanedEmails: reverted (0.0000s) ====================

== 20200819192143 AddEmailsUserIdForeignKey: reverting ========================
-- foreign_keys(:emails)
   -> 0.0040s
-- remove_foreign_key(:emails, {:column=>:user_id, :name=>"fk_emails_user_id"})
   -> 0.0041s
== 20200819192143 AddEmailsUserIdForeignKey: reverted (0.0098s) ===============

GitLab.com query plan

Query:

DELETE FROM emails
  WHERE not exists (
    SELECT 1 FROM users WHERE users.id = emails.user_id
);
 ModifyTable on public.emails  (cost=0.85..473501.05 rows=1 width=12) (actual time=178146.749..178146.749 rows=0 loops=1)
   Buffers: shared hit=1154627 read=166121 dirtied=9392
   I/O Timings: read=174962.357
   ->  Nested Loop Anti Join  (cost=0.85..473501.05 rows=1 width=12) (actual time=101053.558..178146.641 rows=1 loops=1)
         Buffers: shared hit=1154626 read=166121 dirtied=9391
         I/O Timings: read=174962.357
         ->  Index Scan using index_emails_on_user_id on public.emails  (cost=0.42..15478.33 rows=266532 width=10) (actual time=2.620..5430.985 rows=267344 loops=1)
               Buffers: shared hit=246286 read=5036 dirtied=721
               I/O Timings: read=4692.728
         ->  Index Scan using users_pkey on public.users  (cost=0.43..1.71 rows=1 width=10) (actual time=0.645..0.645 rows=1 loops=267344)
               Index Cond: (users.id = emails.user_id)
               Buffers: shared hit=908340 read=161085 dirtied=8670
               I/

Execution took 2.9 min in #database-lab. I think the query plan indicates only 1 row would be removed on GitLab.com? I also used #database-lab to see how many might have NULL user_id values and that seemed to be only 1, too.

Screenshots

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 Drew Blessing

Merge request reports

Loading