ankane/pghero · error · PgHero::NotEnabled
Query hash stats not enabled
Error message
Query hash stats not enabled
What it means
query_hash_stats(query_hash) charts a single query's historical performance from the pghero_query_stats/pghero_queries tables that pghero's stats capture maintains. Unlike current stats (pg_stat_statements), this requires the historical pipeline to be configured; when historical_query_stats_enabled? is false it raises NotEnabled "Query hash stats not enabled".
Source
Thrown at lib/pghero/methods/query_stats.rb:158
db_query_stats = query_stats(limit: 100)
if db_query_stats.any? && reset_query_stats(raise_errors: raise_errors)
insert_query_stats(db_query_stats, captured_at)
end
end
def clean_query_stats(before: nil)
before ||= 14.days.ago
PgHero::QueryStats.where(database: id).where("captured_at < ?", before).delete_all
end
def slow_queries(query_stats: nil, **options)
query_stats ||= self.query_stats(**options)
query_stats.select { |q| q[:calls].to_i >= slow_query_calls.to_i && q[:average_time].to_f >= slow_query_ms.to_f }
end
def query_hash_stats(query_hash, user: nil, current: true)
if !historical_query_stats_enabled?
raise NotEnabled, "Query hash stats not enabled"
end
start_at = 24.hours.ago
# specify pghero_queries.query in case pghero_query_stats.query exists
sql = <<~SQL
SELECT
captured_at,
total_time,
calls,
(SELECT regexp_matches(pghero_queries.query, '.*/\\*(.+?)\\*/'))[1] AS origin
FROM
pghero_query_stats
INNER JOIN
pghero_queries ON pghero_queries.id = pghero_query_stats.query_id
WHERE
database = :id
AND captured_at >= :start_at
AND query_hash = :query_hashView on GitHub (pinned to 7edb57986f)
Solutions
- Set up historical query stats: configure the stats database (stats_database_url or the default), install the stats tables (pghero stats migration), and schedule pghero:capture_query_stats -a
- Guard the call: check database.historical_query_stats_enabled? before invoking query_hash_stats
- Verify capture is enabled for the specific database: config key capture_query_stats must not be false
Defensive patterns
Strategy: validation
Validate before calling
if database.historical_query_stats_enabled? stats = database.query_hash_stats(query_hash) end
Try / catch
begin
database.query_hash_stats(query_hash)
rescue PgHero::NotEnabled
# historical stats are optional - fall back to current stats or hide the view
render_not_available("Historical query stats are not configured")
end Prevention
- Gate UI elements for historical stats on historical_query_stats_enabled?
- Automate the pghero:capture_query_stats task and the stats migration in every environment that shows query trends
When it happens
Trigger: Calling PgHero.database.query_hash_stats(12345) in console or opening a query's info view on an installation that never set up the stats database (no stats_database_url / no stats capture schedule / migration not installed), or where capture_query_stats is disabled for that database.
Common situations: Fresh pghero installs that show the Queries tab (current stats work via pg_stat_statements) but never ran the historical setup steps; environments where the pghero stats migration ran on a different database; disabling capture_query_stats in pghero.yml.
Related errors
- Query stats not enabled
- Historical query stats not enabled
- Invalid sort
- Database not found: #{database}
- User not found: #{user}
AI-assisted analysis of ankane/pghero@7edb57986f (2026-08-21).
Data as JSON: /api/errors/9f7762a224a52015.
Report an issue: GitHub.