Class: RailsRedshiftReplicator::Tools::Vacuum

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_redshift_replicator/tools/vacuum.rb

Instance Method Summary collapse

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]

Parameters:

  • table (String, nil) (defaults to: nil)

    table to perform the command or :all

  • options (String, nil) (defaults to: nil)

    command options: FULL, SORT ONLY, DELETE ONLY, REINDEX, auto_tune



9
10
11
12
# File 'lib/rails_redshift_replicator/tools/vacuum.rb', line 9

def initialize(table = nil, options = nil)
  @table = table
  @options = options
end

Instance Method Details

#auto_tuned_vacuum(table) ⇒ Object

See Also:

  • vacuum


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

See Also:

  • vacuum


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

#performObject



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.

Parameters:

  • table_name (String)

    table name

Returns:

  • ("compound", "interleaved")

    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_typesObject

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_sqlString

Lists tables to replicate

Returns:

  • (String)

    table names



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