PostgreSQL 18.7 uses wrong plans for prepared statements in skewed tenants
The author examines how PostgreSQL handles execution plans for prepared statements in multi-tenant databases with skewed tenant sizes. Because PostgreSQL chooses between custom and generic plans based on initial executions, a query for a large tenant can mistakenly inherit a generic plan optimized for small tenants, causing severe performance degradation. The author demonstrates that deferred joins and covering indexes can mitigate this issue by isolating the LIMIT clause and avoiding unnecessary heap fetches. Open article →