{"record":{"id":"065a894d2f7ae584","repo":"ankane/pghero","slug":"database-not-found-database-id","errorCode":null,"errorMessage":"Database not found: #{database_id}","messagePattern":"Database not found: #(.+?)","errorType":"exception","errorClass":"PgHero::Error","httpStatus":null,"severity":"error","filePath":"lib/pghero/methods/indexes.rb","lineNumber":95,"sourceCode":"            pg_relation_size(i.indexrelid) AS size_bytes,\n            idx_scan as index_scans\n          FROM\n            pg_stat_user_indexes ui\n          INNER JOIN\n            pg_index i ON ui.indexrelid = i.indexrelid\n          WHERE\n            NOT indisunique\n            AND idx_scan <= :max_scans\n            AND pg_relation_size(i.indexrelid) >= :min_size\n          ORDER BY\n            pg_relation_size(i.indexrelid) DESC,\n            relname ASC\n        SQL\n        result = select_all_size(sql, {max_scans: max_scans.to_i, min_size: min_size.to_i})\n\n        across.each do |database_id|\n          database = PgHero.databases.values.find { |d| d.id == database_id }\n          raise PgHero::Error, \"Database not found: #{database_id}\" unless database\n          across_result = Set.new(database.unused_indexes(max_scans: max_scans).map { |v| [v[:schema], v[:index]] })\n          result.select! { |v| across_result.include?([v[:schema], v[:index]]) }\n        end\n\n        result\n      end\n\n      def reset_stats\n        execute(\"SELECT pg_stat_reset()\")\n        true\n      end\n\n      def last_stats_reset_time\n        select_one <<~SQL\n          SELECT\n            pg_stat_get_db_stat_reset_time(oid) AS reset_time\n          FROM\n            pg_database","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/ankane/pghero/blob/7edb57986ffd36f9d64f0830c4ccc90a5eac46d6/lib/pghero/methods/indexes.rb#L77-L113","documentation":"unused_indexes(max_scans:, min_size:, across:) can intersect results with other configured databases so only indexes unused everywhere are reported. For each id in the across array it looks the database up in PgHero.databases (keyed by id from pghero.yml); an id with no matching configured database raises PgHero::Error \"Database not found: {id}\".","triggerScenarios":"Calling PgHero.database.unused_indexes(across: [\"replica\"]) when pghero.yml keys that database differently (\"replica-2\", \"reporting\"); passing the display name (\"Replica\") instead of the id; a database removed from pghero.yml while another tool or cached dashboard still requests it.","commonSituations":"Multi-database pghero setups after renaming keys in pghero.yml; scripts or cron jobs hardcoding database ids; UI links built from stale configuration.","solutions":["Use the exact id from pghero.yml - list valid ids with PgHero.databases.keys in rails console","Validate the across array before calling: across.all? { |id| PgHero.databases.key?(id) }","If a database was renamed, update every caller that references the old id"],"exampleFix":"# before - id does not match pghero.yml\ndb.unused_indexes(across: [\"replica\"])\n\n# after - use exact key from PgHero.databases\ndb.unused_indexes(across: [\"replica-2\"])","handlingStrategy":"validation","validationCode":"across = across.select { |id| PgHero.databases.key?(id) }\ndatabase.unused_indexes(across: across)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat database ids as an API: list them with PgHero.databases.keys before composing across arrays","Update scripts that reference database ids whenever pghero.yml keys change"],"tags":["pghero","multi-database","config","indexes"],"backgroundTag":"resource-not-found","analyzedSha":"7edb57986ffd36f9d64f0830c4ccc90a5eac46d6","analyzedAt":"2026-08-21T17:33:54.942Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}