codekingpro/portable-devtools
115k
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 66. Custom WAL Resource Managers</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="generic-wal.html" title="Chapter 65. Generic WAL Records" /><link rel="next" href="btree.html" title="Chapter 67. B-Tree Indexes" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">Chapter 66. Custom WAL Resource Managers</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="generic-wal.html" title="Chapter 65. Generic WAL Records">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="internals.html" title="Part VII. Internals">Up</a></td><th width="60%" align="center">Part VII. Internals</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="btree.html" title="Chapter 67. B-Tree Indexes">Next</a></td></tr></table><hr /></div><div class="chapter" id="CUSTOM-RMGR"><div class="titlepage"><div><div><h2 class="title">Chapter 66. Custom WAL Resource Managers</h2></div></div></div><p>3 This chapter explains the interface between the core4 <span class="productname">PostgreSQL</span> system and custom WAL resource5 managers, which enable extensions to integrate directly with the <a class="link" href="wal.html" title="Chapter 30. Reliability and the Write-Ahead Log"><acronym class="acronym">WAL</acronym></a>.6 </p><p>7 An extension, especially a <a class="link" href="tableam.html" title="Chapter 63. Table Access Method Interface Definition">Table Access8 Method</a> or <a class="link" href="indexam.html" title="Chapter 64. Index Access Method Interface Definition">Index Access Method</a>, may9 need to use WAL for recovery, replication, and/or <a class="link" href="logicaldecoding.html" title="Chapter 49. Logical Decoding">Logical Decoding</a>. Custom resource managers10 are a more flexible alternative to <a class="link" href="generic-wal.html" title="Chapter 65. Generic WAL Records">Generic11 WAL</a> (which does not support logical decoding), but more complex for12 an extension to implement.13 </p><p>14 To create a new custom WAL resource manager, first define an15 <code class="structname">RmgrData</code> structure with implementations for the16 resource manager methods. Refer to17 <code class="filename">src/backend/access/transam/README</code> and18 <code class="filename">src/include/access/xlog_internal.h</code> in the19 <span class="productname">PostgreSQL</span> source.20</p><pre class="programlisting">21/*22 * Method table for resource managers.23 *24 * This struct must be kept in sync with the PG_RMGR definition in25 * rmgr.c.26 *27 * rm_identify must return a name for the record based on xl_info (without28 * reference to the rmid). For example, XLOG_BTREE_VACUUM would be named29 * "VACUUM". rm_desc can then be called to obtain additional detail for the30 * record, if available (e.g. the last block).31 *32 * rm_mask takes as input a page modified by the resource manager and masks33 * out bits that shouldn't be flagged by wal_consistency_checking.34 *35 * RmgrTable[] is indexed by RmgrId values (see rmgrlist.h). If rm_name is36 * NULL, the corresponding RmgrTable entry is considered invalid.37 */38typedef struct RmgrData39{40 const char *rm_name;41 void (*rm_redo) (XLogReaderState *record);42 void (*rm_desc) (StringInfo buf, XLogReaderState *record);43 const char *(*rm_identify) (uint8 info);44 void (*rm_startup) (void);45 void (*rm_cleanup) (void);46 void (*rm_mask) (char *pagedata, BlockNumber blkno);47 void (*rm_decode) (struct LogicalDecodingContext *ctx,48 struct XLogRecordBuffer *buf);49} RmgrData;50</pre><p>51 </p><p>52 The <code class="filename">src/test/modules/test_custom_rmgrs</code> module53 contains a working example, which demonstrates usage of custom WAL54 resource managers.55 </p><p>56 Then, register your new resource57 manager.58 59</p><pre class="programlisting">60/*61 * Register a new custom WAL resource manager.62 *63 * Resource manager IDs must be globally unique across all extensions. Refer64 * to https://wiki.postgresql.org/wiki/CustomWALResourceManagers to reserve a65 * unique RmgrId for your extension, to avoid conflicts with other extension66 * developers. During development, use RM_EXPERIMENTAL_ID to avoid needlessly67 * reserving a new ID.68 */69extern void RegisterCustomRmgr(RmgrId rmid, const RmgrData *rmgr);70</pre><p>71 <code class="function">RegisterCustomRmgr</code> must be called from the72 extension module's <a class="link" href="xfunc-c.html#XFUNC-C-DYNLOAD" title="38.10.1. Dynamic Loading">_PG_init</a> function.73 While developing a new extension, use <code class="literal">RM_EXPERIMENTAL_ID</code>74 for <em class="parameter"><code>rmid</code></em>. When you are ready to release the extension75 to users, reserve a new resource manager ID at the <a class="ulink" href="https://wiki.postgresql.org/wiki/CustomWALResourceManagers" target="_top">Custom WAL76 Resource Manager</a> page.77 </p><p>78 Place the extension module implementing the custom resource manager in <a class="xref" href="runtime-config-client.html#GUC-SHARED-PRELOAD-LIBRARIES">shared_preload_libraries</a> so that it will be loaded early79 during <span class="productname">PostgreSQL</span> startup.80 </p><div class="note"><h3 class="title">Note</h3><p>81 The extension must remain in shared_preload_libraries as long as any82 custom WAL records may exist in the system. Otherwise83 <span class="productname">PostgreSQL</span> will not be able to apply or decode84 the custom WAL records, which may prevent the server from starting.85 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="generic-wal.html" title="Chapter 65. Generic WAL Records">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="internals.html" title="Part VII. Internals">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="btree.html" title="Chapter 67. B-Tree Indexes">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 65. Generic WAL Records </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Chapter 67. B-Tree Indexes</td></tr></table></div></body></html>