# 31.2. When to JIT?

JIT compilation is beneficial primarily for long-running CPU-bound queries. Frequently these will be analytical queries. For short queries the added overhead of performing JIT compilation will often be higher than the time it can save.

To determine whether JIT compilation should be used, the total estimated cost of a query (see [Chapter 70](https://www.postgresql.org/docs/13/planner-stats-details.html) and [Section 19.7.2](https://www.postgresql.org/docs/13/runtime-config-query.html#RUNTIME-CONFIG-QUERY-CONSTANTS)) is used. The estimated cost of the query will be compared with the setting of [jit\_above\_cost](https://www.postgresql.org/docs/13/runtime-config-query.html#GUC-JIT-ABOVE-COST). If the cost is higher, JIT compilation will be performed. Two further decisions are then needed. Firstly, if the estimated cost is more than the setting of [jit\_inline\_above\_cost](https://www.postgresql.org/docs/13/runtime-config-query.html#GUC-JIT-INLINE-ABOVE-COST), short functions and operators used in the query will be inlined. Secondly, if the estimated cost is more than the setting of [jit\_optimize\_above\_cost](https://www.postgresql.org/docs/13/runtime-config-query.html#GUC-JIT-OPTIMIZE-ABOVE-COST), expensive optimizations are applied to improve the generated code. Each of these options increases the JIT compilation overhead, but can reduce query execution time considerably.

These cost-based decisions will be made at plan time, not execution time. This means that when prepared statements are in use, and a generic plan is used (see [PREPARE](https://www.postgresql.org/docs/13/sql-prepare.html)), the values of the configuration parameters in effect at prepare time control the decisions, not the settings at execution time.

#### Note

If [jit](https://www.postgresql.org/docs/13/runtime-config-query.html#GUC-JIT) is set to `off`, or if no JIT implementation is available (for example because the server was compiled without `--with-llvm`), JIT will not be performed, even if it would be beneficial based on the above criteria. Setting [jit](https://www.postgresql.org/docs/13/runtime-config-query.html#GUC-JIT) to `off` has effects at both plan and execution time.

[EXPLAIN](https://www.postgresql.org/docs/13/sql-explain.html) can be used to see whether JIT is used or not. As an example, here is a query that is not using JIT:

```
=# EXPLAIN ANALYZE SELECT SUM(relpages) FROM pg_class;
                                                 QUERY PLAN
-------------------------------------------------------------------​------------------------------------------
 Aggregate  (cost=16.27..16.29 rows=1 width=8) (actual time=0.303..0.303 rows=1 loops=1)
   ->  Seq Scan on pg_class  (cost=0.00..15.42 rows=342 width=4) (actual time=0.017..0.111 rows=356 loops=1)
 Planning Time: 0.116 ms
 Execution Time: 0.365 ms
(4 rows)
```

Given the cost of the plan, it is entirely reasonable that no JIT was used; the cost of JIT would have been bigger than the potential savings. Adjusting the cost limits will lead to JIT use:

```
=# SET jit_above_cost = 10;
SET
=# EXPLAIN ANALYZE SELECT SUM(relpages) FROM pg_class;
                                                 QUERY PLAN
-------------------------------------------------------------------​------------------------------------------
 Aggregate  (cost=16.27..16.29 rows=1 width=8) (actual time=6.049..6.049 rows=1 loops=1)
   ->  Seq Scan on pg_class  (cost=0.00..15.42 rows=342 width=4) (actual time=0.019..0.052 rows=356 loops=1)
 Planning Time: 0.133 ms
 JIT:
   Functions: 3
   Options: Inlining false, Optimization false, Expressions true, Deforming true
   Timing: Generation 1.259 ms, Inlining 0.000 ms, Optimization 0.797 ms, Emission 5.048 ms, Total 7.104 ms
 Execution Time: 7.416 ms
```

As visible here, JIT was used, but inlining and expensive optimization were not. If [jit\_inline\_above\_cost](https://www.postgresql.org/docs/13/runtime-config-query.html#GUC-JIT-INLINE-ABOVE-COST) or [jit\_optimize\_above\_cost](https://www.postgresql.org/docs/13/runtime-config-query.html#GUC-JIT-OPTIMIZE-ABOVE-COST) were also lowered, that would change.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.postgresql.tw/13/server-administration/just-in-time-compilation/when-to-jit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
