データベース技術調査ブログ

LinuxやPostgreSQL、Oracleデータベース、AWSの知識をアウトプットしていきます

【PostgreSQL】OSS-DB Exam Silver 技術解説無料セミナー(PostgreSQLのバックアップ方法)を受講

LPIが開催するOSS-DB Exam Silverのオンラインセミナーを受講した感想です。最後に検証環境での実行ログも残しておきます。参考にどうぞ

【オンライン開催】OSS-DB Exam Silver 技術解説無料セミナー
PostgreSQLのバックアップ方法
(2020/7/19(日) 13:00~14:15)
connpass.com


資料は以下からダウンロードできます。
https://fo-pro.s3.ap-northeast-1.amazonaws.com/files/5f0fb75b37471d78510554e4/20200719-silver-01.pdf



目次

セミナーの内容

講師はSRA OSS, Inc. 日本支社の正野裕大さんでした。
SRA OSS, IncはPowerGresというPostgreSQLをベースとして商用DBとして使えるようにいろいろ機能やサポートを追加した製品も提供していたりします。PostgreSQLのライセンスは独特な体系ですが、こういう商用版を作りやすいライセンス体系だった気がします。
SRA OSS, Incの回し者でないのですが、OSS-DB Goldレベルのセミナーをやっているのはここくらいしかないと思います。(有償テキストとかもその流れで提供していたと思います。Goldの有償テキストもSRA OSS, Incが出しているテキストくらいしかなかったかと)
archegirl.net


セミナー動画はLPIのYoutubeチャンネルに投稿されています。
www.youtube.comhttps://www.youtube.com/watch?v=BUInKr22dD4



内容はもっぱらOSS-DB Silverの中で重要度でいうと上から2番目のバックアップ・リストアに関する内容です。
私は事前にこの本で勉強していたのですっと内容が入っていきました。(そうでないとやや消化不良かなってなるかもしれないです…それくらい中身が濃いってことですね。)
・論理バックアップとしてpg_dumpやpg_dumpallを説明
・物理バックアップとしてコールド版とオンライン版を説明
・PITRのやり方を説明

基本的にすべて検証環境で実行しながらの説明があったので、ここでは以下の環境を利用して同じように演習してみたいと思います。
実行ログまで残すので参考になればと幸いです。
jimatomo.hatenablog.com



演習

演習用のDBクラスタに切り替え

演習環境として別のDBクラスタを使用したいと思います。
PGDATAを一時的に書き換えましょう。

[root@cent82-pg12-01 ~]# su - postgres
[postgres@cent82-pg12-01 ~]$ id
uid=1000(postgres) gid=1000(postgres) groups=1000(postgres),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[postgres@cent82-pg12-01 ~]$ env | grep PGDATA
PGDATA=/usr/local/pgsql/data
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # PGDATAを演習環境用に書き換え
[postgres@cent82-pg12-01 ~]$ export PGDATA=/usr/local/pgsql/data2
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ env | grep PGDATA
PGDATA=/usr/local/pgsql/data2
[postgres@cent82-pg12-01 ~]$

元のデータベースクラスタは停止しておきましょう

[postgres@cent82-pg12-01 ~]$ # 元のDBクラスタが起動しているか確認
[postgres@cent82-pg12-01 ~]$ pg_ctl status -D /usr/local/pgsql/data
pg_ctl: server is running (PID: 14524)
/usr/local/pgsql/bin/postgres
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # 起動している場合は停止する
[postgres@cent82-pg12-01 ~]$ pg_ctl stop -D /usr/local/pgsql/data
waiting for server to shut down.... done
server stopped
[postgres@cent82-pg12-01 ~]$

下準備

initdbの実行
[postgres@cent82-pg12-01 ~]$ # initdbの実行
[postgres@cent82-pg12-01 ~]$ # --no-locale:ロケールを使用しない(慣習的にロケールを使用しないことが推奨される。ロケールは共通の仕様がないため移植性が落ちるなどの理由)
[postgres@cent82-pg12-01 ~]$ # --encoding==ENCODING:データベースのデフォルト文字エンコーディングを指定
[postgres@cent82-pg12-01 ~]$ # ※$PGDATAで指定しているため--pgdataは不要
[postgres@cent82-pg12-01 ~]$ initdb --no-locale --encoding=UTF8
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 /usr/local/pgsql/data2 ... 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:

    pg_ctl -D /usr/local/pgsql/data2 -l logfile start

[postgres@cent82-pg12-01 ~]$
設定の変更
[postgres@cent82-pg12-01 ~]$ # listen_addressの設定変更
[postgres@cent82-pg12-01 ~]$ grep listen_address $PGDATA/postgresql.conf
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # sedで置換しちゃいます!
[postgres@cent82-pg12-01 ~]$ sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" $PGDATA/postgresql.conf
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # listen_addressの設定変更
[postgres@cent82-pg12-01 ~]$ grep listen_address $PGDATA/postgresql.conf
listen_addresses = '*'          # what IP address(es) to listen on;
[postgres@cent82-pg12-01 ~]$
pg_ctlで起動
[postgres@cent82-pg12-01 ~]$ pg_ctl start
waiting for server to start....2020-07-19 16:09:22.233 JST [14983] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
2020-07-19 16:09:22.235 JST [14983] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-07-19 16:09:22.235 JST [14983] LOG:  listening on IPv6 address "::", port 5432
2020-07-19 16:09:22.238 JST [14983] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-07-19 16:09:22.249 JST [14984] LOG:  database system was shut down at 2020-07-19 15:35:46 JST
2020-07-19 16:09:22.252 JST [14983] LOG:  database system is ready to accept connections
 done
server started
[postgres@cent82-pg12-01 ~]$
benchmarkデータベースの作成
[postgres@cent82-pg12-01 ~]$ # benchmarkデータベースの作成
[postgres@cent82-pg12-01 ~]$ createdb benchmark
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 benchmark | postgres | UTF8     | C       | C     |
 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
(4 rows)

[postgres@cent82-pg12-01 ~]$
pgbenchでバックアップ対象データベースにデータを作製
[postgres@cent82-pg12-01 ~]$ # pgbenchの初期化
[postgres@cent82-pg12-01 ~]$ # --initialize:ベンチマークテーブルの初期化
[postgres@cent82-pg12-01 ~]$ # --scale=NUM:ベンチマークテーブルの規模(1で15MB程度)
[postgres@cent82-pg12-01 ~]$ # 第一引数:pgbenchの対象データベース
[postgres@cent82-pg12-01 ~]$ pgbench --initialize --scale=30 benchmark
dropping old tables...
NOTICE:  table "pgbench_accounts" does not exist, skipping
NOTICE:  table "pgbench_branches" does not exist, skipping
NOTICE:  table "pgbench_history" does not exist, skipping
NOTICE:  table "pgbench_tellers" does not exist, skipping
creating tables...
generating data...
100000 of 3000000 tuples (3%) done (elapsed 0.07 s, remaining 1.94 s)

(中略)

3000000 of 3000000 tuples (100%) done (elapsed 3.93 s, remaining 0.00 s)
vacuuming...
creating primary keys...
done.
[postgres@cent82-pg12-01 ~]$

論理バックアップ

pg_dumpでのテキスト形式のバックアップ
[postgres@cent82-pg12-01 ~]$ # 論理バックアップ > pg_dump
[postgres@cent82-pg12-01 ~]$ # --file=FILENAME:バックアップの出力先
[postgres@cent82-pg12-01 ~]$ # 第一引数:バックアップ対象データベース
[postgres@cent82-pg12-01 ~]$ pg_dump --file=benchmark.sql benchmark
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls -l
合計 285152
-rw-rw-r--. 1 postgres postgres 291995553  719 16:30 benchmark.sql
[postgres@cent82-pg12-01 ~]$
pg_dumpallでのバックアップ
[postgres@cent82-pg12-01 ~]$ # 論理バックアップ > pg_dump
[postgres@cent82-pg12-01 ~]$ # --file=FILENAME:バックアップの出力先
[postgres@cent82-pg12-01 ~]$ pg_dumpall --file=db_all.sql
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls -l
合計 570308
-rw-rw-r--. 1 postgres postgres 291995553  719 16:30 benchmark.sql
-rw-rw-r--. 1 postgres postgres 291997617  719 16:35 db_all.sql
[postgres@cent82-pg12-01 ~]$

テキスト形式なのでless等を使用して中身が見れます。pg_dumpとpg_dumpallの違いはdiffをとってみるとわかります。(pg_dumpall側だけにあるSQLが出力される)

[postgres@cent82-pg12-01 ~]$ # pg_dumpall側だけに出力されるテキスト
[postgres@cent82-pg12-01 ~]$ diff benchmark.sql db_all.sql
1a2,58
> -- PostgreSQL database cluster dump
> --
>
> SET default_transaction_read_only = off;
>
> SET client_encoding = 'UTF8';
> SET standard_conforming_strings = on;
>
> --
> -- Roles
> --
>
> CREATE ROLE postgres;
> ALTER ROLE postgres WITH SUPERUSER INHERIT CREATEROLE CREATEDB LOGIN REPLICATION BYPASSRLS;
>
>
>
>
>
>
> --
> -- Databases
> --
>
> --
> -- Database "template1" dump
> --
>
> \connect template1
>
> --
> -- PostgreSQL database dump
> --
>
> -- Dumped from database version 12.3
> -- Dumped by pg_dump version 12.3
>
> SET statement_timeout = 0;
> SET lock_timeout = 0;
> SET idle_in_transaction_session_timeout = 0;
> SET client_encoding = 'UTF8';
> SET standard_conforming_strings = on;
> SELECT pg_catalog.set_config('search_path', '', false);
> SET check_function_bodies = false;
> SET xmloption = content;
> SET client_min_messages = warning;
> SET row_security = off;
>
> --
> -- PostgreSQL database dump complete
> --
>
> --
> -- Database "benchmark" dump
> --
>
> --
18a76,97
> --
> -- Name: benchmark; Type: DATABASE; Schema: -; Owner: postgres
> --
>
> CREATE DATABASE benchmark WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C' LC_CTYPE = 'C';
>
>
> ALTER DATABASE benchmark OWNER TO postgres;
>
> \connect benchmark
>
> SET statement_timeout = 0;
> SET lock_timeout = 0;
> SET idle_in_transaction_session_timeout = 0;
> SET client_encoding = 'UTF8';
> SET standard_conforming_strings = on;
> SELECT pg_catalog.set_config('search_path', '', false);
> SET check_function_bodies = false;
> SET xmloption = content;
> SET client_min_messages = warning;
> SET row_security = off;
>
3000470a3000550,3000581
> --
>
> --
> -- Database "postgres" dump
> --
>
> \connect postgres
>
> --
> -- PostgreSQL database dump
> --
>
> -- Dumped from database version 12.3
> -- Dumped by pg_dump version 12.3
>
> SET statement_timeout = 0;
> SET lock_timeout = 0;
> SET idle_in_transaction_session_timeout = 0;
> SET client_encoding = 'UTF8';
> SET standard_conforming_strings = on;
> SELECT pg_catalog.set_config('search_path', '', false);
> SET check_function_bodies = false;
> SET xmloption = content;
> SET client_min_messages = warning;
> SET row_security = off;
>
> --
> -- PostgreSQL database dump complete
> --
>
> --
> -- PostgreSQL database cluster dump complete
[postgres@cent82-pg12-01 ~]$

DBクラスタの設定が引っ張られるのでロールやDB作成辺りまで出力されていることがわかる
逆に言うと次の演習のようにpg_dumpではDBの指定をしないといけないのです。事前にDBが作成されている必要があります。


っと、その前に…

pg_dumpでのカスタム形式のバックアップの取得
[postgres@cent82-pg12-01 ~]$ # pg_dumpでのカスタム形式のバックアップ
[postgres@cent82-pg12-01 ~]$ pg_dump --format=c --file=benchmark.dump benchmark
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls -l
合計 578556
-rw-rw-r--. 1 postgres postgres   8443356  719 20:50 benchmark.dump
-rw-rw-r--. 1 postgres postgres 291995553  719 16:30 benchmark.sql
-rw-rw-r--. 1 postgres postgres 291997617  719 16:35 db_all.sql
[postgres@cent82-pg12-01 ~]$

カスタム形式は圧倒的にサイズが小さいことがわかる。

pg_dumpしたバックアップデータのリストア方法
[postgres@cent82-pg12-01 ~]$ # テキスト形式のリストア(psql)
[postgres@cent82-pg12-01 ~]$ # リストア先のDBを作成
[postgres@cent82-pg12-01 ~]$ createdb benchmark02-t
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ psql --file=benchmark.sql benchmark02-t
SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
COPY 3000000
COPY 30
COPY 0
COPY 300
ALTER TABLE
ALTER TABLE
ALTER TABLE
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # カスタム形式(pg_restore)
[postgres@cent82-pg12-01 ~]$ # リストア先のDBを作成
[postgres@cent82-pg12-01 ~]$ createdb benchmark02-c
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ pg_restore --dbname=benchmark02-c benchmark.dump
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # データの確認
[postgres@cent82-pg12-01 ~]$ psql benchmark -c "SELECT count(*) FROM pgbench_accounts"
  count
---------
 3000000
(1 row)

[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ psql benchmark02-t -c "SELECT count(*) FROM pgbench_accounts"
  count
---------
 3000000
(1 row)

[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ psql benchmark02-c -c "SELECT count(*) FROM pgbench_accounts"
  count
---------
 3000000
(1 row)

[postgres@cent82-pg12-01 ~]$
pg_dumpallしたバックアップデータのリストア方法
[postgres@cent82-pg12-01 ~]$ # 1) 既存のPostgreSQLを停止
[postgres@cent82-pg12-01 ~]$ pg_ctl stop
waiting for server to shut down....2020-07-19 21:24:29.354 JST [14983] LOG:  received fast shutdown request
2020-07-19 21:24:29.360 JST [14983] LOG:  aborting any active transactions
2020-07-19 21:24:29.361 JST [14983] LOG:  background worker "logical replication launcher" (PID 14990) exited with exit code 1
2020-07-19 21:24:29.364 JST [14985] LOG:  shutting down
2020-07-19 21:24:29.409 JST [14983] LOG:  database system is shut down
 done
server stopped
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # 2) 既存のデータベースクラスタは退避(または削除)
[postgres@cent82-pg12-01 ~]$ mv $PGDATA $PGDATA.back01
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls -ld $PGDATA
ls: '/usr/local/pgsql/data2' にアクセスできません: そのようなファイルやディレクトリはありません
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls -ld $PGDATA.back01
drwx------. 19 postgres postgres 4096  719 21:24 /usr/local/pgsql/data2.back01
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # 3) データベースクラスタの初期化と設定ファイルのリストア
[postgres@cent82-pg12-01 ~]$ initdb --no-locale --encoding=UTF8
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 /usr/local/pgsql/data2 ... 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:

    pg_ctl -D /usr/local/pgsql/data2 -l logfile start

[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ cp $PGDATA.back01/postgresql.conf $PGDATA
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # 4) 起動してpsqlコマンドでリストア
[postgres@cent82-pg12-01 ~]$ pg_ctl start
waiting for server to start....2020-07-19 21:34:46.942 JST [16265] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
2020-07-19 21:34:46.943 JST [16265] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-07-19 21:34:46.943 JST [16265] LOG:  listening on IPv6 address "::", port 5432
2020-07-19 21:34:46.949 JST [16265] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-07-19 21:34:46.971 JST [16266] LOG:  database system was shut down at 2020-07-19 21:33:10 JST
2020-07-19 21:34:46.977 JST [16265] LOG:  database system is ready to accept connections
 done
server started
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ psql --file=db_all.sql postgres
SET
SET
SET
2020-07-19 21:34:55.966 JST [16274] ERROR:  role "postgres" already exists
2020-07-19 21:34:55.966 JST [16274] STATEMENT:  CREATE ROLE postgres;
psql:db_all.sql:14: ERROR:  role "postgres" already exists
ALTER ROLE
You are now connected to database "template1" as user "postgres".
SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
SET
SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
SET
CREATE DATABASE
ALTER DATABASE
You are now connected to database "benchmark" as user "postgres".
SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
COPY 3000000
COPY 30
COPY 0
COPY 300
ALTER TABLE
ALTER TABLE
ALTER TABLE
You are now connected to database "postgres" as user "postgres".
SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
SET
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ psql -l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 benchmark | postgres | UTF8     | C       | C     |
 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
(4 rows)

[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # 5) データの確認
[postgres@cent82-pg12-01 ~]$ psql benchmark -c "SELECT count(*) FROM pgbench_accounts"
  count
---------
 3000000
(1 row)

[postgres@cent82-pg12-01 ~]$

論理バックアップはデータのバックアップなので、設定ファイル等を戻す必要があるといことをこの演習から学び取ることができます。

物理バックアップ

コールドバックアップ

→演習は割愛(単にDB停止してコピーして起動するという手順。戻すときも同じ)

オンラインバックアップ

◆pg_basebackupによるオンラインバックアップ

[postgres@cent82-pg12-01 ~]$ # pg_basebackupによるオンラインバックアップ
[postgres@cent82-pg12-01 ~]$ # 起動するデータベースの状態を確認
[postgres@cent82-pg12-01 ~]$ pg_ctl status
pg_ctl: server is running (PID: 16265)
/usr/local/pgsql/bin/postgres
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # --pgdata=DIRECTORY:物理バックアップの出力先
[postgres@cent82-pg12-01 ~]$ pg_basebackup --pgdata=$PGDATA.bak02
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls -l $PGDATA.bak02
合計 56
-rw-------. 1 postgres postgres     3  719 22:06 PG_VERSION
-rw-------. 1 postgres postgres   226  719 22:06 backup_label
drwx------. 6 postgres postgres    54  719 22:06 base
drwx------. 2 postgres postgres  4096  719 22:06 global
drwx------. 2 postgres postgres     6  719 22:06 pg_commit_ts
drwx------. 2 postgres postgres     6  719 22:06 pg_dynshmem
-rw-------. 1 postgres postgres  4513  719 22:06 pg_hba.conf
-rw-------. 1 postgres postgres  1636  719 22:06 pg_ident.conf
drwx------. 4 postgres postgres    68  719 22:06 pg_logical
drwx------. 4 postgres postgres    36  719 22:06 pg_multixact
drwx------. 2 postgres postgres     6  719 22:06 pg_notify
drwx------. 2 postgres postgres     6  719 22:06 pg_replslot
drwx------. 2 postgres postgres     6  719 22:06 pg_serial
drwx------. 2 postgres postgres     6  719 22:06 pg_snapshots
drwx------. 2 postgres postgres     6  719 22:06 pg_stat
drwx------. 2 postgres postgres     6  719 22:06 pg_stat_tmp
drwx------. 2 postgres postgres     6  719 22:06 pg_subtrans
drwx------. 2 postgres postgres     6  719 22:06 pg_tblspc
drwx------. 2 postgres postgres     6  719 22:06 pg_twophase
drwx------. 3 postgres postgres    60  719 22:06 pg_wal
drwx------. 2 postgres postgres    18  719 22:06 pg_xact
-rw-------. 1 postgres postgres    88  719 22:06 postgresql.auto.conf
-rw-------. 1 postgres postgres 26580  719 22:06 postgresql.conf
[postgres@cent82-pg12-01 ~]$

このセミナーでは触れられていませんが、pg_basebackupはストリーミングレプリケーションでスタンバイのDBを作成する際にも使用されるコマンドです。


◆物理バックアップ > 低レベルAPIを使ったオンラインバックアップ
pg_start_backup関数とpg_stop_backup関数を使用したバックアップ
→演習は割愛(このあたりの検証手順は別途記事を書こうと思っています。)

個人的になるほどと思った内容として、この手順についての説明です。

pg_basebackupコマンド以前の古い手法だが、クラウド環境や仮想環境では使われやすい
→物理コピーを仮想化基盤のスナップショットに任せられる

今やpg_basebackupが登場してから仕組みを知るために演習したことはあっても実際には使わないのかなと思っていましたが、この説明を聞いて、確かにそうだな~と思いました。ただ、クラウド環境だとAWSのRDS的なマネージドサービスとしてバックアップ手法が提供されていたりするので、マネージドサービスで要件を満たせる場合はそもそも使われないのですが…

物理バックアップのリストア

単純にコピーするだけでリストアが完了します。
後はpg_ctlで起動するだけ。
→演習は割愛

PITR (Point In Time Recovery)

WAL自体の説明はセミナーに譲って、ちょっとWALという仕組みの背景的なところの私なりの解釈を残しておこうかと思います。
RDBMSは枯れた技術でそのデータの信頼性の高さが特徴の一つです。一方で性能のボトルネックになりがちな物理I/Oを減らして如何にメモリ上の処理に完結させるかという点が性能のチューニングの一番のポイントでもあります。メモリは揮発性のストレージなので、クラッシュした瞬間にデータが消えてしまいます。片方を立てるともう片方が立たなくなる性能と信頼性の妥協点としてWALという仕組みが実装されています。書き込みのログを単純なシーケンシャルにすることでI/Oの負荷を下げるという戦略を取っているわけです。
このWALを適用してメモリとストレージの差分を埋める操作こそがリカバリです。クラッシュリカバリという観点ではチェックポイント処理の間隔。PITRとしてはベースバックアップの取得の間隔。これらの調整がリカバリ時間に影響したりします。
話はそれますが、AWSのAurora PostgreSQLクラウド時代のストレージの性質とWALの仕組みを考え直しているので、この辺りの信頼性を保ちつつ性能のボトルネックを避ける仕組みを比較してみると面白いです。(すでにこの辺りの違いは誰かがまとめてくれているので検索してみるといいと思います…わかりやすいかどうかは置いておいて…)


ここではベースバックアップからWALを適用して任意の地点までに戻すという手順を演習していきます。

ベースバックアップとアーカイブログの格納領域を作製
[postgres@cent82-pg12-01 ~]$ # ベースバックアップとアーカイブログの格納領域を作製
[postgres@cent82-pg12-01 ~]$ ls -l $PGHOME/backups
ls: '/usr/local/pgsql/backups' にアクセスできません: そのようなファイルやディレクトリはありません
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ mkdir -p $PGHOME/backups/base    # ベースバックアップ
[postgres@cent82-pg12-01 ~]$ mkdir -p $PGHOME/backups/arc     # アーカイブログ
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls -l $PGHOME/backups
合計 0
drwxrwxr-x. 2 postgres postgres 6  720 19:10 arc
drwxrwxr-x. 2 postgres postgres 6  720 19:10 base
[postgres@cent82-pg12-01 ~]$
WALアーカイブの設定
[postgres@cent82-pg12-01 ~]$ # WALアーカイブの設定
[postgres@cent82-pg12-01 ~]$ # postgresql.confの編集と適用
[postgres@cent82-pg12-01 ~]$ grep archive_ $PGDATA/postgresql.conf
#archive_mode = off             # enables archiving; off, on, or always
#archive_command = ''           # command to use to archive a logfile segment
#archive_timeout = 0            # force a logfile segment switch after this
#archive_cleanup_command = ''   # command to execute at every restartpoint
#max_standby_archive_delay = 30s        # max delay before canceling queries
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ sed -i "s/#archive_mode = off/archive_mode = on/g" $PGDATA/postgresql.conf
[postgres@cent82-pg12-01 ~]$ cat << 'EOF' >> $PGDATA/postgresql.conf
> archive_command = 'cp "%p" "/usr/local/pgsql/backups/arc/%f"'
> EOF
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ grep archive_ $PGDATA/postgresql.conf
archive_mode = on               # enables archiving; off, on, or always
#archive_command = ''           # command to use to archive a logfile segment
#archive_timeout = 0            # force a logfile segment switch after this
#archive_cleanup_command = ''   # command to execute at every restartpoint
#max_standby_archive_delay = 30s        # max delay before canceling queries
archive_command = 'cp "%p" "/usr/local/pgsql/backups/arc/%f"'
[postgres@cent82-pg12-01 ~]$

今回変更したパラメータは再起動が必要なパラメータです。pg_ctl restartで再起動しましょう。

[postgres@cent82-pg12-01 ~]$ # postgresql.confの反映
[postgres@cent82-pg12-01 ~]$ pg_ctl restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2020-07-21 03:34:36.600 JST [23134] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
2020-07-21 03:34:36.602 JST [23134] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-07-21 03:34:36.602 JST [23134] LOG:  listening on IPv6 address "::", port 5432
2020-07-21 03:34:36.607 JST [23134] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-07-21 03:34:36.621 JST [23135] LOG:  database system was shut down at 2020-07-21 03:34:36 JST
2020-07-21 03:34:36.624 JST [23134] LOG:  database system is ready to accept connections
 done
server started
[postgres@cent82-pg12-01 ~]$

ちなみに再起動が必要かどうかはドキュメント見てもいいですが、pg_settingsシステムビューを参照しても確認できます。

[postgres@cent82-pg12-01 ~]$ psql -qc "SELECT name, setting, boot_val, context FROM pg_settings WHERE  name LIKE '%archive%';"
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".
           name            |                  setting                  | boot_val |  context
---------------------------+-------------------------------------------+----------+------------
 archive_cleanup_command   |                                           |          | sighup
 archive_command           | cp "%p" "/usr/local/pgsql/backups/arc/%f" |          | sighup
 archive_mode              | on                                        | off      | postmaster
 archive_timeout           | 0                                         | 0        | sighup
 max_standby_archive_delay | 30000                                     | 30000    | sighup
(5 rows)

[postgres@cent82-pg12-01 ~]$

context列を見ると、archive_modeの行の値がpostmasterになっています。
ドキュメントを見てみましょう(51.86. pg_settings

postmaster
これらの設定はサーバ起動時にのみ適用することができます。 このため何かを変更するためにはサーバを再起動しなければなりません。 これらの設定用の値は通常postgresql.confファイル内に格納されている、あるいは、サーバを起動する際のコマンドラインから渡されます。 当然ながら、より低い種類のcontextを持つ設定もサーバ起動時に設定することができます。

ちなみにsignupはこうです。

sighup
これらの設定は、サーバを再起動することなくpostgresql.conf内を変更することで行うことができます。 postgresql.confを再度読み込み、変更を適用させるためには、postmasterにSIGHUPシグナルを送信してください。 すべての子プロセスが新しい値を選択するように、postmasterは同時に子プロセスにSIGHUPシグナルを転送します。

signupのものはpg_ctl reloadで更新することができます。

ベースバックアップの取得
[postgres@cent82-pg12-01 ~]$ # $PGDATAの物理バックアップ
[postgres@cent82-pg12-01 ~]$ # --wal-method=none:物理バックアップ対象にWALを含めない
[postgres@cent82-pg12-01 ~]$ pg_basebackup --pgdata=$PGHOME/backups/base/001 --wal-method=none
NOTICE:  all required WAL segments have been archived
[postgres@cent82-pg12-01 ~]$
pgbenchでベンチマークを実行してデータ書き込みを発生させる
[postgres@cent82-pg12-01 ~]$ # pgbenchでベンチマークを実行してデータ書き込みを発生させる
[postgres@cent82-pg12-01 ~]$ # --client=NUM:ベンチマーククライアントの同時接続数
[postgres@cent82-pg12-01 ~]$ # --time=NUM:ベンチマーク実行時間(秒)
[postgres@cent82-pg12-01 ~]$ pgbench --client=10 --time=30 benchmark
starting vacuum...end.
transaction type: <builtin: TPC-B (sort of)>
scaling factor: 30
query mode: simple
number of clients: 10
number of threads: 1
duration: 30 s
number of transactions actually processed: 65578
latency average = 4.578 ms
tps = 2184.239747 (including connections establishing)
tps = 2184.371686 (excluding connections establishing)
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # 履歴テーブルから最新のデータ行を確認しておく
[postgres@cent82-pg12-01 ~]$ psql benchmark -qc "SELECT * FROM pgbench_history ORDER BY mtime LIMIT 1"
You are connected to database "benchmark" as user "postgres" via socket in "/tmp" at port "5432".
 tid | bid |  aid  | delta |           mtime            | filler
-----+-----+-------+-------+----------------------------+--------
  76 |  18 | 10886 | -2087 | 2020-07-21 08:56:08.707481 |
(1 row)

[postgres@cent82-pg12-01 ~]$
なんちゃって疑似クラッシュ

実際にテーブルのファイルをddでぶっ壊したりしてもいいですが、演習に合わせてデータベースクラスタを強制終了させます。

[postgres@cent82-pg12-01 ~]$ # ベンチマークを実行したデータベースクラスタを強制終了
[postgres@cent82-pg12-01 ~]$ # --mode=MODE:シャットダウンモードの指定
[postgres@cent82-pg12-01 ~]$ pg_ctl stop --mode=immediate
waiting for server to shut down.... done
server stopped
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ pg_ctl status
pg_ctl: no server running
[postgres@cent82-pg12-01 ~]$
強制終了したデータベースクラスタを退避
[postgres@cent82-pg12-01 ~]$ # 強制終了したデータベースクラスタを退避
[postgres@cent82-pg12-01 ~]$ mv $PGDATA $PGDATA.crash
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls -l $PGHOME
合計 32
drwxrwxr-x.  4 postgres postgres   29  720 19:10 backups
drwxrwxr-x.  2 postgres postgres 4096  719 12:16 bin
drwx------. 19 postgres postgres 4096  719 15:27 data
drwx------. 19 postgres postgres 4096  719 21:24 data2.back01
drwx------. 19 postgres postgres 4096  719 22:06 data2.bak02
drwx------. 19 postgres postgres 4096  721 08:57 data2.crash
drwxrwxr-x.  6 postgres postgres 4096  719 12:16 include
drwxrwxr-x.  4 postgres postgres 4096  719 12:16 lib
drwxrwxr-x.  8 postgres postgres 4096  719 12:16 share
[postgres@cent82-pg12-01 ~]$
リカバリ

リカバリの手順は
①ベースバックアップを戻す
②recovery.confを作製する(アーカイブを適用するコマンドやらtarget時間を指定)
③未アーカイブのWALファイルを退避させたもと$PGADTAから復元
リカバリ実行(単に起動するだけ)
★私の検証環境はPostgreSQL12.3なので②のところが変わっていますので注意。(後述)

①ベースバックアップを戻す

[postgres@cent82-pg12-01 ~]$ # ベースバックアップを$PGDATAに配置してログを削除
[postgres@cent82-pg12-01 ~]$ cp -a $PGHOME/backups/base/001 $PGDATA
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # 不要なpgwal
[postgres@cent82-pg12-01 ~]$ rm -rf $PGDATA/pg_wal/*
[postgres@cent82-pg12-01 ~]$

②recovery.confを作製する(アーカイブを適用するコマンドやらtarget時間を指定)
OSS-DBの試験は11まで対応しているのでrecovery.confを作製するで覚えてもらえればいいのですが、PostgreSQL12ではrecovey.confはpostgresql.confに統合されたので、手順が変わっています。

recovery.confの設定をpostgresql.confに移動しました。 (Masao Fujii, Simon Riggs, Abhijit Menon-Sen, Sergei Kornilov)

recovery.confはもはや使われず、このファイルがあるとサーバは起動しなくなります。 非プライマリモードに切り替えするのに、これからはrecovery.signalとstandby.signalファイルが使われます。 trigger_file設定はpromote_trigger_fileに改名されました。 standby_mode設定は廃止されました。

【PostgreSQL11での手順】

# リカバリ設定ファイル(recovery.conf)を作製
ls -l recovery.conf
cat << 'EOF' > $PGDATA/recovery.conf
restore_command = 'cp "/usr/local/pgsql/backups/arc/%f" "%p"'
EOF

cat $PGDATA/recovery.conf

recovery_command:アーカイブログを適用するOSコマンドを指定
recovery_target_time:指定時刻でリカバリを終了



【PostgreSQL12での手順】
②-1:postgresql.confに追記する(archive_commandと一緒に設定しておいてもよい)
※11のバージョンの手順との比較という観点で今回はこの手順で紹介
②-2:recovery.signalというファイルをPGDATAディレクトリ内に作成する

[postgres@cent82-pg12-01 ~]$ # リカバリの設定をpostgresql.confに追記
[postgres@cent82-pg12-01 ~]$ grep restore_command $PGDATA/postgresql.conf
#restore_command = ''           # command to use to restore an archived logfile segment
restore_command = 'cp "/usr/local/pgsql/backups/arc/%f" "%p"'
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # recovery.signalファイルを作製
[postgres@cent82-pg12-01 ~]$ ls -l $PGDATA/recovery.signal
ls: '/usr/local/pgsql/data2/recovery.signal' にアクセスできません: そのようなファイルやディレクトリはありません
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ touch $PGDATA/recovery.signal
[postgres@cent82-pg12-01 ~]$ ls -l $PGDATA/recovery.signal
-rw-rw-r--. 1 postgres postgres 0  721 08:59 /usr/local/pgsql/data2/recovery.signal
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # 【おまけ】archive_commandの再確認
[postgres@cent82-pg12-01 ~]$ grep archive_command $PGDATA/postgresql.conf
#archive_command = ''           # command to use to archive a logfile segment
archive_command = 'cp "%p" "/usr/local/pgsql/backups/arc/%f"'
[postgres@cent82-pg12-01 ~]$

③未アーカイブのWALファイルを退避させたもと$PGADTAから復元

[postgres@cent82-pg12-01 ~]$ # 未アーカイブのWALを$PGDATA/pg_walに配置
[postgres@cent82-pg12-01 ~]$ ls $PGDATA.crash/pg_wal
00000001000000000000001B.00000028.backup  00000003000000000000004E  000000030000000000000058
00000002.history                          00000003000000000000004F  000000030000000000000059
00000003.history                          000000030000000000000050  00000003000000000000005A
000000030000000000000048                  000000030000000000000051  00000003000000000000005B
000000030000000000000048.00000028.backup  000000030000000000000052  00000003000000000000005C
000000030000000000000049                  000000030000000000000053  00000003000000000000005D
00000003000000000000004A                  000000030000000000000054  00000003000000000000005E
00000003000000000000004B                  000000030000000000000055  archive_status
00000003000000000000004C                  000000030000000000000056
00000003000000000000004D                  000000030000000000000057
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls $PGHOME/backups/arc
00000001000000000000001A                  000000010000000000000032          000000030000000000000047
00000001000000000000001B                  000000010000000000000033.partial  000000030000000000000048
00000001000000000000001B.00000028.backup  00000002.history                  000000030000000000000048.00000028.backup
00000001000000000000001C                  000000020000000000000033          000000030000000000000049
00000001000000000000001D                  000000020000000000000034          00000003000000000000004A
00000001000000000000001E                  000000020000000000000035          00000003000000000000004B
00000001000000000000001F                  000000020000000000000036          00000003000000000000004C
000000010000000000000020                  000000020000000000000037          00000003000000000000004D
000000010000000000000021                  000000020000000000000038          00000003000000000000004E
000000010000000000000022                  000000020000000000000039          00000003000000000000004F
000000010000000000000023                  00000002000000000000003A          000000030000000000000050
000000010000000000000024                  00000002000000000000003B          000000030000000000000051
000000010000000000000025                  00000002000000000000003C          000000030000000000000052
000000010000000000000026                  00000002000000000000003D          000000030000000000000053
000000010000000000000027                  00000002000000000000003E          000000030000000000000054
000000010000000000000028                  00000002000000000000003F          000000030000000000000055
000000010000000000000029                  000000020000000000000040          000000030000000000000056
00000001000000000000002A                  000000020000000000000041          000000030000000000000057
00000001000000000000002B                  000000020000000000000042          000000030000000000000058
00000001000000000000002C                  000000020000000000000043          000000030000000000000059
00000001000000000000002D                  000000020000000000000044          00000003000000000000005A
00000001000000000000002E                  000000020000000000000045          00000003000000000000005B
00000001000000000000002F                  000000020000000000000046          00000003000000000000005C
000000010000000000000030                  000000020000000000000047
000000010000000000000031                  00000003.history
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ ls $PGDATA/pg_wal
00000001000000000000001B.00000028.backup  00000003000000000000004B  000000030000000000000052  000000030000000000000059
00000002.history                          00000003000000000000004C  000000030000000000000053  00000003000000000000005A
00000003.history                          00000003000000000000004D  000000030000000000000054  00000003000000000000005B
000000030000000000000048                  00000003000000000000004E  000000030000000000000055  00000003000000000000005C
000000030000000000000048.00000028.backup  00000003000000000000004F  000000030000000000000056  00000003000000000000005D
000000030000000000000049                  000000030000000000000050  000000030000000000000057  00000003000000000000005E
00000003000000000000004A                  000000030000000000000051  000000030000000000000058
[postgres@cent82-pg12-01 ~]$

リカバリ実行(単に起動するだけ)

[postgres@cent82-pg12-01 ~]$ # リカバリ実行
[postgres@cent82-pg12-01 ~]$ pg_ctl start
waiting for server to start....2020-07-21 09:00:27.886 JST [24711] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.3.1 20191121 (Red Hat 8.3.1-5), 64-bit
2020-07-21 09:00:27.887 JST [24711] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-07-21 09:00:27.887 JST [24711] LOG:  listening on IPv6 address "::", port 5432
2020-07-21 09:00:27.889 JST [24711] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-07-21 09:00:27.901 JST [24712] LOG:  database system was interrupted; last known up at 2020-07-21 08:56:00 JST
2020-07-21 09:00:27.901 JST [24712] LOG:  creating missing WAL directory "pg_wal/archive_status"
cp: cannot stat '/usr/local/pgsql/backups/arc/00000004.history': No such file or directory
2020-07-21 09:00:28.201 JST [24712] LOG:  starting archive recovery
2020-07-21 09:00:28.204 JST [24712] LOG:  restored log file "00000003.history" from archive
2020-07-21 09:00:28.217 JST [24712] LOG:  restored log file "000000030000000000000048" from archive
2020-07-21 09:00:28.246 JST [24712] LOG:  redo starts at 0/48000028
2020-07-21 09:00:28.247 JST [24712] LOG:  consistent recovery state reached at 0/48000100
2020-07-21 09:00:28.247 JST [24711] LOG:  database system is ready to accept read only connections
2020-07-21 09:00:28.257 JST [24712] LOG:  restored log file "000000030000000000000049" from archive
 done
server started
[postgres@cent82-pg12-01 ~]$ 2020-07-21 09:00:28.307 JST [24712] LOG:  restored log file "00000003000000000000004A" from archive
2020-07-21 09:00:28.355 JST [24712] LOG:  restored log file "00000003000000000000004B" from archive
2020-07-21 09:00:28.408 JST [24712] LOG:  restored log file "00000003000000000000004C" from archive
2020-07-21 09:00:28.457 JST [24712] LOG:  restored log file "00000003000000000000004D" from archive
2020-07-21 09:00:28.508 JST [24712] LOG:  restored log file "00000003000000000000004E" from archive
2020-07-21 09:00:28.564 JST [24712] LOG:  restored log file "00000003000000000000004F" from archive
2020-07-21 09:00:28.617 JST [24712] LOG:  restored log file "000000030000000000000050" from archive
2020-07-21 09:00:28.670 JST [24712] LOG:  restored log file "000000030000000000000051" from archive
2020-07-21 09:00:28.732 JST [24712] LOG:  restored log file "000000030000000000000052" from archive
2020-07-21 09:00:28.789 JST [24712] LOG:  restored log file "000000030000000000000053" from archive
2020-07-21 09:00:28.859 JST [24712] LOG:  restored log file "000000030000000000000054" from archive
2020-07-21 09:00:28.924 JST [24712] LOG:  restored log file "000000030000000000000055" from archive
2020-07-21 09:00:28.986 JST [24712] LOG:  restored log file "000000030000000000000056" from archive
2020-07-21 09:00:29.053 JST [24712] LOG:  restored log file "000000030000000000000057" from archive
2020-07-21 09:00:29.128 JST [24712] LOG:  restored log file "000000030000000000000058" from archive
2020-07-21 09:00:29.198 JST [24712] LOG:  restored log file "000000030000000000000059" from archive
2020-07-21 09:00:29.269 JST [24712] LOG:  restored log file "00000003000000000000005A" from archive
2020-07-21 09:00:29.351 JST [24712] LOG:  restored log file "00000003000000000000005B" from archive
2020-07-21 09:00:29.433 JST [24712] LOG:  restored log file "00000003000000000000005C" from archive
cp: cannot stat '/usr/local/pgsql/backups/arc/00000003000000000000005D': No such file or directory
2020-07-21 09:00:29.539 JST [24712] LOG:  invalid record length at 0/5D4AAB58: wanted 24, got 0
2020-07-21 09:00:29.539 JST [24712] LOG:  redo done at 0/5D4AAB20
2020-07-21 09:00:29.539 JST [24712] LOG:  last completed transaction was at log time 2020-07-21 08:56:38.708373+09
cp: cannot stat '/usr/local/pgsql/backups/arc/00000003000000000000005D': No such file or directory
cp: cannot stat '/usr/local/pgsql/backups/arc/00000004.history': No such file or directory
2020-07-21 09:00:29.551 JST [24712] LOG:  selected new timeline ID: 4
2020-07-21 09:00:29.597 JST [24712] LOG:  archive recovery complete
2020-07-21 09:00:29.600 JST [24712] LOG:  restored log file "00000003.history" from archive
2020-07-21 09:00:30.597 JST [24711] LOG:  database system is ready to accept connections

[postgres@cent82-pg12-01 ~]$

「LOG: archive recovery completeと表示されていることを確認

リカバリの確認

[postgres@cent82-pg12-01 ~]$ # ログの確認
[postgres@cent82-pg12-01 ~]$ # しまったログを取らない設定になっていた…
[postgres@cent82-pg12-01 ~]$ psql -qc "show logging_collector;"
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".
 logging_collector
-------------------
 off
(1 row)

[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ # データの確認
[postgres@cent82-pg12-01 ~]$ psql benchmark -qc "SELECT * FROM pgbench_history ORDER BY mtime LIMIT 1;"
You are connected to database "benchmark" as user "postgres" via socket in "/tmp" at port "5432".
 tid | bid |  aid  | delta |           mtime            | filler
-----+-----+-------+-------+----------------------------+--------
  76 |  18 | 10886 | -2087 | 2020-07-21 08:56:08.707481 |
(1 row)

[postgres@cent82-pg12-01 ~]$


recovery_target_actionというリカバリ対象に達した場合にサーバがする動作を指定するパラメータがshutdownになっている場合以外は削除されます。

[postgres@cent82-pg12-01 ~]$ # 【おまけ】recovery.signalファイルが削除されていることを確認
[postgres@cent82-pg12-01 ~]$ ls -l $PGDATA/recovery.signal
ls: '/usr/local/pgsql/data2/recovery.signal' にアクセスできません: そのようなファイルやディレクトリはありません
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$
[postgres@cent82-pg12-01 ~]$ psql -qc "show recovery_target_action"
You are connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5432".
 recovery_target_action
------------------------
 pause
(1 row)

[postgres@cent82-pg12-01 ~]$