MathJax

MathJax-2

MathJax-3

Google Code Prettify

置頂入手筆記

EnterproseDB Quickstart — 快速入門筆記

由於考慮採用 EnterpriseDB 或是直接用 PostgreSQL 的人,通常需要一些入手的資料。這邊紀錄便提供相關快速上手的簡單筆記 ~ 這篇筆記以 資料庫安裝完畢後的快速使用 為目標,基本紀錄登入使用的範例:

2016年3月14日 星期一

PostgreSQL 的 Physical Replication Slot 功能

在 PostgreSQL 9.4 中,引進了用來防止 streaming replication 中斷後就無法再次建立 streaming replication 問題的功能:Replication Slot。

Replication Slot 有兩種:Physical Replication Slot 與 Logical Replication Slot。在此使用的是 Physical Replication Slot,是不涉及解析 WAL 交易日誌內容的 Replication Slot 功能。

一般使用 streaming replication,必須要設定 wal_keep_segment 選項,才能避免在 standby 不小心備份不及(例如,某時段的 primary 交易量太大,交易日誌產生太快),造成 standby 還沒抓交易日誌,就被 primary 洗掉了。如果交易日誌被洗掉,streaming replication 就會中斷,並且無法再繼續進行,這樣就得重設立 streaming replication 了。

現在使用 replication slot, primary 就會知道目前 standby 同步的狀況,primary 就不會把還沒同步完畢的交易日誌洗掉,等待 standby 抓取完交易日誌才刪去舊的日誌。




Replication Slot 使用方式還蠻簡便的。基於我在 streaming replication 筆記的設定過程,再稍微修改就能完成設定了。

1. 設定檔修改
[primary]
    設定 postgresql.conf 內容

  wal_level = hot_standby
  max_wal_senders = 3
  max_replication_slots = 1     #使用replication slot
  hot_standby = on

[standby]
    設定 recovery.conf

  standby_mode = 'on'
  primary_conninfo = 'host=192.168.57.102 port=5432 user=strmrepl'
  primary_slot_name = 'myslot1'

2. 啟用 replication slot
    a) 用 psql 連線到 primary server 裡面,啟動上面在 standby server 裡設定要連的 replication slot 名稱

[primary]
  postgres=# SELECT* FROM pg_create_physical_replication_slot('myslot1');

   b) 這時候再去重新啟動 standby,就完成了

[standby]
  [aaa]$  pg_ctl -D /home/aaa/pgsql/9.5/data/ -l logfile restart

3. 檢視 replication slot
啟動之後,我們能夠在 primary 上檢視 slot 的資訊 :

 [primary]
  postgres=# SELECT slot_name, active FROM pg_replication_slots;
   slot_name | active 
  -----------+--------
   myslot1   | t
  (1 row)
關閉 Replication Slot
要關掉 slot,必須要先使 slot 停止運作

[standby]
 1. 先修改 recovery.conf,去除 primary_slot_name 一列設定
 
   standby_mode = 'on'
   primary_conninfo = 'host=192.168.57.102 port=5432 user=strmrepl'
   # primary_slot_name = 'myslot1'

2. 重新「啟動」standby( reload 沒用)
3. 這時就可以關閉 replication slot 了
[primary]
 [postgres] # SELECT pg_drop_replication_slot('myslot1');
後記:
replication slot 是基於未來 PostgreSQL 要發展新的技術:bi-directional replication 而發展的功能。照開發專案網頁描述,這功能是在發展 PostgreSQL 上的 multi-master replication。這表示未來將會有另一套 replication 方式,就讓我們拭目以待吧。

 參考資料:
 
Replication slots in Postgresql by Torbjørn Kristoffersen
Streaming replication slots in PostgreSQL 9.4 by Craig Ringer
PostgreSQL 9.4: Replication slots by 谭峰(franc)
PgSQL · 特性分析· Replication Slot - 数据库内核月报連結2
Postgres 9.4 feature highlight: Logical replication receiver

Logical Replication Slot 的資訊



有關 bi-directional replication 的訊息,目前這個功能是由 2ndQuadrant 所發展的專案,有興趣的能夠到這找到相關資訊。

沒有留言:

張貼留言