Skip to content

Commit 1ccb7f3

Browse files
committed
feat: deploy pgbouncer using system manager
1 parent 423257c commit 1ccb7f3

File tree

11 files changed

+141
-179
lines changed

11 files changed

+141
-179
lines changed

ansible/files/pgbouncer_config/pgbouncer.service.j2

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

ansible/files/pgbouncer_config/tmpfiles.d-pgbouncer.conf.j2

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

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,12 +1,14 @@
11
{ self, inputs, ... }:
22
let
33
mkModules = system: [
4+
self.systemModules.pgbouncer
45
self.systemModules.postgres
56
(
67
{ pkgs, ... }:
78
{
89
services.nginx.enable = true;
910
nixpkgs.hostPlatform = system;
11+
supabase.services.pgbouncer.enable = true;
1012
supabase.services.postgres = {
1113
enable = true;
1214
package = self.packages.${system}."psql_17/bin";

nix/systemModules/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
flake = {
77
systemModules = {
88
postgres = ./postgres;
9+
pgbouncer = ./pgbouncer.nix;
910
};
1011
};
1112
}
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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
lib,
3+
pkgs,
4+
nixosModulesPath,
5+
system,
6+
config,
7+
...
8+
}:
9+
let
10+
cfg = config.supabase.services.pgbouncer;
11+
12+
# From https://github.com/mightyiam/catppuccin-nix/blob/main/modules/lib/default.nix#L78-L89
13+
fromINI =
14+
file:
15+
let
16+
json = pkgs.runCommand "converted.json" { } ''
17+
${lib.getExe pkgs.jc} --ini < ${file} > $out
18+
'';
19+
in
20+
builtins.fromJSON (builtins.readFile json);
21+
in
22+
{
23+
imports = [
24+
# TODO: actually open the ports it needs with ufw
25+
./dummy-firewall.nix
26+
]
27+
++ map (path: nixosModulesPath + path) [
28+
"/services/databases/pgbouncer.nix"
29+
];
30+
31+
options = {
32+
supabase.services.pgbouncer = {
33+
enable = lib.mkEnableOption "Whether to enable PostgreSQL connection pooler.";
34+
};
35+
};
36+
37+
config = lib.mkIf cfg.enable {
38+
environment.etc = {
39+
# By default allow ssl connections.
40+
"/etc/pgbouncer-custom/ssl-config.ini".text = ''
41+
client_tls_sslmode = allow
42+
'';
43+
};
44+
45+
# Nixpkgs pgbouncer systemd service is quite what we had set up by ansible before:
46+
#
47+
# [Service]
48+
# Type=notify
49+
# User=pgbouncer
50+
# ExecStart=/usr/local/bin/pgbouncer /etc/pgbouncer/pgbouncer.ini
51+
# ExecReload=/bin/kill -HUP $MAINPID
52+
# KillSignal=SIGINT
53+
# LimitNOFILE=65536
54+
# Restart=always
55+
# RestartSec=5
56+
services.pgbouncer = {
57+
enable = true;
58+
package =
59+
(import (fetchTarball {
60+
# pgbouncer v1.19.0
61+
url = "https://github.com/NixOS/nixpkgs/archive/db7534df5fb9b7dfd3404ec26d977997ff2cc1a0.tar.gz";
62+
sha256 = "sha256:0lrsnz80a3jfjdyjs4njipvmq34w6wjr5ql645z1l1s9f9cyvk0g";
63+
}) { system = system; }).pgbouncer;
64+
settings =
65+
let
66+
iniJson = fromINI ./pgbouncer/pgbouncer.ini;
67+
in
68+
iniJson
69+
// {
70+
pgbouncer = iniJson.pgbouncer // {
71+
# jc --ini treat all values as strings, so we must manually convert
72+
# every numeric option to its expected type for NixOS module validation ...
73+
default_pool_size = lib.toInt iniJson.pgbouncer.default_pool_size;
74+
listen_port = lib.toInt iniJson.pgbouncer.listen_port;
75+
};
76+
};
77+
user = "pgbouncer"; # n.b. this is the nixpkgs default, but since everything depends on it ...
78+
group = "pgbouncer"; # ... we might as well be explicit here!
79+
};
80+
systemd.services.pgbouncer = {
81+
wantedBy = lib.mkForce [
82+
"system-manager.target"
83+
];
84+
};
85+
86+
# TODO: double check if all these are really needed
87+
systemd.tmpfiles.rules = [
88+
"d /run/pgbouncer 2775 pgbouncer postgres - -"
89+
"d /etc/pgbouncer-custom 0775 pgbouncer pgbouncer - -"
90+
"C /etc/pgbouncer/userlist.txt 0700 pgbouncer pgbouncer - -"
91+
"C /etc/pgbouncer-custom/custom-overrides.ini 0664 pgbouncer pgbouncer - -"
92+
"C /etc/pgbouncer-custom/generated-optimizations.ini 0664 pgbouncer pgbouncer - -"
93+
"C /etc/pgbouncer-custom/ssl-config.ini 0664 pgbouncer pgbouncer - -"
94+
];
95+
};
96+
}

ansible/files/pgbouncer_config/pgbouncer.ini.j2 renamed to nix/systemModules/pgbouncer/pgbouncer.ini

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ default_pool_size = 15
359359
;; Read additional config from other file
360360
;%include /etc/pgbouncer/pgbouncer-other.ini
361361

362-
%include /etc/pgbouncer-custom/generated-optimizations.ini
363-
%include /etc/pgbouncer-custom/custom-overrides.ini
364-
%include /etc/pgbouncer-custom/ssl-config.ini
362+
;; TODO: I have no idea how to include these files, since there're not defined in this repo,
363+
;; jc --ini isn't able to parse %include, and settings.pgbouncer doesn't have a way to add those either.
364+
; %include /etc/pgbouncer-custom/generated-optimizations.ini
365+
; %include /etc/pgbouncer-custom/custom-overrides.ini
366+
; %include /etc/pgbouncer-custom/ssl-config.ini

0 commit comments

Comments
 (0)