From c861856dffecf71b7edd8de7e98f2f315945c555 Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 10 Apr 2025 18:27:22 +0200 Subject: [PATCH] Create missing indexes for all the foreign keys in the database. --- ...10000000_idx_compat_access_tokens_session_fk.sql | 9 +++++++++ ...0000001_idx_compat_refresh_tokens_session_fk.sql | 9 +++++++++ ...02_idx_compat_refresh_tokens_access_token_fk.sql | 9 +++++++++ .../20250410000003_idx_compat_sessions_user_fk.sql | 13 +++++++++++++ ...10000004_idx_compat_sessions_user_session_fk.sql | 9 +++++++++ ..._drop_compat_sessions_user_id_last_active_at.sql | 8 ++++++++ ...50410000006_idx_compat_sso_logins_session_fk.sql | 9 +++++++++ ...10000007_idx_oauth2_access_tokens_session_fk.sql | 9 +++++++++ ...8_idx_oauth2_authorization_grants_session_fk.sql | 9 +++++++++ ...09_idx_oauth2_authorization_grants_client_fk.sql | 9 +++++++++ ...20250410000010_idx_oauth2_consents_client_fk.sql | 9 +++++++++ .../20250410000011_idx_oauth2_consents_user_fk.sql | 9 +++++++++ ...0012_idx_oauth2_device_code_grants_client_fk.sql | 9 +++++++++ ...013_idx_oauth2_device_code_grants_session_fk.sql | 9 +++++++++ ...dx_oauth2_device_code_grants_user_session_fk.sql | 9 +++++++++ ...0000015_idx_oauth2_refresh_tokens_session_fk.sql | 9 +++++++++ ...16_idx_oauth2_refresh_tokens_access_token_fk.sql | 9 +++++++++ ..._oauth2_refresh_tokens_next_refresh_token_fk.sql | 9 +++++++++ ...10000018_idx_oauth2_sessions_user_session_fk.sql | 9 +++++++++ ...20250410000019_idx_oauth2_sessions_client_fk.sql | 9 +++++++++ .../20250410000020_idx_oauth2_sessions_user_fk.sql | 13 +++++++++++++ ..._drop_oauth2_sessions_user_id_last_active_at.sql | 8 ++++++++ .../20250410000022_idx_queue_jobs_started_by_fk.sql | 9 +++++++++ ...0250410000023_idx_queue_jobs_next_attempt_fk.sql | 9 +++++++++ ...250410000024_idx_queue_jobs_schedule_name_fk.sql | 9 +++++++++ ...eam_oauth_authorization_sessions_provider_fk.sql | 9 +++++++++ ...pstream_oauth_authorization_sessions_link_fk.sql | 9 +++++++++ ...0000027_idx_upstream_oauth_links_provider_fk.sql | 9 +++++++++ ...50410000028_idx_upstream_oauth_links_user_fk.sql | 9 +++++++++ ...email_authentication_codes_authentication_fk.sql | 9 +++++++++ ...x_user_email_authentications_user_session_fk.sql | 9 +++++++++ ...r_email_authentications_user_registration_fk.sql | 9 +++++++++ .../20250410000032_idx_user_emails_user_fk.sql | 9 +++++++++ .../20250410000033_idx_user_emails_email_idx.sql | 10 ++++++++++ .../20250410000034_idx_user_passwords_user_fk.sql | 9 +++++++++ ...0000035_idx_user_recovery_tickets_session_fk.sql | 9 +++++++++ ...0036_idx_user_recovery_tickets_user_email_fk.sql | 9 +++++++++ ...x_user_registrations_email_authentication_fk.sql | 9 +++++++++ ...user_session_authentications_user_session_fk.sql | 9 +++++++++ ...ser_session_authentications_user_password_fk.sql | 9 +++++++++ ...on_authentications_upstream_oauth_session_fk.sql | 9 +++++++++ .../20250410000041_idx_user_sessions_user_fk.sql | 13 +++++++++++++ ...42_drop_user_sessions_user_id_last_active_at.sql | 8 ++++++++ .../20250410000043_idx_user_terms_user_fk.sql | 9 +++++++++ .../20250410000044_idx_users_primary_email_fk.sql | 11 +++++++++++ ...0000045_idx_user_recovery_tickets_ticket_idx.sql | 10 ++++++++++ 46 files changed, 427 insertions(+) create mode 100644 crates/storage-pg/migrations/20250410000000_idx_compat_access_tokens_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000001_idx_compat_refresh_tokens_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000002_idx_compat_refresh_tokens_access_token_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000003_idx_compat_sessions_user_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000004_idx_compat_sessions_user_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000005_drop_compat_sessions_user_id_last_active_at.sql create mode 100644 crates/storage-pg/migrations/20250410000006_idx_compat_sso_logins_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000007_idx_oauth2_access_tokens_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000008_idx_oauth2_authorization_grants_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000009_idx_oauth2_authorization_grants_client_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000010_idx_oauth2_consents_client_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000011_idx_oauth2_consents_user_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000012_idx_oauth2_device_code_grants_client_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000013_idx_oauth2_device_code_grants_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000014_idx_oauth2_device_code_grants_user_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000015_idx_oauth2_refresh_tokens_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000016_idx_oauth2_refresh_tokens_access_token_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000017_idx_oauth2_refresh_tokens_next_refresh_token_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000018_idx_oauth2_sessions_user_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000019_idx_oauth2_sessions_client_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000020_idx_oauth2_sessions_user_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000021_drop_oauth2_sessions_user_id_last_active_at.sql create mode 100644 crates/storage-pg/migrations/20250410000022_idx_queue_jobs_started_by_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000023_idx_queue_jobs_next_attempt_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000024_idx_queue_jobs_schedule_name_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000025_idx_upstream_oauth_authorization_sessions_provider_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000026_idx_upstream_oauth_authorization_sessions_link_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000027_idx_upstream_oauth_links_provider_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000028_idx_upstream_oauth_links_user_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000029_idx_user_email_authentication_codes_authentication_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000030_idx_user_email_authentications_user_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000031_idx_user_email_authentications_user_registration_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000032_idx_user_emails_user_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000033_idx_user_emails_email_idx.sql create mode 100644 crates/storage-pg/migrations/20250410000034_idx_user_passwords_user_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000035_idx_user_recovery_tickets_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000036_idx_user_recovery_tickets_user_email_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000037_idx_user_registrations_email_authentication_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000038_idx_user_session_authentications_user_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000039_idx_user_session_authentications_user_password_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000040_idx_user_session_authentications_upstream_oauth_session_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000041_idx_user_sessions_user_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000042_drop_user_sessions_user_id_last_active_at.sql create mode 100644 crates/storage-pg/migrations/20250410000043_idx_user_terms_user_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000044_idx_users_primary_email_fk.sql create mode 100644 crates/storage-pg/migrations/20250410000045_idx_user_recovery_tickets_ticket_idx.sql diff --git a/crates/storage-pg/migrations/20250410000000_idx_compat_access_tokens_session_fk.sql b/crates/storage-pg/migrations/20250410000000_idx_compat_access_tokens_session_fk.sql new file mode 100644 index 000000000..777118233 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000000_idx_compat_access_tokens_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + compat_access_tokens_session_fk + ON compat_access_tokens (compat_session_id); diff --git a/crates/storage-pg/migrations/20250410000001_idx_compat_refresh_tokens_session_fk.sql b/crates/storage-pg/migrations/20250410000001_idx_compat_refresh_tokens_session_fk.sql new file mode 100644 index 000000000..d0e99ad9b --- /dev/null +++ b/crates/storage-pg/migrations/20250410000001_idx_compat_refresh_tokens_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + compat_refresh_tokens_session_fk + ON compat_refresh_tokens (compat_session_id); diff --git a/crates/storage-pg/migrations/20250410000002_idx_compat_refresh_tokens_access_token_fk.sql b/crates/storage-pg/migrations/20250410000002_idx_compat_refresh_tokens_access_token_fk.sql new file mode 100644 index 000000000..133b00072 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000002_idx_compat_refresh_tokens_access_token_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + compat_refresh_tokens_access_token_fk + ON compat_refresh_tokens (compat_access_token_id); diff --git a/crates/storage-pg/migrations/20250410000003_idx_compat_sessions_user_fk.sql b/crates/storage-pg/migrations/20250410000003_idx_compat_sessions_user_fk.sql new file mode 100644 index 000000000..3234fc8ca --- /dev/null +++ b/crates/storage-pg/migrations/20250410000003_idx_compat_sessions_user_fk.sql @@ -0,0 +1,13 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- Including the `last_active_at` column lets us effeciently filter in-memory +-- for those sessions without fetching the rows, and without including it in the +-- index btree +CREATE INDEX CONCURRENTLY + compat_sessions_user_fk + ON compat_sessions (user_id) + INCLUDE (last_active_at); diff --git a/crates/storage-pg/migrations/20250410000004_idx_compat_sessions_user_session_fk.sql b/crates/storage-pg/migrations/20250410000004_idx_compat_sessions_user_session_fk.sql new file mode 100644 index 000000000..4b35f9960 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000004_idx_compat_sessions_user_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + compat_sessions_user_session_fk + ON compat_sessions (user_session_id); diff --git a/crates/storage-pg/migrations/20250410000005_drop_compat_sessions_user_id_last_active_at.sql b/crates/storage-pg/migrations/20250410000005_drop_compat_sessions_user_id_last_active_at.sql new file mode 100644 index 000000000..4c37289ff --- /dev/null +++ b/crates/storage-pg/migrations/20250410000005_drop_compat_sessions_user_id_last_active_at.sql @@ -0,0 +1,8 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- Redundant with the `compat_sessions_user_fk` +DROP INDEX IF EXISTS compat_sessions_user_id_last_active_at; diff --git a/crates/storage-pg/migrations/20250410000006_idx_compat_sso_logins_session_fk.sql b/crates/storage-pg/migrations/20250410000006_idx_compat_sso_logins_session_fk.sql new file mode 100644 index 000000000..e88224e66 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000006_idx_compat_sso_logins_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + compat_sso_logins_session_fk + ON compat_sso_logins (compat_session_id); diff --git a/crates/storage-pg/migrations/20250410000007_idx_oauth2_access_tokens_session_fk.sql b/crates/storage-pg/migrations/20250410000007_idx_oauth2_access_tokens_session_fk.sql new file mode 100644 index 000000000..7495af2a3 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000007_idx_oauth2_access_tokens_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_access_tokens_session_fk + ON oauth2_access_tokens (oauth2_session_id); diff --git a/crates/storage-pg/migrations/20250410000008_idx_oauth2_authorization_grants_session_fk.sql b/crates/storage-pg/migrations/20250410000008_idx_oauth2_authorization_grants_session_fk.sql new file mode 100644 index 000000000..dac57bf91 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000008_idx_oauth2_authorization_grants_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_authorization_grants_session_fk + ON oauth2_authorization_grants (oauth2_session_id); diff --git a/crates/storage-pg/migrations/20250410000009_idx_oauth2_authorization_grants_client_fk.sql b/crates/storage-pg/migrations/20250410000009_idx_oauth2_authorization_grants_client_fk.sql new file mode 100644 index 000000000..fd5d30344 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000009_idx_oauth2_authorization_grants_client_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_authorization_grants_client_fk + ON oauth2_authorization_grants (oauth2_client_id); diff --git a/crates/storage-pg/migrations/20250410000010_idx_oauth2_consents_client_fk.sql b/crates/storage-pg/migrations/20250410000010_idx_oauth2_consents_client_fk.sql new file mode 100644 index 000000000..ae8e0b5dd --- /dev/null +++ b/crates/storage-pg/migrations/20250410000010_idx_oauth2_consents_client_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_consents_client_fk + ON oauth2_consents (oauth2_client_id); diff --git a/crates/storage-pg/migrations/20250410000011_idx_oauth2_consents_user_fk.sql b/crates/storage-pg/migrations/20250410000011_idx_oauth2_consents_user_fk.sql new file mode 100644 index 000000000..c9226b7d6 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000011_idx_oauth2_consents_user_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_consents_user_fk + ON oauth2_consents (user_id); diff --git a/crates/storage-pg/migrations/20250410000012_idx_oauth2_device_code_grants_client_fk.sql b/crates/storage-pg/migrations/20250410000012_idx_oauth2_device_code_grants_client_fk.sql new file mode 100644 index 000000000..a9981fe4b --- /dev/null +++ b/crates/storage-pg/migrations/20250410000012_idx_oauth2_device_code_grants_client_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_device_code_grants_client_fk + ON oauth2_device_code_grant (oauth2_client_id); diff --git a/crates/storage-pg/migrations/20250410000013_idx_oauth2_device_code_grants_session_fk.sql b/crates/storage-pg/migrations/20250410000013_idx_oauth2_device_code_grants_session_fk.sql new file mode 100644 index 000000000..be8f685d1 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000013_idx_oauth2_device_code_grants_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_device_code_grants_session_fk + ON oauth2_device_code_grant (oauth2_session_id); diff --git a/crates/storage-pg/migrations/20250410000014_idx_oauth2_device_code_grants_user_session_fk.sql b/crates/storage-pg/migrations/20250410000014_idx_oauth2_device_code_grants_user_session_fk.sql new file mode 100644 index 000000000..f3f6613d7 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000014_idx_oauth2_device_code_grants_user_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_device_code_grants_user_session_fk + ON oauth2_device_code_grant (user_session_id); diff --git a/crates/storage-pg/migrations/20250410000015_idx_oauth2_refresh_tokens_session_fk.sql b/crates/storage-pg/migrations/20250410000015_idx_oauth2_refresh_tokens_session_fk.sql new file mode 100644 index 000000000..897d247c8 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000015_idx_oauth2_refresh_tokens_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_refresh_tokens_session_fk + ON oauth2_refresh_tokens (oauth2_session_id); diff --git a/crates/storage-pg/migrations/20250410000016_idx_oauth2_refresh_tokens_access_token_fk.sql b/crates/storage-pg/migrations/20250410000016_idx_oauth2_refresh_tokens_access_token_fk.sql new file mode 100644 index 000000000..5d391e6a5 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000016_idx_oauth2_refresh_tokens_access_token_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_refresh_tokens_access_token_fk + ON oauth2_refresh_tokens (oauth2_access_token_id); diff --git a/crates/storage-pg/migrations/20250410000017_idx_oauth2_refresh_tokens_next_refresh_token_fk.sql b/crates/storage-pg/migrations/20250410000017_idx_oauth2_refresh_tokens_next_refresh_token_fk.sql new file mode 100644 index 000000000..f593f1d28 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000017_idx_oauth2_refresh_tokens_next_refresh_token_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_refresh_tokens_next_refresh_token_fk + ON oauth2_refresh_tokens (next_oauth2_refresh_token_id); diff --git a/crates/storage-pg/migrations/20250410000018_idx_oauth2_sessions_user_session_fk.sql b/crates/storage-pg/migrations/20250410000018_idx_oauth2_sessions_user_session_fk.sql new file mode 100644 index 000000000..44d6d0e3c --- /dev/null +++ b/crates/storage-pg/migrations/20250410000018_idx_oauth2_sessions_user_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_sessions_user_session_fk + ON oauth2_sessions (user_session_id); diff --git a/crates/storage-pg/migrations/20250410000019_idx_oauth2_sessions_client_fk.sql b/crates/storage-pg/migrations/20250410000019_idx_oauth2_sessions_client_fk.sql new file mode 100644 index 000000000..ad868a310 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000019_idx_oauth2_sessions_client_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + oauth2_sessions_client_fk + ON oauth2_sessions (oauth2_client_id); diff --git a/crates/storage-pg/migrations/20250410000020_idx_oauth2_sessions_user_fk.sql b/crates/storage-pg/migrations/20250410000020_idx_oauth2_sessions_user_fk.sql new file mode 100644 index 000000000..b47bb00a8 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000020_idx_oauth2_sessions_user_fk.sql @@ -0,0 +1,13 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- Including the `last_active_at` column lets us effeciently filter in-memory +-- for those sessions without fetching the rows, and without including it in the +-- index btree +CREATE INDEX CONCURRENTLY + oauth2_sessions_user_fk + ON oauth2_sessions (user_id) + INCLUDE (last_active_at); diff --git a/crates/storage-pg/migrations/20250410000021_drop_oauth2_sessions_user_id_last_active_at.sql b/crates/storage-pg/migrations/20250410000021_drop_oauth2_sessions_user_id_last_active_at.sql new file mode 100644 index 000000000..108dc290b --- /dev/null +++ b/crates/storage-pg/migrations/20250410000021_drop_oauth2_sessions_user_id_last_active_at.sql @@ -0,0 +1,8 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- Redundant with the `oauth2_sessions_user_fk` +DROP INDEX IF EXISTS oauth2_sessions_user_id_last_active_at; diff --git a/crates/storage-pg/migrations/20250410000022_idx_queue_jobs_started_by_fk.sql b/crates/storage-pg/migrations/20250410000022_idx_queue_jobs_started_by_fk.sql new file mode 100644 index 000000000..38bb79ede --- /dev/null +++ b/crates/storage-pg/migrations/20250410000022_idx_queue_jobs_started_by_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + queue_jobs_started_by_fk + ON queue_jobs (started_by); diff --git a/crates/storage-pg/migrations/20250410000023_idx_queue_jobs_next_attempt_fk.sql b/crates/storage-pg/migrations/20250410000023_idx_queue_jobs_next_attempt_fk.sql new file mode 100644 index 000000000..ea611f76e --- /dev/null +++ b/crates/storage-pg/migrations/20250410000023_idx_queue_jobs_next_attempt_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + queue_jobs_next_attempt_fk + ON queue_jobs (next_attempt_id); diff --git a/crates/storage-pg/migrations/20250410000024_idx_queue_jobs_schedule_name_fk.sql b/crates/storage-pg/migrations/20250410000024_idx_queue_jobs_schedule_name_fk.sql new file mode 100644 index 000000000..02b2bfaea --- /dev/null +++ b/crates/storage-pg/migrations/20250410000024_idx_queue_jobs_schedule_name_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + queue_jobs_schedule_name_fk + ON queue_jobs (schedule_name); diff --git a/crates/storage-pg/migrations/20250410000025_idx_upstream_oauth_authorization_sessions_provider_fk.sql b/crates/storage-pg/migrations/20250410000025_idx_upstream_oauth_authorization_sessions_provider_fk.sql new file mode 100644 index 000000000..f5e388613 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000025_idx_upstream_oauth_authorization_sessions_provider_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + upstream_oauth_authorization_sessions_provider_fk + ON upstream_oauth_authorization_sessions (upstream_oauth_provider_id); diff --git a/crates/storage-pg/migrations/20250410000026_idx_upstream_oauth_authorization_sessions_link_fk.sql b/crates/storage-pg/migrations/20250410000026_idx_upstream_oauth_authorization_sessions_link_fk.sql new file mode 100644 index 000000000..aa08e7fa1 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000026_idx_upstream_oauth_authorization_sessions_link_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + upstream_oauth_authorization_sessions_link_fk + ON upstream_oauth_authorization_sessions (upstream_oauth_link_id); diff --git a/crates/storage-pg/migrations/20250410000027_idx_upstream_oauth_links_provider_fk.sql b/crates/storage-pg/migrations/20250410000027_idx_upstream_oauth_links_provider_fk.sql new file mode 100644 index 000000000..9f6a9301f --- /dev/null +++ b/crates/storage-pg/migrations/20250410000027_idx_upstream_oauth_links_provider_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + upstream_oauth_links_provider_fk + ON upstream_oauth_links (upstream_oauth_provider_id); diff --git a/crates/storage-pg/migrations/20250410000028_idx_upstream_oauth_links_user_fk.sql b/crates/storage-pg/migrations/20250410000028_idx_upstream_oauth_links_user_fk.sql new file mode 100644 index 000000000..af7791f18 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000028_idx_upstream_oauth_links_user_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + upstream_oauth_links_user_fk + ON upstream_oauth_links (user_id); diff --git a/crates/storage-pg/migrations/20250410000029_idx_user_email_authentication_codes_authentication_fk.sql b/crates/storage-pg/migrations/20250410000029_idx_user_email_authentication_codes_authentication_fk.sql new file mode 100644 index 000000000..889fac2ab --- /dev/null +++ b/crates/storage-pg/migrations/20250410000029_idx_user_email_authentication_codes_authentication_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_email_authentication_codes_authentication_fk + ON user_email_authentication_codes (user_email_authentication_id); diff --git a/crates/storage-pg/migrations/20250410000030_idx_user_email_authentications_user_session_fk.sql b/crates/storage-pg/migrations/20250410000030_idx_user_email_authentications_user_session_fk.sql new file mode 100644 index 000000000..3a6284f67 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000030_idx_user_email_authentications_user_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_email_authentications_user_session_fk + ON user_email_authentications (user_session_id); diff --git a/crates/storage-pg/migrations/20250410000031_idx_user_email_authentications_user_registration_fk.sql b/crates/storage-pg/migrations/20250410000031_idx_user_email_authentications_user_registration_fk.sql new file mode 100644 index 000000000..9ad343ac9 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000031_idx_user_email_authentications_user_registration_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_email_authentications_user_registration_fk + ON user_email_authentications (user_registration_id); diff --git a/crates/storage-pg/migrations/20250410000032_idx_user_emails_user_fk.sql b/crates/storage-pg/migrations/20250410000032_idx_user_emails_user_fk.sql new file mode 100644 index 000000000..f66117d43 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000032_idx_user_emails_user_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_emails_user_fk + ON user_emails (user_id); diff --git a/crates/storage-pg/migrations/20250410000033_idx_user_emails_email_idx.sql b/crates/storage-pg/migrations/20250410000033_idx_user_emails_email_idx.sql new file mode 100644 index 000000000..e75e1e028 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000033_idx_user_emails_email_idx.sql @@ -0,0 +1,10 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- This isn't a foreign key, but we really need that to be indexed +CREATE INDEX CONCURRENTLY + user_emails_email_idx + ON user_emails (email); diff --git a/crates/storage-pg/migrations/20250410000034_idx_user_passwords_user_fk.sql b/crates/storage-pg/migrations/20250410000034_idx_user_passwords_user_fk.sql new file mode 100644 index 000000000..cfc1a9df7 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000034_idx_user_passwords_user_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_passwords_user_fk + ON user_passwords (user_id); diff --git a/crates/storage-pg/migrations/20250410000035_idx_user_recovery_tickets_session_fk.sql b/crates/storage-pg/migrations/20250410000035_idx_user_recovery_tickets_session_fk.sql new file mode 100644 index 000000000..22537f6a1 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000035_idx_user_recovery_tickets_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_recovery_tickets_session_fk + ON user_recovery_tickets (user_recovery_session_id); diff --git a/crates/storage-pg/migrations/20250410000036_idx_user_recovery_tickets_user_email_fk.sql b/crates/storage-pg/migrations/20250410000036_idx_user_recovery_tickets_user_email_fk.sql new file mode 100644 index 000000000..81bee0605 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000036_idx_user_recovery_tickets_user_email_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_recovery_tickets_user_email_fk + ON user_recovery_tickets (user_email_id); diff --git a/crates/storage-pg/migrations/20250410000037_idx_user_registrations_email_authentication_fk.sql b/crates/storage-pg/migrations/20250410000037_idx_user_registrations_email_authentication_fk.sql new file mode 100644 index 000000000..3385114d1 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000037_idx_user_registrations_email_authentication_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_registrations_email_authentication_fk + ON user_registrations (email_authentication_id); diff --git a/crates/storage-pg/migrations/20250410000038_idx_user_session_authentications_user_session_fk.sql b/crates/storage-pg/migrations/20250410000038_idx_user_session_authentications_user_session_fk.sql new file mode 100644 index 000000000..e71abc0a6 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000038_idx_user_session_authentications_user_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_session_authentications_user_session_fk + ON user_session_authentications (user_session_id); diff --git a/crates/storage-pg/migrations/20250410000039_idx_user_session_authentications_user_password_fk.sql b/crates/storage-pg/migrations/20250410000039_idx_user_session_authentications_user_password_fk.sql new file mode 100644 index 000000000..c6e262d0e --- /dev/null +++ b/crates/storage-pg/migrations/20250410000039_idx_user_session_authentications_user_password_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_session_authentications_user_password_fk + ON user_session_authentications (user_password_id); diff --git a/crates/storage-pg/migrations/20250410000040_idx_user_session_authentications_upstream_oauth_session_fk.sql b/crates/storage-pg/migrations/20250410000040_idx_user_session_authentications_upstream_oauth_session_fk.sql new file mode 100644 index 000000000..98f5728ad --- /dev/null +++ b/crates/storage-pg/migrations/20250410000040_idx_user_session_authentications_upstream_oauth_session_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_session_authentications_upstream_oauth_session_fk + ON user_session_authentications (upstream_oauth_authorization_session_id); diff --git a/crates/storage-pg/migrations/20250410000041_idx_user_sessions_user_fk.sql b/crates/storage-pg/migrations/20250410000041_idx_user_sessions_user_fk.sql new file mode 100644 index 000000000..e2b83345c --- /dev/null +++ b/crates/storage-pg/migrations/20250410000041_idx_user_sessions_user_fk.sql @@ -0,0 +1,13 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- Including the `last_active_at` column lets us effeciently filter in-memory +-- for those sessions without fetching the rows, and without including it in the +-- index btree +CREATE INDEX CONCURRENTLY + user_sessions_user_fk + ON user_sessions (user_id) + INCLUDE (last_active_at); diff --git a/crates/storage-pg/migrations/20250410000042_drop_user_sessions_user_id_last_active_at.sql b/crates/storage-pg/migrations/20250410000042_drop_user_sessions_user_id_last_active_at.sql new file mode 100644 index 000000000..1c95573c1 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000042_drop_user_sessions_user_id_last_active_at.sql @@ -0,0 +1,8 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- Redundant with the `user_sessions_user_fk` +DROP INDEX IF EXISTS user_sessions_user_id_last_active_at; diff --git a/crates/storage-pg/migrations/20250410000043_idx_user_terms_user_fk.sql b/crates/storage-pg/migrations/20250410000043_idx_user_terms_user_fk.sql new file mode 100644 index 000000000..f95b63a72 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000043_idx_user_terms_user_fk.sql @@ -0,0 +1,9 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +CREATE INDEX CONCURRENTLY + user_terms_user_fk + ON user_terms (user_id); diff --git a/crates/storage-pg/migrations/20250410000044_idx_users_primary_email_fk.sql b/crates/storage-pg/migrations/20250410000044_idx_users_primary_email_fk.sql new file mode 100644 index 000000000..57ae2d0d0 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000044_idx_users_primary_email_fk.sql @@ -0,0 +1,11 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- We don't use this column anymore, but… it will still tank the performance on +-- deletions of user_emails if we don't have it +CREATE INDEX CONCURRENTLY + users_primary_email_fk + ON users (primary_user_email_id); diff --git a/crates/storage-pg/migrations/20250410000045_idx_user_recovery_tickets_ticket_idx.sql b/crates/storage-pg/migrations/20250410000045_idx_user_recovery_tickets_ticket_idx.sql new file mode 100644 index 000000000..3030078f9 --- /dev/null +++ b/crates/storage-pg/migrations/20250410000045_idx_user_recovery_tickets_ticket_idx.sql @@ -0,0 +1,10 @@ +-- no-transaction +-- Copyright 2025 New Vector Ltd. +-- +-- SPDX-License-Identifier: AGPL-3.0-only +-- Please see LICENSE in the repository root for full details. + +-- This isn't a foreign key, but we really need that to be indexed +CREATE INDEX CONCURRENTLY + user_recovery_tickets_ticket_idx + ON user_recovery_tickets (ticket);