PostgreSQL cluster role switchover between primary and standby

虽然PostgreSQL没有明确指出 primary 和 standby 节点的角色可以相互切换。(即standby promote成primary ,primary demote成standby .不需要重建集群)

但是经过实际测试,是可以实现的。
以下测试数据库版本9.0.2 . 低版本可能有一定差异。(比如有一个版本改进了standby shutdown后自动获取checkpoint的功能,最初的9.0是没有的,没有这个功能重启后需要用到一些已经APPLY的WAL,带来比较麻烦的问题。有兴趣的朋友可以参考我以前写的BLOG)
首先来看几个和XLOG相关的东西,要确保安全的SWITCHOVER,必须确保一下提及的数据在primary和standby切换前是一致的。
1. primary 和 standby 关闭后使用pg_controldata输出的checkpoint location (-m immediate关闭的话这个值不可信,因此必须确保都是-m fast 或者 smart模式关闭的,fast和smart关闭都是干净的关闭模式,起来时不需要recovery)

具体的操作步骤:
1. on primary 
pg_ctl stop -m fast $PGDATA 
确认返回成功后
查看pg_controldata输出 
Latest checkpoint location:           1B/6C000020
Latest checkpoint's REDO location:    1B/6C000020
2. on standby
pg_ctl stop -m fast $PGDATA
确认返回成功后
查看pg_controldata输出 
Latest checkpoint location:           1B/6C000020
Latest checkpoint's REDO location:    1B/6C000020
3. 确保standby和primary 的pg_controldata    checkpoint的输出结果一致。就可以切换standby 和 primary了。
4. 启动standby,并且touch一个在recovery.conf中的trigger文件,使standby promote.为primary库
5. 修改原primary库的配置文件 并新建recovery.conf,需要注意的是新增一条
recovery_target_timeline = 'latest' ,否则会报类似如下错误timeline 6 of the primary does not match recovery target timeline 5。(其他配置略,清参考我以前写的BLOG。)
6. 启动原primary , 此时切换完成,primary 变standby ,standby 变primary ,而且可以再次切换。

切换完成后查看pg_controldata的输出,TimeLineID 将变大。
TimeLineID递增后,在新的PRIMARY 库会出现类似如下进程
postgres: archiver process   last was 00000005.history
而TimeLineID可能就等于5.
与此同时,新的standby TimeLineID还是老的,因为还没有更新过来。
要更新过来的话,要么等CHECKPOINT,要么关闭主节点,然后再关闭副节点。起来之后就同步了。

下面来分析一下,一个archive和stream都打开的主副数据库集群.
1. 主节点在干净的关闭数据库时干了些啥?
对于一个STREAM复制的环境,主节点上面有一个WAL_SENDER进程,干净关闭数据库时,postmaster对这个进程的处理方法和普通的SERVER PROCESS不一样,数据库关闭时首先发信号给server process关闭,然后写shutdown checkpoint ,(有兴趣的朋友可以看一看CHECKPOINT的源码,CHECKPOINT分为好几种),然后postmaster告知wal_sender shutdown_checkpoint,并且把截止这个CHECKPOINT的XLOG发送给standby上面的wal_receiver进程。

从而主节点干净的关闭数据库时,standby节点可以接收到shutdown_checkpoint,达到一致的目的。

原文引用:

Therefore postmaster treats walsenders like the pgarch process,

and instructs them to terminate at PM_SHUTDOWN_2 phase, after all regular

backends have died and bgwriter has written the shutdown checkpoint.

2. 副节点在干净的关闭数据库时干了些啥?
standby节点干净的关闭数据库,直接关闭,目前的版本不尝试且不等待正在从archive ,pg_xlog,或wal_sender恢复完成。但是已经接收到的CHECKPOINT将被写入持久化存储,因此关闭后pg_controldata与主节点一致.

干净的关闭后的primary 和standby 库的pg_controldata输入示例 : 
primary :

postgres@digoal-172-16-3-39-> pg_controldata
pg_control version number:            903
Catalog version number:               201008051
Database system identifier:           5556352067747738614
Database cluster state:               shut down
pg_control last modified:             Wed 11 May 2011 08:52:38 PM CST
Latest checkpoint location:           1B/70000020
Prior checkpoint location:            1B/6C000078
Latest checkpoint's REDO location:    1B/70000020
Latest checkpoint's TimeLineID:       5
Latest checkpoint's NextXID:          0/4369
Latest checkpoint's NextOID:          2074455
Latest checkpoint's NextMultiXactId:  1
Latest checkpoint's NextMultiOffset:  0
Latest checkpoint's oldestXID:        655
Latest checkpoint's oldestXID's DB:   1
Latest checkpoint's oldestActiveXID:  0
Time of latest checkpoint:            Wed 11 May 2011 08:52:38 PM CST
Minimum recovery ending location:     0/0
Backup start location:                0/0
Current wal_level setting:            hot_standby
Current max_connections setting:      2000
Current max_prepared_xacts setting:   50
Current max_locks_per_xact setting:   64
Maximum data alignment:               8
Database block size:                  8192
Blocks per segment of large relation: 1048576
WAL block size:                       8192
Bytes per WAL segment:                67108864
Maximum length of identifiers:        64
Maximum columns in an index:          32
Maximum size of a TOAST chunk:        1996
Date/time type storage:               64-bit integers
Float4 argument passing:              by value
Float8 argument passing:              by value

standby : 

postgres@digoal-172-16-3-33-> pg_controldata
pg_control version number:            903
Catalog version number:               201008051
Database system identifier:           5556352067747738614
Database cluster state:               shut down in recovery
pg_control last modified:             Wed 11 May 2011 08:52:52 PM CST
Latest checkpoint location:           1B/70000020
Prior checkpoint location:            1B/6C000020
Latest checkpoint's REDO location:    1B/70000020
Latest checkpoint's TimeLineID:       5
Latest checkpoint's NextXID:          0/4369
Latest checkpoint's NextOID:          2074455
Latest checkpoint's NextMultiXactId:  1
Latest checkpoint's NextMultiOffset:  0
Latest checkpoint's oldestXID:        655
Latest checkpoint's oldestXID's DB:   1
Latest checkpoint's oldestActiveXID:  0
Time of latest checkpoint:            Wed 11 May 2011 08:52:38 PM CST
Minimum recovery ending location:     1B/6C000020
Backup start location:                0/0
Current wal_level setting:            hot_standby
Current max_connections setting:      2000
Current max_prepared_xacts setting:   50
Current max_locks_per_xact setting:   64
Maximum data alignment:               8
Database block size:                  8192
Blocks per segment of large relation: 1048576
WAL block size:                       8192
Bytes per WAL segment:                67108864
Maximum length of identifiers:        64
Maximum columns in an index:          32
Maximum size of a TOAST chunk:        1996
Date/time type storage:               64-bit integers
Float4 argument passing:              by value
Float8 argument passing:              by value

对于一个因异常而FAILOVER的库,因为failover时不能确保primary 和 standby 的checkpoint一致,所以,在primary数据库服务器修复后,不能直接转换为standby,而需要一个checkpoint小于standby切换为primary时的standby库的checkpoint的基础备份,并且需要足够的archive log来做apply,
因此failover后主备集群的重新建立的速度或简易程度,大部分决定于平时对standby和archive log的备份。

# 补充

# 2011-06-09 今天对一个生产系统做了切换,版本是9.0.3

# 切换后,起主节点的时候一直处于等待状态

postgres: startup process   waiting for 00000001000001200000001C

# 日志里面报错如下:

cp: cannot stat `/opt/pg_arch/00000002.history': No such file or directory

# 00000002.history这个文件来自于新的primary节点pg_xlog目录,拷贝到/opt/pg_arch/00000002.history,另外

拷贝00000001000001200000001C到/opt/pg_arch/.

# 拷贝完后重启老的primary节点.

2011-06-09 14:31:57.095 CST,,,1357,,4df068dd.54d,1,,2011-06-09 14:31:57 CST,,0,LOG,00000,"database system was shut down in recovery at 2011-06-09 14:31:53 CST",,,,,,,,,""
2011-06-09 14:31:57.099 CST,,,1357,,4df068dd.54d,2,,2011-06-09 14:31:57 CST,,0,LOG,00000,"restored log file ""00000002.history"" from archive",,,,,,,,,""
2011-06-09 14:31:57.105 CST,,,1357,,4df068dd.54d,3,,2011-06-09 14:31:57 CST,,0,LOG,00000,"restored log file ""00000002.history"" from archive",,,,,,,,,""
2011-06-09 14:31:57.105 CST,,,1357,,4df068dd.54d,4,,2011-06-09 14:31:57 CST,,0,LOG,00000,"entering standby mode",,,,,,,,,""
2011-06-09 14:31:57.275 CST,,,1357,,4df068dd.54d,5,,2011-06-09 14:31:57 CST,,0,LOG,00000,"restored log file ""00000001000001200000001C"" from archive",,,,,,,,,""
2011-06-09 14:31:58.120 CST,,,1357,,4df068dd.54d,6,,2011-06-09 14:31:57 CST,1/0,0,LOG,00000,"consistent recovery state reached at 120/70000078",,,,,,,,,""
2011-06-09 14:31:58.120 CST,,,1357,,4df068dd.54d,7,,2011-06-09 14:31:57 CST,1/0,0,LOG,00000,"invalid record length at 120/70000078",,,,,,,,,""
2011-06-09 14:31:58.121 CST,,,1355,,4df068dc.54b,1,,2011-06-09 14:31:56 CST,,0,LOG,00000,"database system is ready to accept read only connections",,,,,,,,,""
2011-06-09 14:31:58.126 CST,,,1365,,4df068de.555,1,,2011-06-09 14:31:58 CST,,0,LOG,00000,"streaming replication successfully connected to primary",,,,,,,,,""
2011-06-09 14:31:59.320 CST,,,1357,,4df068dd.54d,8,,2011-06-09 14:31:57 CST,1/0,0,LOG,00000,"redo starts at 120/70000078",,,,,,,,,""

成功的切换为standby节点.

时间: 2024-11-08 19:44:24

PostgreSQL cluster role switchover between primary and standby的相关文章

PostgreSQL 9.1,9.2,9.3 clean switchover Primary and Standby Patch.

前段时间在使用9.3 beta2的流复制时发现无法完成主备数据库的角色互相切换. 原因是备节点的receiver进程在知晓主节点的sender进程关闭后也会自动关闭,可能导致无法接收primary节点关闭时完全的xlog信息. 因此角色无法互相切换. 9.3beta2发布的第二天这个补丁就提交了, 如下  :  Support clean switchover. author Fujii Masao <fujii@postgresql.org> Tue, 25 Jun 2013 17:14:3

PostgreSQL 9.3 Allow a streaming replication standby to follow a timeline switch

Allow a streaming replication standby to follow a timeline switch. Before this patch, streaming replication would refuse to start replicating if the timeline in the primary doesn't exactly match the standby. The situation where it doesn't match is wh

MongoDB Master Slave role switchover

测试环境:MASTER(A):172.16.3.176/opt/mongodb/bin/mongod --master --config /opt/mongodb/conf/mongodb1.conf --oplogSize=256config:[root@db5 data1]# cat /opt/mongodb/conf/mongodb1.conf port=5281fork=truelogpath=/var/log/mongodb1.loglogappend=truedbpath=/home

PostgreSQL HOT STANDBY using log shipping

PostgreSQL HOT STANDBY by log shipping 测试:一.准备硬件1. 主节点硬件配置DISK : 146GB*6MEM : 14GBCPU : 2.83GHz*82. standby节点硬件配置DISK : 146GB*4MEM : 8GBCPU : 2.0GHz*8 二.准备环境1. 系统Red Hat Enterprise Linux Server release 5.5 (Tikanga) x642. 时钟同步8 * * * * /usr/sbin/ntpd

PostgreSQL 9.0 流复制介绍

PostgreSQL9提供了一个非常兴奋的功能,hot-standby,功能与ORACLE 11G的ACTIVE STANDBY类似.并且增加了流复制的功能,这个与oracle 的standby redo log功能类似,大大的缩短了备份库与主库的事务间隔. HOT-STANDBY可以提供容灾,恢复的同时可以把数据库打开,提供查询功能.以前的版本恢复的时候是不能打开的. 首先看一张postgreSQL的高可用,负载均衡,复制特征矩阵图 这里有一个很好的特性 Slaves accept read-

【DG】[三思笔记]一步一步学DataGuard

[DG][三思笔记]一步一步学DataGuard 它有无数个名字,有人叫它dg,有人叫它数据卫士,有人叫它data guard,在oracle的各项特性中它有着举足轻理的地位,它就是(掌声)......................Oracle Data Guard.而对于我而言,我一定要亲切的叫它:DG(注:主要是因为打着方便). 不少未实际接触过dg的初学者可能会下意识以为dg是一个备份恢复的工具.我要说的是,这种形容不完全错,dg拥有备份的功能,某些情况下它甚至可以与primary数据库

oracle10g data guard 主备数据库配置参数说明

oracle10g data guard 主备数据库配置参数说明: 下列参数为primary 角色相关的初始化参数: DB_NAME 注意保持同一个Data Guard 中所有数据库DB_NAME 相同. 例如:DB_NAME=jytest DB_UNIQUE_NAME 为每一个数据库指定一个唯一的名称,该参数一经指定不会再发生变化, 除非你主动修改它. 例如:DB_UNIQUE_NAME=jytest LOG_ARCHIVE_CONFIG 该参数通过DG_CONFIG 属性罗列同一个Data

PostgreSQL 9.3 beta2 stream replication primary standby switchover bug?

[更新] 已有patch. 请参见. PostgreSQL 9.1,9.2,9.3 clean switchover Primary and Standby Patch.  http://blog.163.com/digoal@126/blog/static/16387704020136197354054/ 打补丁前的测试 :  PostgreSQL 9.3 beta2 无法完成正常的主备角色切换. Primary : psql checkpont; pg_controldata  pg_con

PostgreSQL standby in 64bit to 32bit or reverse enviroment

64和32位系统上安装的PostgreSQL能不能做流复制搭建standby. 今天问同事借了一台32位的机器测试一下. 测试环境 :  CentOS 5.x 32bit 和64bit 各一台. PostgreSQL 9.1.4源码 32bit 作为primary 64bit 作为standby 结果是可行的, 只是需要在64bit上使用32bit的pgsql程序. 而反过来则不行(64bit primary , 32bit standby). 说白了, 程序要兼容. 在这种环境中建standb