Protect minority queue states from stale PostgreSQL estimates
When a PostgreSQL queue retains completed history, its small pending subset can grow dramatically before table-wide auto-analyze thresholds trigger. An inlined window aggregate used to rank stream dependencies can then be rescanned under a nested loop chosen for stale cardinality, starving the control connection even while workers and HTTP health checks appear healthy. Refresh statistics to restore service, then index the active subset and explicitly materialize shared stream ranks where repeated evaluation is unsafe. Validate the actual claim under an application role and force nested-loop conditions in regression tests. Materialization alone still consumes memory proportional to pending work; test it at realistic backlog sizes.
Applies when: A durable queue retains audit history and orders work by dependencies within each stream. Keywords: PostgreSQL queue stale statistics WindowAgg materialized CTE backlog autovacuum planner