|
#!/usr/bin/env ruby |
|
# frozen_string_literal: true |
|
|
|
require "bundler/inline" |
|
require "csv" |
|
|
|
gemfile do |
|
source "https://rubygems.org" |
|
gem "pg_query" |
|
gem "pry" |
|
gem "sqlite3" |
|
end |
|
|
|
require "json" |
|
|
|
db = SQLite3::Database.new "pg_stat_statements.sqlite" |
|
|
|
db.execute_batch <<-SQL |
|
drop table if exists pg_stat_statements; |
|
create table pg_stat_statements ( |
|
server string, |
|
queryid integer, |
|
query text, |
|
calls integer, |
|
total_time real, |
|
min_time real, |
|
max_time real, |
|
mean_time real, |
|
stddev_time real, |
|
rows integer, |
|
shared_blks_hit integer, |
|
shared_blks_read integer, |
|
shared_blks_dirtied integer, |
|
shared_blks_written integer, |
|
local_blks_hit integer, |
|
local_blks_read integer, |
|
local_blks_dirtied integer, |
|
local_blks_written integer, |
|
temp_blks_read integer, |
|
temp_blks_written integer, |
|
blk_read_time real, |
|
blk_write_time real |
|
); |
|
|
|
drop table if exists statement_filter_columns; |
|
create table statement_filter_columns ( |
|
server string, |
|
relname text, |
|
colname text, |
|
queryid integer |
|
); |
|
SQL |
|
|
|
Dir.glob("pg_stat_statements/*.csv").each do |csv_path| |
|
server = File.basename(csv_path, ".csv") |
|
CSV.foreach(csv_path, headers: true) do |row| |
|
next if row["query"].nil? |
|
db.execute <<~SQL, [server] + row.to_h.fetch_values("queryid", "query", "calls", "total_time", "min_time", "max_time", "mean_time", "stddev_time", "rows", "shared_blks_hit", "shared_blks_read", "shared_blks_dirtied", "shared_blks_written", "local_blks_hit", "local_blks_read", "local_blks_dirtied", "local_blks_written", "temp_blks_read", "temp_blks_written", "blk_read_time", "blk_write_time") |
|
insert into pg_stat_statements (server, queryid, query, calls, total_time, min_time, max_time, mean_time, stddev_time, rows, shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written, blk_read_time, blk_write_time) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
|
SQL |
|
parsed_query = PgQuery.parse(row["query"]) |
|
parsed_query.filter_columns.each do |pair| |
|
relname, colname = pair |
|
db.execute "insert into statement_filter_columns (server, relname, colname, queryid) values (?, ?, ?, ?)", server, relname, colname, row["queryid"] |
|
end |
|
rescue => e |
|
puts "=> error" |
|
p e |
|
p row |
|
end |
|
end |
|
|
|
db.execute "create index idx_pg_stat_statements on pg_stat_statements (queryid);" |
|
db.execute "create index idx_statement_filter_columns on statement_filter_columns (queryid, relname, colname);" |