From e9e44d445c08fe0f2bd42ffc3b14875cde141398 Mon Sep 17 00:00:00 2001
From: Kristian Klausen <kristian@klausen.dk>
Date: Sat, 22 Oct 2022 21:12:41 +0200
Subject: [PATCH] Migrate PostgreSQL to C.UTF-8

C.UTF-8 is installed by default, so we can avoid messing with locale-gen
by using that.

All the postgres servers (excluding matrix due to[1]) have been migrated
with the following commands:
# sudo -u postgres pg_dumpall > d
# sed "s/LOCALE = 'C'/LOCALE = 'C.UTF-8'/" -i d
# systemctl stop postgresql.service
# mv /var/lib/postgres/data{,.old}
$ ansible-playbook --diff -t postgres playbooks/<host>.yml
# sudo -u postgres psql < d

[1] https://github.com/matrix-org/synapse/blob/19a57f4a3710d6c3f7cc9d031e0e59bc2ed3b052/docs/postgres.md#fixing-incorrect-collate-or-ctype

Fix #470
---
 roles/common/tasks/main.yml                 | 5 -----
 roles/install_arch/tasks/main.yml           | 8 --------
 roles/postgres/files/upgrade_pg.sh          | 2 +-
 roles/postgres/tasks/main.yml               | 2 +-
 roles/postgres/templates/postgresql.conf.j2 | 8 ++++----
 5 files changed, 6 insertions(+), 19 deletions(-)

diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml
index 470dd51e7..8f1d75d9b 100644
--- a/roles/common/tasks/main.yml
+++ b/roles/common/tasks/main.yml
@@ -48,11 +48,6 @@
     - item.fstype == 'btrfs'
     - not 'backup' in item.mount
 
-- name: Generate locales
-  locale_gen: name={{ item }} state=present
-  with_items:
-    - en_US.UTF-8
-
 - name: Configure locales
   template: src=locale.conf.j2 dest=/etc/locale.conf owner=root group=root mode=0644
 
diff --git a/roles/install_arch/tasks/main.yml b/roles/install_arch/tasks/main.yml
index 07b4adb26..0a664e124 100644
--- a/roles/install_arch/tasks/main.yml
+++ b/roles/install_arch/tasks/main.yml
@@ -127,14 +127,6 @@
 - name: Mount /dev to new chroot  # noqa command-instead-of-module
   command: mount --rbind /dev /mnt/dev creates=/mnt/dev/zero
 
-- name: Configure locale.gen
-  lineinfile: dest=/mnt/etc/locale.gen line="en_US.UTF-8 UTF-8" owner=root group=root mode=0644
-
-- name: Run locale-gen inside chroot
-  command: chroot /mnt locale-gen
-  register: chroot_locale_gen
-  changed_when: "chroot_locale_gen.rc == 0"
-
 - name: Run systemd-firstboot
   command: chroot /mnt systemd-firstboot --locale=C.UTF-8 --timezone=UTC --hostname={{ hostname }}
   register: chroot_systemd_firstboot
diff --git a/roles/postgres/files/upgrade_pg.sh b/roles/postgres/files/upgrade_pg.sh
index 460f07b58..5ed4de9cf 100755
--- a/roles/postgres/files/upgrade_pg.sh
+++ b/roles/postgres/files/upgrade_pg.sh
@@ -46,7 +46,7 @@ chown postgres: /var/lib/postgres/data
 chattr -f +C /var/lib/postgres/data || :
 
 # initialize the new cluster
-su - postgres -c 'initdb --locale en_US.UTF-8 -E UTF8 -D /var/lib/postgres/data'
+su - postgres -c 'initdb --locale C.UTF-8 -E UTF8 -D /var/lib/postgres/data'
 
 # copy existing configuration and SSL certs from the old cluster
 for f in pg_hba.conf postgresql.conf {fullchain,chain,privkey}.pem; do
diff --git a/roles/postgres/tasks/main.yml b/roles/postgres/tasks/main.yml
index 2dc0940ad..c46fa8660 100644
--- a/roles/postgres/tasks/main.yml
+++ b/roles/postgres/tasks/main.yml
@@ -21,7 +21,7 @@
   become: true
   become_user: postgres
   become_method: su
-  command: initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'
+  command: initdb --locale C.UTF-8 -E UTF8 -D '/var/lib/postgres/data'
   args:
     chdir: /var/lib/postgres
     creates: /var/lib/postgres/data/postgresql.conf
diff --git a/roles/postgres/templates/postgresql.conf.j2 b/roles/postgres/templates/postgresql.conf.j2
index 4441c7360..58d082b52 100644
--- a/roles/postgres/templates/postgresql.conf.j2
+++ b/roles/postgres/templates/postgresql.conf.j2
@@ -707,11 +707,11 @@ timezone = 'UTC'
 					# encoding
 
 # These settings are initialized by initdb, but they can be changed.
-lc_messages = 'en_US.UTF-8'			# locale for system error message
+lc_messages = 'C.UTF-8'			# locale for system error message
 					# strings
-lc_monetary = 'en_US.UTF-8'			# locale for monetary formatting
-lc_numeric = 'en_US.UTF-8'			# locale for number formatting
-lc_time = 'en_US.UTF-8'				# locale for time formatting
+lc_monetary = 'C.UTF-8'			# locale for monetary formatting
+lc_numeric = 'C.UTF-8'			# locale for number formatting
+lc_time = 'C.UTF-8'				# locale for time formatting
 
 # default configuration for text search
 default_text_search_config = 'pg_catalog.english'
-- 
GitLab