lib/pgslice.rb in pgslice-0.3.3 vs lib/pgslice.rb in pgslice-0.3.4
- old
+ new
@@ -35,12 +35,14 @@
swap
when "unswap"
unswap
when "unprep"
unprep
+ when "analyze"
+ analyze
when nil
- log "Commands: add_partitions, fill, prep, swap, unprep, unswap"
+ log "Commands: add_partitions, analyze, fill, prep, swap, unprep, unswap"
else
abort "Unknown command: #{@command}"
end
ensure
@connection.close if @connection
@@ -240,10 +242,11 @@
ending_time = advance_date(DateTime.strptime(existing_tables.last.split("_").last, name_format), period, 1)
end
end
primary_key = self.primary_key(table)
+ abort "No primary key" unless primary_key
max_source_id = max_id(source_table, primary_key)
max_dest_id =
if options[:start]
options[:start]
@@ -262,10 +265,15 @@
fields = columns(source_table).map { |c| PG::Connection.quote_ident(c) }.join(", ")
batch_size = options[:batch_size]
i = 1
batch_count = ((max_source_id - starting_id) / batch_size.to_f).ceil
+
+ if batch_count == 0
+ log_sql "/* nothing to fill */"
+ end
+
while starting_id < max_source_id
where = "#{primary_key} > #{starting_id} AND #{primary_key} <= #{starting_id + batch_size}"
if starting_time
where << " AND #{field} >= #{sql_date(starting_time, cast)} AND #{field} < #{sql_date(ending_time, cast)}"
end
@@ -278,13 +286,11 @@
INSERT INTO #{dest_table} (#{fields})
SELECT #{fields} FROM #{source_table}
WHERE #{where}
SQL
- log_sql(query)
- log_sql
- execute(query)
+ run_query(query)
starting_id += batch_size
i += 1
if options[:sleep] && starting_id <= max_source_id
@@ -337,10 +343,21 @@
end
run_queries(queries)
end
+ def analyze
+ table = arguments.first
+ parent_table = options[:swapped] ? table : intermediate_name(table)
+
+ abort "Usage: pgslice analyze <table>" if arguments.length != 1
+
+ existing_tables = self.existing_tables(like: "#{table}_%").select { |t| /\A#{Regexp.escape("#{table}_")}\d{6,8}\z/.match(t) }
+ analyze_list = existing_tables + [parent_table]
+ run_queries_without_transaction analyze_list.map { |t| "ANALYZE VERBOSE #{t};" }
+ end
+
# arguments
def parse_args(args)
opts = Slop.parse(args) do |o|
o.boolean "--intermediate"
@@ -418,21 +435,29 @@
def run_queries(queries)
connection.transaction do
execute("SET LOCAL client_min_messages TO warning") unless options[:dry_run]
log_sql "BEGIN;"
log_sql
- queries.each do |query|
- log_sql query
- log_sql
- unless options[:dry_run]
- begin
- execute(query)
- rescue PG::ServerError => e
- abort("#{e.class.name}: #{e.message}")
- end
- end
- end
+ run_queries_without_transaction(queries)
log_sql "COMMIT;"
+ end
+ end
+
+ def run_query(query)
+ log_sql query
+ unless options[:dry_run]
+ begin
+ execute(query)
+ rescue PG::ServerError => e
+ abort("#{e.class.name}: #{e.message}")
+ end
+ end
+ log_sql
+ end
+
+ def run_queries_without_transaction(queries)
+ queries.each do |query|
+ run_query(query)
end
end
def server_version_num
execute("SHOW server_version_num")[0]["server_version_num"].to_i