本章解释核心 PostgreSQL 系统与自定义 WAL 资源管理器之间的接口,这些资源管理器使扩展能够直接与 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.ac.cn/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 记录,这可能会阻止服务器启动。
如果您在文档中看到任何不正确、与您对特定功能的体验不符或需要进一步澄清的内容,请使用此表单报告文档问题。