Class: RailsRedshiftReplicator::Tools::Vacuum
- Inherits:
-
Object
- Object
- RailsRedshiftReplicator::Tools::Vacuum
- Defined in:
- lib/rails_redshift_replicator/tools/vacuum.rb
Instance Method Summary collapse
- #auto_tuned_vacuum(table) ⇒ Object
- #exec_vacuum_command(table, sort_type) ⇒ Object
-
#initialize(table = nil, options = nil) ⇒ Vacuum
constructor
Executes Redshift’s VACUUM command to reclaim space see [docs.aws.amazon.com/redshift/latest/dg/r_VACUUM_command.html].
- #perform ⇒ Object
-
#sort_type_for_table(table_name) ⇒ "compound", "interleaved"
Sort key style.
-
#sort_types ⇒ Object
Finds which sort keys are present on a set of tables.
-
#tables_for_sql ⇒ String
Lists tables to replicate.
Constructor Details
#initialize(table = nil, options = nil) ⇒ Vacuum
Note:
auto_tune chooses the best VACUUM strategy based on each table sort key style.
Executes Redshift’s VACUUM command to reclaim space see [docs.aws.amazon.com/redshift/latest/dg/r_VACUUM_command.html]
9 10 11 12 |
# File 'lib/rails_redshift_replicator/tools/vacuum.rb', line 9 def initialize(table = nil, = nil) @table = table @options = end |
Instance Method Details
#auto_tuned_vacuum(table) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/rails_redshift_replicator/tools/vacuum.rb', line 27 def auto_tuned_vacuum(table) if table.to_s == "all" sort_types.each do |line| exec_vacuum_command line['tablename'], line['sort_type'] end else exec_vacuum_command table, sort_type_for_table(table) end end |
#exec_vacuum_command(table, sort_type) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/rails_redshift_replicator/tools/vacuum.rb', line 38 def exec_vacuum_command(table, sort_type) command = if sort_type.in? ["compound", nil] "VACUUM FULL #{table};" else "VACUUM REINDEX #{table};" end.squish RailsRedshiftReplicator.logger.debug(command) RailsRedshiftReplicator.connection.exec command end |
#perform ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rails_redshift_replicator/tools/vacuum.rb', line 14 def perform if @options == "auto_tune" auto_tuned_vacuum(@table) else @table = (@table.blank? || @table.to_s == "all") ? nil : @table command = "VACUUM #{@options} #{@table};".squish RailsRedshiftReplicator.logger.debug(command) RailsRedshiftReplicator.connection.exec command end end |
#sort_type_for_table(table_name) ⇒ "compound", "interleaved"
Returns sort key style.
67 68 69 |
# File 'lib/rails_redshift_replicator/tools/vacuum.rb', line 67 def sort_type_for_table(table_name) sort_types.select{ |el| el["tablename"] == table_name }.first.try(:fetch, "sort_type") || "compound" end |
#sort_types ⇒ Object
Finds which sort keys are present on a set of tables
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rails_redshift_replicator/tools/vacuum.rb', line 49 def sort_types @sort_types ||= begin sql = <<-SQLT SELECT CASE WHEN min(sortkey) < 0 THEN 'interleaved' ELSE 'compound' END AS sort_type, tablename FROM pg_table_def WHERE tablename in (#{tables_for_sql}) GROUP BY tablename SQLT RailsRedshiftReplicator.connection.exec(sql.squish).entries end end |
#tables_for_sql ⇒ String
Lists tables to replicate
72 73 74 |
# File 'lib/rails_redshift_replicator/tools/vacuum.rb', line 72 def tables_for_sql @tables_for_sql ||= RailsRedshiftReplicator.replicable_target_tables.join(",") end |