Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgresql: support JIT #150801

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nixos/modules/services/databases/postgresql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ in
extraPlugins = mkOption {
type = types.listOf types.path;
default = [];
example = literalExpression "with pkgs.postgresql_11.pkgs; [ postgis pg_repack ]";
example = literalExpression "with config.services.postgresql.package.pkgs; [ postgis pg_repack ]";
description = ''
List of PostgreSQL plugins. PostgreSQL version for each plugin should
match version for <literal>services.postgresql.package</literal> value.
Expand Down
297 changes: 149 additions & 148 deletions pkgs/servers/sql/postgresql/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,166 +9,172 @@ let
# This is important to obtain a version of `libpq` that does not depend on systemd.
, enableSystemd ? (lib.versionAtLeast version "9.6" && !stdenv.isDarwin)
, gssSupport ? with stdenv.hostPlatform; !isWindows && !isStatic, libkrb5

# Disable to avoid depending on LLVM and Clang by default.
, jitSupport ? false, llvm, clang

# for postgreql.pkgs
, this, self, newScope, buildEnv
, self, newScope, buildEnv

# source specification
, version, sha256, psqlSchema,

# for tests
nixosTests, thisAttr
}:

let
atLeast = lib.versionAtLeast version;
icuEnabled = atLeast "10";
lz4Enabled = atLeast "14";
in
assert jitSupport -> atLeast "11";

let this = stdenv.mkDerivation rec {
pname = "postgresql";
inherit version;

src = fetchurl {
url = "mirror://postgresql/source/v${version}/${pname}-${version}.tar.bz2";
inherit sha256;
};

hardeningEnable = lib.optionals (!stdenv.cc.isClang) [ "pie" ];

outputs = [ "out" "lib" "doc" "man" ];
setOutputFlags = false; # $out retains configureFlags :-/

buildInputs =
[ zlib readline openssl libxml2 ]
++ lib.optionals icuEnabled [ icu ]
++ lib.optionals lz4Enabled [ lz4 ]
++ lib.optionals enableSystemd [ systemd ]
++ lib.optionals gssSupport [ libkrb5 ]
++ lib.optionals (!stdenv.isDarwin) [ libossp_uuid ]
++ lib.optionals jitSupport [ llvm clang ];

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

enableParallelBuilding = !stdenv.isDarwin;

separateDebugInfo = true;

buildFlags = [ "world" ];

NIX_CFLAGS_COMPILE = "-I${libxml2.dev}/include/libxml2";

# Otherwise it retains a reference to compiler and fails; see #44767. TODO: better.
preConfigure = "CC=${stdenv.cc.targetPrefix}cc";

configureFlags = [
"--with-openssl"
"--with-libxml"
"--sysconfdir=/etc"
"--libdir=$(lib)/lib"
"--with-system-tzdata=${tzdata}/share/zoneinfo"
"--enable-debug"
(lib.optionalString enableSystemd "--with-systemd")
(if stdenv.isDarwin then "--with-uuid=e2fs" else "--with-ossp-uuid")
] ++ lib.optionals icuEnabled [ "--with-icu" ]
++ lib.optionals lz4Enabled [ "--with-lz4" ]
++ lib.optionals gssSupport [ "--with-gssapi" ]
++ lib.optionals stdenv.hostPlatform.isRiscV [ "--disable-spinlocks" ]
++ lib.optionals jitSupport [ "--with-llvm" ];

patches =
[ (if atLeast "9.4" then ./patches/disable-resolve_symlinks-94.patch else ./patches/disable-resolve_symlinks.patch)
(if atLeast "9.6" then ./patches/less-is-more-96.patch else ./patches/less-is-more.patch)
(if atLeast "9.6" then ./patches/hardcode-pgxs-path-96.patch else ./patches/hardcode-pgxs-path.patch)
./patches/specify_pkglibdir_at_runtime.patch
./patches/findstring.patch
]
++ lib.optional stdenv.isLinux (if atLeast "13" then ./patches/socketdir-in-run-13.patch else ./patches/socketdir-in-run.patch);

installTargets = [ "install-world" ];

LC_ALL = "C";

postConfigure =
let path = if atLeast "9.6" then "src/common/config_info.c" else "src/bin/pg_config/pg_config.c"; in
''
# Hardcode the path to pgxs so pg_config returns the path in $out
substituteInPlace "${path}" --replace HARDCODED_PGXS_PATH $out/lib
'';

postInstall =
''
moveToOutput "lib/pgxs" "$out" # looks strange, but not deleting it
moveToOutput "lib/libpgcommon*.a" "$out"
moveToOutput "lib/libpgport*.a" "$out"
moveToOutput "lib/libecpg*" "$out"

# Prevent a retained dependency on gcc-wrapper.
substituteInPlace "$out/lib/pgxs/src/Makefile.global" --replace ${stdenv.cc}/bin/ld ld

if [ -z "''${dontDisableStatic:-}" ]; then
# Remove static libraries in case dynamic are available.
for i in $out/lib/*.a $lib/lib/*.a; do
name="$(basename "$i")"
ext="${stdenv.hostPlatform.extensions.sharedLibrary}"
if [ -e "$lib/lib/''${name%.a}$ext" ] || [ -e "''${i%.a}$ext" ]; then
rm "$i"
fi
done
fi
'';

in stdenv.mkDerivation rec {
pname = "postgresql";
inherit version;

src = fetchurl {
url = "mirror://postgresql/source/v${version}/${pname}-${version}.tar.bz2";
inherit sha256;
};

hardeningEnable = lib.optionals (!stdenv.cc.isClang) [ "pie" ];

outputs = [ "out" "lib" "doc" "man" ];
setOutputFlags = false; # $out retains configureFlags :-/

buildInputs =
[ zlib readline openssl libxml2 ]
++ lib.optionals icuEnabled [ icu ]
++ lib.optionals lz4Enabled [ lz4 ]
++ lib.optionals enableSystemd [ systemd ]
++ lib.optionals gssSupport [ libkrb5 ]
++ lib.optionals (!stdenv.isDarwin) [ libossp_uuid ];

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

enableParallelBuilding = !stdenv.isDarwin;

separateDebugInfo = true;

buildFlags = [ "world" ];

NIX_CFLAGS_COMPILE = "-I${libxml2.dev}/include/libxml2";

# Otherwise it retains a reference to compiler and fails; see #44767. TODO: better.
preConfigure = "CC=${stdenv.cc.targetPrefix}cc";

configureFlags = [
"--with-openssl"
"--with-libxml"
"--sysconfdir=/etc"
"--libdir=$(lib)/lib"
"--with-system-tzdata=${tzdata}/share/zoneinfo"
"--enable-debug"
(lib.optionalString enableSystemd "--with-systemd")
(if stdenv.isDarwin then "--with-uuid=e2fs" else "--with-ossp-uuid")
] ++ lib.optionals icuEnabled [ "--with-icu" ]
++ lib.optionals lz4Enabled [ "--with-lz4" ]
++ lib.optionals gssSupport [ "--with-gssapi" ]
++ lib.optionals stdenv.hostPlatform.isRiscV [ "--disable-spinlocks" ];

patches =
[ (if atLeast "9.4" then ./patches/disable-resolve_symlinks-94.patch else ./patches/disable-resolve_symlinks.patch)
(if atLeast "9.6" then ./patches/less-is-more-96.patch else ./patches/less-is-more.patch)
(if atLeast "9.6" then ./patches/hardcode-pgxs-path-96.patch else ./patches/hardcode-pgxs-path.patch)
./patches/specify_pkglibdir_at_runtime.patch
./patches/findstring.patch
]
++ lib.optional stdenv.isLinux (if atLeast "13" then ./patches/socketdir-in-run-13.patch else ./patches/socketdir-in-run.patch);

installTargets = [ "install-world" ];

LC_ALL = "C";

postConfigure =
let path = if atLeast "9.6" then "src/common/config_info.c" else "src/bin/pg_config/pg_config.c"; in
postFixup = lib.optionalString (!stdenv.isDarwin && stdenv.hostPlatform.libc == "glibc")
''
# Hardcode the path to pgxs so pg_config returns the path in $out
substituteInPlace "${path}" --replace HARDCODED_PGXS_PATH $out/lib
# initdb needs access to "locale" command from glibc.
wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin
'';

postInstall =
''
moveToOutput "lib/pgxs" "$out" # looks strange, but not deleting it
moveToOutput "lib/libpgcommon*.a" "$out"
moveToOutput "lib/libpgport*.a" "$out"
moveToOutput "lib/libecpg*" "$out"

# Prevent a retained dependency on gcc-wrapper.
substituteInPlace "$out/lib/pgxs/src/Makefile.global" --replace ${stdenv.cc}/bin/ld ld

if [ -z "''${dontDisableStatic:-}" ]; then
# Remove static libraries in case dynamic are available.
for i in $out/lib/*.a $lib/lib/*.a; do
name="$(basename "$i")"
ext="${stdenv.hostPlatform.extensions.sharedLibrary}"
if [ -e "$lib/lib/''${name%.a}$ext" ] || [ -e "''${i%.a}$ext" ]; then
rm "$i"
fi
done
fi
'';

postFixup = lib.optionalString (!stdenv.isDarwin && stdenv.hostPlatform.libc == "glibc")
''
# initdb needs access to "locale" command from glibc.
wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin
'';

doCheck = !stdenv.isDarwin;
# autodetection doesn't seem to able to find this, but it's there.
checkTarget = "check";

preCheck =
# On musl, comment skip the following tests, because they break due to
# ! ERROR: could not load library "/build/postgresql-11.5/tmp_install/nix/store/...-postgresql-11.5-lib/lib/libpqwalreceiver.so": Error loading shared library libpq.so.5: No such file or directory (needed by /build/postgresql-11.5/tmp_install/nix/store/...-postgresql-11.5-lib/lib/libpqwalreceiver.so)
# See also here:
# https://git.alpinelinux.org/aports/tree/main/postgresql/disable-broken-tests.patch?id=6d7d32c12e073a57a9e5946e55f4c1fbb68bd442
if stdenv.hostPlatform.isMusl then ''
substituteInPlace src/test/regress/parallel_schedule \
--replace "subscription" "" \
--replace "object_address" ""
'' else null;

doInstallCheck = false; # needs a running daemon?

disallowedReferences = [ stdenv.cc ];

passthru = {
inherit readline psqlSchema;

pkgs = let
scope = { postgresql = this; };
newSelf = self // scope;
newSuper = { callPackage = newScope (scope // this.pkgs); };
in import ./packages.nix newSelf newSuper;

withPackages = postgresqlWithPackages {
inherit makeWrapper buildEnv;
postgresql = this;
}
this.pkgs;

tests.postgresql = nixosTests.postgresql-wal-receiver.${thisAttr};
};

meta = with lib; {
homepage = "https://www.postgresql.org";
description = "A powerful, open source object-relational database system";
license = licenses.postgresql;
maintainers = with maintainers; [ thoughtpolice danbst globin marsam ivan ];
platforms = platforms.unix;
knownVulnerabilities = optional (!atLeast "9.4")
"PostgreSQL versions older than 9.4 are not maintained anymore!";
};
};
doCheck = !stdenv.isDarwin;
# autodetection doesn't seem to able to find this, but it's there.
checkTarget = "check";

preCheck =
# On musl, comment skip the following tests, because they break due to
# ! ERROR: could not load library "/build/postgresql-11.5/tmp_install/nix/store/...-postgresql-11.5-lib/lib/libpqwalreceiver.so": Error loading shared library libpq.so.5: No such file or directory (needed by /build/postgresql-11.5/tmp_install/nix/store/...-postgresql-11.5-lib/lib/libpqwalreceiver.so)
# See also here:
# https://git.alpinelinux.org/aports/tree/main/postgresql/disable-broken-tests.patch?id=6d7d32c12e073a57a9e5946e55f4c1fbb68bd442
if stdenv.hostPlatform.isMusl then ''
substituteInPlace src/test/regress/parallel_schedule \
--replace "subscription" "" \
--replace "object_address" ""
'' else null;

doInstallCheck = false; # needs a running daemon?

disallowedReferences = [ stdenv.cc ];

passthru = {
inherit readline psqlSchema;

pkgs = let
scope = { postgresql = this; };
newSelf = self // scope;
newSuper = { callPackage = newScope (scope // this.pkgs); };
in import ./packages.nix newSelf newSuper;

withPackages = postgresqlWithPackages {
inherit makeWrapper buildEnv;
postgresql = this;
}
this.pkgs;

tests.postgresql = nixosTests.postgresql-wal-receiver.${thisAttr};
};

meta = with lib; {
homepage = "https://www.postgresql.org";
description = "A powerful, open source object-relational database system";
license = licenses.postgresql;
maintainers = with maintainers; [ thoughtpolice danbst globin marsam ivan ];
platforms = platforms.unix;
knownVulnerabilities = optional (!atLeast "9.4")
"PostgreSQL versions older than 9.4 are not maintained anymore!";
};
}; in this;

postgresqlWithPackages = { postgresql, makeWrapper, buildEnv }: pkgs: f: buildEnv {
name = "postgresql-and-plugins-${postgresql.version}";
Expand Down Expand Up @@ -205,7 +211,6 @@ in self: {
version = "10.19";
psqlSchema = "10.0"; # should be 10, but changing it is invasive
sha256 = "sha256-brgwtCi2DoSuh+IENrzmecTZ0CAr567A5BsMZ9kTQjk=";
this = self.postgresql_10;
thisAttr = "postgresql_10";
inherit self;
icu = self.icu67;
Expand All @@ -215,7 +220,6 @@ in self: {
version = "11.14";
psqlSchema = "11.1"; # should be 11, but changing it is invasive
sha256 = "sha256-llx/S+lvtk+VgYUsWMTwXDgS1K2CPA8+K9/nd8Fi+Zk=";
this = self.postgresql_11;
thisAttr = "postgresql_11";
inherit self;
};
Expand All @@ -224,7 +228,6 @@ in self: {
version = "12.9";
psqlSchema = "12";
sha256 = "sha256-if2i3jPtBKmFSOQ/PuXxW4gr4XUF1jH+DdGlQKK1bc4=";
this = self.postgresql_12;
thisAttr = "postgresql_12";
inherit self;
};
Expand All @@ -233,7 +236,6 @@ in self: {
version = "13.5";
psqlSchema = "13";
sha256 = "sha256-m4EGelXtuqvEGKrO9FfdhHdkKCdJlWCwBhWm6mwT9rM=";
this = self.postgresql_13;
thisAttr = "postgresql_13";
inherit self;
};
Expand All @@ -242,7 +244,6 @@ in self: {
version = "14.1";
psqlSchema = "14";
sha256 = "sha256-TTwQHqeuOJgvBr3HN1i1Nyf7ZALs2TggBvpezHwspB8=";
this = self.postgresql_14;
thisAttr = "postgresql_14";
inherit self;
};
Expand Down
2 changes: 1 addition & 1 deletion pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21433,7 +21433,7 @@ with pkgs;
postgresql_13
postgresql_14
;
postgresql = postgresql_13.override { this = postgresql; };
postgresql = postgresql_13;
postgresqlPackages = recurseIntoAttrs postgresql.pkgs;
postgresql11Packages = recurseIntoAttrs postgresql_11.pkgs;
postgresql12Packages = recurseIntoAttrs postgresql_12.pkgs;
Expand Down