{"record":{"id":"9f7762a224a52015","repo":"ankane/pghero","slug":"query-hash-stats-not-enabled","errorCode":null,"errorMessage":"Query hash stats not enabled","messagePattern":"Query hash stats not enabled","errorType":"exception","errorClass":"PgHero::NotEnabled","httpStatus":null,"severity":"error","filePath":"lib/pghero/methods/query_stats.rb","lineNumber":158,"sourceCode":"        db_query_stats = query_stats(limit: 100)\n        if db_query_stats.any? && reset_query_stats(raise_errors: raise_errors)\n          insert_query_stats(db_query_stats, captured_at)\n        end\n      end\n\n      def clean_query_stats(before: nil)\n        before ||= 14.days.ago\n        PgHero::QueryStats.where(database: id).where(\"captured_at < ?\", before).delete_all\n      end\n\n      def slow_queries(query_stats: nil, **options)\n        query_stats ||= self.query_stats(**options)\n        query_stats.select { |q| q[:calls].to_i >= slow_query_calls.to_i && q[:average_time].to_f >= slow_query_ms.to_f }\n      end\n\n      def query_hash_stats(query_hash, user: nil, current: true)\n        if !historical_query_stats_enabled?\n          raise NotEnabled, \"Query hash stats not enabled\"\n        end\n\n        start_at = 24.hours.ago\n        # specify pghero_queries.query in case pghero_query_stats.query exists\n        sql = <<~SQL\n          SELECT\n            captured_at,\n            total_time,\n            calls,\n            (SELECT regexp_matches(pghero_queries.query, '.*/\\\\*(.+?)\\\\*/'))[1] AS origin\n          FROM\n            pghero_query_stats\n          INNER JOIN\n            pghero_queries ON pghero_queries.id = pghero_query_stats.query_id\n          WHERE\n            database = :id\n            AND captured_at >= :start_at\n            AND query_hash = :query_hash","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/ankane/pghero/blob/7edb57986ffd36f9d64f0830c4ccc90a5eac46d6/lib/pghero/methods/query_stats.rb#L140-L176","documentation":"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\".","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"if database.historical_query_stats_enabled?\n  stats = database.query_hash_stats(query_hash)\nend","typeGuard":null,"tryCatchPattern":"begin\n  database.query_hash_stats(query_hash)\nrescue PgHero::NotEnabled\n  # historical stats are optional - fall back to current stats or hide the view\n  render_not_available(\"Historical query stats are not configured\")\nend","preventionTips":["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"],"tags":["pghero","query-stats","not-enabled","historical-stats"],"backgroundTag":"feature-not-enabled","analyzedSha":"7edb57986ffd36f9d64f0830c4ccc90a5eac46d6","analyzedAt":"2026-08-21T17:33:54.942Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}