PostgreSQL can execute queries using multiple CPU cores in parallel. For analytical queries over large tables, parallel query can reduce execution time by 4-8x. Here is how to enable and tune it.
Parallel Query Configuration
# postgresql.conf
max_parallel_workers_per_gather = 4 # workers per query node
max_parallel_workers = 8 # total parallel workers system-wide
max_worker_processes = 16 # total background workers
parallel_tuple_cost = 0.1
parallel_setup_cost = 1000
min_parallel_table_scan_size = 8MB
min_parallel_index_scan_size = 512kBVerify Parallel Execution
EXPLAIN (ANALYZE, BUFFERS)
SELECT region, sum(amount)
FROM orders
GROUP BY region;
-- Look for:
-- Gather (cost=... rows=... width=...)
-- Workers Planned: 4
-- Workers Launched: 4
-- -> Partial HashAggregate
-- -> Parallel Seq Scan on ordersForce Parallel Query (Testing)
SET max_parallel_workers_per_gather = 4;
SET parallel_tuple_cost = 0;
SET parallel_setup_cost = 0;
SET min_parallel_table_scan_size = 0;Disable Parallel for Specific Queries
-- Parallel query is not always faster (small tables, OLTP)
SET max_parallel_workers_per_gather = 0;
-- Or per table
ALTER TABLE small_lookup_table SET (parallel_workers = 0);Parallel-Unsafe Functions
Custom functions must be marked PARALLEL SAFE to be used in parallel queries:
CREATE OR REPLACE FUNCTION calculate_discount(price NUMERIC)
RETURNS NUMERIC
LANGUAGE sql
IMMUTABLE PARALLEL SAFE
AS $$
SELECT price * 0.9;
$$;JIT Compilation
-- Enable JIT for long-running analytical queries
SET jit = on;
SET jit_above_cost = 100000; -- only for expensive queries
SET jit_inline_above_cost = 500000;
SET jit_optimize_above_cost = 500000;Key Takeaways
- Set
max_parallel_workers_per_gatherto (CPU cores / 2) as a starting point - Parallel query helps aggregations, sorts, and seq scans on large tables — not index lookups
- Mark custom functions
PARALLEL SAFEto enable parallelism in queries that use them - Enable JIT for long-running analytical queries — it reduces CPU overhead for expression evaluation
JusDB Can Help
Parallel query tuning requires balancing analytical and OLTP workloads. JusDB can configure PostgreSQL parallelism for your specific workload mix.