↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

支持中的版本: 当前版本 (18) / 17 / 16 / 15
开发中的版本: 19 / 20devel

第 66 章 自定义 WAL 资源管理器

本章说明核心 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.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);

必须从扩展模块的_PG_init 函数中调用 RegisterCustomRmgr。在开发新扩展期间,请对 rmid 使用 RM_EXPERIMENTAL_ID。当准备向用户发布该扩展时,请在 Custom WAL Resource Manager 页面上为其保留一个新的资源管理器 ID。

将实现该自定义资源管理器的扩展模块加入 shared_preload_libraries,以便它在 PostgreSQL 启动期间尽早装载。

注意

只要系统中可能仍存在任何自定义 WAL 记录,该扩展就必须继续保留在 shared_preload_libraries 中。否则,PostgreSQL 将无法应用或解码这些自定义 WAL 记录,这可能导致服务器无法启动。

报告文档问题

阅读 上游文档. 通过 PostgreSQL 文档反馈表单.