From e5c7e11840871e3138ad32693d86de8d7cd7c670 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 14 Jul 2016 18:39:08 -0700 Subject: Disable PostgreSQL statement timeout during migrations Long-running migrations may take more than the timeout allowed by the database. Disable the session's statement timeout to ensure migrations don't get killed prematurely. --- CHANGELOG | 1 + lib/gitlab/database/migration_helpers.rb | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 19286abd74f..23f8bfa5d34 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ Please view this file on the master branch, on stable branches it's out of date. v 8.10.0 (unreleased) - Expose {should,force}_remove_source_branch (Ben Boeckel) + - Disable PostgreSQL statement timeout during migrations - Fix projects dropdown loading performance with a simplified api cal. !5113 (tiagonbotelho) - Fix commit builds API, return all builds for all pipelines for given commit. !4849 - Replace Haml with Hamlit to make view rendering faster. !3666 diff --git a/lib/gitlab/database/migration_helpers.rb b/lib/gitlab/database/migration_helpers.rb index dec20d8659b..593ee5d9568 100644 --- a/lib/gitlab/database/migration_helpers.rb +++ b/lib/gitlab/database/migration_helpers.rb @@ -25,6 +25,13 @@ module Gitlab add_index(table_name, column_name, options) end + # Long-running migrations may take more than the timeout allowed by + # the database. Disable the session's statement timeout to ensure + # migrations don't get killed prematurely. (PostgreSQL only) + def disable_statement_timeout + ActiveRecord::Base.connection.execute('SET statement_timeout TO 0') if Database.postgresql? + end + # Updates the value of a column in batches. # # This method updates the table in batches of 5% of the total row count. @@ -134,6 +141,7 @@ module Gitlab end transaction do + disable_statement_timeout add_column(table, column, type, default: nil) # Changing the default before the update ensures any newly inserted -- cgit v1.2.1 From f700f3ec5d524afd1575a27da76b39b08947b7ea Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 14 Jul 2016 18:50:46 -0700 Subject: Disable statement timeout outside of transaction and during adding concurrent index --- lib/gitlab/database/migration_helpers.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/gitlab/database/migration_helpers.rb b/lib/gitlab/database/migration_helpers.rb index 593ee5d9568..927f9dad20b 100644 --- a/lib/gitlab/database/migration_helpers.rb +++ b/lib/gitlab/database/migration_helpers.rb @@ -20,6 +20,7 @@ module Gitlab if Database.postgresql? options = options.merge({ algorithm: :concurrently }) + disable_statement_timeout end add_index(table_name, column_name, options) @@ -140,8 +141,9 @@ module Gitlab 'in the body of your migration class' end + disable_statement_timeout + transaction do - disable_statement_timeout add_column(table, column, type, default: nil) # Changing the default before the update ensures any newly inserted -- cgit v1.2.1 From 3fd304c1a5063ad9fd53b851de79322f49c68997 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Thu, 14 Jul 2016 20:55:33 -0700 Subject: Fix spec Don't attempt to disable statement timeout on a MySQL DB --- spec/lib/gitlab/database/migration_helpers_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/lib/gitlab/database/migration_helpers_spec.rb b/spec/lib/gitlab/database/migration_helpers_spec.rb index 9096ad101b0..4ec3f19e03f 100644 --- a/spec/lib/gitlab/database/migration_helpers_spec.rb +++ b/spec/lib/gitlab/database/migration_helpers_spec.rb @@ -13,6 +13,10 @@ describe Gitlab::Database::MigrationHelpers, lib: true do context 'outside a transaction' do before do expect(model).to receive(:transaction_open?).and_return(false) + + unless Gitlab::Database.postgresql? + allow_any_instance_of(Gitlab::Database::MigrationHelpers).to receive(:disable_statement_timeout) + end end context 'using PostgreSQL' do -- cgit v1.2.1