// troubleshooting guide

JIT overhead and planning time

Some PostgreSQL queries are slow before they read a single row. How to tell from EXPLAIN ANALYZE that JIT compilation or planning took longer than the work itself, and how to bring each one down.

Covers PostgreSQL

The symptom

Two lines at the bottom of an EXPLAIN ANALYZE plan describe time spent outside the plan's operators. Planning Time is how long the planner took to choose the plan. The JIT block, when there is one, is how long PostgreSQL spent compiling parts of the query to machine code before and while running it. For a long report query both are noise. For a query that should take a millisecond they can be most of the bill.

這 query plan viewer flags a plan where JIT took a large share of the run time, and one where planning took longer than execution. The examples use a shop schema with 20,000 customers, 400,000 orders and 800,000 order items.

Diagnose it

The JIT block

PostgreSQL turns JIT on for a query when the plan's estimated cost passes a threshold. The sample tables are too small to pass the defaults, so this session lowers the thresholds to show what the block looks like:

SET jit_above_cost = 0;
SET jit_inline_above_cost = 0;
SET jit_optimize_above_cost = 0;

EXPLAIN (ANALYZE, BUFFERS OFF)
SELECT count(*) FROM customers WHERE country = 'DE';
  ->  Seq Scan on customers  (cost=0.00..417.00 rows=4000 width=0) (actual time=40.401..40.921 rows=4000.00 loops=1)
Planning Time: 0.138 ms
JIT:
  Functions: 4
  Options: Inlining true, Optimization true, Expressions true, Deforming true
  Timing: Generation 0.101 ms (Deform 0.027 ms), Inlining 33.114 ms, Optimization 3.335 ms, Emission 3.927 ms, Total 40.477 ms
Execution Time: 51.044 ms

比較 Total 在 Timing 行首加上 Execution Time. Here compilation is most of the run. The time shows up inside the plan too: the scan's actual time starts late, because the first row had to wait for the compiler. Without JIT the same query is far quicker:

SET jit = off;

EXPLAIN (ANALYZE, BUFFERS OFF)
SELECT count(*) FROM customers WHERE country = 'DE';
  ->  Seq Scan on customers  (cost=0.00..417.00 rows=4000 width=0) (actual time=0.004..0.585 rows=4000.00 loops=1)
Planning Time: 0.052 ms
Execution Time: 0.689 ms

Why JIT was chosen

The decision uses the estimated cost of the whole plan and nothing else. Put the session back to its defaults and look at them:

RESET jit;
RESET jit_above_cost;
RESET jit_inline_above_cost;
RESET jit_optimize_above_cost;

SELECT name, setting
FROM pg_settings
WHERE name IN ('jit', 'jit_above_cost', 'jit_inline_above_cost', 'jit_optimize_above_cost')
ORDER BY name;
         name           | setting
jit                     | on
jit_above_cost          | 100000
jit_inline_above_cost   | 500000
jit_optimize_above_cost | 500000

A plan costed above jit_above_cost is compiled. Above the other two thresholds it is also inlined and optimized, which are the expensive steps in the Timing line. So JIT on a query that turns out to be short usually means the cost estimate was too high, and that leads back to a row estimate that is far off. Check the estimates in the plan before touching the settings.

Planning time

A point lookup across several joined tables runs in microseconds, but the planner still has to consider the join orders:

EXPLAIN (ANALYZE, BUFFERS OFF)
SELECT c.email, o1.total, i1.product_id, i2.product_id,
       o2.order_id, o3.order_id, c2.email, i3.quantity
FROM orders o1
JOIN customers c ON c.customer_id = o1.customer_id
JOIN order_items i1 ON i1.order_id = o1.order_id AND i1.line_no = 1
JOIN order_items i2 ON i2.order_id = o1.order_id AND i2.line_no = 2
JOIN orders o2 ON o2.order_id = o1.order_id + 1
JOIN orders o3 ON o3.order_id = o1.order_id + 2
JOIN customers c2 ON c2.customer_id = o2.customer_id
JOIN order_items i3 ON i3.order_id = o3.order_id AND i3.line_no = 1
WHERE o1.order_id = 42;
Planning Time: 1.055 ms
Execution Time: 0.118 ms

Planning took many times longer than running. The first plan in a new session is slower still, because the session has to load the table and index definitions into its cache; run the statement twice before you read the number.

Planning time grows with the number of joined tables, the number of indexes on them and the number of partitions the planner has to look at. A query over a table with hundreds of partitions that does not filter on the partition key plans every partition; partition pruning covers that case.

Fix it

JIT: fix the estimate, then the threshold

If the plan's row estimates are wrong, fix those first; a correct, lower cost keeps the query under the threshold with no setting changed. If the estimates are right and short queries still get compiled, raise the threshold for the session, role or database that runs them:

SET jit_above_cost = 500000;

Or turn JIT off where the workload is all short queries. It can be set per session, as above, or for one role:

CREATE ROLE app_web;
ALTER ROLE app_web SET jit = off;

這 jit setting page covers the server-wide switch.

Planning: prepare the statement

A prepared statement can keep its plan. Once PostgreSQL settles on a generic plan for it, later executions skip planning. Setting plan_cache_mode makes that happen from the first execution:

PREPARE order_detail(int) AS
SELECT c.email, o1.total, i1.product_id, i2.product_id,
       o2.order_id, o3.order_id, c2.email, i3.quantity
FROM orders o1
JOIN customers c ON c.customer_id = o1.customer_id
JOIN order_items i1 ON i1.order_id = o1.order_id AND i1.line_no = 1
JOIN order_items i2 ON i2.order_id = o1.order_id AND i2.line_no = 2
JOIN orders o2 ON o2.order_id = o1.order_id + 1
JOIN orders o3 ON o3.order_id = o1.order_id + 2
JOIN customers c2 ON c2.customer_id = o2.customer_id
JOIN order_items i3 ON i3.order_id = o3.order_id AND i3.line_no = 1
WHERE o1.order_id = $1;

SET plan_cache_mode = force_generic_plan;
EXPLAIN (ANALYZE, BUFFERS OFF) EXECUTE order_detail(42);
EXPLAIN (ANALYZE, BUFFERS OFF) EXECUTE order_detail(42);
Planning Time: 0.707 ms
Execution Time: 0.030 ms
Planning Time: 0.002 ms
Execution Time: 0.019 ms

The first execution plans the statement; the second reuses the plan. Most drivers prepare statements for you when you use bind parameters. A generic plan is built without knowing the parameter values, which is fine for a key lookup like this one and risky where different values need different plans; parameter-sensitive plans explains the trade.

Planning: give the planner less to consider

For a query whose join order you already know, join_collapse_limit = 1 makes the planner join the tables in the order they are written:

SET join_collapse_limit = 1;

EXPLAIN (ANALYZE, BUFFERS OFF)
SELECT c.email, o1.total, i1.product_id, i2.product_id,
       o2.order_id, o3.order_id, c2.email, i3.quantity
FROM orders o1
JOIN customers c ON c.customer_id = o1.customer_id
JOIN order_items i1 ON i1.order_id = o1.order_id AND i1.line_no = 1
JOIN order_items i2 ON i2.order_id = o1.order_id AND i2.line_no = 2
JOIN orders o2 ON o2.order_id = o1.order_id + 1
JOIN orders o3 ON o3.order_id = o1.order_id + 2
JOIN customers c2 ON c2.customer_id = o2.customer_id
JOIN order_items i3 ON i3.order_id = o3.order_id AND i3.line_no = 1
WHERE o1.order_id = 42;

RESET join_collapse_limit;
Planning Time: 0.152 ms
Execution Time: 0.028 ms

Set it for that one statement or transaction only. With the limit at 1 a badly ordered query gets exactly the plan it was written as.

When to leave it alone

A JIT block on a query that runs for seconds is doing its job, and its cost is a rounding error. Planning that takes longer than execution is normal for any fast query; a millisecond of planning in front of a tenth of a millisecond of work is still a millisecond. It is worth fixing when that statement runs thousands of times a second, or when planning takes tens of milliseconds.

The checklist

Work through it in order. Each step gives you the evidence for the next one.

  1. Run EXPLAIN ANALYZE twice and read Planning Time, Execution Time and the JIT block of the second run.
  2. JIT: compare the Total on the Timing line with Execution Time.
  3. Check the plan's row estimates before changing a JIT threshold; an inflated cost is the usual trigger.
  4. Raise jit_above_cost, or set jit = off for the role that runs short queries.
  5. Planning: count the joined tables, their indexes and the partitions the query touches.
  6. Use prepared statements so repeated executions reuse the plan.

Questions that come up

What is the JIT section in a PostgreSQL EXPLAIN ANALYZE plan?

It reports just-in-time compilation: PostgreSQL compiled expressions and row decoding for this query to machine code. The Timing line gives the time spent generating, inlining, optimizing and emitting that code. That time is part of the query's run time.

Should I turn JIT off in PostgreSQL?

For a workload of short queries, turning it off or raising jit_above_cost is reasonable, because compilation can cost more than it saves. For long analytic queries JIT usually pays for itself. You can set it per role or per session and keep both.

Why is Planning Time higher than Execution Time?

The planner's work depends on how many tables, indexes and partitions it has to consider, not on how many rows the query returns. A lookup that joins eight tables by primary key runs in microseconds but still has many join orders to cost. The first statement in a new session also pays for loading catalog information.

Do prepared statements reduce planning time in PostgreSQL?

Yes, once the statement uses a generic plan. PostgreSQL plans a prepared statement with the actual parameter values for its first executions, then switches to a cached generic plan if that is not costlier. plan_cache_mode = force_generic_plan makes it use the cached plan right away.

Does JIT time count toward Execution Time?

Yes. Compilation happens during execution, so the Execution Time at the bottom of the plan includes it. In a parallel plan the Timing line adds up the compilation done by the leader and every worker, so its total can be larger than the elapsed time.

// fix it where the query lives

Run these queries in SQLly

SQLly is a native SQL workbench for macOS, Windows and Linux. The query plan viewer and formatter these guides link to are built into its editor.