$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[January 18, 2026] PostgreSQL

PostgreSQL Parallel Queries

PostgreSQL Parallel Queries

────────────────────────────────────────────────────────

PostgreSQL Parallel Queries

Tip: Enable Parallel Queries

max_parallel_workers_per_gather = 4
max_parallel_workers = 8

Allows up to 4 workers per query.

Gotcha: Not All Queries Go Parallel

PostgreSQL only uses parallelism for sequential scans, aggregates, and joins on large tables.

Tip: Check Parallel Execution

EXPLAIN (ANALYZE, COSTS, VERBOSE) SELECT count(*) FROM large_table;

Look for "Gather" and "Parallel Seq Scan" in the output.

Gotcha: Small Tables Don't Benefit

Parallel queries add overhead. For small tables, sequential execution is faster.

Tip: Parallel Index Scans

min_parallel_index_scan_size = 512kB

Controls when index scans go parallel.

Gotcha: Parallel Queries and Memory

Each worker gets its own work_mem. With 4 workers and 64MB work_mem, that's 256MB per query.

Tip: EXPLAIN (ANALYZE, BUFFERS) Is Your Best Friend

For query debugging, always use EXPLAIN (ANALYZE, BUFFERS) instead of plain EXPLAIN. The BUFFERS option shows hit/miss rates for every node, revealing whether your indexes are actually in memory.

Tip: Partial Indexes Are Underutilized

CREATE INDEX ON orders (status) WHERE status = 'pending' creates a tiny index that covers only the rows your query needs. It's faster to scan and cheaper to maintain than a full-column index.

Gotcha: NULL Sorting Is Non-Obvious

By default, NULLs sort AFTER non-null values in ascending order. ORDER BY col DESC puts NULLs FIRST. Use NULLS LAST or NULLS FIRST to be explicit.

Senior Insight

PostgreSQL's parallel query execution is automatic but finicky. The planner considers parallel workers only for sequential scans, not index scans. If a table is small enough to be cached in memory, parallel query adds overhead. I've seen parallel queries underperform because min_parallel_table_scan_size was set too low, causing the planner to assign parallel workers to tiny tables. Monitor explain (analyze) output for parallel workers in your queries.

Source: pganalyze Blog (https://pganalyze.com/blog), PostgreSQL Docs (https://www.postgresql.org/docs/current/), Crunchy Data Blog (https://www.crunchydata.com/blog)

────────────────────────────────────────────────────────
<-- back to posts