diff --git a/db/post_migrate/20170206040400_remove_inactive_default_email_services.rb b/db/post_migrate/20170206040400_remove_inactive_default_email_services.rb
index d69f68a13d2f7b2cde797c7b83a24753daf94b6d..52e7f91bb0130559b5e9ffb5007192a0b6846564 100644
--- a/db/post_migrate/20170206040400_remove_inactive_default_email_services.rb
+++ b/db/post_migrate/20170206040400_remove_inactive_default_email_services.rb
@@ -3,38 +3,27 @@ class RemoveInactiveDefaultEmailServices < ActiveRecord::Migration
 
   DOWNTIME = false
 
-  def up
-    builds_service = spawn <<-SQL.strip_heredoc
-      DELETE FROM services
-        WHERE type = 'BuildsEmailService'
-          AND active IS FALSE
-          AND properties = '{"notify_only_broken_builds":true}';
-    SQL
-
-    pipelines_service = spawn <<-SQL.strip_heredoc
-      DELETE FROM services
-        WHERE type = 'PipelinesEmailService'
-          AND active IS FALSE
-          AND properties = '{"notify_only_broken_pipelines":true}';
-    SQL
+  disable_ddl_transaction!
 
-    [builds_service, pipelines_service].each(&:join)
-  end
-
-  private
+  def up
+    Gitlab::Database::ThreadedConnectionPool.with_pool(2) do |pool|
+      pool.execute_async <<-SQL.strip_heredoc
+        DELETE FROM services
+          WHERE type = 'BuildsEmailService'
+            AND active IS FALSE
+            AND properties = '{"notify_only_broken_builds":true}';
+      SQL
 
-  def spawn(query)
-    Thread.new do
-      with_connection do |connection|
-        connection.execute(query)
-      end
+      pool.execute_async <<-SQL.strip_heredoc
+        DELETE FROM services
+          WHERE type = 'PipelinesEmailService'
+            AND active IS FALSE
+            AND properties = '{"notify_only_broken_pipelines":true}';
+      SQL
     end
   end
 
-  def with_connection(&block)
-    pool = ActiveRecord::Base.establish_connection
-    pool.with_connection(&block)
-  ensure
-    pool.disconnect!
+  def down
+    # Nothing can be done to restore the records
   end
 end
diff --git a/lib/gitlab/database/threaded_connection_pool.rb b/lib/gitlab/database/threaded_connection_pool.rb
new file mode 100644
index 0000000000000000000000000000000000000000..1316b005741a9cdde6ccaee2f366840ead4b67a7
--- /dev/null
+++ b/lib/gitlab/database/threaded_connection_pool.rb
@@ -0,0 +1,48 @@
+module Gitlab
+  module Database
+    class ThreadedConnectionPool
+      def self.with_pool(pool_size)
+        pool = new(pool_size)
+
+        yield(pool)
+
+      ensure
+        pool.join
+        pool.close
+      end
+
+      def initialize(pool_size)
+        config = ActiveRecord::Base.configurations[Rails.env]
+        @ar_pool = ActiveRecord::Base.establish_connection(
+          config.merge(pool: pool_size))
+        @workers = []
+        @mutex = Mutex.new
+      end
+
+      def execute_async(sql)
+        @mutex.synchronize do
+          @workers << Thread.new do
+            @ar_pool.with_connection do |connection|
+              connection.execute(sql)
+            end
+          end
+        end
+      end
+
+      def join
+        threads = nil
+
+        @mutex.synchronize do
+          threads = @workers.dup
+          @workers.clear
+        end
+
+        threads.each(&:join)
+      end
+
+      def close
+        @ar_pool.disconnect!
+      end
+    end
+  end
+end