PostgreSQL on OpenBSD: 14 から 15 へのアップグレード - pg_upgrade 使用

作成
( 更新 )
@nabbisen

はじめに

OpenBSD はすばらしいドキュメンテーションをプロジェクトの一環として提供してくれています。 (例えば man ページ (英語) は丁寧に維持されています。) これは OS だけで無く主要なサービス・パッケージにもあてはまります。PostgreSQL についても同様です。

さて PostgreSQL の pkg-readme (/usr/local/share/doc/pkg-readmes/postgresql-server) によると、データベースをアップグレードするのには 2 つの方法があるということです:

  1. pg_dumpall
  2. pg_upgrade

前者は相対的に低速で巨大なデータベースには向きません。しかしパッケージの追加が不要で済みます。後者はその逆です。

前者についての記事を過去に書きました。そこで PostgreSQLを 12 から 13 にアップグレードしました。今回は後者の番です。

環境

  • OS: OpenBSD 7.3 (<- 7.2)
  • DB: PostgreSQL 15 (<- 14)

チュートリアル

ステップ・バイ・ステップ式のガイド形式で記述します。始めましょう。

バックアップ (必要な場合)

バックアップが気になる場合 pg_dumpall で取得できます:

$ pg_dumpall > pg.sqldump 

ここでデータベースの数だけ Password: とたずねられるでしょう。

pg_upgrade のインストール

Ports パッケージ・システムから取得します:

$ doas pkg_add postgresql-pg_upgrade

結果は以下の通りでした:

quirks-6.42 signed on 2023-04-06T19:16:59Z
postgresql-pg_upgrade-14.5:postgresql-previous-13.5p0: ok
postgresql-pg_upgrade-14.5: ok

OBSD 7.2 -> 7.3 ならびに PG パッケージのアップグレード

PostgreSQL では無く OpenBSD のアップグレードに関わる節です。

sysupgrade / sysmerge / pkg_add -u をこの順番で実行します。 詳細は こちらの記事 に書いています。

さあ、上記の手順の中で、PostgreSQL について以下のように問われるでしょう:

postgresql-server-15.2p0: Updating across a major version - data migration needed, see the pkg-readme.
Do you want to update now ? [y/N/a] y

“y” を入力して、PostgreSQL のパッケージ (データベース自体では無く) をアップグレードします。以下のように出力されるでしょう:

postgresql-client+postgresql-contrib+postgresql-pg_upgrade+postgresql-server-14.5->postgresql-client-15.2+postgresql-contrib-15.2+postgresql-pg_upgrade-15.2+postgresql-server-15.2p0: ok
(...)
New and changed readme(s):
	/usr/local/share/doc/pkg-readmes/postgresql-server

デーモンの停止

サーバーを停止して準備しましょう:

$ doas rcctl stop postgresql

結果は以下の通りでした:

postgresql(ok)

準備完了です。データベースをアップグレードしましょう !!

PostgreSQL 15 クラスターの作成

まずは data ディレクトリを作成しましょう。 現行のものを移動します:

$ doas mv /var/postgresql/data /var/postgresql/data-14

そして mkdir で新たに作成します。さらに PostgreSQL の initdb を実行します:

$ # doas su _postgresql -c "mkdir /var/postgresql/data && cd /var/postgresql && \
        initdb -D /var/postgresql/data -U postgres -A scram-sha-256 -E UTF8 -W"
$ doas su _postgresql -c "mkdir /var/postgresql/data && cd /var/postgresql && \
      initdb -D /var/postgresql/data -U postgres -A scram-sha-256 -E UTF8 --locale=ja_JP.UTF-8 -W"

上記の内、前者は pkg-readme の内容に由来します。後者は私のケースに適用したもので、--locale=(...) オプションを追加しました。

結果は以下の通りでした:

The files belonging to this database system will be owned by user "_postgresql".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

Enter new superuser password: 
Enter it again: 

PostgreSQL スーパーユーザーのパスワードを二度入力します。以下のように続きます:

fixing permissions on existing directory /var/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 20
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

Success. You can now start the database server using:

    pg_ctl -D /var/postgresql/data -l logfile start

やりました。データベースが初期化されて諸々のファイルが作成されました。

マイグレーション向け設定

この節の手順はデータ・マイグレーションのための一時的なものたちです。

現行 (と次バージョン) の pg_hba.conf を編集します:

$ doas nvim /var/postgresql/data-14/pg_hba.conf

スーパーユーザーからの local 接続を許容するためです:

  # TYPE  DATABASE        USER            ADDRESS                 METHOD
+ local all postgres trust

注意すべきことがあります。こちらの行は先頭に配置すべきです。優先度を最も高くするためです。

それから pg_hba.conf を含む以下のファイルたちを 15 すなわち最新版の data ディレクトリにコピーします:

$ doas cp -p /var/postgresql/data-14/pg_hba.conf /var/postgresql/data/

$ # in addition, in case that tls is used:
$ doas cp -p /var/postgresql/data-14/server.{crt,key} /var/postgresql/data/

pg_upgrade の実行

OK です。データベースは今やアップグレードされるのを待っています !!! 次のコマンドを実行しましょう (このソフトウェアと pkg-readme に感謝しつつ):

$ doas su _postgresql -c "cd /var/postgresql && \
      pg_upgrade -b /usr/local/bin/postgresql-14/ -B /usr/local/bin \
      -U postgres -d /var/postgresql/data-14/ -D /var/postgresql/data"

私の実行時、結果は以下の通りでした:

Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_xact to new server                           ok
Setting oldest XID for new cluster                          ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new cluster
                                                            ok
Copying user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to delete old cluster                       ok
Checking for extension updates                              ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
    /usr/local/bin/vacuumdb -U postgres --all --analyze-in-stages

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

成功です :)

設定のリストア

一時的に追加した行を削除して、設定をリストアしましょう:

$ doas nvim /var/postgresql/data/pg_hba.conf

$ # in addition, if you care about restoring the previous one:
$ doas nvim /var/postgresql/data/pg_hba.conf

以下のようにします:

  # TYPE  DATABASE        USER            ADDRESS                 METHOD
- local all postgres trust

PostgreSQL 15 向け設定 (必要な場合)

postgresql.conf その他に独自の設定がある場合、それらを適用します:

$ # create the backup (optional):
$ doas cp -p /var/postgresql/data/postgresql.conf /var/postgresql/data/postgresql.conf.org

$ doas nvim /var/postgresql/data/postgresql.conf

以下のような感じです (一例です):

  (...)
+ listen_addresses = '*'
  (...)
+ ssl = on
  (...)

なお postgresql.conf については、オリジナルの設定ファイルが /usr/local/share/postgresql/postgresql.conf.sample にあります。

アップグレードのための操作がすべて終わりました。

デーモンの起動

データベース・サーバーを起動しましょう:

$ doas rcctl start postgresql

うまく行くことを願っています :)

バッケージの削除 (任意)

追加したパッケージに対して、うまく動いてくれたことに感謝しつつ、別れを告げることもできます:

$ doas pkg_delete postgresql-pg_upgrade

結果は以下の通りでした:

postgresql-pg_upgrade-15.2: ok

おわりに

上記の手順を通して、OpenBSD を 7.2 から 7.3 にアップグレードするとともに、PostgreSQL を 14 から 15 へアップグレードできます。

余談ですが data-14 ディレクトリはいずれ “old” 扱いになるかもしれません。

最新の OpenBSD で最新の PostgreSQL を動かすことであなたに最高に幸せな時間が訪れますように 🌟


Comments or feedbacks are welcomed and appreciated.