Skip to content

Commit 4cd1171

Browse files
committed
Merge branch 'develop' into or/ext-pgmq
2 parents c8676f8 + db49dcc commit 4cd1171

36 files changed

+1062
-129
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/common.sh

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,23 @@ CI_start_postgres() {
8888

8989
swap_postgres_and_supabase_admin() {
9090
run_sql <<'EOSQL'
91+
alter database postgres connection limit 0;
92+
select pg_terminate_backend(pid) from pg_stat_activity where backend_type = 'client backend' and pid != pg_backend_pid();
93+
EOSQL
94+
run_sql <<'EOSQL'
95+
set statement_timeout = '600s';
9196
begin;
9297
create role supabase_tmp superuser;
9398
set session authorization supabase_tmp;
9499
100+
do $$
101+
begin
102+
if exists (select from pg_extension where extname = 'timescaledb') then
103+
execute(format('select %I.timescaledb_pre_restore()', (select pronamespace::regnamespace from pg_proc where proname = 'timescaledb_pre_restore')));
104+
end if;
105+
end
106+
$$;
107+
95108
do $$
96109
declare
97110
postgres_rolpassword text := (select rolpassword from pg_authid where rolname = 'postgres');
@@ -245,7 +258,12 @@ begin
245258
obj->>'role',
246259
case when obj->>'database' is null then '' else format('in database %I', obj->>'database') end,
247260
rec.key,
248-
rec.value
261+
-- https://github.com/postgres/postgres/blob/70d1c664f4376fd3499e3b0c6888cf39b65d722b/src/bin/pg_dump/dumputils.c#L861
262+
case
263+
when rec.key in ('local_preload_libraries', 'search_path', 'session_preload_libraries', 'shared_preload_libraries', 'temp_tablespaces', 'unix_socket_directories')
264+
then rec.value
265+
else quote_literal(rec.value)
266+
end
249267
));
250268
end loop;
251269
end loop;
@@ -492,6 +510,19 @@ begin
492510
end
493511
$$;
494512
513+
do $$
514+
begin
515+
if exists (select from pg_extension where extname = 'timescaledb') then
516+
execute(format('select %I.timescaledb_post_restore()', (select pronamespace::regnamespace from pg_proc where proname = 'timescaledb_post_restore')));
517+
end if;
518+
end
519+
$$;
520+
521+
alter database postgres connection limit -1;
522+
523+
-- #incident-2024-09-12-project-upgrades-are-temporarily-disabled
524+
grant pg_read_all_data, pg_signal_backend to postgres;
525+
495526
set session authorization supabase_admin;
496527
drop role supabase_tmp;
497528
commit;

ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,13 @@ function apply_auth_scheme_updates {
170170

171171
function start_vacuum_analyze {
172172
echo "complete" > /tmp/pg-upgrade-status
173-
if ! command -v nix &> /dev/null; then
174-
su -c 'vacuumdb --all --analyze-in-stages' -s "$SHELL" postgres
175-
else
176-
su -c '. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && vacuumdb --all --analyze-in-stages' -s "$SHELL" postgres
173+
174+
# shellcheck disable=SC1091
175+
if [ -f "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh" ]; then
176+
# shellcheck disable=SC1091
177+
source "/nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"
177178
fi
179+
vacuumdb --all --analyze-in-stages -U supabase_admin -h localhost -p 5432
178180
echo "Upgrade job completed"
179181
}
180182

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ PGBINOLD="/usr/lib/postgresql/bin"
4949
PGLIBOLD="/usr/lib/postgresql/lib"
5050

5151
PG_UPGRADE_BIN_DIR="/tmp/pg_upgrade_bin/$PGVERSION"
52+
NIX_INSTALLER_PATH="/tmp/persistent/nix-installer"
53+
NIX_INSTALLER_PACKAGE_PATH="$NIX_INSTALLER_PATH.tar.gz"
5254

5355
if [ -L "$PGBINOLD/pg_upgrade" ]; then
5456
BINARY_PATH=$(readlink -f "$PGBINOLD/pg_upgrade")
@@ -125,6 +127,9 @@ cleanup() {
125127
echo "Removing SUPERUSER grant from postgres"
126128
run_sql -c "ALTER USER postgres WITH NOSUPERUSER;"
127129

130+
echo "Resetting postgres database connection limit"
131+
run_sql -c "ALTER DATABASE postgres CONNECTION LIMIT -1;"
132+
128133
if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then
129134
echo "Unmounting data disk from ${MOUNT_POINT}"
130135
umount $MOUNT_POINT
@@ -283,9 +288,20 @@ function initiate_upgrade {
283288
if ! command -v nix > /dev/null; then
284289
echo "1.1. Nix is not installed; installing."
285290

286-
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm \
287-
--extra-conf "substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com" \
288-
--extra-conf "trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI=% cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
291+
if [ -f "$NIX_INSTALLER_PACKAGE_PATH" ]; then
292+
echo "1.1.1. Installing Nix using the provided installer"
293+
tar -xzf "$NIX_INSTALLER_PACKAGE_PATH" -C /tmp/persistent/
294+
chmod +x "$NIX_INSTALLER_PATH"
295+
"$NIX_INSTALLER_PATH" install --no-confirm \
296+
--extra-conf "substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com" \
297+
--extra-conf "trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI=% cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
298+
else
299+
echo "1.1.1. Installing Nix using the official installer"
300+
301+
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install --no-confirm \
302+
--extra-conf "substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com" \
303+
--extra-conf "trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI=% cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
304+
fi
289305
else
290306
echo "1.1. Nix is installed; moving on."
291307
fi

ansible/tasks/stage2-setup-postgres.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@
1313
- name: Install pg_prove from nix binary cache
1414
become: yes
1515
shell: |
16-
sudo -u postgres bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && nix profile install github:supabase/postgres/sam/2-stage-ami-nix#pg_prove"
16+
sudo -u postgres bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && nix profile install github:supabase/postgres/{{ git_commit_sha }}#pg_prove"
17+
when: stage2_nix
18+
19+
- name: Install supabase-groonga from nix binary cache
20+
become: yes
21+
shell: |
22+
sudo -u postgres bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && nix profile install github:supabase/postgres/sam/pgroonga-deps#supabase-groonga"
1723
when: stage2_nix
1824

1925
- name: Set ownership and permissions for /etc/ssl/private
@@ -220,3 +226,9 @@
220226
# script is expected to be placed by finalization tasks for different target platforms
221227
line: pgsodium.getkey_script= '{{ pg_bindir }}/pgsodium_getkey.sh'
222228
when: stage2_nix
229+
230+
- name: Append GRN_PLUGINS_DIR to /etc/environment.d/postgresql.env
231+
ansible.builtin.lineinfile:
232+
path: /etc/environment.d/postgresql.env
233+
line: 'GRN_PLUGINS_DIR=/var/lib/postgresql/.nix-profile/lib/groonga/plugins'
234+
become: yes

ansible/vars.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ groonga_release_checksum: sha256:1c2d1a6981c1ad3f02a11aff202b15ba30cb1c6147f1fa9
133133
pgroonga_release: "3.0.7"
134134
pgroonga_release_checksum: sha256:885ff3878cc30e9030e5fc56d561bc8b66df3ede1562c9d802bc0ea04fe5c203
135135

136-
wrappers_release: "0.4.1"
136+
wrappers_release: "0.4.2"
137137

138138
hypopg_release: "1.4.1"
139139
hypopg_release_checksum: sha256:9afe6357fd389d8d33fad81703038ce520b09275ec00153c6c89282bcdedd6bc

flake.nix

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,39 @@
4646
#This variable works the same as 'oriole_pkgs' but builds using the upstream
4747
#nixpkgs builds of postgresql 15 and 16 + the overlays listed below
4848
pkgs = import nixpkgs {
49-
config = { allowUnfree = true; };
49+
config = {
50+
allowUnfree = true;
51+
permittedInsecurePackages = [
52+
"v8-9.7.106.18"
53+
];
54+
};
5055
inherit system;
5156
overlays = [
5257
# NOTE (aseipp): add any needed overlays here. in theory we could
5358
# pull them from the overlays/ directory automatically, but we don't
5459
# want to have an arbitrary order, since it might matter. being
5560
# explicit is better.
61+
(final: prev: {
62+
postgresql = final.callPackage ./nix/postgresql/default.nix {
63+
inherit (final) lib;
64+
inherit (final) stdenv;
65+
inherit (final) fetchurl;
66+
inherit (final) makeWrapper;
67+
inherit (final) callPackage;
68+
};
69+
})
5670
(import ./nix/overlays/cargo-pgrx-0-11-3.nix)
5771
# (import ./nix/overlays/postgis.nix)
5872
#(import ./nix/overlays/gdal-small.nix)
5973

6074
];
6175
};
62-
76+
postgresql_15 = pkgs.postgresql.postgresql_15;
77+
postgresql = pkgs.postgresql.postgresql_15;
6378
sfcgal = pkgs.callPackage ./nix/ext/sfcgal/sfcgal.nix { };
64-
pg_regress = pkgs.callPackage ./nix/ext/pg_regress.nix { };
65-
79+
pg_regress = pkgs.callPackage ./nix/ext/pg_regress.nix { inherit postgresql; };
80+
supabase-groonga = pkgs.callPackage ./nix/supabase-groonga.nix { };
81+
mecab-naist-jdic = pkgs.callPackage ./nix/ext/mecab-naist-jdic/default.nix { };
6682
# Our list of PostgreSQL extensions which come from upstream Nixpkgs.
6783
# These are maintained upstream and can easily be used here just by
6884
# listing their name. Anytime the version of nixpkgs is upgraded, these
@@ -130,7 +146,10 @@
130146
#this var is a convenience setting to import the orioledb patched version of postgresql
131147
postgresql_orioledb_16 = oriole_pkgs.postgresql_orioledb_16;
132148
#postgis_override = pkgs.postgis_override;
133-
149+
getPostgresqlPackage = version:
150+
pkgs.postgresql."postgresql_${version}";
151+
#we will add supported versions to this list in the future
152+
supportedVersions = [ "15" ];
134153
# Create a 'receipt' file for a given postgresql package. This is a way
135154
# of adding a bit of metadata to the package, which can be used by other
136155
# tools to inspect what the contents of the install are: the PSQL
@@ -172,7 +191,7 @@
172191
in map (path: pkgs.callPackage path { inherit postgresql; }) orioledbExtension;
173192

174193
makeOurPostgresPkgs = version:
175-
let postgresql = pkgs."postgresql_${version}";
194+
let postgresql = getPostgresqlPackage version;
176195
in map (path: pkgs.callPackage path { inherit postgresql; }) ourExtensions;
177196

178197
# Create an attrset that contains all the extensions included in a server for the orioledb version of postgresql + extension.
@@ -204,7 +223,7 @@
204223
# basis for building extensions, etc.
205224
makePostgresBin = version:
206225
let
207-
postgresql = pkgs."postgresql_${version}";
226+
postgresql = getPostgresqlPackage version;
208227
upstreamExts = map
209228
(ext: {
210229
name = postgresql.pkgs."${ext}".pname;
@@ -268,13 +287,40 @@
268287
# name in 'nix flake show' in order to make sure exactly what name you
269288
# want.
270289
basePackages = {
290+
supabase-groonga = supabase-groonga;
271291
# PostgreSQL versions.
272292
psql_15 = makePostgres "15";
273293
#psql_16 = makePostgres "16";
274294
#psql_orioledb_16 = makeOrioleDbPostgres "16_23" postgresql_orioledb_16;
275295
sfcgal = sfcgal;
276296
pg_regress = pg_regress;
277297
pg_prove = pkgs.perlPackages.TAPParserSourceHandlerpgTAP;
298+
postgresql_15 = pkgs.postgresql_15;
299+
300+
postgresql_15_src = pkgs.stdenv.mkDerivation {
301+
pname = "postgresql-15-src";
302+
version = pkgs.postgresql_15.version;
303+
304+
src = pkgs.postgresql_15.src;
305+
306+
nativeBuildInputs = [ pkgs.bzip2 ];
307+
308+
phases = [ "unpackPhase" "installPhase" ];
309+
310+
installPhase = ''
311+
mkdir -p $out
312+
cp -r . $out
313+
'';
314+
315+
meta = with pkgs.lib; {
316+
description = "PostgreSQL 15 source files";
317+
homepage = "https://www.postgresql.org/";
318+
license = licenses.postgresql;
319+
platforms = platforms.all;
320+
};
321+
};
322+
mecab_naist_jdic = mecab-naist-jdic;
323+
supabase_groonga = supabase-groonga;
278324
# Start a version of the server.
279325
start-server =
280326
let
@@ -302,19 +348,24 @@
302348
name = "pg_ident.conf";
303349
path = ./ansible/files/postgresql_config/pg_ident.conf.j2;
304350
};
351+
postgresqlExtensionCustomScriptsPath = builtins.path {
352+
name = "extension-custom-scripts";
353+
path = ./ansible/files/postgresql_extension_custom_scripts;
354+
};
305355
getkeyScript = ./nix/tests/util/pgsodium_getkey.sh;
306356
localeArchive = if pkgs.stdenv.isDarwin
307357
then "${pkgs.darwin.locale}/share/locale"
308358
else "${pkgs.glibcLocales}/lib/locale/locale-archive";
309359
in
310360
pkgs.runCommand "start-postgres-server" { } ''
311-
mkdir -p $out/bin $out/etc/postgresql-custom $out/etc/postgresql
361+
mkdir -p $out/bin $out/etc/postgresql-custom $out/etc/postgresql $out/extension-custom-scripts
312362
cp ${supautilsConfigFile} $out/etc/postgresql-custom/supautils.conf || { echo "Failed to copy supautils.conf"; exit 1; }
313363
cp ${pgconfigFile} $out/etc/postgresql/postgresql.conf || { echo "Failed to copy postgresql.conf"; exit 1; }
314364
cp ${loggingConfigFile} $out/etc/postgresql-custom/logging.conf || { echo "Failed to copy logging.conf"; exit 1; }
315365
cp ${readReplicaConfigFile} $out/etc/postgresql-custom/read-replica.conf || { echo "Failed to copy read-replica.conf"; exit 1; }
316366
cp ${pgHbaConfigFile} $out/etc/postgresql/pg_hba.conf || { echo "Failed to copy pg_hba.conf"; exit 1; }
317367
cp ${pgIdentConfigFile} $out/etc/postgresql/pg_ident.conf || { echo "Failed to copy pg_ident.conf"; exit 1; }
368+
cp -r ${postgresqlExtensionCustomScriptsPath}/* $out/extension-custom-scripts/ || { echo "Failed to copy custom scripts"; exit 1; }
318369
echo "Copy operation completed"
319370
chmod 644 $out/etc/postgresql-custom/supautils.conf
320371
chmod 644 $out/etc/postgresql/postgresql.conf
@@ -331,40 +382,33 @@
331382
--subst-var-by 'SUPAUTILS_CONF_FILE' "$out/etc/postgresql-custom/supautils.conf" \
332383
--subst-var-by 'PG_HBA' "$out/etc/postgresql/pg_hba.conf" \
333384
--subst-var-by 'PG_IDENT' "$out/etc/postgresql/pg_ident.conf" \
334-
--subst-var-by 'LOCALES' '${localeArchive}'
335-
385+
--subst-var-by 'LOCALES' '${localeArchive}' \
386+
--subst-var-by 'EXTENSION_CUSTOM_SCRIPTS_DIR' "$out/extension-custom-scripts" \
387+
--subst-var-by 'MECAB_LIB' '${basePackages.psql_15.exts.pgroonga}/lib/groonga/plugins/tokenizers/tokenizer_mecab.so' \
388+
--subst-var-by 'GROONGA_DIR' '${supabase-groonga}'
389+
336390
chmod +x $out/bin/start-postgres-server
337391
'';
338392

339-
# Start a version of the client.
340-
start-client = pkgs.runCommand "start-postgres-client" { } ''
341-
mkdir -p $out/bin
342-
substitute ${./nix/tools/run-client.sh.in} $out/bin/start-postgres-client \
343-
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
344-
--subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
345-
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
346-
chmod +x $out/bin/start-postgres-client
347-
'';
348-
349393
# Start a version of the client and runs migrations script on server.
350-
start-client-and-migrate =
394+
start-client =
351395
let
352396
migrationsDir = ./migrations/db;
353397
postgresqlSchemaSql = ./nix/tools/postgresql_schema.sql;
354398
pgbouncerAuthSchemaSql = ./ansible/files/pgbouncer_config/pgbouncer_auth_schema.sql;
355399
statExtensionSql = ./ansible/files/stat_extension.sql;
356400
in
357-
pkgs.runCommand "start-postgres-client-migrate" { } ''
401+
pkgs.runCommand "start-postgres-client" { } ''
358402
mkdir -p $out/bin
359-
substitute ${./nix/tools/run-client-migrate.sh.in} $out/bin/start-postgres-client-migrate \
403+
substitute ${./nix/tools/run-client.sh.in} $out/bin/start-postgres-client \
360404
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
361405
--subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
362406
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
363407
--subst-var-by 'MIGRATIONS_DIR' '${migrationsDir}' \
364408
--subst-var-by 'POSTGRESQL_SCHEMA_SQL' '${postgresqlSchemaSql}' \
365409
--subst-var-by 'PGBOUNCER_AUTH_SCHEMA_SQL' '${pgbouncerAuthSchemaSql}' \
366410
--subst-var-by 'STAT_EXTENSION_SQL' '${statExtensionSql}'
367-
chmod +x $out/bin/start-postgres-client-migrate
411+
chmod +x $out/bin/start-postgres-client
368412
'';
369413

370414
# Migrate between two data directories.
@@ -412,10 +456,11 @@
412456
let
413457
sqlTests = ./nix/tests/smoke;
414458
pg_prove = pkgs.perlPackages.TAPParserSourceHandlerpgTAP;
459+
supabase-groonga = pkgs.callPackage ./nix/supabase-groonga.nix { };
415460
in
416461
pkgs.runCommand "postgres-${pgpkg.version}-check-harness"
417462
{
418-
nativeBuildInputs = with pkgs; [ coreutils bash pgpkg pg_prove pg_regress procps ];
463+
nativeBuildInputs = with pkgs; [ coreutils bash pgpkg pg_prove pg_regress procps supabase-groonga ];
419464
} ''
420465
TMPDIR=$(mktemp -d)
421466
if [ $? -ne 0 ]; then
@@ -433,7 +478,7 @@
433478
mkdir -p $TMPDIR/logfile
434479
# Generate a random key and store it in an environment variable
435480
export PGSODIUM_KEY=$(head -c 32 /dev/urandom | od -A n -t x1 | tr -d ' \n')
436-
481+
export GRN_PLUGINS_DIR=${supabase-groonga}/lib/groonga/plugins
437482
# Create a simple script to echo the key
438483
echo '#!/bin/sh' > $TMPDIR/getkey.sh
439484
echo 'echo $PGSODIUM_KEY' >> $TMPDIR/getkey.sh
@@ -519,7 +564,6 @@
519564
{
520565
start-server = mkApp "start-server" "start-postgres-server";
521566
start-client = mkApp "start-client" "start-postgres-client";
522-
start-client-and-migrate = mkApp "start-client-and-migrate" "start-postgres-client-migrate";
523567
start-replica = mkApp "start-replica" "start-postgres-replica";
524568
migration-test = mkApp "migrate-tool" "migrate-postgres";
525569
sync-exts-versions = mkApp "sync-exts-versions" "sync-exts-versions";

0 commit comments

Comments
 (0)