Class: Gitlab::Database::Partitioning::PartitionManager

Inherits:
Object
  • Object
show all
Includes:
MigrationHelpers::LooseForeignKeyHelpers, Utils::StrongMemoize
Defined in:
lib/gitlab/database/partitioning/partition_manager.rb

Constant Summary collapse

UnsafeToDetachPartitionError =
Class.new(StandardError)
LEASE_TIMEOUT =
1.hour
STATEMENT_TIMEOUT =
1.hour
MANAGEMENT_LEASE_KEY =
'database_partition_management_%s'
RETAIN_DETACHED_PARTITIONS_FOR =
1.week
MAX_PARTITION_SIZE =
150.gigabytes

Constants included from MigrationHelpers::LooseForeignKeyHelpers

MigrationHelpers::LooseForeignKeyHelpers::INSERT_FUNCTION_NAME, MigrationHelpers::LooseForeignKeyHelpers::INSERT_FUNCTION_NAME_OVERRIDE_TABLE

Instance Method Summary collapse

Methods included from MigrationHelpers::LooseForeignKeyHelpers

#has_loose_foreign_key?, #track_record_deletions, #track_record_deletions_override_table_name, #untrack_record_deletions

Methods included from SchemaHelpers

#assert_not_in_transaction_block, #create_comment, #create_trigger, #create_trigger_function, #drop_function, #drop_trigger, #find_all_id_columns_sql, #function_exists?, #object_name, #reset_all_trigger_functions, #reset_trigger_function, #tmp_table_name, #trigger_exists?

Constructor Details

#initialize(model, connection: nil) ⇒ PartitionManager

Returns a new instance of PartitionManager.



18
19
20
21
22
# File 'lib/gitlab/database/partitioning/partition_manager.rb', line 18

def initialize(model, connection: nil)
  @model = model
  @connection = connection || model.connection
  @connection_name = @connection.pool.db_config.name
end

Instance Method Details

#execute(sql) ⇒ Object



24
25
26
# File 'lib/gitlab/database/partitioning/partition_manager.rb', line 24

def execute(sql)
  @connection.execute(sql)
end

#sync_partitions(analyze: true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gitlab/database/partitioning/partition_manager.rb', line 28

def sync_partitions(analyze: true)
  return skip_syncing_partitions unless table_partitioned?

  Gitlab::AppLogger.info(
    message: "Checking state of dynamic postgres partitions",
    table_name: model.table_name,
    connection_name: @connection_name
  )

  only_with_exclusive_lease(model, lease_key: MANAGEMENT_LEASE_KEY) do
    model.partitioning_strategy.validate_and_fix

    partitions_to_create = missing_partitions
    partitions_to_detach = extra_partitions

    create(partitions_to_create) unless partitions_to_create.empty?
    detach(partitions_to_detach) unless partitions_to_detach.empty?

    run_analyze_on_partitioned_table if analyze
  end
rescue ArgumentError => e
  Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
rescue StandardError => e
  Gitlab::AppLogger.error(
    message: "Failed to create / detach partition(s)",
    table_name: model.table_name,
    exception_class: e.class,
    exception_message: e.message,
    connection_name: @connection_name
  )
end