Enable or Disable JIT Compilation
Check whether Just-in-Time (JIT) compilation helps your queries on Managed PostgreSQL, and turn it on or off for the whole service, one database, one role or one session.
What JIT Does
With JIT, PostgreSQL compiles parts of a query to machine code with LLVM before running it:
expression evaluation (WHERE clauses, target lists, aggregates) and tuple deforming (extracting
column values from stored rows). For costlier queries it also inlines small functions and runs
LLVM optimization passes.
Compilation takes time before the first row is produced, and it is repeated at every execution. It pays off on long analytical queries and costs time on short ones.
Defaults
JIT is on for new services. Read the current values:
SHOW jit;
SHOW jit_above_cost;
SHOW jit_inline_above_cost;
SHOW jit_optimize_above_cost;| Parameter | Default | Above this planner cost |
|---|---|---|
jit_above_cost | 100000 | The query is compiled |
jit_inline_above_cost | 500000 | Functions and operators are inlined |
jit_optimize_above_cost | 500000 | LLVM optimization passes run |
The thresholds compare with the planner’s estimated cost, not with the measured run time. -1
disables a stage.
All four parameters can be changed by avnadmin per session, per database and per role. Only
jit itself is also a service setting.
When JIT Helps and When It Hurts
JIT tends to help:
- Analytical queries over millions of rows.
- Queries with many expressions per row: long target lists, several aggregates,
CASEchains. - Wide tables, where extracting columns dominates.
- Long batch and reporting jobs, where tens of milliseconds of compilation do not matter.
JIT tends to hurt:
- Short queries whose estimated cost is inflated, for example by stale statistics. They pay for compilation they never recover.
- Queries on partitioned tables with many partitions: code is generated per partition.
- Queries that run many times per second: compilation is repeated each time.
Tip
When latency and CPU rise without a code change, after a table has grown, check whether the
plan now shows a JIT section: the estimated cost may have crossed jit_above_cost.
Measure
Run the query with EXPLAIN ANALYZE and look for the JIT section at the end of the plan:
EXPLAIN (ANALYZE, BUFFERS)
SELECT customer_id, sum(amount), count(*)
FROM public.orders
WHERE created_at >= now() - interval '90 days'
GROUP BY customer_id;JIT:
Functions: 34
Options: Inlining false, Optimization false, Expressions true, Deforming true
Timing: Generation 2.767 ms (Deform 0.703 ms), Inlining 0.000 ms, Optimization 10.702 ms, Emission 77.226 ms, Total 90.695 ms
Execution Time: 1984.149 msCompare Total with Execution Time. With parallel workers, each worker also prints its own
JIT section. Then run the same query with JIT off in your session and compare:
SET jit = off;
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
RESET jit;Functions is the number of compiled functions. Hundreds of functions usually point to a
partitioned table.
Change the Setting for the Service
exo dbaas update SERVICE_NAME -z ZONE -f --pg-settings '{"jit":false}'Parameters:
SERVICE_NAME: name of the service.ZONE: zone of the service, for examplech-gva-2.jit:trueorfalse.
The change applies without a restart. Settings sent with --pg-settings are merged with the
stored ones; to turn JIT back on, send '{"jit":true}'. Read the stored settings:
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q '"pg-settings"'See Configure PostgreSQL Settings for how service settings work.
Change the Setting for a Database, a Role or a Session
For new sessions on one database:
ALTER DATABASE DATABASE_NAME SET jit = off;For new sessions of one role, in any database:
ALTER ROLE ROLE_NAME SET jit = on;
ALTER ROLE ROLE_NAME SET jit_above_cost = 500000;Parameters:
DATABASE_NAME: the database, for exampledefaultdb.ROLE_NAME: the role, for example the one used by a reporting tool.
A common pattern is JIT off for the application role that runs short queries, and on for the reporting role.
For the current session only:
SET jit = on;Database and role settings apply to new sessions only. A connection pool keeps its server connections open, so they keep the old value until they are recycled. See Manage connection pools.
Check what a session uses:
SELECT current_setting('jit'), current_setting('jit_above_cost');Raise the Threshold Instead
Rather than turning JIT off, raise the cost threshold so that only expensive queries are compiled:
ALTER DATABASE DATABASE_NAME SET jit_above_cost = 1000000;Keep statistics current with ANALYZE, so that cost estimates stay realistic.