Skip to content

Add tmp index to oauth-access-tokens on application_id where token created after 20220705 and not revoked

What does this MR do and why?

Add tmp-idx-oauth-access-tokens-appid-after-20220705-not-revoked

MR !120820 needs to query OauthAccessToken by application_id, where the token was not revoked and was created after 20220705. OauthAccessToken is big. Even the batched query could take minutes.

This tmp index allows the batched query to finish in several seconds. The tmp index will be removed in the future #385343

DB migrate up output
% rails db:migrate
main: == [advisory_lock_connection] object_id: 279040, pg_backend_pid: 70605
main: == 20230525122726 AddTmpIdxOauthAccessTokensAppidAfter20220705NotRevoked: migrating
main: -- transaction_open?()
main:    -> 0.0001s
main: -- view_exists?(:postgres_partitions)
main:    -> 0.0470s
main: -- index_exists?(:oauth_access_tokens, :application_id, {:where=>"revoked_at IS NULL AND created_at > '2022-07-25'", :name=>"tmp_idx_oauth_access_tokens_appid_after_20220705_not_revoked", :algorithm=>:concurrently})
main:    -> 0.0036s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0004s
main: -- add_index(:oauth_access_tokens, :application_id, {:where=>"revoked_at IS NULL AND created_at > '2022-07-25'", :name=>"tmp_idx_oauth_access_tokens_appid_after_20220705_not_revoked", :algorithm=>:concurrently})
main:    -> 0.0037s
main: -- execute("RESET statement_timeout")
main:    -> 0.0004s
main: == 20230525122726 AddTmpIdxOauthAccessTokensAppidAfter20220705NotRevoked: migrated (0.0656s)

main: == [advisory_lock_connection] object_id: 279040, pg_backend_pid: 70605
ci: == [advisory_lock_connection] object_id: 279260, pg_backend_pid: 70607
ci: == 20230525122726 AddTmpIdxOauthAccessTokensAppidAfter20220705NotRevoked: migrating
ci: -- transaction_open?()
ci:    -> 0.0000s
ci: -- view_exists?(:postgres_partitions)
ci:    -> 0.0005s
ci: -- index_exists?(:oauth_access_tokens, :application_id, {:where=>"revoked_at IS NULL AND created_at > '2022-07-25'", :name=>"tmp_idx_oauth_access_tokens_appid_after_20220705_not_revoked", :algorithm=>:concurrently})
ci:    -> 0.0030s
ci: -- execute("SET statement_timeout TO 0")
ci:    -> 0.0003s
ci: -- add_index(:oauth_access_tokens, :application_id, {:where=>"revoked_at IS NULL AND created_at > '2022-07-25'", :name=>"tmp_idx_oauth_access_tokens_appid_after_20220705_not_revoked", :algorithm=>:concurrently})
ci:    -> 0.0034s
ci: -- execute("RESET statement_timeout")
ci:    -> 0.0003s
ci: == 20230525122726 AddTmpIdxOauthAccessTokensAppidAfter20220705NotRevoked: migrated (0.0179s)

ci: == [advisory_lock_connection] object_id: 279260, pg_backend_pid: 70607
DB rollback output
% rails db:rollback:main
main: == [advisory_lock_connection] object_id: 278840, pg_backend_pid: 69749
main: == 20230525122726 AddTmpIdxOauthAccessTokensAppidAfter20220705NotRevoked: reverting
main: -- transaction_open?()
main:    -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main:    -> 0.0729s
main: -- indexes(:oauth_access_tokens)
main:    -> 0.0038s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0002s
main: -- remove_index(:oauth_access_tokens, {:algorithm=>:concurrently, :name=>"tmp_idx_oauth_access_tokens_appid_after_20220705_not_revoked"})
main:    -> 0.0013s
main: -- execute("RESET statement_timeout")
main:    -> 0.0003s
main: == 20230525122726 AddTmpIdxOauthAccessTokensAppidAfter20220705NotRevoked: reverted (0.0892s)

main: == [advisory_lock_connection] object_id: 278840, pg_backend_pid: 69749


% rails db:rollback:ci
ci: == [advisory_lock_connection] object_id: 278780, pg_backend_pid: 70153
ci: == 20230525122726 AddTmpIdxOauthAccessTokensAppidAfter20220705NotRevoked: reverting
ci: -- transaction_open?()
ci:    -> 0.0000s
ci: -- view_exists?(:postgres_partitions)
ci:    -> 0.0491s
ci: -- indexes(:oauth_access_tokens)
ci:    -> 0.0039s
ci: -- execute("SET statement_timeout TO 0")
ci:    -> 0.0003s
ci: -- remove_index(:oauth_access_tokens, {:algorithm=>:concurrently, :name=>"tmp_idx_oauth_access_tokens_appid_after_20220705_not_revoked"})
ci:    -> 0.0025s
ci: -- execute("RESET statement_timeout")
ci:    -> 0.0004s
ci: == 20230525122726 AddTmpIdxOauthAccessTokensAppidAfter20220705NotRevoked: reverted (0.0718s)

ci: == [advisory_lock_connection] object_id: 278780, pg_backend_pid: 70153

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

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

Related to #404467

Edited by Qingyu Zhao

Merge request reports