Skip to content

Commit ff9dc28

Browse files
committed
(WIP) feat: deploy pgbouncer using system manager
1 parent fc77a26 commit ff9dc28

File tree

8 files changed

+84
-153
lines changed

8 files changed

+84
-153
lines changed

ansible/playbook.yml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
dest: "00-schema.sql",
1414
}
1515
- { source: "stat_extension.sql", dest: "01-extension.sql" }
16-
16+
1717
environment:
1818
PATH: /usr/lib/postgresql/bin:{{ ansible_env.PATH }}
1919

@@ -29,13 +29,6 @@
2929
- name: Install Postgres from source
3030
import_tasks: tasks/setup-postgres.yml
3131

32-
- name: Install PgBouncer
33-
import_tasks: tasks/setup-pgbouncer.yml
34-
tags:
35-
- install-pgbouncer
36-
- install-supabase-internal
37-
when: debpkg_mode or nixpkg_mode
38-
3932
- name: Install WAL-G
4033
import_tasks: tasks/setup-wal-g.yml
4134
when: debpkg_mode or nixpkg_mode or stage2_nix
@@ -46,7 +39,7 @@
4639
- install-gotrue
4740
- install-supabase-internal
4841
when: debpkg_mode or nixpkg_mode
49-
42+
5043
- name: Install PostgREST
5144
import_tasks: tasks/setup-postgrest.yml
5245
tags:
@@ -96,7 +89,7 @@
9689
src: files/apt_periodic
9790
dest: /etc/apt/apt.conf.d/10periodic
9891
when: debpkg_mode or nixpkg_mode
99-
92+
10093
- name: Transfer init SQL files
10194
copy:
10295
src: files/{{ item.source }}
@@ -131,13 +124,13 @@
131124
tags:
132125
- install-supabase-internal
133126
when: debpkg_mode or stage2_nix
134-
127+
135128
- name: Finalize AMI
136129
import_tasks: tasks/finalize-ami.yml
137130
tags:
138131
- install-supabase-internal
139132
when: debpkg_mode or nixpkg_mode
140-
133+
141134
- name: Enhance fail2ban
142135
import_tasks: tasks/setup-fail2ban.yml
143136
when: debpkg_mode or nixpkg_mode
@@ -218,7 +211,7 @@
218211
systemctl stop postgresql.service
219212
when: stage2_nix
220213

221-
- name: Remove osquery
214+
- name: Remove osquery
222215
become: yes
223216
shell: |
224217
sudo -u ubuntu bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && nix profile remove osquery"

ansible/tasks/setup-pgbouncer.yml

Lines changed: 0 additions & 135 deletions
This file was deleted.

ansible/vars.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ postgres_release:
1313
postgres17: "17.6.1.003-nixpkgs-4"
1414
postgres15: "15.14.1.003-nixpkgs-4"
1515

16-
# Non Postgres Extensions
17-
pgbouncer_release: "1.19.0"
18-
pgbouncer_release_checksum: sha256:af0b05e97d0e1fd9ad45fe00ea6d2a934c63075f67f7e2ccef2ca59e3d8ce682
19-
2016
# The checksum can be found under "Assets", in the GitHub release page for each version.
2117
# The binaries used are: ubuntu-aarch64 and linux-static.
2218
# https://github.com/PostgREST/postgrest/releases

nix/systemConfigs.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{ self, inputs, ... }:
22
let
33
mkModules = system: [
4+
self.systemModules.pgbouncer
45
({
56
services.nginx.enable = true;
67
nixpkgs.hostPlatform = system;
8+
supabase.services.pgbouncer.enable = true;
79
})
810
];
911

nix/systemModules/default.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
{
55
imports = [ ./tests ];
66
flake = {
7-
systemModules = { };
7+
systemModules = {
8+
pgbouncer = ./pgbouncer.nix;
9+
};
810
};
911
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{ lib, ... }:
2+
{
3+
options.networking.firewall = lib.mkOption {
4+
type = lib.types.attrs;
5+
};
6+
}

nix/systemModules/pgbouncer.nix

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
lib,
3+
nixosModulesPath,
4+
system,
5+
config,
6+
...
7+
}:
8+
let
9+
cfg = config.supabase.services.pgbouncer;
10+
in
11+
{
12+
imports = [
13+
# TODO: actually open the ports it needs with ufw
14+
./dummy-firewall.nix
15+
]
16+
++ map (path: nixosModulesPath + path) [
17+
"/services/databases/pgbouncer.nix"
18+
];
19+
20+
options = {
21+
supabase.services.pgbouncer = {
22+
enable = lib.mkEnableOption "Whether to enable PostgreSQL connection pooler.";
23+
};
24+
};
25+
26+
config = lib.mkIf cfg.enable {
27+
services.pgbouncer = {
28+
enable = true;
29+
package =
30+
(import (fetchTarball {
31+
# pgbouncer v1.19.0
32+
url = "https://github.com/NixOS/nixpkgs/archive/db7534df5fb9b7dfd3404ec26d977997ff2cc1a0.tar.gz";
33+
sha256 = "sha256:0lrsnz80a3jfjdyjs4njipvmq34w6wjr5ql645z1l1s9f9cyvk0g";
34+
}) { system = system; }).pgbouncer;
35+
};
36+
systemd.services.pgbouncer = {
37+
wantedBy = lib.mkForce [
38+
"system-manager.target"
39+
];
40+
};
41+
};
42+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# from time import sleep
2+
3+
4+
def test_pgbouncer_service(host):
5+
# sleep(5000) # Handy for interactive debugging (with docker exec -it $CONTAINER_ID /bin/bash)
6+
assert host.service("pgbouncer.service").is_valid
7+
assert host.service("pgbouncer.service").is_running, (
8+
"Auth service should be running but failed: {}".format(
9+
host.run("systemctl status pgbouncer.service").stdout
10+
)
11+
)
12+
13+
14+
# FIXME: AssertionError: Auth service should be running but failed: × pgbouncer.service - PgBouncer - PostgreSQL connection pooler
15+
# Loaded: loaded (/etc/systemd/system/pgbouncer.service; enabled; preset: enabled)
16+
# Active: failed (Result: exit-code) since Fri 2025-09-19 12:36:00 UTC; 12s ago
17+
# Process: 372 ExecStart=/nix/store/bcj53gxm9i2y4hd21jr7zpi2r1hw8wlq-pgbouncer-1.24.1/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini (code=exited, status=217/USER)
18+
# Main PID: 372 (code=exited, status=217/USER)
19+
# CPU: 4ms
20+
#
21+
# Sep 19 12:36:00 f803c2922bff systemd[1]: Starting pgbouncer.service - PgBouncer - PostgreSQL connection pooler...
22+
# Sep 19 12:36:00 f803c2922bff (gbouncer)[372]: pgbouncer.service: Failed to determine user credentials: No such process
23+
# Sep 19 12:36:00 f803c2922bff systemd[1]: pgbouncer.service: Main process exited, code=exited, status=217/USER
24+
# Sep 19 12:36:00 f803c2922bff systemd[1]: pgbouncer.service: Failed with result 'exit-code'.
25+
# Sep 19 12:36:00 f803c2922bff systemd[1]: Failed to start pgbouncer.service - PgBouncer - PostgreSQL connection pooler.

0 commit comments

Comments
 (0)