PostgreSQL 12 通过分区增强、计划器改进、一些SQL特性、索引改进等方面对性能进行了革命性的提升。你可能会在以后的博客文章中看到这些特性。在PostgreSQL 12中你可能已经看到一些新变化,那就是备库中不再有recovery.conf
文件,并且复制设置(流复制)也略有更改。我们之前已经在博客中介绍了在PostgreSQL 11之前设置简单流复制的步骤,以及如何使用复制槽。让我们看看在PostgreSQL 12 中设置相同的流复制有什么不同。
在主库服务器和备库服务器上安装PostgreSQL 12
在CentOS/RedHat上,你可以使用PGDG repo中提供的rpm(下面的链接可能要根据你的OS版本而进行更改)。
# 用root用户:yum install -y https://yum.postgresql.org/12/redhat/rhel-7.4-x86_64/pgdg-redhat-repo-latest.noarch.rpm -yyum install -y postgresql12-server
在PostgreSQL 12中设置流复制的步骤
在以下步骤中,主库服务器为:192.168.0.108,备库服务器为:192.168.0.107。
步骤1:
初始化和启动PostgreSQL
## 准备环境$ sudo su - postgres$ echo "export PATH=/usr/pgsql-12/bin:$PATH PAGER=less" >> ~/.pgsql_profile$ source ~/.pgsql_profile## 用root用户,在主服务器上初始化并启动PostgreSQL 12$ /usr/pgsql-12/bin/postgresql-12-setup initdb$ systemctl start postgresql-12
步骤 2 :
修改参数listen_addresses
以允许特定的IP接口或全部(使用*)。修改此参数需要重新启动PostgreSQL实例,才能使更改生效。
# 用postgres用户$ psql -c "ALTER SYSTEM SET listen_addresses TO '*'";ALTER SYSTEM# 用root用户,重启服务$ systemctl restart postgresql-12
对于简单的复制设置,你可能不需要在主服务器上设置任何其他参数,默认就行了。
步骤3 :
在主库中创建用于复制的用户。不鼓励使用超级用户postgres来设置复制,尽管它也可以。
postgres=# CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'secret';CREATE ROLE
步骤4 :
通过在主库的pg_hba.conf
文件中添加类似的代码,允许从备库到主库的复制连接。如果使用任何外部工具启用自动故障转移,则还允许从主库到备库的复制连接。在发生故障转移时,备库可能会被提升为主库,旧的主库需要复制新主库(以前是备库)的更改。你可以使用任何今天PostgreSQL支持的身份验证方法。
$ echo "host replication replicator 192.168.0.107/32 md5" >> $PGDATA/pg_hba.conf## 通过重新加载使更改生效$ psql -c "select pg_reload_conf()"
步骤5 :
可以使用pg_basebackup
在备库备份主库的数据目录。在创建备份时,你还可以告诉pg_basebackup
使用"-R"
在数据目录中创建特定用于复制的文件和目录。
## 此命令必须在备库服务器上执行$ pg_basebackup -h 192.168.0.108 -U replicator -p 5432 -D $PGDATA -Fp -Xs -P -RPassword:25314/25314 kB (100%), 1/1 tablespace
可以使用多种方法(例如rsync或任何其他磁盘备份方法)将主库的数据目录复制到备库。但是,在备库目录中必须存在一个重要文件(standby.signal),以帮助postgres确定其备用状态。在使用pg_basebackup
时使用"-R"
选项会自动创建它。如果没有,你可以简单地使用touch创建这个空文件。
$ touch $PGDATA/standby.signal$ ls -l $PGDATAtotal 60-rw-------. 1 postgres postgres 224 Oct 8 16:41 backup_labeldrwx------. 5 postgres postgres 41 Oct 8 16:41 base-rw-------. 1 postgres postgres 30 Oct 8 16:41 current_logfilesdrwx------. 2 postgres postgres 4096 Oct 8 16:41 globaldrwx------. 2 postgres postgres 32 Oct 8 16:41 logdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_commit_tsdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_dynshmem-rw-------. 1 postgres postgres 4581 Oct 8 16:41 pg_hba.conf-rw-------. 1 postgres postgres 1636 Oct 8 16:41 pg_ident.confdrwx------. 4 postgres postgres 68 Oct 8 16:41 pg_logicaldrwx------. 4 postgres postgres 36 Oct 8 16:41 pg_multixactdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_notifydrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_replslotdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_serialdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_snapshotsdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_statdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_stat_tmpdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_subtransdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_tblspcdrwx------. 2 postgres postgres 6 Oct 8 16:41 pg_twophase-rw-------. 1 postgres postgres 3 Oct 8 16:41 PG_VERSIONdrwx------. 3 postgres postgres 60 Oct 8 16:41 pg_waldrwx------. 2 postgres postgres 18 Oct 8 16:41 pg_xact-rw-------. 1 postgres postgres 288 Oct 8 16:41 postgresql.auto.conf-rw-------. 1 postgres postgres 26638 Oct 8 16:41 postgresql.conf-rw-------. 1 postgres postgres 0 Oct 8 16:41 standby.signal
最重要的发现之一应该是备库中的postgresql.auto.conf
文件中的内容。正如你在以下信息中看到的,一个附加参数primary_conninfo
被添加到这个文件中。此参数告诉备库关于其主库的信息。如果你还没有使用-R
选项的pg_basebackup
命令,那么在备库上就不会在这个文件中看到此条目(primary_conninfo
条目),这意味着你必须手动添加它。
$ cat $PGDATA/postgresql.auto.conf# Do not edit this file manually!# It will be overwritten by the ALTER SYSTEM command.listen_addresses = '*'primary_conninfo = 'user=replicator password=secret host=192.168.0.108 port=5432 sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any'
postgresql.auto.conf
文件是Postgres启动时最后读取的配置文件。因此,如果在postgresql.conf
和postgresql.auto.conf
文件中存在一个具有不同值的参数,则PostgreSQL将考虑在postgresql.auto.conf
中设置的值。另外,任何使用ALTER SYSTEM
修改的参数将由postgres
自动写入到postgresql.auto.conf
文件中。
在Postgres 11之前如何处理复制配置?
在PostgreSQL 11之前,我们必须创建一个名为:recovery.conf
的文件,其中包含以下的简单参数。如果standby_mode
为ON,则其被视为备库。
$ cat $PGDATA/recovery.confstandby_mode = 'on'primary_conninfo = 'host=192.168.0.8 port=5432 user=replicator password=secret'
因此,PostgreSQL 12和早期版本(直到PostgreSQL 11)之间的第一个区别是PostgreSQL 12中不存在standby_mode
参数,而相同的参数已被数据目录中的一个空文件standby.signal
而替代。第二个区别是参数primary_conninfo
。现在可以将其添加到备库的数据目录的postgresql.conf
文件或postgresql.auto.conf
文件中。
步骤6 :
在备库上使用pg_ctl
启动PostgreSQL。
$ pg_ctl -D $PGDATA start步骤7 :
验证主库和备库之间的复制。为了验证,请在主库上运行下列命令。在命令后面的日志中,你会看到很多备库的详细信息,以及主库和备库之间的延迟。
$ psql -x -c "select * from pg_stat_replication"-[ RECORD 1 ]----+------------------------------pid | 2522usesysid | 16384usename | replicatorapplication_name | walreceiverclient_addr | 192.168.0.107client_hostname |client_port | 36382backend_start | 2019-10-08 17:15:19.658917-04backend_xmin |state | streamingsent_lsn | 0/CB02A90write_lsn | 0/CB02A90flush_lsn | 0/CB02A90replay_lsn | 0/CB02A90write_lag | 00:00:00.095746flush_lag | 00:00:00.096522replay_lag | 00:00:00.096839sync_priority | 0sync_state | asyncreply_time | 2019-10-08 17:18:04.783975-04
在主库上启用归档,并用归档恢复备库
大多数情况下,主库上的WAL段的默认或修改的保留设置可能不足以维持其自身与备库之间的正常复制。因此,我们需要将WAL安全地归档到另一个磁盘或远程备份服务器。当WAL脱离主库时,备库可以使用这些已归档地WAL段来重放它们。
要在主库上启用归档,我们仍然可以使用相同的方法来设置以下两个参数。
archive_mode = ONarchive_command = 'cp %p archives/%f' ## Modify this with an appropriate shell command.
但是要在备库中启用从归档恢复,直到PostgreSQL 11,我们都在recovery.conf
文件中添加了一个名为restore_command
的参数。但是从PostgreSQL 12开始,我们可以将相同的参数添加到postgresql.conf
文件或postgresql.auto.conf
备用文件中。请注意,需要重新启动PostgreSQL来更新对archive_mode
和restore_command
参数的更改。
echo "restore_command = 'cp archives/%f %p'" >> $PGDATA/postgresql.auto.conf
pg_ctl -D $PGDATA restart -mf
本文翻译自:https://www.percona.com/blog/2019/10/11/how-to-set-up-streaming-replication-in-postgresql-12/




