Skip to content

Commit a387fe6

Browse files
committed
Generate optimized config for PG
1 parent 44d5138 commit a387fe6

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Postgresql optimizations
3+
4+
[Service]
5+
Type=oneshot
6+
# we don't want failures from this command to cause PG startup to fail
7+
ExecStart=/bin/bash -c "/opt/supabase-admin-api optimize db --destination-config-file-path /etc/postgresql-custom/generated-optimizations.conf ; exit 0"
8+
User=adminapi
9+
10+
[Install]
11+
WantedBy=multi-user.target

ansible/files/postgresql_config/postgresql.conf.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,10 @@ pgsodium.getkey_script= '/usr/lib/postgresql/bin/pgsodium_getkey_urandom.sh'
788788
#include_if_exists = '...' # include file only if it exists
789789
#include = '...' # include file
790790

791+
# Automatically generated optimizations
792+
include = '/etc/postgresql-custom/generated-optimizations.conf'
793+
# User-supplied custom parameters, override any automatically generated ones
794+
include = '/etc/postgresql-custom/custom-overrides.conf'
791795

792796
#------------------------------------------------------------------------------
793797
# CUSTOMIZED OPTIONS

ansible/files/postgresql_config/postgresql.service.j2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[Unit]
22
Description=PostgreSQL database server
33
Documentation=man:postgres(1)
4+
Requires=generate-optimizations.service
5+
After=generate-optimizations.service
46

57
[Service]
68
Type=notify
@@ -12,4 +14,4 @@ KillSignal=SIGINT
1214
TimeoutSec=0
1315

1416
[Install]
15-
WantedBy=multi-user.target
17+
WantedBy=multi-user.target

ansible/tasks/setup-postgres.yml

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Downloading dependencies
22
- name: Postgres dependencies
3-
become: yes
43
apt:
54
pkg:
65
- build-essential
@@ -18,14 +17,12 @@
1817
- ssl-cert
1918

2019
- name: Download LLVM & Clang
21-
become: yes
2220
apt:
2321
pkg:
2422
- llvm-11-dev
2523
- clang-11
2624

2725
- name: Download GCC 10
28-
become: yes
2926
apt:
3027
pkg:
3128
- gcc-10
@@ -34,7 +31,6 @@
3431
- name: Switch to GCC 10
3532
shell:
3633
cmd: update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
37-
become: yes
3834

3935
# Building Postgres from source
4036
- name: Postgres - download latest release
@@ -49,7 +45,6 @@
4945
remote_src: yes
5046
src: /tmp/postgresql-{{ postgresql_release }}.tar.gz
5147
dest: /tmp
52-
become: yes
5348

5449
- name: Setting CFLAGS (arm)
5550
set_fact:
@@ -65,19 +60,16 @@
6560
shell:
6661
cmd: CFLAGS='{{ cflags }} -ggdb -O0 -g -fno-omit-frame-pointer' LLVM_CONFIG=/usr/bin/llvm-config-11 CLANG=/usr/bin/clang-11 ./configure --enable-cassert --enable-debug --with-llvm --with-openssl --with-systemd --with-uuid=e2fs --exec-prefix=/usr/lib/postgresql --datarootdir=/var/lib/postgresql
6762
chdir: /tmp/postgresql-{{ postgresql_release }}
68-
become: yes
6963

7064
- name: Postgres - build
7165
make:
7266
target: world
7367
chdir: /tmp/postgresql-{{ postgresql_release }}
74-
become: yes
7568

7669
- name: Postgres - install
7770
make:
7871
target: install-world
7972
chdir: /tmp/postgresql-{{ postgresql_release }}
80-
become: yes
8173

8274
# Create postgres user
8375
- name: Create postgres user
@@ -87,29 +79,39 @@
8779
comment: Postgres user
8880
groups: ssl-cert
8981

90-
- name: Recursively change ownership of a directory
82+
- name: Create relevant directories
9183
file:
92-
path: /var/lib/postgresql
93-
state: directory
84+
path: '{{ item }}'
9485
recurse: yes
86+
state: directory
9587
owner: postgres
9688
group: postgres
89+
with_items:
90+
- '/etc/postgresql'
91+
- '/etc/postgresql-custom'
92+
- '/var/log/postgresql'
93+
- '/var/lib/postgresql'
9794

98-
# Create /etc/postgresql directory and make sure postgres group owns it
99-
- name: Create a directory if it does not exist
95+
- name: Allow adminapi to write custom config
10096
file:
101-
path: /etc/postgresql
97+
path: '{{ item }}'
98+
recurse: yes
10299
state: directory
103100
owner: postgres
104101
group: postgres
102+
mode: 0664
103+
with_items:
104+
- '/etc/postgresql-custom'
105105

106-
- name: Create logs dir
107-
become: yes
106+
- name: create placeholder files
108107
file:
109-
path: /var/log/postgresql
110-
state: directory
108+
path: '/etc/postgresql-custom/{{ item }}'
109+
state: touch
111110
owner: postgres
112111
group: postgres
112+
with_items:
113+
- 'generated-optimizations.conf'
114+
- 'custom-overrides.conf'
113115

114116
# Move Postgres configuration files into /etc/postgresql
115117
# Add postgresql.conf
@@ -144,12 +146,15 @@
144146
# Circumvents the following error:
145147
# "Timeout (12s) waiting for privilege escalation prompt"
146148

147-
# Add systemd file for Postgres
148-
- name: import postgresql.service
149+
- name: copy PG systemd unit
149150
template:
150151
src: files/postgresql_config/postgresql.service.j2
151152
dest: /etc/systemd/system/postgresql.service
152-
become: yes
153+
154+
- name: copy optimizations systemd unit
155+
template:
156+
src: files/generate-optimizations.service.j2
157+
dest: /etc/systemd/system/generate-optimizations.service
153158

154159
# Reload
155160
- name: System - systemd reload

common.vars.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"postgres-version": "14.1.0.15-rc0"
2+
"postgres-version": "14.1.0.15-rc1"
33
}

0 commit comments

Comments
 (0)