关于 JIT
PostgreSQL 11 版本的一个重量级新特性是引入了 JIT (Just-in-Time) 编译来加速SQL中的表达式计算效率。
JIT 表达式的编译使用LLVM项目编译器的架构来提升在WHERE条件、指定列表、聚合以及一些内部操作表达式的编译执行。
使用 JIT 必须在首先编译安装 LLVM ,之后编译安装 PostgreSQL 时设置 --with-llvm
选项,本文主要包括两部分,如下:
- CentOS7 编译安装 LLVM。
- CentOS7 编译安装PostgreSQL 11,启用并演示 JIT。
JIT 使用场景
JIT 常用于CPU密集型SQL(分析统计SQL),执行很快的SQL使用JIT由于产生一定开销,反而可能引起性能下降。
手册 Release说明
Add Just-in-Time (JIT) compilation of some parts of query plans to improve execution speed (Andres Freund)
This feature requires LLVM to be available. It is not currently enabled by default, even in builds that support it.
安装环境
操作系统: CentOS Linux release 7.4.1708 (Core)
硬件环境: 8核4G/80G 的云主机
LLVM安装前提条件
LLVM 安装依赖较多,如下:
The minimum required version of LLVM is currently 3.9 --本实验使用 LLVM 5.0.2
CMake. Version 3.4.3 is the minimum required. --本实验使用 Cmake 3.12.3
Python 2.7 or newer is required --本实验使用 Python 2.7.9
GCC version must be at least 4.8! --本实验使用 gcc 4.8.5
安装 Cmake 3.12.3
下载并编译安装 cmake 3.12.3,如下:
# wget -c https://cmake.org/files/v3.12/cmake-3.12.3.tar.gz
# tar xvf cmake-3.12.3.tar.gz
# cd cmake-3.12.3
# ./bootstrap
# make -j 4
# make install
安装Python 2.7.9
下载并编译安装 python 2.7.9,如下:
# wget -c https://www.python.org/downloads/release/python-279/
# tar jxvf Python-2.7.9.tgz
# cd Python-2.7.9
# ./configure
# make
# make install
安装 LLVM 5.0.2
LLVM 的安装步骤较繁琐,并且编译安装过程时间较长,性能好的机器能减少编译时间,注意操作系统需启用 swap
,否则编译过程中会报错,本人开始编译安装时没有启用 swap
,折腾了很久。
下载LLVM安装介质
在 LLVM官网下载安装介质,如下:
llvm-5.0.2.src.tar.xz
cfe-5.0.2.src.tar.xz
clang-tools-extra-5.0.2.src.tar.xz
compiler-rt-5.0.2.src.tar.xz
libcxx-5.0.2.src.tar.xz
libcxxabi-5.0.2.src.tar.xz
libunwind-5.0.2.src.tar.xz
编译安装LLVM
解压 llvm-5.0.2.src.tar.xz
# cd /opt/soft_bak/
# tar xvf llvm-5.0.2.src.tar.xz
# mv llvm-5.0.2.src llvm
解压安装包并重命名,目录结构对应如下,如下:
安装包 | 安装目录 |
---|---|
llvm-5.0.2.src.tar.xz | /opt/soft_bak/llvm |
cfe-5.0.2.src.tar.xz | /opt/soft_bak/tools/clang |
clang-tools-extra-5.0.2.src.tar.xz | /opt/soft_bak/tools/clang/tools/extra |
compiler-rt-5.0.2.src.tar.xz | /opt/soft_bak/projects/compiler-rt |
libcxx-5.0.2.src.tar.xz | /opt/soft_bak/projects/libcxx |
libcxxabi-5.0.2.src.tar.xz | /opt/soft_bak/projects/libcxxabi |
libunwind-5.0.2.src.tar.xz | /opt/soft_bak/projects/libunwind |
LLVM 官网的其它安装包非必须,可根据情况选择。
编译安装 LLVM,如下:
# mkdir -p /opt/soft_bak/llvm_build/
# cd /opt/soft_bak/llvm_build/
# cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local/llvm -DCLANG_DEFAULT_CXX_STDLIB=libc++ -DCMAKE_BUILD_TYPE="Release" /opt/soft_bak/llvm
# make -j 4
# make install
设置环境变量,如下:
export PATH=$PATH:/usr/local/llvm/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/llvm/lib
查看版本
[root@pghost7 ~]# llvm-cat --version
LLVM (http://llvm.org/):
LLVM version 5.0.2
Optimized build.
Default target: x86_64-unknown-linux-gnu
Host CPU: broadwell
[root@pghost7 ~]# clang --version
clang version 5.0.2 (tags/RELEASE_502/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/llvm/bin
至此 LLVM 已安装成功。
PostgreSQL 11 安装
安装相关包,如下:
# yum -y install gcc readline readline-devel zlib zlib-devel python-devel
下载PostgreSQL 11 并编译安装,编译时指定 --with-llvm
选项, 如下:
# wget -c https://ftp.postgresql.org/pub/source/v11.0/postgresql-11.0.tar.bz2
# tar xvf postgresql-11.0.tar.bz2
#./configure --prefix=/opt/pgsql_11.0 --with-wal-blocksize=16 -with-pgport=1930 --with-llvm LLVM_CONFIG='/usr/local/llvm/bin/llvm-config'
# make wolrd -j 4
# make install-world
设置 .bash_profile
,如下:
export PGPORT=1930
export PGUSER=postgres
export PGDATA=/database/pg11/pg_root
export LANG=en_US.utf8
export PGHOME=/opt/pgsql_11.0
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib
export DATE=`date +"%Y%m%d%H%M"`
export PATH=$PGHOME/bin:$PATH:.
export MANPATH=$PGHOME/share/man:$MANPATH
alias rm='rm -i'
alias ll='ls -lh'
使用 initdb
初始化数据库,如下:
[pg11@pghost7 pg_root]$ initdb -D /database/pg11/pg_root -E=UTF8 --locale=C -U postgres -W
postgresql.conf 设置以下 JIT 配置参数,其它参数按需配置,这里不贴出,如下:
# - Other Defaults -
#dynamic_library_path = '$libdir'
jit = on # allow JIT compilation
jit_provider = 'llvmjit' # JIT implementation to use
设置 pg_hba.conf,如下:
host all all 0.0.0.0/0 md5
之后启动数据库,如下:
[pg11@pghost7 pg_root]$ pg_ctl start
waiting for server to start....2018-10-31 11:13:26.154 CST [19742] LOG: listening on IPv4 address "0.0.0.0", port 1930
2018-10-31 11:13:26.154 CST [19742] LOG: listening on IPv6 address "::", port 1930
2018-10-31 11:13:26.159 CST [19742] LOG: listening on Unix socket "/tmp/.s.PGSQL.1930"
2018-10-31 11:13:26.185 CST [19742] LOG: redirecting log output to logging collector process
2018-10-31 11:13:26.185 CST [19742] HINT: Future log output will appear in directory "log".
done
server started
JIT 测试
以下大致演示 JIT,测试样例很简单,不做充分的性能测试,有兴趣的朋友可以做 TPC-H 性能测试。
测试数据准备
创建一张5千万的数据表,如下:
CREATE TABLE t_llvm1(a int4, b int4, info text, ctime timestamp(6) without time zone);
INSERT INTO t_llvm1 (a,b,info,ctime) SELECT n,n*2,n||'_llvm1',clock_timestamp() FROM generate_series(1,50000000) n;
查看 JIT 相关参数
postgres=# SELECT name,setting FROM pg_settings WHERE name LIKE 'jit%';
name | setting
-------------------------+---------
jit | on
jit_above_cost | 100000
jit_debugging_support | off
jit_dump_bitcode | off
jit_expressions | on
jit_inline_above_cost | 500000
jit_optimize_above_cost | 500000
jit_profiling_support | off
jit_provider | llvmjit
jit_tuple_deforming | on
(10 rows)
开启 JIT
开启 JIT,执行计划如下:
postgres=# SET JIT = on;
SET
postgres=# EXPLAIN ANALYZE SELECT count(*),sum(a) FROM t_llvm1 WHERE (a+b) > 10;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=576982.30..576982.31 rows=1 width=16) (actual time=2148.607..2148.608 rows=1 loops=1)
-> Gather (cost=576981.86..576982.28 rows=4 width=16) (actual time=2148.457..2153.185 rows=5 loops=1)
Workers Planned: 4
Workers Launched: 4
-> Partial Aggregate (cost=575981.86..575981.88 rows=1 width=16) (actual time=2134.919..2134.919 rows=1 loops=5)
-> Parallel Seq Scan on t_llvm1 (cost=0.00..555148.48 rows=4166677 width=4) (actual time=105.597..1516.253 rows=9999999 loops=5)
Filter: ((a + b) > 10)
Rows Removed by Filter: 1
Planning Time: 0.078 ms
JIT:
Functions: 28
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 5.842 ms, Inlining 226.589 ms, Optimization 191.071 ms, Emission 107.027 ms, Total 530.529 ms
Execution Time: 2154.870 ms
(14 rows)
从以上看出执行计划中包含 JIT 编译信息,执行时间为 2154 ms 左右。
关闭 JIT
关闭 JIT,查看执行计划和扫行时间,如下:
postgres=# SET JIT = off;
SET
postgres=# EXPLAIN ANALYZE SELECT count(*),sum(a) FROM t_llvm1 WHERE (a+b) > 10;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=576982.30..576982.31 rows=1 width=16) (actual time=2382.035..2382.035 rows=1 loops=1)
-> Gather (cost=576981.86..576982.28 rows=4 width=16) (actual time=2381.939..2385.143 rows=5 loops=1)
Workers Planned: 4
Workers Launched: 4
-> Partial Aggregate (cost=575981.86..575981.88 rows=1 width=16) (actual time=2371.143..2371.143 rows=1 loops=5)
-> Parallel Seq Scan on t_llvm1 (cost=0.00..555148.48 rows=4166677 width=4) (actual time=0.560..1600.125 rows=9999999 loops=5)
Filter: ((a + b) > 10)
Rows Removed by Filter: 1
Planning Time: 0.083 ms
Execution Time: 2385.209 ms
(10 rows)
从以上看出执行计划中没有包含 JIT 信息,执行时间为 2385 ms 左右,开启JIT性能提升了9.7% 左右。
参考
- Chapter 32. Just-in-Time Compilation (JIT)
- The LLVM Compiler Infrastructure
- PostgreSQL 已包含对 LLVM JIT 支持的提交性能将飙升
- How to compile PostgreSQL 11 with support for JIT compilation on RHEL/CentOS 7
- PostgreSQL 11 and Just In Time Compilation of Queries
- Speeding up query execution in PostgreSQL using LLVM JIT compiler
- PostgreSQL 11.0 正式版更新版本发布说明
新书推荐
最后推荐和张文升共同编写的《PostgreSQL实战》,本书基于PostgreSQL 10 编写,共18章,重点介绍SQL高级特性、并行查询、分区表、物理复制、逻辑复制、备份恢复、高可用、性能优化、PostGIS等,涵盖大量实战用例!