Choosing the Wrong Execution Plan

Hi guys I’m trying to better understand how PostgreSQL’s query planner behaves as a database grows.
I’ve noticed that some queries perform well with a small dataset but become much slower as the tables get larger, even though the query itself hasn’t changed.
For example: SELECT *
FROM workflow_jobs
WHERE tenant_id = ‘tenant_001’
AND status = ‘pending’
ORDER BY created_at DESC
LIMIT 100;
I’ve been using EXPLAIN ANALYZE to inspect query plans, but I’m not always sure when the planner is making a poor choice or whether I should intervene.
I would really appreciate hearing about real production experiences

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Selena_Gloria

A good starting point is to trust the query planner, but verify its decisions with real execution data.

Recommended approach

I usually begin with EXPLAIN ANALYZE to compare the estimated cost with the actual execution time.

EXPLAIN ANALYZE
SELECT *
FROM workflow_jobs
WHERE tenant_id = ‘tenant_001’
AND status = ‘pending’
ORDER BY created_at DESC
LIMIT 100;

If PostgreSQL is doing a sequential scan when an index should be more efficient, or if there’s a large difference between the estimated and actual row counts, it often indicates outdated statistics or a missing index.

Best practices
Run ANALYZE regularly so the planner has accurate statistics.
Create indexes based on actual query patterns, not assumptions.
Review slow queries before changing indexes or rewriting SQL.
Re-test queries after any schema or index changes.

In production, the query planner is usually very good. The biggest improvements often come from accurate statistics, proper indexing, and understanding how your application queries the data.

Hi @Selena_Gloria

For this specific query, you shouldn’t rely on single-column indexes. You need a composite index that follows the Equality $\to$ Sort $\to$ Range rule.

CREATE INDEX idx_workflow_jobs_tenant_status_created 
ON workflow_jobs (tenant_id, status, created_at DESC);

Why this works:

  1. tenant_id and status are used for equality filtering.
  2. created_at is included in the index in the requested sort order.
  3. The planner can jump straight to the section of the index for that tenant/status and simply read the first 100 rows in order. This turns a potentially massive search into a “surgical strike.”

When using EXPLAIN ANALYZE, don’t just look at the time. Use:

EXPLAIN (ANALYZE, BUFFERS) SELECT ...

The BUFFERS flag is critical. It tells you how many blocks were read from shared hit (memory) vs. read (disk). If you see a high number of read blocks, your query is bottlenecked by I/O, likely because the planner chose an index scan that is causing “random I/O” thrashing.

You should intervene if you see:

  1. High “Rows Removed by Filter”: This means your index is not selective enough.
  2. A switch from Index Scan to Seq Scan: This means the planner thinks the cost of random I/O is too high. This is a sign you need a better composite index or better clustering.
  3. Discrepancy between estimated rows and actual rows: If the planner expects 10 rows but gets 100,000, your statistics are stale. Run ANALYZE workflow_jobs; manually.

Hi @Selena_Gloria
Postgres estimates tenant_id and status independently and multiplies the two selectivities, so when those columns are correlated it predicts far fewer matching rows than really exist. In a multi-tenant jobs table they usually are correlated, since status mix varies a lot per tenant. That underestimate is what flips the plan as the table grows: believing only a handful of rows match, the planner takes the early-abort route down a created_at index expecting to hit 100 matches almost immediately, then walks a large slice of the table to fill the LIMIT. The query never changed, the estimate just stopped being cheap to get wrong.
Give it cross-column statistics so the estimate is right in the first place:

CREATE STATISTICS workflow_jobs_tenant_status (dependencies, mcv)
ON tenant_id, status FROM workflow_jobs;
ANALYZE workflow_jobs;

For production, auto_explain with auto_explain.log_min_duration set to a few hundred milliseconds logs the plan that actually ran, which is the only reliable way to catch a flip that won’t reproduce in your own session.