ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL DBA(40) - PG 12 pg_promote

PostgreSQL DBA(40) - PG 12 pg_promote

原创 PostgreSQL 作者:husthxd 时间:2019-06-20 17:25:00 0 删除 编辑

在PG 12以前的版本,备库提升为主库需在备库主机上执行命令或者通过生成触发文件进行触发,在PG 12中,可通过客户端连接到数据库后执行pg_promote函数实现.
其更新说明如下:

This function is able to promote a standby with this new SQL-callable function. Execution access can be granted to non-superusers so that failover tools can observe the principle of least privilege.Catalog version is bumped.

下面以一个简单的例子进行说明.

搭建流复制环境
参照 PostgreSQL DBA(31) - Backup&Recovery#4(搭建流复制) ,注意在PG 12,recovery.conf文件已废弃,相关的配置信息已合并至postgresql.conf文件中.
下面是搭建完毕后,在主库创建数据表t1后,备库的情况.
备库


[pg12@localhost pg12db1]$ pg_ctl start
waiting for server to start....2019-06-20 17:04:04.361 CST [23158] LOG:  starting PostgreSQL 12beta1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
2019-06-20 17:04:04.362 CST [23158] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2019-06-20 17:04:04.362 CST [23158] LOG:  listening on IPv6 address "::", port 5432
2019-06-20 17:04:04.365 CST [23158] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-06-20 17:04:04.422 CST [23158] LOG:  redirecting log output to logging collector process
2019-06-20 17:04:04.422 CST [23158] HINT:  Future log output will appear in directory "pg_log".
 done
server started
[pg12@localhost pg12db1]$ psql -d testdb
psql (12beta1)
Type "help" for help.
testdb=# select count(*) from t1;
psql: ERROR:  relation "t1" does not exist
LINE 1: select count(*) from t1;
                             ^
testdb=# select count(*) from t1;
 count 
-------
     0
(1 row)

下面通过远程客户端连接到备库,执行函数pg_promote提升备库为主库.


[pg12@localhost pg12db1]$ psql -h 192.168.26.27 -U pg12 -d testdb
psql (12beta1)
Type "help" for help.
testdb=# -- Wait for at most 30 seconds.
testdb=# SELECT pg_promote(true, 30);
 pg_promote 
------------
 t
(1 row)

日志输出


",,,,,"SELECT pg_promote(true, 30);",,,"psql"
2019-06-20 17:14:22.584 CST,,,23160,,5d0b4c04.5a78,5,,2019-06-20 17:04:04 CST,1/0,0,LOG,00000,"received promote request",,,,,,,,,""
2019-06-20 17:14:22.584 CST,,,23167,,5d0b4c04.5a7f,2,,2019-06-20 17:04:04 CST,,0,FATAL,57P01,"terminating walreceiver process due to administrator command",,,,,,,,,""
2019-06-20 17:14:22.625 CST,,,23160,,5d0b4c04.5a78,6,,2019-06-20 17:04:04 CST,1/0,0,LOG,00000,"invalid record length at 0/5016D48: wanted 24, got 0",,,,,,,,,""
2019-06-20 17:14:22.625 CST,,,23160,,5d0b4c04.5a78,7,,2019-06-20 17:04:04 CST,1/0,0,LOG,00000,"redo done at 0/5016D10",,,,,,,,,""
2019-06-20 17:14:22.625 CST,,,23160,,5d0b4c04.5a78,8,,2019-06-20 17:04:04 CST,1/0,0,LOG,00000,"last completed transaction was at log time 2019-06-20 17:04:49.180746+08",,,,,,,,,""
2019-06-20 17:14:22.649 CST,,,23160,,5d0b4c04.5a78,9,,2019-06-20 17:04:04 CST,1/0,0,LOG,00000,"selected new timeline ID: 2",,,,,,,,,""
2019-06-20 17:14:22.738 CST,,,23160,,5d0b4c04.5a78,10,,2019-06-20 17:04:04 CST,1/0,0,LOG,00000,"archive recovery complete",,,,,,,,,""
2019-06-20 17:14:22.755 CST,,,23158,,5d0b4c04.5a76,3,,2019-06-20 17:04:04 CST,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
2019-06-20 17:14:22.764 CST,,,23277,,5d0b4e6e.5aed,1,,2019-06-20 17:14:22 CST,,0,FATAL,XX000,"archive command failed with exit code 127","The failed archive command was: /home/pg12/archive.sh",,,,,,,,""
2019-06-20 17:14:22.766 CST,,,23158,,5d0b4c04.5a76,4,,2019-06-20 17:04:04 CST,,0,LOG,00000,"archiver process (PID 23277) exited with exit code 1",,,,,,,,,""
2019-06-20 17:15:22.779 CST,,,23329,,5d0b4eaa.5b21,1,,2019-06-20 17:15:22 CST,,0,FATAL,XX000,"archive command failed with exit code 127","The failed archive command was: /home/pg12/archive.sh",,,,,,,,""
2019-06-20 17:15:22.781 CST,,,23158,,5d0b4c04.5a76,5,,2019-06-20 17:04:04 CST,,0,LOG,00000,"archiver process (PID 23329) exited with exit code 1",,,,,,,,,""
[pg12@localhost pg_log]$

pg_control中的状态


[pg12@localhost pg12db1]$ pg_controldata|grep state
Database cluster state:               in production

从日志&状态可看出,备库已提升为主库(出错提示是没有找到归档命令).

参考资料
Postgres 12 highlight - pg_promote

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2648261/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
长期从事政务、金融等行业产品研发和架构设计工作,ITPUB数据库版块资深版主,对Oracle、PostgreSQL有深入研究。现就职于广州云图数据技术有限公司,系统架构师。

注册时间:2007-12-28

  • 博文量
    1308
  • 访问量
    3784651