天天看點

PostgreSQL 10.0 preview 功能增強 - 增加access method CHECK接口amcheck

postgresql , 10.0 , amcheck , 邏輯一緻性檢測 , 實體存儲檢測

一些高端存儲、包括zfs檔案系統,在使用了raid後,有塊檢測和異常塊的修複功能。

對于資料庫來說,資料的可靠性是非常重要的名額,例如:

1. 寫進入是什麼,讀出來就應該是什麼。

2. 當作業系統的collate發生變化時,索引的順序可能與實際的collate順序不比對。造成不穩定現象。

3. 資料塊partial write,可能導緻資料損壞。

4. 記憶體頁異常,使用到某些異常頁時,可能帶來問題。

postgresql通過full page write來避免3的問題。另外在資料頁上面有checksum提供檢測。

postgresql 10.0 提供了一個check接口,可以對資料進行檢測,發現以上問題。

amcheck是一個架構,用于檢測資料的一緻性。例如pg有heap存儲,b-tree,gist,gin,sp-gist,brin索引存儲。amcheck可以用于檢測各種接口對應資料存儲的一緻性。

命名為amcheck, am指的是access method,檢測的自然是access method相關的。

目前amcheck已經做到可以檢測索引的異常(例如前面提到的作業系統collate變化引發的索引的邏輯順序異常)。未來會擴充更多的檢測接口。

10.0也推出了對icu的支援,從根源上避免了collate的問題。

<a href="https://github.com/digoal/blog/blob/master/201703/20170330_04.md">《postgresql 10.0 preview 功能增強 - 國際化功能增強,支援icu(international components for unicode)》</a>

1. structural inconsistencies caused by incorrect operator class implementations.

問題可能來自作業系統collate的變化,導緻collate變化前後,query輸出不一緻(順序)的結果.

檢測方法,參考每種access method的一緻性校驗function

<a href="https://www.postgresql.org/docs/devel/static/xindex.html#xindex-support">https://www.postgresql.org/docs/devel/static/xindex.html#xindex-support</a>

2. corruption caused by hypothetical undiscovered bugs in the underlying postgresql access method code or sort code.

3. filesystem or storage subsystem faults where checksums happen to simply not be enabled.

4. corruption caused by faulty ram, and the broader memory subsystem and operating system.

不同的異常,修複的方法不一樣,通常能直接修複的是reindex。(但并不是所有的異常都有方法修複。)

但是社群給出了一個建議,如果是代碼的bug,reindex可能是無法修複的,但是通過pageinspect插件,可以幫助進行問題診斷。

對于b-tree索引資料,通過這兩個接口可以進行檢測。

1. bt_index_check(index regclass) returns void

加select一樣的accessshared鎖。基本無影響。注意,如果被檢測的索引頁在shared buffer中時,不會掃磁盤。

傳回空表示正常。

2. bt_index_parent_check(index regclass) returns void

被檢測的索引,以及索引對應的表加sharelock鎖。沖突較大,堵塞insert, update, and delete,表的vacuum,以及更大的鎖操作。

hot stnadby不允許執行 bt_index_parent_check(index regclass) 。

這個patch的讨論,詳見郵件組,本文末尾url。

postgresql社群的作風非常嚴謹,一個patch可能在郵件組中讨論幾個月甚至幾年,根據大家的意見反複的修正,patch合并到master已經非常成熟,是以postgresql的穩定性也是遠近聞名的。

<a href="https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=3717dc149ecf44b8be95350a68605ba7299474fd">https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=3717dc149ecf44b8be95350a68605ba7299474fd</a>

<a href="https://www.postgresql.org/docs/devel/static/amcheck.html">https://www.postgresql.org/docs/devel/static/amcheck.html</a>