Skip to content

Commit

Permalink
postgresql: support JIT compilation for version >= 11
Browse files Browse the repository at this point in the history
PostgreSQL (from 11 onwards AFAICT) supports JIT compilation of
(frequently used) queries. This should provide a speedup to some queries
where operations such as comparing rows is a bottleneck.

For JIT support we have to build PostgreSQL with with the --with-llvm
config flag and pass both clang and llvm into the build environment.
PostPostgreSQL then calls llvm-config to obtain the required compiler
flags.
  • Loading branch information
andir committed May 29, 2021
1 parent e44cffc commit abac45e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
18 changes: 18 additions & 0 deletions nixos/tests/postgresql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ let
CREATE TABLE xmltest ( doc xml );
INSERT INTO xmltest (doc) VALUES ('<test>ok</test>'); -- check if libxml2 enabled
'';

jit-test-sql = pkgs.writeText "postgresql-jit-test" ''
SET jit_above_cost = 1;
SET jit = 1;
EXPLAIN ANALYZE SELECT CONCAT('jit result = ', SUM(id)) FROM sth;
SELECT CONCAT('jit result = ', SUM(id)) from sth;
'';

make-postgresql-test = postgresql-name: postgresql-package: backup-all: makeTest {
name = postgresql-name;
meta = with pkgs.lib.maintainers; {
Expand Down Expand Up @@ -76,6 +84,16 @@ let
"stat -c '%a' /var/backup/postgresql/${backupName}.sql.gz | grep 600",
)
${optionalString (versionAtLeast postgresql-package.version "11") ''
with subtest("Postgresql JIT should work on postgresql >= 11"):
output = machine.succeed(
"cat ${jit-test-sql} | sudo -u postgres psql"
)
assert "JIT:" in output
assert "jit result = 5" in output
''}
with subtest("Initdb works"):
machine.succeed("sudo -u postgres initdb -D /tmp/testpostgres2")
Expand Down
11 changes: 9 additions & 2 deletions pkgs/servers/sql/postgresql/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ let
, enableSystemd ? (lib.versionAtLeast version "9.6" && !stdenv.isDarwin)
, gssSupport ? with stdenv.hostPlatform; !isWindows && !isStatic, libkrb5

# enable JIT support
, supportJIT ? (lib.versionAtLeast version "11")
, llvm, clang


# for postgreql.pkgs
, this, self, newScope, buildEnv
Expand Down Expand Up @@ -43,7 +47,9 @@ let
++ lib.optionals gssSupport [ libkrb5 ]
++ lib.optionals (!stdenv.isDarwin) [ libossp_uuid ];

nativeBuildInputs = [ makeWrapper ] ++ lib.optionals icuEnabled [ pkg-config ];
nativeBuildInputs = [ makeWrapper ]
++ lib.optionals icuEnabled [ pkg-config ]
++ lib.optionals supportJIT [ llvm clang ];

enableParallelBuilding = !stdenv.isDarwin;

Expand All @@ -66,7 +72,8 @@ let
(lib.optionalString enableSystemd "--with-systemd")
(if stdenv.isDarwin then "--with-uuid=e2fs" else "--with-ossp-uuid")
] ++ lib.optionals icuEnabled [ "--with-icu" ]
++ lib.optionals gssSupport [ "--with-gssapi" ];
++ lib.optionals gssSupport [ "--with-gssapi" ]
++ lib.optionals supportJIT [ "--with-llvm" ];

patches =
[ (if atLeast "9.4" then ./patches/disable-resolve_symlinks-94.patch else ./patches/disable-resolve_symlinks.patch)
Expand Down

0 comments on commit abac45e

Please sign in to comment.