環境
- Windows10+WSL2+Ubuntu22.04+PostgreSQL14.
TL;DR
- 一応以下コマンドでできる
/usr/lib/postgresql/14/bin/initdb -E UTF8 --no-locale -D /var/lib/postgresql/14/main/
- apt installしたPostgreSQLはロケールがja_JP.UTF-8になってると勘違いしてたけど、自分の環境見ると、実は元からC.UTF-8が設定されていた(C.UTF-8はCロケールの拡張で、ソートはCと同じくバイト数に基づいて実行される)。なので意味なかった。単に検証めも
変更前(初期状態)
- TL;DR記載のとおり、実はすでにソートパフォーマンスに影響するCollate/CtypeはC.UTF-8(CのUTF-8拡張)になってるので対応不要だった感
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-----------------------
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
postgres=# SELECT name, setting, context FROM pg_settings WHERE name LIKE 'lc%';
name | setting | context
-------------+---------+-----------
lc_collate | C.UTF-8 | internal
lc_ctype | C.UTF-8 | internal
lc_messages | C.UTF-8 | superuser
lc_monetary | C.UTF-8 | user
lc_numeric | C.UTF-8 | user
lc_time | C.UTF-8 | user
変更
PostgreSQL停止と既存のDBクラスタのバックアップ
systemctl stop postgresql #rootで実行
cd /var/lib/postgresql/14
mv main main.org
initdb
/usr/lib/postgresql/14/bin/initdb -E UTF8 --no-locale -D /var/lib/postgresql/14/nolocale
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".
Data page checksums are disabled.
creating directory /var/lib/postgresql/14/nolocale ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Tokyo
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
/usr/lib/postgresql/14/bin/pg_ctl -D /var/lib/postgresql/14/nolocale -l logfile start
PostgreSQL起動
systemctl start postgresql #rootで実行
変更後
各DBのCollate/Ctypeが、C.UTF-8から、Cになった
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
postgres=# SELECT name, setting, context FROM pg_settings WHERE name LIKE 'lc%';
name | setting | context
-------------+---------+-----------
lc_collate | C | internal
lc_ctype | C | internal
lc_messages | C.UTF-8 | superuser
lc_monetary | C.UTF-8 | user
lc_numeric | C.UTF-8 | user
lc_time | C.UTF-8 | user
その他
バックアップした元DBクラスタのmain.orgディレクトリは、動作確認後に削除する
各パラメータについて
lc_collate/lc_ctypeがCになっていればソートパフォーマンス的には良い。
ld_messages以下は、ログ出力とか金銭/数値/時刻表示についてのロケールなのでソートパフォーマンスに影響しない。