]> begriffs open source - ai-pg/blob - full-docs/html/jit-decision.html
Include latest toc output
[ai-pg] / full-docs / html / jit-decision.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>30.2. When to JIT?</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="jit-reason.html" title="30.1. What Is JIT compilation?" /><link rel="next" href="jit-configuration.html" title="30.3. Configuration" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">30.2. When to <acronym class="acronym">JIT</acronym>?</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="jit-reason.html" title="30.1. What Is JIT compilation?">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="jit.html" title="Chapter 30. Just-in-Time Compilation (JIT)">Up</a></td><th width="60%" align="center">Chapter 30. Just-in-Time Compilation (<acronym class="acronym">JIT</acronym>)</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="jit-configuration.html" title="30.3. Configuration">Next</a></td></tr></table><hr /></div><div class="sect1" id="JIT-DECISION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">30.2. When to <acronym class="acronym">JIT</acronym>? <a href="#JIT-DECISION" class="id_link">#</a></h2></div></div></div><p>
3    <acronym class="acronym">JIT</acronym> compilation is beneficial primarily for long-running
4    CPU-bound queries. Frequently these will be analytical queries.  For short
5    queries the added overhead of performing <acronym class="acronym">JIT</acronym> compilation
6    will often be higher than the time it can save.
7   </p><p>
8    To determine whether <acronym class="acronym">JIT</acronym> compilation should be used,
9    the total estimated cost of a query (see
10    <a class="xref" href="planner-stats-details.html" title="Chapter 69. How the Planner Uses Statistics">Chapter 69</a> and
11    <a class="xref" href="runtime-config-query.html#RUNTIME-CONFIG-QUERY-CONSTANTS" title="19.7.2. Planner Cost Constants">Section 19.7.2</a>) is used.
12    The estimated cost of the query will be compared with the setting of <a class="xref" href="runtime-config-query.html#GUC-JIT-ABOVE-COST">jit_above_cost</a>. If the cost is higher,
13    <acronym class="acronym">JIT</acronym> compilation will be performed.
14    Two further decisions are then needed.
15    Firstly, if the estimated cost is more
16    than the setting of <a class="xref" href="runtime-config-query.html#GUC-JIT-INLINE-ABOVE-COST">jit_inline_above_cost</a>, short
17    functions and operators used in the query will be inlined.
18    Secondly, if the estimated cost is more than the setting of <a class="xref" href="runtime-config-query.html#GUC-JIT-OPTIMIZE-ABOVE-COST">jit_optimize_above_cost</a>, expensive optimizations are
19    applied to improve the generated code.
20    Each of these options increases the <acronym class="acronym">JIT</acronym> compilation
21    overhead, but can reduce query execution time considerably.
22   </p><p>
23    These cost-based decisions will be made at plan time, not execution
24    time. This means that when prepared statements are in use, and a generic
25    plan is used (see <a class="xref" href="sql-prepare.html" title="PREPARE"><span class="refentrytitle">PREPARE</span></a>), the values of the
26    configuration parameters in effect at prepare time control the decisions,
27    not the settings at execution time.
28   </p><div class="note"><h3 class="title">Note</h3><p>
29     If <a class="xref" href="runtime-config-query.html#GUC-JIT">jit</a> is set to <code class="literal">off</code>, or if no
30     <acronym class="acronym">JIT</acronym> implementation is available (for example because
31     the server was compiled without <code class="literal">--with-llvm</code>),
32     <acronym class="acronym">JIT</acronym> will not be performed, even if it would be
33     beneficial based on the above criteria.  Setting <a class="xref" href="runtime-config-query.html#GUC-JIT">jit</a>
34     to <code class="literal">off</code> has effects at both plan and execution time.
35    </p></div><p>
36    <a class="xref" href="sql-explain.html" title="EXPLAIN"><span class="refentrytitle">EXPLAIN</span></a> can be used to see whether
37    <acronym class="acronym">JIT</acronym> is used or not.  As an example, here is a query that
38    is not using <acronym class="acronym">JIT</acronym>:
39 </p><pre class="screen">
40 =# EXPLAIN ANALYZE SELECT SUM(relpages) FROM pg_class;
41                                                  QUERY PLAN
42 -------------------------------------------------------------------​------------------------------------------
43  Aggregate  (cost=16.27..16.29 rows=1 width=8) (actual time=0.303..0.303 rows=1.00 loops=1)
44    Buffers: shared hit=14
45    -&gt;  Seq Scan on pg_class  (cost=0.00..15.42 rows=342 width=4) (actual time=0.017..0.111 rows=356.00 loops=1)
46          Buffers: shared hit=14
47  Planning Time: 0.116 ms
48  Execution Time: 0.365 ms
49 </pre><p>
50    Given the cost of the plan, it is entirely reasonable that no
51    <acronym class="acronym">JIT</acronym> was used; the cost of <acronym class="acronym">JIT</acronym> would
52    have been bigger than the potential savings. Adjusting the cost limits
53    will lead to <acronym class="acronym">JIT</acronym> use:
54 </p><pre class="screen">
55 =# SET jit_above_cost = 10;
56 SET
57 =# EXPLAIN ANALYZE SELECT SUM(relpages) FROM pg_class;
58                                                  QUERY PLAN
59 -------------------------------------------------------------------​------------------------------------------
60  Aggregate  (cost=16.27..16.29 rows=1 width=8) (actual time=6.049..6.049 rows=1.00 loops=1)
61    Buffers: shared hit=14
62    -&gt;  Seq Scan on pg_class  (cost=0.00..15.42 rows=342 width=4) (actual time=0.019..0.052 rows=356.00 loops=1)
63          Buffers: shared hit=14
64  Planning Time: 0.133 ms
65  JIT:
66    Functions: 3
67    Options: Inlining false, Optimization false, Expressions true, Deforming true
68    Timing: Generation 1.259 ms (Deform 0.000 ms), Inlining 0.000 ms, Optimization 0.797 ms, Emission 5.048 ms, Total 7.104 ms
69  Execution Time: 7.416 ms
70 </pre><p>
71    As visible here, <acronym class="acronym">JIT</acronym> was used, but inlining and
72    expensive optimization were not. If <a class="xref" href="runtime-config-query.html#GUC-JIT-INLINE-ABOVE-COST">jit_inline_above_cost</a> or <a class="xref" href="runtime-config-query.html#GUC-JIT-OPTIMIZE-ABOVE-COST">jit_optimize_above_cost</a> were also lowered,
73    that would change.
74   </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="jit-reason.html" title="30.1. What Is JIT compilation?">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="jit.html" title="Chapter 30. Just-in-Time Compilation (JIT)">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="jit-configuration.html" title="30.3. Configuration">Next</a></td></tr><tr><td width="40%" align="left" valign="top">30.1. What Is <acronym class="acronym">JIT</acronym> compilation? </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 30.3. Configuration</td></tr></table></div></body></html>