PostgreSQL主从序列的last_value不一致

前端之家收集整理的这篇文章主要介绍了PostgreSQL主从序列的last_value不一致前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_0@ 一直以为Postgresql的主从所有数据都是一致的,但直到最近测试过程中发现的一个奇怪的序列问题让我有所改观,一开始以为是某个版本的问题,发现9.x的版本都存在这个问题,让我一度认为是postgresql的一个BUG。 @H_404_0@
@H_404_0@ 我们的一个应用会定期同步Postgresql流复制的从机数据到一台测试机上,此时测试机上的数据和生产的现存数据是一致的,但是序列的last_value值是不一样的,导致测试机上新增数据时,最新的序列值和生产上是不一样的。
@H_404_0@
@H_404_0@ 简单的测试过程如下
@H_404_0@
@H_404_0@ 环境:
OS :CentOS 6.5
@H_404_0@ DB:Postgresql 9.4beta3
@H_404_0@ Primary:10.1.11.71 @H_404_0@ StandBy:10.1.11.72 @H_404_0@ 主从环境部署略 @H_404_0@
@H_404_0@
@H_404_0@ Primary端新建一张表:
[postgres @localhost ~]$ psql
psql (9.4beta3)
Type "help" for help.
@H_404_0@ postgres=# create table test(id serial,remark varchar(100));
CREATE TABLE
postgres=# select sequence_name,last_value,log_cnt from test_id_seq;
sequence_name | last_value | log_cnt
---------------+------------+---------
test_id_seq | 1 | 0
(1 row) @H_404_0@
@H_404_0@ 此时在Standby端查看 @H_404_0@ [postgres @localhost ~]$ psql
psql (9.4beta3)
Type "help" for help.

postgres=# select sequence_name,log_cnt from test_id_seq;
sequence_name | last_value | log_cnt
---------------+------------+---------
test_id_seq | 1 | 0
(1 row)

更新Primary端的测试数据
postgres=# insert into test(remark) select generate_series(1,100)||'hello,world';
INSERT 0 100
postgres=# select sequence_name,log_cnt from test_id_seq;
sequence_name | last_value | log_cnt
---------------+------------+---------
test_id_seq | 100 | 32
(1 row) @H_404_0@
@H_404_0@ 再次去查看standby端的序列值 @H_404_0@ postgres=# select sequence_name,log_cnt from test_id_seq;
sequence_name | last_value | log_cnt
---------------+------------+---------
test_id_seq | 132 | 0
(1 row) @H_404_0@
@H_404_0@ 其实多更新几次,你会发现备机上的last_value比主机上的略大,而且是主机上的last_value与 log_cnt之和。log_cnt在官方文档上没有详细说明,只是标注一个内部参数,并在replication中才有用处,通常认为是备机应用其他WAL时预留的nextval值,默认是32,所以这并不是一个BUG,而是故意设置的。 @H_404_0@ 如果从备机上同步数据到模测环境上需要注意这一点。

猜你在找的Postgre SQL相关文章