バージョンごとのドキュメント一覧

32.2. どんなときにJITを使うべきか? #

<title>When to <acronym>JIT</acronym>?</title>

<acronym>JIT</acronym> compilation is beneficial primarily for long-running CPU-bound queries. Frequently these will be analytical queries. For short queries the added overhead of performing <acronym>JIT</acronym> compilation will often be higher than the time it can save. JITコンパイルは、主に長時間実行するCPUバウンドの問い合わせに有益です。 短い問い合わせでは、JITコンパイルを行うことにより加わるオーバーヘッドはしばしばそれによって短縮できる時間よりも大きくなるでしょう。

To determine whether <acronym>JIT</acronym> compilation should be used, the total estimated cost of a query (see <xref linkend="planner-stats-details"/> and <xref linkend="runtime-config-query-constants"/>) is used. The estimated cost of the query will be compared with the setting of <xref linkend="guc-jit-above-cost"/>. If the cost is higher, <acronym>JIT</acronym> compilation will be performed. Two further decisions are then needed. Firstly, if the estimated cost is more than the setting of <xref linkend="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 <xref linkend="guc-jit-optimize-above-cost"/>, expensive optimizations are applied to improve the generated code. Each of these options increases the <acronym>JIT</acronym> compilation overhead, but can reduce query execution time considerably. JITコンパイルを使うべきかどうかを決めるために、問い合わせの合計推定コスト(第76章20.7.2を参照)が使用されます。 問い合わせの推定コストはjit_above_costの設定と比較されます。 もしもそのコストが大きければ、JITコンパイルが実行されます。 さらなる二つの決定が必要になります。 まず、推定コストがjit_inline_above_costの設定よりも大きければ、問い合わせ中で使用される短い関数と演算子がインライン展開されます。 次に、推定コストがjit_optimize_above_costの設定よりも大きければ、生成コードを改善するために、高価な最適化が適用されます。 これらのオプションはJITコンパイルのオーバーヘッドを大きくしますが、かなりクエリの実行時間を短縮します。

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 <xref linkend="sql-prepare"/>), the values of the configuration parameters in effect at prepare time control the decisions, not the settings at execution time. これらのコストに基づく決定は実行時ではなく、プラン時に行われます。 このことは、準備された文が使われ、汎用プラン(PREPARE参照)が用いられるときには、実行時ではなく、準備時に参照される設定パラメータの値が決定を左右することを意味します。

注記

If <xref linkend="guc-jit"/> is set to <literal>off</literal>, or if no <acronym>JIT</acronym> implementation is available (for example because the server was compiled without <literal>&#45;-with-llvm</literal>), <acronym>JIT</acronym> will not be performed, even if it would be beneficial based on the above criteria. Setting <xref linkend="guc-jit"/> to <literal>off</literal> has effects at both plan and execution time. jitoffか、JIT実装が適用外(たとえばサーバが--with-llvm付きでコンパイルされていない)場合は、たとえ上記の基準からは有益であったとしてもJITは実行されません。 jitoffにすると、プラン時と実行時の両方に影響を与えます。

<xref linkend="sql-explain"/> can be used to see whether <acronym>JIT</acronym> is used or not. As an example, here is a query that is not using <acronym>JIT</acronym>: EXPLAINを使ってJITが使われているかどうかを確認できます。 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 <acronym>JIT</acronym> was used; the cost of <acronym>JIT</acronym> would have been bigger than the potential savings. Adjusting the cost limits will lead to <acronym>JIT</acronym> use: プランに与えられたコストによれば、JITが使われないのは完全に合理的です。 JITのコストは潜在的な節約よりも大きいのです。 コスト上限を調整すると、JITが使われるようになります。

=# 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, <acronym>JIT</acronym> was used, but inlining and expensive optimization were not. If <xref linkend="guc-jit-inline-above-cost"/> or <xref linkend="guc-jit-optimize-above-cost"/> were also lowered, that would change. これを見るとわかるように、JITは使われていますが、インライン展開と高価な最適化は行われていません。 加えてjit_inline_above_costあるいはjit_optimize_above_costを小さくすれば、これは変わることでしょう。