From 5b7c9255732cc99ec5bed1aecfedd67079014081 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 8 Aug 2023 09:09:58 +0200 Subject: [PATCH] Fix adding column with default value taking longer on Postgres >= 11 (#26375) --- lib/mastodon/migration_helpers.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/mastodon/migration_helpers.rb b/lib/mastodon/migration_helpers.rb index 5a252b3512..4a43f67c27 100644 --- a/lib/mastodon/migration_helpers.rb +++ b/lib/mastodon/migration_helpers.rb @@ -195,7 +195,14 @@ module Mastodon def supports_drop_index_concurrently? version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i - version >= 90200 + version >= 90_200 + end + + # Only available on Postgresql >= 11 + def supports_add_column_with_default? + version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i + + version >= 11_000 end # Adds a foreign key with only minimal locking on the tables involved. @@ -414,6 +421,11 @@ module Mastodon # This method can also take a block which is passed directly to the # `update_column_in_batches` method. def add_column_with_default(table, column, type, default:, limit: nil, allow_null: false, &block) + if supports_add_column_with_default? + add_column(table, column, type, default: default, limit: limit, null: allow_null) + return + end + if transaction_open? raise 'add_column_with_default can not be run inside a transaction, ' \ 'you can disable transactions by calling disable_ddl_transaction! ' \