{"record":{"id":"d7bcaa784228574c","repo":"apache/cassandra","slug":"prepared-query-with-id-s-not-found-either-the-qu-d7bcaa","errorCode":null,"errorMessage":"Prepared query with ID %s not found (either the query was not prepared on this host (maybe the host has been restarted?) or you have prepared too many queries and it has been evicted from the internal cache)","messagePattern":"Prepared query with ID (.+?) not found \\(either the query was not prepared on this host \\(maybe the host has been restarted\\?\\) or you have prepared too many queries and it has been evicted from the internal cache\\)","errorType":"exception","errorClass":"PreparedQueryNotFoundException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java","lineNumber":140,"sourceCode":"        return true;\n    }\n\n    @Override\n    protected boolean isTrackable()\n    {\n        return true;\n    }\n\n    @Override\n    protected Message.Response execute(QueryState state, Dispatcher.RequestTime requestTime, boolean traceRequest)\n    {\n        QueryHandler.Prepared prepared = null;\n        try\n        {\n            QueryHandler handler = ClientState.getCQLQueryHandler();\n            prepared = handler.getPrepared(statementId);\n            if (prepared == null)\n                throw new PreparedQueryNotFoundException(statementId);\n\n            if (!prepared.fullyQualified && prepared.statement.eligibleAsPreparedStatement() && !Objects.equals(state.getClientState().getRawKeyspace(), prepared.keyspace))\n            {\n                state.getClientState().warnAboutUseWithPreparedStatements(statementId, prepared.keyspace);\n\n                String msg = String.format(\"Tried to execute a prepared unqualified statement on a keyspace it was not prepared on. \" +\n                                           \" Executing the resulting prepared statement will return unexpected results: %s (on keyspace %s, previously prepared on %s)\",\n                                           statementId, state.getClientState().getRawKeyspace(), prepared.keyspace);\n                nospam.error(msg);\n            }\n\n            CQLStatement statement = prepared.statement;\n            options.prepare(statement.getBindVariables());\n\n            if (options.getPageSize() == 0)\n                throw new ProtocolException(\"The page size cannot be 0\");\n\n            if (traceRequest)","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/transport/messages/ExecuteMessage.java#L122-L158","documentation":"An EXECUTE message references a prepared statement id that is not in this node's prepared-statement cache; handler.getPrepared(statementId) returned null so Cassandra throws PreparedQueryNotFoundException. The id was never prepared on this host, the host was restarted (in-memory cache lost), or the entry was evicted from the bounded cache.","triggerScenarios":"Sending ExecuteMessage with a statementId not present in the QueryHandler cache — node restart, prepare sent to one node and execute routed to another, cache eviction under high statement cardinality, or client retaining ids from a previous cluster generation.","commonSituations":"Failover after a datacenter outage where clients resume with old ids; rolling upgrades/restarts evicting all prepared statements; applications storing prepared ids in persistent state; long-running workloads exceeding the prepared statement cache capacity.","solutions":["Re-issue PREPARE for the original CQL string and retry the execution (drivers do this transparently — keep the driver current)","Enable the driver's reprepare-on-up/reconnect behavior so prepares are replayed after topology or restart events","Do not persist or share prepared statement ids across sessions/restarts; treat them as ephemeral","Reduce distinct prepared statement count if eviction pressure is high"],"exampleFix":"// before\nsession.execute(preparedId, values); // stale id after restart\n// after\ntry {\n    session.execute(preparedId, values);\n} catch (PreparedQueryNotFound e) {\n    PreparedStatement ps = session.prepare(cql);\n    session.execute(ps, values);\n}","handlingStrategy":"retry","validationCode":"if (!session.getPreparedIds().contains(statementId)) {\n    PreparedStatement ps = session.prepare(cql);\n    statementId = ps.getPreparedId();\n}","typeGuard":null,"tryCatchPattern":"try {\n    session.execute(preparedId, values);\n} catch (PreparedQueryNotFoundException e) {\n    PreparedStatement ps = session.prepare(cql);\n    session.execute(ps.bind(values));\n}","preventionTips":["Use a maintained driver that handles re-preparation transparently","Enable reprepare-on-up for all contact points","Treat prepared ids as ephemeral — no caching across restarts","Monitor cache eviction metrics if preparing a very large number of distinct statements"],"tags":["cql","prepared-statement","cache-miss","restart"],"backgroundTag":"resource-not-found","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}