支援的版本:目前 (17) / 16 / 15
開發版本:devel

63.2. 自訂 WAL 資源管理器 #

本節說明核心 PostgreSQL 系統和自訂 WAL 資源管理器之間的介面,它使擴充功能可以直接與 WAL 整合。

擴充功能,尤其是 資料表存取方法索引存取方法,可能需要使用 WAL 進行復原、複製和/或 邏輯解碼

要建立新的自訂 WAL 資源管理器,首先定義一個 RmgrData 結構,其中包含資源管理器方法的實作。 請參考 PostgreSQL 原始碼中的 src/backend/access/transam/READMEsrc/include/access/xlog_internal.h

/*
 * Method table for resource managers.
 *
 * This struct must be kept in sync with the PG_RMGR definition in
 * rmgr.c.
 *
 * rm_identify must return a name for the record based on xl_info (without
 * reference to the rmid). For example, XLOG_BTREE_VACUUM would be named
 * "VACUUM". rm_desc can then be called to obtain additional detail for the
 * record, if available (e.g. the last block).
 *
 * rm_mask takes as input a page modified by the resource manager and masks
 * out bits that shouldn't be flagged by wal_consistency_checking.
 *
 * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h). If rm_name is
 * NULL, the corresponding RmgrTable entry is considered invalid.
 */
typedef struct RmgrData
{
    const char *rm_name;
    void        (*rm_redo) (XLogReaderState *record);
    void        (*rm_desc) (StringInfo buf, XLogReaderState *record);
    const char *(*rm_identify) (uint8 info);
    void        (*rm_startup) (void);
    void        (*rm_cleanup) (void);
    void        (*rm_mask) (char *pagedata, BlockNumber blkno);
    void        (*rm_decode) (struct LogicalDecodingContext *ctx,
                              struct XLogRecordBuffer *buf);
} RmgrData;

src/test/modules/test_custom_rmgrs 模組包含一個可運作的範例,示範了自訂 WAL 資源管理器的使用方式。

然後,註冊您的新資源管理器。

/*
 * Register a new custom WAL resource manager.
 *
 * Resource manager IDs must be globally unique across all extensions. Refer
 * to https://wiki.postgresql.org/wiki/CustomWALResourceManagers to reserve a
 * unique RmgrId for your extension, to avoid conflicts with other extension
 * developers. During development, use RM_EXPERIMENTAL_ID to avoid needlessly
 * reserving a new ID.
 */
extern void RegisterCustomRmgr(RmgrId rmid, const RmgrData *rmgr);

RegisterCustomRmgr 必須從擴充模組的 _PG_init 函式中呼叫。 在開發新的擴充功能時,請為 rmid 使用 RM_EXPERIMENTAL_ID。 當您準備好向使用者發布擴充功能時,請在 自訂 WAL 資源管理器頁面上保留新的資源管理器 ID。

將實作自訂資源管理器的擴充模組放在 shared_preload_libraries 中,以便在 PostgreSQL 啟動期間提早載入它。

注意

只要系統中存在任何自訂 WAL 記錄,擴充功能就必須保留在 shared_preload_libraries 中。 否則,PostgreSQL 將無法套用或解碼自訂 WAL 記錄,這可能會阻止伺服器啟動。

提交更正

如果您在文件中發現任何不正確、與您使用特定功能不符或需要進一步說明的地方,請使用此表單回報文件問題。