Postgresql 中文操作指南

32.1. What Is JIT compilation? #

即时编译 (JIT) 是将某种形式的解释程序评估转换为本机程序并使其在运行时执行的过程。例如,与其使用通用的代码来评估任意 SQL 表达式以评估 WHERE a.col = 3 这样的特定 SQL 谓词,可以生成专门针对该表达式的函数,然后可以由 CPU 原生执行,从而提高速度。

Just-in-Time (JIT) compilation is the process of turning some form of interpreted program evaluation into a native program, and doing so at run time. For example, instead of using general-purpose code that can evaluate arbitrary SQL expressions to evaluate a particular SQL predicate like WHERE a.col = 3, it is possible to generate a function that is specific to that expression and can be natively executed by the CPU, yielding a speedup.

当使用 —​with-llvm 构建 PostgreSQL 时,PostgreSQL 具有内置支持来使用 LLVM 执行 JIT 编译。

PostgreSQL has builtin support to perform JIT compilation using LLVM when PostgreSQL is built with —​with-llvm.

有关更多详细信息,请参见 src/backend/jit/README

See src/backend/jit/README for further details.

32.1.1. JIT Accelerated Operations #

当前 PostgreSQL 的 JIT 实施支持加速表达式评估和元组变形。将来可能会加速其他几种操作。

Currently PostgreSQL’s JIT implementation has support for accelerating expression evaluation and tuple deforming. Several other operations could be accelerated in the future.

表达式评估用于评估 WHERE 子句、目标列表、聚合和投影。它可以通过针对每种情况生成特定的代码来加速。

Expression evaluation is used to evaluate WHERE clauses, target lists, aggregates and projections. It can be accelerated by generating code specific to each case.

元组变形是将磁盘元组(参见 Section 73.6.1)转换成其内存中表示的过程。通过创建特定于表布局和要提取的列数的函数,可以加速它。

Tuple deforming is the process of transforming an on-disk tuple (see Section 73.6.1) into its in-memory representation. It can be accelerated by creating a function specific to the table layout and the number of columns to be extracted.

32.1.2. Inlining #

PostgreSQL 很容易扩展,并且允许定义新的数据类型、函数、运算符和其他数据库对象;参见 Chapter 38。事实上,内置对象几乎使用相同的机制实现。这种可扩展性意味着一些开销,例如由于函数调用(参见 Section 38.3)。为了减少这种开销,JIT 编译可以将小函数的主体内联到使用它们的表达式中。这允许通过优化消除大量的开销。

PostgreSQL is very extensible and allows new data types, functions, operators and other database objects to be defined; see Chapter 38. In fact the built-in objects are implemented using nearly the same mechanisms. This extensibility implies some overhead, for example due to function calls (see Section 38.3). To reduce that overhead, JIT compilation can inline the bodies of small functions into the expressions using them. That allows a significant percentage of the overhead to be optimized away.

32.1.3. Optimization #

LLVM 支持针对生成代码进行优化。一些优化足够廉价,可以在每次使用 JIT 时执行,而另一些优化只对长期运行的查询有益。有关优化的更多详细信息,请参见 https://llvm.org/docs/Passes.html#transform-passes

LLVM has support for optimizing generated code. Some of the optimizations are cheap enough to be performed whenever JIT is used, while others are only beneficial for longer-running queries. See https://llvm.org/docs/Passes.html#transform-passes for more details about optimizations.