mongodb runtime_error ai_generated true

MongoServerError: PlanCache invalidated entry for query shape but could not re-plan: internal error

ID: mongodb/plan-cache-invalidation-failed

Also available as: JSON · Markdown · 中文
82%Fix Rate
88%Confidence
1Evidence
2024-01-10First Seen

Root Cause

The plan cache entry for a query shape was invalidated due to index changes or data distribution shifts, but the query planner failed to generate a new plan due to corrupted cache metadata or missing index.

generic

中文

由于索引更改或数据分布变化,查询形状的计划缓存条目失效,但查询规划器因缓存元数据损坏或索引缺失而无法生成新计划。

Official Documentation

https://www.mongodb.com/docs/manual/reference/command/planCacheClear/

Workarounds

  1. 85% success Run db.collection.getPlanCache().clear() followed by db.collection.createIndex() for the missing index, then re-execute the query to force a fresh plan.
    Run db.collection.getPlanCache().clear() followed by db.collection.createIndex() for the missing index, then re-execute the query to force a fresh plan.
  2. 80% success Upgrade to MongoDB 7.0+ where plan cache invalidation is more robust; if not possible, set internalQueryPlanCacheSize to 0 in the configuration to disable caching temporarily.
    Upgrade to MongoDB 7.0+ where plan cache invalidation is more robust; if not possible, set internalQueryPlanCacheSize to 0 in the configuration to disable caching temporarily.

中文步骤

  1. Run db.collection.getPlanCache().clear() followed by db.collection.createIndex() for the missing index, then re-execute the query to force a fresh plan.
  2. Upgrade to MongoDB 7.0+ where plan cache invalidation is more robust; if not possible, set internalQueryPlanCacheSize to 0 in the configuration to disable caching temporarily.

Dead Ends

Common approaches that don't work:

  1. 90% fail

    Restarting mongod clears the plan cache temporarily, but the error reoccurs if the underlying cause (e.g., missing index or stale statistics) isn't addressed.

  2. 70% fail

    Manually running db.collection.getPlanCache().clear() may not help if the query shape is still cached in the server's internal structures; the error persists.