本節解釋了核心 PostgreSQL 系統與自定義 WAL 資源管理器之間的介面,這些資源管理器使擴充套件能夠直接與 WAL 整合。
擴充套件,特別是 表訪問方法 或 索引訪問方法,可能需要使用 WAL 進行恢復、複製和/或 邏輯解碼。
要建立新的自定義 WAL 資源管理器,請首先定義一個包含資源管理器方法實現的 RmgrData 結構。請參閱 PostgreSQL 原始碼中的 src/backend/access/transam/README 和 src/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 記錄,這可能會阻止伺服器啟動。
如果您在文件中發現任何不正確、與您在使用特定功能時的體驗不符或需要進一步澄清的內容,請使用 此表單 報告文件問題。