postgreSQL - バックアップ・リストア - pg_upgrade


 クラウディア


1. 概要
2. バックアップ
3. リストア

1. 概要

 「pg_upgrade」は、その名の通り、「PostgreSQL」バージョンアップの際、データのアップグレードを行います。  というのも「pg_dumpall」は、バックアップに要する時間は、そこまでかからないのですが、リストアの時間が、おそろしくかかります。  数十分程度ですめばいい方ですが、数時間かかることもあり、巨大なデータでは、数日かかるかもしれません。  ここでは、「FreeBSD」での作業について、記述します。  というのも、「pg_upgrade」を使用するには、新旧のバージョンが存在することが必要になるからです。  「Linux」であれば、2つのバージョンが混在することも可能なようですが、「FreeBSD」では、旧バージョンをアンインストールしないと新バージョンをインストールできないのです。  例題として「PostgreSQL 16」を「PostgreSQL 18」へアップグレードした際の内容について記述します。

2. バックアップ

 「PostgreSQL 16」をアンインストールする前に、事前準備を行います。  「pg_upgrade」が失敗するかもしれないので、それも考慮して「pg_dumlall」と物理バックアップも行っておきます。

sudo su postgres -c "pg_dumpall --column-inserts > pg_dumpall.sql"
 サーバを停止。

service postgresql stop
 物理バックアップ。

cp -R /var/db/postgres/data16 /var/db/postgres/data16-20260101-backup
 事前準備で、「PostgreSQL 16」のアプリケーションとライブラリをコピーしておきます。

mkdir -pv /tmp/pgsql16/bin /tmp/pgsql16/lib

cp -Rp /usr/local/bin/* /tmp/pgsql16/bin/

cp -Rp /usr/local/lib/postgresql /tmp/pgsql16/lib/

mkdir -p /tmp/pgsql16/share

ln -s /usr/local/share/postgresql /tmp/pgsql16/share/postgresql

3. リストア

 バックアップが終わったら、「PostgreSQL 16」をアンインストールして、「PostgreSQL 18」をインストールします。  この手順については、「メンテナンス・トラブルシュート - データベース - PostgreSQL」をご参照ください。  「PostgreSQL 18」のインストールが終わったら、まずデータ領域を作成します。

su - postgres -c "/usr/local/bin/initdb -D /var/db/postgres/data18 --no-data-checksums"
 「--no-data-checksums」をつけないと「pg_upgrade」がエラーになります。  下記のメッセージが出力されます。

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.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory /var/db/postgres/data18 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default "max_connections" ... 100
selecting default "shared_buffers" ... 128MB
selecting default time zone ... Japan
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
initdb: hint: 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/local/bin/pg_ctl -D /var/db/postgres/data18 -l logfile start
 データベースは。

/var/db/postgres/data18/
 に作成されます。  「PostgreSQL 16」のデータを「PostgreSQL 18」のものへアップグレード。  まずチェック。

su - postgres -c "LD_LIBRARY_PATH=/tmp/pgsql16/lib:/tmp/pgsql16/lib/postgresql /usr/local/bin/pg_upgrade \
  --old-bindir=/tmp/pgsql16/bin \
  --new-bindir=/usr/local/bin \
  --old-datadir=/var/db/postgres/data16 \
  --new-datadir=/var/db/postgres/data18 \
  --check"
 この結果。

Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database connection settings                         ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Checking for not-null constraint inconsistencies              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

*Clusters are compatible*
 となれば、データのアップグレード可能です。  アップグレードの実行。

su - postgres -c "LD_LIBRARY_PATH=/tmp/pgsql16/lib:/tmp/pgsql16/lib/postgresql /usr/local/bin/pg_upgrade \
  --old-bindir=/tmp/pgsql16/bin \
  --new-bindir=/usr/local/bin \
  --old-datadir=/var/db/postgres/data16 \
  --new-datadir=/var/db/postgres/data18"
 今度は、もっと長いです。

Performing Consistency Checks
-----------------------------
Checking cluster versions                                     ok
Checking database connection settings                         ok
Checking database user is the install user                    ok
Checking for prepared transactions                            ok
Checking for contrib/isn with bigint-passing mismatch         ok
Checking data type usage                                      ok
Checking for not-null constraint inconsistencies              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
------------------
Setting locale and encoding for new cluster                   ok
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
----------------
Some statistics are not transferred by pg_upgrade.
Once you start the new server, consider running these two commands:
    /usr/local/bin/vacuumdb --all --analyze-in-stages --missing-stats-only
    /usr/local/bin/vacuumdb --all --analyze-only
Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh
 メッセージはある程度の長さではありますが、数分で終わります。  「pg_dumpall」とは、比べ物にならないくらいの早さです。  これで、リストアは完了です。  サーバを起動します。

service postgresql start
 下記のメッセージが出力されます。

start postgresql
 わたしの経験では、ロール、データベースは、前のバージョンと同じものがはいっていました。  が、データが入ったものの最適化されていない状態なので、最適化します。

su - postgres -c "/usr/local/bin/vacuumdb --all --analyze-in-stages"
 この結果。

vacuumdb: processing database "database_name": Generating minimal optimizer statistics (n target)
 という行が、データベースの数だけ出ます。

4. 参考サイト

 本ページは、「Google」大先生および「Gemini」伍長を参考にさせていただきました。

audiobook.jp
【usus ウズウズ】
U-NEXT
ハイスピードプラン
TikTok Shop 【リピート用プログラム】
Amazonギフトカード
サウンドハウス
それがだいじWi-Fi
健康サポート特集
アフィリエイトのアクセストレード
ネットオークションの相場、統計、価格比較といえばオークファン
JETBOY
薬屋の独り言
損保との違い
5G CONNECT
葬送のフリーレン Prime Video
ベルリッツ
earthcar(アースカー)