Background migrations and batched migrations are not the same, so you should check that both are
complete before updating.
Decrease the time required to complete these migrations by increasing the number of
Sidekiq workers
that can process jobs in the
background_migration
queue.
To update database tables in batches, GitLab can use batched background migrations. These migrations
are created by GitLab developers and run automatically on upgrade. However, such migrations are
limited in scope to help with migrating some
integer
database columns to
bigint
. This is needed to
prevent integer overflow for some tables.
Batched background migrations are handled by Sidekiq and
run in isolation
, so an instance can remain operational while the migrations are processed. However, there may be performance degradation on larger instances that are heavily used while batched background migrations are run, so it’s a good idea to
actively monitor the Sidekiq status
until all migrations are completed.
Run the query multiple times within a few minutes to ensure no new row has been added.
If no new row has been added, the migration has been paused.
After confirming the migration has paused, restart the migration (using the enable
command above) to proceed with the batch when ready. On larger instances,
background migrations can take as long as 48 hours to complete each batch.
To maximize throughput of batched background migrations (in terms of the number of tuples updated per time unit), batch sizes are automatically adjusted based on how long the previous batches took to complete.
Enable or disable automatic batch size optimization
These flags are enabled by default. Disable them only as a last resort
to limit database operations in special circumstances, like database host maintenance.
Do not disable either of these flags unless you fully understand the ramifications. If you disable
the
execute_background_migrations
or
execute_batched_migrations_on_schedule
feature flag,
GitLab upgrades might fail and data loss might occur.
Database migrations failing because of batched background migration not finished
Update to either 14.0.5 or 14.1
before
updating to 14.2+
Check the status
of the batched background migrations and
make sure they are all marked as finished before attempting to upgrade again. If any remain marked as active,
you can
manually finish them
.
Roll forward and finish the migrations on the upgraded version
Check the status
of the batched background migrations in the
database, and
manually run them
with the appropriate
arguments until the status query returns no rows.
When the status of all of all them is marked as complete, re-run migrations for your installation.
Check the status
of the batched background migration from
the error message, and make sure it is listed as finished. If it is still active, either wait until it is done,
or
manually finish it
.
Re-run migrations for your installation, so the remaining post-deployment migrations finish.
Replace the values in angle brackets with the correct
arguments. For example, if you receive an error similar to this:
StandardError: An error has occurred, all later migrations canceled:
Expected batched background migration for the given configuration to be marked as 'finished', but it is 'active':
{:job_class_name=>"CopyColumnUsingBackgroundMigrationJob", :table_name=>"push_event_payloads", :column_name=>"event_id", :job_arguments=>[["event_id"], ["event_id_convert_to_bigint"]]}
Plug the arguments from the error message into the command:
If you need to manually run a batched background migration to continue an upgrade, you can
check the status
in the database and get the
arguments from the query results. For example, if the query returns this:
When the background migration is determined to be “safe” to skip, the migration can be manually marked finished:
Make sure you create a backup before proceeding.
# Start the rails consoleconnection=ApplicationRecord.connection# or Ci::ApplicationRecord.connection, depending on which DB was the migration scheduledGitlab::Database::SharedModel.using_connection(connection)domigration=Gitlab::Database::BackgroundMigration::BatchedMigration.find_for_configuration(Gitlab::Database.gitlab_schemas_for_connection(connection),'BackfillUserDetailsFields',:users,:id,# mark all jobs completedmigration.batched_jobs.update_all(status: Gitlab::Database::BackgroundMigration::BatchedJob.state_machine.states['succeeded'].value)migration.update_attribute(:status,Gitlab::Database::BackgroundMigration::BatchedMigration.state_machine.states[:finished].value)
The
BackfillNamespaceIdForNamespaceRoute
batched migration job fails
GitLab 14.2 introduced an issue where a background migration named
BackfillDraftStatusOnMergeRequests
can be permanently stuck in a
pending
state across upgrades when the instance lacks records that match
the migration’s target. To clean up this stuck migration, see the
14.2.0 version-specific instructions
.
GitLab 14.4 introduced an issue where a background migration named
PopulateTopicsTotalProjectsCountCache
can be permanently stuck in a
pending
state across upgrades when the instance lacks records that match
the migration’s target. To clean up this stuck migration, see the
14.4.0 version-specific instructions
.
GitLab 14.5 introduced an issue where a background migration named
UpdateVulnerabilityOccurrencesLocation
can be permanently stuck in a
pending
state across upgrades when the instance lacks records that match
the migration’s target. To clean up this stuck migration, see the
14.5.0 version-specific instructions
.
GitLab 14.8 introduced an issue where a background migration named
PopulateTopicsNonPrivateProjectsCount
can be permanently stuck in a
pending
state across upgrades. To clean up this stuck migration, see the
14.8.0 version-specific instructions
.
GitLab 14.9 introduced an issue where a background migration named
ResetDuplicateCiRunnersTokenValuesOnProjects
can be permanently stuck in a
pending
state across upgrades when the instance lacks records that match
the migration’s target. To clean up this stuck migration, see the
14.9.0 version-specific instructions
.
For other background migrations stuck in pending, run the following check. If
it returns non-zero and the count does not decrease over time, follow the rest
of the steps in this section.
# For Omnibus installations:sudo gitlab-rails runner -e production 'puts Gitlab::Database::BackgroundMigrationJob.pending.count'# For installations from source:cd /home/git/gitlab
sudo-u git -H bundle exec rails runner -e production 'puts Gitlab::Database::BackgroundMigrationJob.pending.count'
It is safe to re-attempt these migrations to clear them out from a pending status:
For Omnibus installations
# Start the rails consolesudo gitlab-rails c
# Execute the following in the rails console
Gitlab::Database::BackgroundMigrationJob.pending.find_each do |job|
puts "Running pending job '#{job.class_name}' with arguments #{job.arguments}"
result = Gitlab::BackgroundMigration.perform(job.class_name, job.arguments)
puts "Result: #{result}"
For installations from source
# Start the rails consolesudo-u git -H bundle exec rails RAILS_ENV=production
# Execute the following in the rails console
Gitlab::Database::BackgroundMigrationJob.pending.find_each do |job|
puts "Running pending job '#{job.class_name}' with arguments #{job.arguments}"
result = Gitlab::BackgroundMigration.perform(job.class_name, job.arguments)
puts "Result: #{result}"
Docs
Edit this page
to fix an error or add an improvement in a merge request. Create an issue
to suggest an improvement to this page.
View pricing
to see all GitLab tiers and features, or to upgrade. Try GitLab for free
with access to all features for 30 days.
Get Help
If you didn't find what you were looking for,
search the docs.
If you want help with something specific and could use community support,
post on the GitLab forum.
For problems setting up or using this feature (depending on your GitLab
subscription).