lib/pghero/methods/query_stats.rb in pghero-2.4.2 vs lib/pghero/methods/query_stats.rb in pghero-2.5.0
- old
+ new
@@ -164,11 +164,11 @@
# http://www.craigkerstiens.com/2013/01/10/more-on-postgres-performance/
def current_query_stats(limit: nil, sort: nil, database: nil, query_hash: nil)
if query_stats_enabled?
limit ||= 100
sort ||= "total_minutes"
- select_all <<-SQL
+ query = <<-SQL
WITH query_stats AS (
SELECT
LEFT(query, 10000) AS query,
#{supports_query_hash? ? "queryid" : "md5(query)"} AS query_hash,
rolname AS user,
@@ -198,19 +198,24 @@
query_stats
ORDER BY
#{quote_table_name(sort)} DESC
LIMIT #{limit.to_i}
SQL
+
+ # we may be able to skip query_columns
+ # in more recent versions of Postgres
+ # as pg_stat_statements should be already normalized
+ select_all(query, query_columns: [:query])
else
raise NotEnabled, "Query stats not enabled"
end
end
def historical_query_stats(sort: nil, start_at: nil, end_at: nil, query_hash: nil)
if historical_query_stats_enabled?
sort ||= "total_minutes"
- select_all_stats <<-SQL
+ query = <<-SQL
WITH query_stats AS (
SELECT
#{supports_query_hash? ? "query_hash" : "md5(query)"} AS query_hash,
pghero_query_stats.user AS user,
array_agg(LEFT(query, 10000) ORDER BY REPLACE(LEFT(query, 1000), '?', '!') COLLATE "C" ASC) AS query,
@@ -242,9 +247,13 @@
query_stats
ORDER BY
#{quote_table_name(sort)} DESC
LIMIT 100
SQL
+
+ # we can skip query_columns if all stored data is normalized
+ # for now, assume it's not
+ select_all_stats(query, query_columns: [:query, :explainable_query])
else
raise NotEnabled, "Historical query stats not enabled"
end
end