2 30.1. What Is JIT compilation? #
4 30.1.1. JIT Accelerated Operations
8 Just-in-Time (JIT) compilation is the process of turning some form of
9 interpreted program evaluation into a native program, and doing so at
10 run time. For example, instead of using general-purpose code that can
11 evaluate arbitrary SQL expressions to evaluate a particular SQL
12 predicate like WHERE a.col = 3, it is possible to generate a function
13 that is specific to that expression and can be natively executed by the
14 CPU, yielding a speedup.
16 PostgreSQL has builtin support to perform JIT compilation using LLVM
17 when PostgreSQL is built with --with-llvm.
19 See src/backend/jit/README for further details.
21 30.1.1. JIT Accelerated Operations #
23 Currently PostgreSQL's JIT implementation has support for accelerating
24 expression evaluation and tuple deforming. Several other operations
25 could be accelerated in the future.
27 Expression evaluation is used to evaluate WHERE clauses, target lists,
28 aggregates and projections. It can be accelerated by generating code
29 specific to each case.
31 Tuple deforming is the process of transforming an on-disk tuple (see
32 Section 66.6.1) into its in-memory representation. It can be
33 accelerated by creating a function specific to the table layout and the
34 number of columns to be extracted.
38 PostgreSQL is very extensible and allows new data types, functions,
39 operators and other database objects to be defined; see Chapter 36. In
40 fact the built-in objects are implemented using nearly the same
41 mechanisms. This extensibility implies some overhead, for example due
42 to function calls (see Section 36.3). To reduce that overhead, JIT
43 compilation can inline the bodies of small functions into the
44 expressions using them. That allows a significant percentage of the
45 overhead to be optimized away.
47 30.1.3. Optimization #
49 LLVM has support for optimizing generated code. Some of the
50 optimizations are cheap enough to be performed whenever JIT is used,
51 while others are only beneficial for longer-running queries. See
52 https://llvm.org/docs/Passes.html#transform-passes for more details