Documentationpg_stat_insights Documentation

JIT Compilation Analysis

Confirm JIT Instrumentation

JIT metrics require pg_stat_insights.track_planning = true and jit = on. Toggle planning metrics during tuning sessions to limit overhead.

postgresql.conf excerpt

jit = on
pg_stat_insights.track_planning = true

Identify JIT-Compiled Queries

Surface the queries that trigger JIT compilation most often and accumulate the highest compilation time.

Top JIT consumers

SELECT queryid,
       LEFT(query, 160) AS query_preview,
       calls,
       jit_functions,
       jit_generation_time + jit_inlining_time + jit_optimization_time + jit_emission_time AS total_jit_ms,
       mean_exec_time
  FROM pg_stat_insights
 WHERE jit_functions > 0
 ORDER BY total_jit_ms DESC
 LIMIT 20;

Compare Execution vs Compilation Cost

JIT is beneficial when compilation overhead is tiny relative to overall execution. Use the per-call view to spot queries where JIT dominates response time.

Per-call JIT overhead

SELECT queryid,
       (jit_generation_time + jit_inlining_time + jit_optimization_time + jit_emission_time) / calls AS avg_jit_ms,
       mean_exec_time
  FROM pg_stat_insights
 WHERE jit_functions > 0
 ORDER BY avg_jit_ms DESC
 LIMIT 20;

Decide on JIT Strategy

Keep JIT enabled when

  • Queries scan millions of rows or perform complex aggregates where code generation saves CPU.
  • Average JIT overhead is <10% of total execution time.
  • Workload is OLAP-heavy and can amortise compilation cost over long-running calls.

Disable or limit JIT when

  • Queries execute frequently with small result sets and JIT cost exceeds execution time.
  • Latency-sensitive workloads suffer from compilation spikes under burst traffic.
  • Infrastructure has limited CPU headroom to absorb compilation.

Toggle JIT

-- Disable JIT for a single session
SET jit = off;

-- Disable globally (requires restart)
ALTER SYSTEM SET jit = off;
SELECT pg_reload_conf();

Optimise or Sandbox JIT

Force JIT off for specific roles

ALTER ROLE app_user SET jit = off;

Reset planning stats

SELECT pg_stat_insights_reset();

Review EXPLAIN (ANALYZE, BUFFERS, WAL) output to validate whether compiled loops outperform interpreter execution for your workload.