Class: PartitionGardener::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/partition_gardener/executor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection: Connection.connection, batch_size: MOVE_BATCH_SIZE) ⇒ Executor

Returns a new instance of Executor.



12
13
14
15
# File 'lib/partition_gardener/executor.rb', line 12

def initialize(connection: Connection.connection, batch_size: MOVE_BATCH_SIZE)
  @connection = connection
  @batch_size = batch_size
end

Class Method Details

.for_config(config, connection: Connection.connection) ⇒ Object



5
6
7
8
9
10
# File 'lib/partition_gardener/executor.rb', line 5

def self.for_config(config, connection: Connection.connection)
  new(
    connection: connection,
    batch_size: config.fetch(:move_batch_size, MOVE_BATCH_SIZE)
  )
end

Instance Method Details

#attach_default_partition(table_name, partition_name) ⇒ Object



25
26
27
28
29
30
# File 'lib/partition_gardener/executor.rb', line 25

def attach_default_partition(table_name, partition_name)
  sql = <<~SQL
    ALTER TABLE #{quoted_table(table_name)} ATTACH PARTITION #{quoted_table(partition_name)} DEFAULT
  SQL
  @connection.execute(sql)
end

#attach_partition(table_name, partition_name, for_values_clause) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/partition_gardener/executor.rb', line 17

def attach_partition(table_name, partition_name, for_values_clause)
  sql = <<~SQL
    ALTER TABLE #{quoted_table(table_name)} ATTACH PARTITION #{quoted_table(partition_name)}
    FOR VALUES #{for_values_clause}
  SQL
  @connection.execute(sql)
end

#create_partition(table_name, partition_name, for_values_clause) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/partition_gardener/executor.rb', line 47

def create_partition(table_name, partition_name, for_values_clause)
  sql = <<~SQL
    CREATE TABLE IF NOT EXISTS #{quoted_table(partition_name)} PARTITION OF #{quoted_table(table_name)}
    FOR VALUES #{for_values_clause}
  SQL
  @connection.execute(sql)
end

#detach_partition(table_name, partition_name, concurrently: false) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/partition_gardener/executor.rb', line 32

def detach_partition(table_name, partition_name, concurrently: false)
  concurrently_keyword = concurrently ? "CONCURRENTLY " : ""
  sql = <<~SQL
    ALTER TABLE #{quoted_table(table_name)} DETACH PARTITION #{concurrently_keyword}#{quoted_table(partition_name)}
  SQL
  @connection.execute(sql)
end

#drain_rows_between_partitions!(source_partition_name, destination_partition_name, where_condition, conflict_key, cursor_columns: conflict_key) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/partition_gardener/executor.rb', line 81

def drain_rows_between_partitions!(source_partition_name, destination_partition_name, where_condition, conflict_key, cursor_columns: conflict_key)
  move_rows_between_partitions!(
    source_partition_name: source_partition_name,
    destination_partition_name: destination_partition_name,
    where_condition: where_condition,
    conflict_key: conflict_key,
    cursor_columns: cursor_columns
  )
end

#drop_table(table_name) ⇒ Object



40
41
42
43
44
45
# File 'lib/partition_gardener/executor.rb', line 40

def drop_table(table_name)
  sql = <<~SQL
    DROP TABLE IF EXISTS #{quoted_table(table_name)}
  SQL
  @connection.execute(sql)
end

#ensure_detached_partition_table!(table_name, partition_name, conflict_key:) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/partition_gardener/executor.rb', line 55

def ensure_detached_partition_table!(table_name, partition_name, conflict_key:)
  sql = <<~SQL
    CREATE TABLE IF NOT EXISTS #{quoted_table(partition_name)} (
      LIKE #{quoted_table(table_name)} INCLUDING ALL
    )
  SQL
  @connection.execute(sql)

  conflict_columns = conflict_key.map { |column| @connection.quote_column_name(column) }.join(", ")
  sql = <<~SQL
    CREATE UNIQUE INDEX IF NOT EXISTS #{@connection.quote_column_name("#{partition_name}_conflict_key_idx")}
    ON #{quoted_table(partition_name)} (#{conflict_columns})
  SQL
  @connection.execute(sql)
end

#move_all_rows_between_partitions!(source_partition_name, destination_partition_name, conflict_key, cursor_columns: conflict_key) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/partition_gardener/executor.rb', line 71

def move_all_rows_between_partitions!(source_partition_name, destination_partition_name, conflict_key, cursor_columns: conflict_key)
  move_rows_between_partitions!(
    source_partition_name: source_partition_name,
    destination_partition_name: destination_partition_name,
    where_condition: nil,
    conflict_key: conflict_key,
    cursor_columns: cursor_columns
  )
end

#move_all_rows_to_parent!(table_name, source_partition_name, conflict_key, cursor_columns: conflict_key) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/partition_gardener/executor.rb', line 91

def move_all_rows_to_parent!(table_name, source_partition_name, conflict_key, cursor_columns: conflict_key)
  ensure_parent_conflict_index!(table_name, conflict_key)
  move_rows_with_keyset!(
    source_partition_name: source_partition_name,
    insert_target: quoted_table(table_name),
    where_condition: nil,
    conflict_key: conflict_key,
    cursor_columns: cursor_columns
  )
end

#move_rows_to_parent_partition!(table_name:, source_partition_name:, where_condition:, destination_partition_name:, conflict_key:, record_count:, cursor_columns: conflict_key) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/partition_gardener/executor.rb', line 102

def move_rows_to_parent_partition!(
  table_name:,
  source_partition_name:,
  where_condition:,
  destination_partition_name:,
  conflict_key:,
  record_count:,
  cursor_columns: conflict_key
)
  ensure_parent_conflict_index!(table_name, conflict_key)

  PartitionGardener.configuration.notify(
    "[PartitionGardener] Moving #{record_count} rows from #{source_partition_name} to #{destination_partition_name}",
    context: {
      table_name: table_name,
      source_partition_name: source_partition_name,
      destination_partition_name: destination_partition_name,
      record_count: record_count
    }
  )

  moved_rows = move_rows_with_keyset!(
    source_partition_name: source_partition_name,
    insert_target: quoted_table(table_name),
    where_condition: where_condition,
    conflict_key: conflict_key,
    cursor_columns: cursor_columns
  )

  PartitionGardener.configuration.notify(
    "[PartitionGardener] Moved #{moved_rows} rows from #{source_partition_name} to #{destination_partition_name}",
    context: {
      table_name: table_name,
      source_partition_name: source_partition_name,
      destination_partition_name: destination_partition_name,
      moved_rows: moved_rows
    }
  )
end