{"record":{"id":"31b2b489047bafdb","repo":"ankane/pghero","slug":"user-not-found-user","errorCode":null,"errorMessage":"User not found: #{user}","messagePattern":"User not found: #(.+?)","errorType":"exception","errorClass":"PgHero::Error","httpStatus":null,"severity":"error","filePath":"lib/pghero/methods/query_stats.rb","lineNumber":100,"sourceCode":"\n      def enable_query_stats\n        execute(\"CREATE EXTENSION IF NOT EXISTS pg_stat_statements\")\n        true\n      end\n\n      def disable_query_stats\n        execute(\"DROP EXTENSION IF EXISTS pg_stat_statements\")\n        true\n      end\n\n      def reset_query_stats(user: nil, query_hash: nil, raise_errors: false)\n        database = database_name\n        database_id = select_one(\"SELECT oid FROM pg_database WHERE datname = :database\", {database: database})\n        raise Error, \"Database not found: #{database}\" unless database_id\n\n        if user\n          user_id = select_one(\"SELECT usesysid FROM pg_user WHERE usename = :user\", {user: user})\n          raise Error, \"User not found: #{user}\" unless user_id\n        else\n          user_id = 0\n        end\n\n        if query_hash\n          query_id = query_hash.to_i\n          # may not be needed\n          # but not intuitive that all query hashes are reset with 0\n          raise Error, \"Invalid query hash: #{query_hash}\" if query_id == 0\n        else\n          query_id = 0\n        end\n\n        binds = {user_id: user_id, database_id: database_id, query_id: query_id}\n        # use execute to prevent \"unknown OID 2278\" warning\n        execute(\"SELECT pg_stat_statements_reset(:user_id, :database_id, :query_id)\", binds)\n        true\n      rescue ActiveRecord::StatementInvalid => e","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/ankane/pghero/blob/7edb57986ffd36f9d64f0830c4ccc90a5eac46d6/lib/pghero/methods/query_stats.rb#L82-L118","documentation":"reset_query_stats(user:) scopes the reset to queries run by one role. The role name is resolved to its oid via SELECT usesysid FROM pg_user WHERE usename = :user before calling pg_stat_statements_reset; an unknown role raises Error \"User not found: {user}\". On some managed/locked-down Postgres instances pg_user is restricted, which can also make a valid role invisible.","triggerScenarios":"database.reset_query_stats(user: \"app_usr\") where the role is misspelled or does not exist; the role was renamed or dropped after the stats were captured; connecting with a low-privilege role that cannot see other roles in pg_user on a managed service.","commonSituations":"Resetting stats per-user from a script with environment-specific role names; copy-pasting a role from another environment (staging vs production); managed Postgres (some hosted offerings) restricting system catalog visibility.","solutions":["Verify the role exists as the server sees it: SELECT usename FROM pg_user WHERE usename = 'name' - or list roles with \\du in psql","Use the exact rolname shown in the query stats \"user\" column on the pghero Queries page","Drop the user: argument to reset stats for the whole database instead of one role","If pg_user appears empty, connect with a role that has sufficient privileges to read the system catalog"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"user_id = database.select_one(\"SELECT usesysid FROM pg_user WHERE usename = :u\", {u: user})\ndatabase.reset_query_stats(user: user) if user_id","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Take role names from the pghero Queries page 'user' column or \\du output, not from memory","Check pg_user visibility with the connecting role on managed Postgres before scripting per-user resets"],"tags":["pghero","query-stats","postgres-roles","validation"],"backgroundTag":"resource-not-found","analyzedSha":"7edb57986ffd36f9d64f0830c4ccc90a5eac46d6","analyzedAt":"2026-08-21T17:33:54.942Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}