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, align_child_columns: PartitionGardener.configuration.align_child_columns) ⇒ Executor

Returns a new instance of Executor.



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

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

Class Method Details

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



5
6
7
8
9
10
11
12
13
14
# 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),
    align_child_columns: config.fetch(
      :align_child_columns,
      PartitionGardener.configuration.align_child_columns
    )
  )
end

Instance Method Details

#attach_default_partition(table_name, partition_name) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/partition_gardener/executor.rb', line 35

def attach_default_partition(table_name, partition_name)
  align_child_columns!(table_name, partition_name)
  sql = "    ALTER TABLE \#{quoted_table(table_name)} ATTACH PARTITION \#{quoted_table(partition_name)} DEFAULT\n  SQL\n  @connection.execute(sql)\nend\n"

#attach_partition(table_name, partition_name, for_values_clause) ⇒ Object



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

def attach_partition(table_name, partition_name, for_values_clause)
  align_child_columns!(table_name, partition_name)
  sql = "    ALTER TABLE \#{quoted_table(table_name)} ATTACH PARTITION \#{quoted_table(partition_name)}\n    FOR VALUES \#{for_values_clause}\n  SQL\n  @connection.execute(sql)\nend\n"

#create_partition(table_name, partition_name, for_values_clause) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/partition_gardener/executor.rb', line 58

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

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



43
44
45
46
47
48
49
# File 'lib/partition_gardener/executor.rb', line 43

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

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



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

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



51
52
53
54
55
56
# File 'lib/partition_gardener/executor.rb', line 51

def drop_table(table_name)
  sql = "    DROP TABLE IF EXISTS \#{quoted_table(table_name)}\n  SQL\n  @connection.execute(sql)\nend\n"

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/partition_gardener/executor.rb', line 66

def ensure_detached_partition_table!(table_name, partition_name, conflict_key:)
  sql = "    CREATE TABLE IF NOT EXISTS \#{quoted_table(partition_name)} (\n      LIKE \#{quoted_table(table_name)} INCLUDING ALL\n    )\n  SQL\n  @connection.execute(sql)\n  align_child_columns!(table_name, partition_name)\n\n  conflict_columns = conflict_key.map { |column| @connection.quote_column_name(column) }.join(\", \")\n  sql = <<~SQL\n    CREATE UNIQUE INDEX IF NOT EXISTS \#{@connection.quote_column_name(\"\#{partition_name}_conflict_key_idx\")}\n    ON \#{quoted_table(partition_name)} (\#{conflict_columns})\n  SQL\n  @connection.execute(sql)\nend\n"

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



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

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



103
104
105
106
107
108
109
110
111
112
# File 'lib/partition_gardener/executor.rb', line 103

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



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
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/partition_gardener/executor.rb', line 114

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