Database Performance

PostgreSQL Parallel Query: Configuration, JIT, and Tuning for Analytics

Enable and tune PostgreSQL parallel query for analytical workloads. Covers max_parallel_workers settings, PARALLEL SAFE functions, JIT compilation, and execution plan verification.

JusDB Team
July 16, 2025
5 min read
169 views

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

ini
# 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 = 512kB

Verify Parallel Execution

sql
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 orders

Force Parallel Query (Testing)

sql
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

sql
-- 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:

sql
CREATE OR REPLACE FUNCTION calculate_discount(price NUMERIC)
RETURNS NUMERIC
LANGUAGE sql
IMMUTABLE PARALLEL SAFE
AS $$
  SELECT price * 0.9;
$$;

JIT Compilation

sql
-- 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_gather to (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 SAFE to 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.

Share this article

JusDB Team

Official JusDB content team