{
  "id": 23130785,
  "title": "PostgreSQL slow queries: a practical checklist before adding a partial index",
  "url": "https://urgent.news/2026/12/02/postgresql-slow-queries-a-practical-checklist-before-adding-a-partial",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-12-02T14:39:28.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/zongsmcmyk/postgresql-slow-queries-a-practical-checklist-before-adding-a-partial-index-4ced"
  },
  "original_language": null,
  "account": "When a query looks index-friendly but is slow in production, it's crucial to verify several aspects before suggesting a database index. In this case, the query retrieves the 50 most recent pending events for a tenant, ordered by creation time. A partial index might seem like a viable solution, but there are essential factors to consider.\n\nFirstly, it's necessary to examine real plans rather than relying on assumptions. Use the EXPLAIN (ANALYZE, BUFFERS) command in a secure environment to capture actual performance metrics, such as estimated versus actual rows, loop counts, heap fetches, buffer hits/reads, sort nodes, and wall-clock time. Remember that EXPLAIN ANALYZE will execute writes, so avoid running it carelessly in production.\n\nSecondly, be aware of tenant skew and predicate selectivity. If one tenant dominates the dataset while others have small subsets, a plan derived from a smaller tenant may not accurately represent the workload of a heavy tenant. Similarly, the partial predicate 'status = 'pending'' is only effective if the distribution of tenant data supports it. Test representative workloads instead of focusing on a single parameter.\n\nThirdly, determine what the index does not cover. Including a large JSON or JSONB payload in the index to avoid heap visits could lead to increased maintenance costs. In this example, 'id' and 'event_type' are included in the index, but the actual plan should be inspected to determine if this compromise is justified.\n\nFourthly, evaluate the impact of additional writes, locks, and maintenance. More indexes mean more work for INSERT, UPDATE, and DELETE operations, potentially affecting disk space, cache usage, and overall performance. CREATE INDEX CONCURRENTLY is a useful tool, but it must run outside an explicit transaction block. In case of a failed build, cleanup may be required, so coordinate any changes through regular deployment/rollback procedures.\n\nLastly, compare the results against a baseline using representative parameters, data, and load. Record the plan changes, buffer behavior, latency distribution, and write impact. A before/after analysis without controlled conditions is not evidence of a reliable gain. Conduct a bounded review process for each slow query, documenting the affected business operation, query text (with sensitive values removed), PostgreSQL version, relevant indexes/schema, the most likely bottleneck, and two or three changes worth testing. Include validation commands, trade-offs, and rollback steps in the report.\n\nThis thorough approach ensures that any decisions regarding database indexes are based on empirical evidence and not just speculation. The author has published a comprehensive walkthrough and example SQL in their GitHub portfolio and offers a paid audit service for up to three queries, providing a fixed-scope report within 48 hours of agreeing on the scope and receiving sanitized plans. The full disclosure and contact information are available in the linked repository or via email.",
  "summary": "A query can look index-friendly and still be slow in production. Before suggesting CREATE INDEX , I prefer to record a small set of facts: what the planner estimates, what actually executes, how tenant values differ, and what extra writes the proposed index would cause. Below is a worked illustration , not a benchmark taken from a client's database. The query shape Imagine an API or analytics…",
  "summary_source": "publisher",
  "key_points": [
    "Examine real query plans with EXPLAIN (ANALYZE, BUFFERS) in a secure environment",
    "Consider tenant skew and predicate selectivity, test representative workloads",
    "Evaluate index coverage, impact of additional writes, locks, and maintenance"
  ],
  "editors_take": null,
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "outlets_capped": false,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}