Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
90 changes: 89 additions & 1 deletion ansible/files/postgres_prestart.sh.j2
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/bin/bash

set -x # Print commands

log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

check_orioledb_enabled() {
local pg_conf="/etc/postgresql/postgresql.conf"
if [ ! -f "$pg_conf" ]; then
Expand All @@ -26,7 +32,87 @@ update_orioledb_buffers() {
fi
}

check_extensions_file() {
local extensions_file="/etc/adminapi/pg-extensions.json"
if [ ! -f "$extensions_file" ]; then
log "extensions: No extensions file found, skipping extensions versions check"
return 0 #if file not found, skip
fi
if [ ! -r "$extensions_file" ]; then
log "extensions: Cannot read extensions file"
return 1 #a true error, we should be able to read file
fi
return 0
}

switch_extension_version() {
local extension_name="$1"
local version="$2"

# Use BIN_PATH environment variable or default to /var/lib/postgresql/.nix-profile
: ${BIN_PATH:="/var/lib/postgresql/.nix-profile"}

local switch_script="$BIN_PATH/bin/switch_${extension_name}_version"

if [ ! -x "$switch_script" ]; then
log "$extension_name: No version switch script available at $switch_script, skipping"
return 0
fi

log "$extension_name: Switching to version $version"
# Run directly as root since we're already running as root
"$switch_script" "$version"
local exit_code=$?
if [ $exit_code -eq 0 ]; then
log "$extension_name: Version switch completed successfully"
else
log "$extension_name: Version switch failed with exit code $exit_code"
fi
return $exit_code
}

handle_extension_versions() {
if ! check_extensions_file; then
return
fi

local extensions_file="/etc/adminapi/pg-extensions.json"

# Get all extension names from the JSON file
local extensions
extensions=$(jq -r 'keys[]' "$extensions_file" 2>/dev/null)

if [ -z "$extensions" ]; then
log "extensions: No extensions found in configuration"
return
fi

# Iterate through each extension
while IFS= read -r extension_name; do
# Get the version for this extension
local version
version=$(jq -r --arg ext "$extension_name" '.[$ext] // empty' "$extensions_file")

if [ -z "$version" ]; then
log "$extension_name: No version specified, skipping"
continue
fi

log "$extension_name: Found version $version in extensions file"

# Don't fail if version switch fails - just log and continue
switch_extension_version "$extension_name" "$version" || log "$extension_name: Version switch failed but continuing"

done <<< "$extensions"
}

main() {
log "Starting prestart script"

# 1. Handle all extension versions from config file
handle_extension_versions

# 2. orioledb handling
local has_orioledb=$(check_orioledb_enabled)
if [ "$has_orioledb" -lt 1 ]; then
return 0
Expand All @@ -35,6 +121,8 @@ main() {
if [ ! -z "$shared_buffers_value" ]; then
update_orioledb_buffers "$shared_buffers_value"
fi

log "Prestart script completed"
}

# Initial locale setup
Expand All @@ -46,4 +134,4 @@ if [ $(locale -a | grep -c en_US.utf8) -eq 0 ]; then
locale-gen
fi

main
main
6 changes: 3 additions & 3 deletions ansible/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ postgres_major:

# Full version strings for each major version
postgres_release:
postgresorioledb-17: "17.5.1.017-orioledb"
postgres17: "17.4.1.074"
postgres15: "15.8.1.131"
postgresorioledb-17: "17.5.1.018-orioledb"
postgres17: "17.4.1.075"
postgres15: "15.8.1.132"

# Non Postgres Extensions
pgbouncer_release: "1.19.0"
Expand Down
4 changes: 4 additions & 0 deletions nix/checks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@
;
}
// pkgs.lib.optionalAttrs (system == "x86_64-linux") {
pg_net = import ./ext/tests/pg_net.nix {
inherit self;
inherit pkgs;
};
wrappers = import ./ext/tests/wrappers.nix {
inherit self;
inherit pkgs;
Expand Down
211 changes: 186 additions & 25 deletions nix/ext/pg_net.nix
Original file line number Diff line number Diff line change
@@ -1,41 +1,202 @@
{
pkgs,
lib,
stdenv,
fetchFromGitHub,
curl,
postgresql,
libuv,
writeShellApplication,
}:

stdenv.mkDerivation rec {
pname = "pg_net";
version = "0.14.0";

buildInputs = [
curl
postgresql
];

src = fetchFromGitHub {
owner = "supabase";
repo = pname;
rev = "refs/tags/v${version}";
hash = "sha256-c1pxhTyrE5j6dY+M5eKAboQNofIORS+Dccz+7HKEKQI=";
let
switchPgNetVersion = writeShellApplication {
name = "switch_pg_net_version";
runtimeInputs = [ pkgs.coreutils ];
text = ''
# Create version switcher script
set -e
if [ $# -ne 1 ]; then
echo "Usage: $0 <version>"
echo "Example: $0 0.10.0"
echo ""
echo "Optional environment variables:"
echo " NIX_PROFILE - Path to nix profile (default: /var/lib/postgresql/.nix-profile)"
echo " LIB_DIR - Override library directory"
echo " EXTENSION_DIR - Override extension directory"
exit 1
fi
VERSION=$1
# Set defaults, allow environment variable overrides
: "''${NIX_PROFILE:="/var/lib/postgresql/.nix-profile"}"
: "''${LIB_DIR:=""}"
: "''${EXTENSION_DIR:=""}"
# If LIB_DIR not explicitly set, auto-detect it
if [ -z "$LIB_DIR" ]; then
# Follow the complete chain of symlinks to find the multi-version directory
CURRENT_LINK="$NIX_PROFILE/lib/pg_net-$VERSION${postgresql.dlSuffix}"
echo "Starting with link: $CURRENT_LINK"
# Follow first two symlinks to get to the multi-version directory
for _ in 1 2; do
if [ -L "$CURRENT_LINK" ]; then
NEXT_LINK=$(readlink "$CURRENT_LINK")
echo "Following link: $NEXT_LINK"
if echo "$NEXT_LINK" | grep -q '^/'; then
CURRENT_LINK="$NEXT_LINK"
else
CURRENT_LINK="$(dirname "$CURRENT_LINK")/$NEXT_LINK"
fi
echo "Current link is now: $CURRENT_LINK"
fi
done
# The multi-version directory should be the parent of the current link
MULTI_VERSION_DIR=$(dirname "$CURRENT_LINK")
echo "Found multi-version directory: $MULTI_VERSION_DIR"
LIB_DIR="$MULTI_VERSION_DIR"
else
echo "Using provided LIB_DIR: $LIB_DIR"
fi
# If EXTENSION_DIR not explicitly set, use default
if [ -z "$EXTENSION_DIR" ]; then
EXTENSION_DIR="$NIX_PROFILE/share/postgresql/extension"
fi
echo "Using EXTENSION_DIR: $EXTENSION_DIR"
echo "Looking for file: $LIB_DIR/pg_net-$VERSION${postgresql.dlSuffix}"
ls -la "$LIB_DIR" || true
# Check if version exists
if [ ! -f "$LIB_DIR/pg_net-$VERSION${postgresql.dlSuffix}" ]; then
echo "Error: Version $VERSION not found in $LIB_DIR"
echo "Available versions:"
#shellcheck disable=SC2012
ls "$LIB_DIR"/pg_net-*${postgresql.dlSuffix} 2>/dev/null | sed 's/.*pg_net-/ /' | sed 's/${postgresql.dlSuffix}$//' || echo " No versions found"
exit 1
fi
# Update library symlink
ln -sfnv "pg_net-$VERSION${postgresql.dlSuffix}" "$LIB_DIR/pg_net${postgresql.dlSuffix}"
# Update control file
echo "default_version = '$VERSION'" > "$EXTENSION_DIR/pg_net.control"
cat "$EXTENSION_DIR/pg_net--$VERSION.control" >> "$EXTENSION_DIR/pg_net.control"
echo "Successfully switched pg_net to version $VERSION"
EOF
'';
};
pname = "pg_net";
build =
version: hash:
stdenv.mkDerivation rec {
inherit pname version;

buildInputs = [
curl
postgresql
]
++ lib.optional (version == "0.6") libuv;

src = fetchFromGitHub {
owner = "supabase";
repo = pname;
rev = "refs/tags/v${version}";
inherit hash;
};

buildPhase = ''
make PG_CONFIG=${postgresql}/bin/pg_config
'';

postPatch =
lib.optionalString (version == "0.6") ''
# handle collision with pg_net 0.10.0
rm sql/pg_net--0.2--0.3.sql
rm sql/pg_net--0.4--0.5.sql
rm sql/pg_net--0.5.1--0.6.sql
''
+ lib.optionalString (version == "0.7.1") ''
# handle collision with pg_net 0.10.0
rm sql/pg_net--0.5.1--0.6.sql
'';

env.NIX_CFLAGS_COMPILE = "-Wno-error";

installPhase = ''
mkdir -p $out/{lib,share/postgresql/extension}
# Install versioned library
install -Dm755 ${pname}${postgresql.dlSuffix} $out/lib/${pname}-${version}${postgresql.dlSuffix}
if [ -f sql/${pname}.sql ]; then
cp sql/${pname}.sql $out/share/postgresql/extension/${pname}--${version}.sql
else
cp sql/${pname}--${version}.sql $out/share/postgresql/extension/${pname}--${version}.sql
fi
# Install upgrade scripts
find . -name '${pname}--*--*.sql' -exec install -Dm644 {} $out/share/postgresql/extension/ \;
# Create versioned control file with modified module path
sed -e "/^default_version =/d" \
-e "s|^module_pathname = .*|module_pathname = '\$libdir/${pname}'|" \
${pname}.control > $out/share/postgresql/extension/${pname}--${version}.control
'';

env.NIX_CFLAGS_COMPILE = "-Wno-error";
meta = with lib; {
description = "Async networking for Postgres";
homepage = "https://github.com/supabase/pg_net";
platforms = postgresql.meta.platforms;
license = licenses.postgresql;
};
};
allVersions = (builtins.fromJSON (builtins.readFile ./versions.json)).pg_net;
# Filter out versions that don't work on current platform
platformFilteredVersions = lib.filterAttrs (
name: _:
# Exclude 0.11.0 on macOS due to epoll.h dependency
!(stdenv.isDarwin && name == "0.11.0")
) allVersions;
supportedVersions = lib.filterAttrs (
_: value: builtins.elem (lib.versions.major postgresql.version) value.postgresql
) platformFilteredVersions;
versions = lib.naturalSort (lib.attrNames supportedVersions);
latestVersion = lib.last versions;
numberOfVersions = builtins.length versions;
packages = builtins.attrValues (
lib.mapAttrs (name: value: build name value.hash) supportedVersions
);
in
pkgs.buildEnv {
name = pname;
paths = packages ++ [ switchPgNetVersion ];
postBuild = ''
{
echo "default_version = '${latestVersion}'"
cat $out/share/postgresql/extension/${pname}--${latestVersion}.control
} > $out/share/postgresql/extension/${pname}.control
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
installPhase = ''
mkdir -p $out/{lib,share/postgresql/extension}
cp *${postgresql.dlSuffix} $out/lib
cp sql/*.sql $out/share/postgresql/extension
cp *.control $out/share/postgresql/extension
# checks
(set -x
test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${
toString (numberOfVersions + 1)
}"
)
'';

meta = with lib; {
description = "Async networking for Postgres";
homepage = "https://github.com/supabase/pg_net";
platforms = postgresql.meta.platforms;
license = licenses.postgresql;
passthru = {
inherit versions numberOfVersions switchPgNetVersion;
pname = "${pname}-all";
version =
"multi-" + lib.concatStringsSep "-" (map (v: lib.replaceStrings [ "." ] [ "-" ] v) versions);
};
}
Loading
Loading