赞
踩
同步流复制就是当主库发生变化,比如有一条DML语句产生了WAL日志后,通过后台进程传送到备库,备库必须要应用这个日志,然后向主库返回一个成功应用的信号,主库才可以成功的commit;否则主库会一直等待到备库成功应用后,期间的等待就是主库commit后返回成功的时间段。同步流复制是PostgreSQL9.1后才有的。
异步流复制与同步流复制相反,即主库产生变化,再将WAL日志传递到备库,不需要等待备库应用完成,只需要成功传递WAL日志即返回commit。
优点:主备库数据无延迟,适用于负载均衡,读写分离的情况。
缺点:会影响主库的提交性能,如果在一个繁忙的业务系统中备库存在异常问题,备库无法应用或者没有及时的返回commit,主库就会出现长时间的等待或者宕机。而且一主一备环境如果配置了synchronous_standby_names参数后,备库一旦出现异常,无法正常写入,主库写操作也会一直等待,如采用同步流复制建议采用一主多备(大于等于两个备库)。
优点:主库提交不受影响,备库出现异常时主库仍然可以正常运行。
缺点:主备库之间会存在数据延迟情况,主备库切换后可能存在数据丢失,而且备库宕机后无明显提示,如果采用复制槽,主库会积攒大量wal文件,会有磁盘爆满的情况出现。
alter system set synchronous_standby_names = 'standby1';
synchronous_commit = on --默认值为on,一般不需要修改
select pg_reload_conf();
注:synchronous_standby_names 为同步流复制下从库的application_name。
## 添加 application_name
vi postgresql.auto.conf
primary_conninfo = 'application_name=standby1 user=postgres password=admin channel_binding=disable host=192.168.80.239 port=5432 sslmode=disable sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
pg_ctl reload
## 主库检查 postgres=# select client_addr,state,sync_state from pg_stat_replication; client_addr | state | sync_state ----------------+-----------+------------ 192.168.80.240 | streaming | sync (1 row) postgres=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 15941 usesysid | 10 usename | postgres application_name | standby1 client_addr | 192.168.80.240 client_hostname | client_port | 41592 backend_start | 2023-12-14 13:29:51.005534+08 backend_xmin | state | streaming sent_lsn | 0/43000998 write_lsn | 0/43000998 flush_lsn | 0/43000998 replay_lsn | 0/43000998 write_lag | flush_lag | replay_lag | sync_priority | 1 sync_state | sync reply_time | 2023-12-14 13:37:46.05816+08 ## 备库检查 postgres=# select * from pg_stat_wal_receiver; -[ RECORD 1 ]---------+-------------------------------------------------------------------- pid | 15677 status | streaming receive_start_lsn | 0/43000000 receive_start_tli | 2 written_lsn | 0/43000998 flushed_lsn | 0/43000998 received_tli | 2 last_msg_send_time | 2023-12-14 13:38:06.903124+08 last_msg_receipt_time | 2023-12-14 13:38:26.074047+08 latest_end_lsn | 0/43000998 latest_end_time | 2023-12-14 13:34:06.516149+08 slot_name | sender_host | 192.168.80.239 sender_port | 5432 conninfo | user=postgres password=******** channel_binding=disable dbname=replication host=192.168.80.239 port=5432 application_name=standby1 fallback_application_name=walreceiver sslmode=disable sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any
alter system reset synchronous_standby_names;
select pg_reload_conf();
## 删除 application_name
vi postgresql.auto.conf
primary_conninfo = 'user=postgres password=admin channel_binding=disable host=192.168.80.239 port=5432 sslmode=disable sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
pg_ctl reload
## 主库检查 postgres=# select client_addr,state,sync_state from pg_stat_replication; client_addr | state | sync_state ----------------+-----------+------------ 192.168.80.240 | streaming | async (1 row) postgres=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 16296 usesysid | 10 usename | postgres application_name | walreceiver client_addr | 192.168.80.240 client_hostname | client_port | 41594 backend_start | 2023-12-14 13:54:40.95684+08 backend_xmin | state | streaming sent_lsn | 0/43000D40 write_lsn | 0/43000D40 flush_lsn | 0/43000D40 replay_lsn | 0/43000D40 write_lag | flush_lag | replay_lag | sync_priority | 0 sync_state | async reply_time | 2023-12-14 14:03:17.086516+08 ## 备库检查 postgres=# select * from pg_stat_wal_receiver; -[ RECORD 1 ]---------+------------------------------------------------------------------------- pid | 15972 status | streaming receive_start_lsn | 0/43000000 receive_start_tli | 2 written_lsn | 0/43000D40 flushed_lsn | 0/43000D40 received_tli | 2 last_msg_send_time | 2023-12-14 14:03:07.740902+08 last_msg_receipt_time | 2023-12-14 14:03:26.911762+08 latest_end_lsn | 0/43000D40 latest_end_time | 2023-12-14 13:59:07.345619+08 slot_name | sender_host | 192.168.80.239 sender_port | 5432 conninfo | user=postgres password=******** channel_binding=disable dbname=replication host=192.168.80.239 port=5432 application_name=standby1 fallback_application_name=walreceiver sslmode=disable sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。