debian-mirror-gitlab/lib/gitlab/database/partitioning/detached_partition_dropper.rb

59 lines
2.2 KiB
Ruby
Raw Normal View History

2021-10-27 15:23:28 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module Partitioning
class DetachedPartitionDropper
def perform
return unless Feature.enabled?(:drop_detached_partitions, default_enabled: :yaml)
Gitlab::AppLogger.info(message: "Checking for previously detached partitions to drop")
2021-11-18 22:05:49 +05:30
2021-10-27 15:23:28 +05:30
Postgresql::DetachedPartition.ready_to_drop.find_each do |detached_partition|
2021-11-18 22:05:49 +05:30
connection.transaction do
2021-10-27 15:23:28 +05:30
# Another process may have already dropped the table and deleted this entry
next unless (detached_partition = Postgresql::DetachedPartition.lock.find_by(id: detached_partition.id))
2021-11-18 22:05:49 +05:30
drop_detached_partition(detached_partition.table_name)
2021-10-27 15:23:28 +05:30
2021-11-18 22:05:49 +05:30
detached_partition.destroy!
2021-10-27 15:23:28 +05:30
end
rescue StandardError => e
Gitlab::AppLogger.error(message: "Failed to drop previously detached partition",
partition_name: detached_partition.table_name,
exception_class: e.class,
exception_message: e.message)
end
end
private
2021-11-18 22:05:49 +05:30
def drop_detached_partition(partition_name)
partition_identifier = qualify_partition_name(partition_name)
if partition_detached?(partition_identifier)
connection.drop_table(partition_identifier, if_exists: true)
2021-10-27 15:23:28 +05:30
2021-11-18 22:05:49 +05:30
Gitlab::AppLogger.info(message: "Dropped previously detached partition", partition_name: partition_name)
else
Gitlab::AppLogger.error(message: "Attempt to drop attached database partition", partition_name: partition_name)
2021-10-27 15:23:28 +05:30
end
end
2021-11-18 22:05:49 +05:30
def qualify_partition_name(table_name)
"#{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}.#{table_name}"
end
def partition_detached?(partition_identifier)
2021-10-27 15:23:28 +05:30
# PostgresPartition checks the pg_inherits view, so our partition will only show here if it's still attached
# and thus should not be dropped
2021-11-18 22:05:49 +05:30
!Gitlab::Database::PostgresPartition.for_identifier(partition_identifier).exists?
2021-10-27 15:23:28 +05:30
end
2021-11-18 22:05:49 +05:30
def connection
Postgresql::DetachedPartition.connection
2021-10-27 15:23:28 +05:30
end
end
end
end
end