-
-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathpg_repack.nix
More file actions
152 lines (133 loc) · 5.24 KB
/
pg_repack.nix
File metadata and controls
152 lines (133 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{
lib,
stdenv,
fetchFromGitHub,
postgresql,
postgresqlTestHook,
testers,
buildEnv,
makeWrapper,
switch-ext-version,
latestOnly ? false,
}:
let
pname = "pg_repack";
# Load version configuration from external file
allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).${pname};
# Filter versions compatible with current PostgreSQL version
supportedVersions = lib.filterAttrs (
_: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql
) allVersions;
# Derived version information
versions = lib.naturalSort (lib.attrNames supportedVersions);
latestVersion = lib.last versions;
versionsToUse =
if latestOnly then
{ "${latestVersion}" = supportedVersions.${latestVersion}; }
else
supportedVersions;
versionsBuilt = if latestOnly then [ latestVersion ] else versions;
numberOfVersionsBuilt = builtins.length versionsBuilt;
packages = builtins.attrValues (lib.mapAttrs (name: value: build name value.hash) versionsToUse);
# Build function for individual versions
build =
version: hash:
stdenv.mkDerivation (finalAttrs: {
inherit pname version;
buildInputs = postgresql.buildInputs ++ [ postgresql ];
src = fetchFromGitHub {
owner = "reorg";
repo = "pg_repack";
rev = "ver_${finalAttrs.version}";
inherit hash;
};
installPhase = ''
mkdir -p $out/{lib,share/postgresql/extension,bin}
mv bin/${pname} $out/bin/${pname}-${version}
# Install shared library with version suffix
mv lib/${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix}
# Create version-specific control file
sed -e "/^default_version =/d" \
-e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}-${version}'|" \
lib/${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control
# Copy SQL install script
cp lib/${pname}--${version}.sql $out/share/postgresql/extension
# For the latest version, create default control file and symlink and copy SQL upgrade scripts
if [[ "${version}" == "${latestVersion}" ]]; then
{
echo "default_version = '${version}'"
cat $out/share/postgresql/extension/${pname}--${version}.control
} > $out/share/postgresql/extension/${pname}.control
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
ln -sfn $out/bin/${pname}-${latestVersion} $out/bin/${pname}
fi
#install -D bin/pg_repack -t $out/bin/
#install -D lib/pg_repack${postgresql.dlSuffix} -t $out/lib/
#install -D lib/{pg_repack--${finalAttrs.version}.sql,pg_repack.control} -t $out/share/postgresql/extension
'';
passthru.tests = {
version = testers.testVersion { package = finalAttrs.finalPackage; };
extension = stdenv.mkDerivation {
name = "plpgsql-check-test";
dontUnpack = true;
doCheck = true;
buildInputs = [ postgresqlTestHook ];
nativeCheckInputs = [ (postgresql.withPackages (ps: [ ps.pg_repack ])) ];
postgresqlTestUserOptions = "LOGIN SUPERUSER";
failureHook = "postgresqlStop";
checkPhase = ''
runHook preCheck
psql -a -v ON_ERROR_STOP=1 -c "CREATE EXTENSION pg_repack;"
runHook postCheck
'';
installPhase = "touch $out";
};
};
meta = with lib; {
description = "Reorganize tables in PostgreSQL databases with minimal locks";
longDescription = ''
pg_repack is a PostgreSQL extension which lets you remove bloat from tables and indexes, and optionally restore
the physical order of clustered indexes. Unlike CLUSTER and VACUUM FULL it works online, without holding an
exclusive lock on the processed tables during processing. pg_repack is efficient to boot,
with performance comparable to using CLUSTER directly.
'';
homepage = "https://github.com/reorg/pg_repack";
license = licenses.bsd3;
inherit (postgresql.meta) platforms;
mainProgram = "pg_repack";
};
});
in
buildEnv {
name = pname;
paths = packages;
nativeBuildInputs = [ makeWrapper ];
pathsToLink = [
"/bin"
"/lib"
"/share/postgresql/extension"
];
postBuild = ''
# Verify all expected library files are present
expectedFiles=${toString (numberOfVersionsBuilt + 1)}
actualFiles=$(ls -l $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)
if [[ "$actualFiles" != "$expectedFiles" ]]; then
echo "Error: Expected $expectedFiles library files, found $actualFiles"
echo "Files found:"
ls -la $out/lib/*${postgresql.dlSuffix} || true
exit 1
fi
makeWrapper ${lib.getExe switch-ext-version} $out/bin/switch_${pname}_version \
--prefix EXT_WRAPPER : "$out" --prefix EXT_NAME : "${pname}"
'';
passthru = {
versions = versionsBuilt;
numberOfVersions = numberOfVersionsBuilt;
inherit pname latestOnly;
version =
if latestOnly then
latestVersion
else
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
};
}