本节解释了核心 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.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 记录,这可能会阻止服务器启动。