|
| 1 | +/*-------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * test_custom_rmgrs.c |
| 4 | + * Code for testing custom WAL resource managers. |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * IDENTIFICATION |
| 10 | + * src/test/modules/test_custom_rmgrs/test_custom_rmgrs.c |
| 11 | + * |
| 12 | + * Custom WAL resource manager for records containing a simple textual |
| 13 | + * payload, no-op redo, and no decoding. |
| 14 | + * |
| 15 | + * ------------------------------------------------------------------------- |
| 16 | + */ |
| 17 | + |
| 18 | +#include "postgres.h" |
| 19 | + |
| 20 | +#include "access/xlog.h" |
| 21 | +#include "access/xlog_internal.h" |
| 22 | +#include "access/xloginsert.h" |
| 23 | +#include "fmgr.h" |
| 24 | +#include "utils/pg_lsn.h" |
| 25 | + |
| 26 | +PG_MODULE_MAGIC; |
| 27 | + |
| 28 | +/* |
| 29 | + * test_custom_rmgrs WAL record message. |
| 30 | + */ |
| 31 | +typedef struct xl_testcustomrmgrs_message |
| 32 | +{ |
| 33 | + Size message_size; /* size of the message */ |
| 34 | + char message[FLEXIBLE_ARRAY_MEMBER]; /* payload */ |
| 35 | +} xl_testcustomrmgrs_message; |
| 36 | + |
| 37 | +#define SizeOfTestCustomRmgrsMessage (offsetof(xl_testcustomrmgrs_message, message)) |
| 38 | +#define XLOG_TEST_CUSTOM_RMGRS_MESSAGE 0x00 |
| 39 | + |
| 40 | +/* |
| 41 | + * While developing or testing, use RM_EXPERIMENTAL_ID for rmid. For a real |
| 42 | + * extension, reserve a new resource manager ID to avoid conflicting with |
| 43 | + * other extensions; see: |
| 44 | + * https://wiki.postgresql.org/wiki/CustomWALResourceManagers |
| 45 | + */ |
| 46 | +#define RM_TESTCUSTOMRMGRS_ID RM_EXPERIMENTAL_ID |
| 47 | +#define TESTCUSTOMRMGRS_NAME "test_custom_rmgrs" |
| 48 | + |
| 49 | +/* RMGR API, see xlog_internal.h */ |
| 50 | +void testcustomrmgrs_redo(XLogReaderState *record); |
| 51 | +void testcustomrmgrs_desc(StringInfo buf, XLogReaderState *record); |
| 52 | +const char *testcustomrmgrs_identify(uint8 info); |
| 53 | + |
| 54 | +static RmgrData testcustomrmgrs_rmgr = { |
| 55 | + .rm_name = TESTCUSTOMRMGRS_NAME, |
| 56 | + .rm_redo = testcustomrmgrs_redo, |
| 57 | + .rm_desc = testcustomrmgrs_desc, |
| 58 | + .rm_identify = testcustomrmgrs_identify |
| 59 | +}; |
| 60 | + |
| 61 | +/* |
| 62 | + * Module load callback |
| 63 | + */ |
| 64 | +void |
| 65 | +_PG_init(void) |
| 66 | +{ |
| 67 | + /* |
| 68 | + * In order to create our own custom resource manager, we have to be |
| 69 | + * loaded via shared_preload_libraries. Otherwise, registration will fail. |
| 70 | + */ |
| 71 | + RegisterCustomRmgr(RM_TESTCUSTOMRMGRS_ID, &testcustomrmgrs_rmgr); |
| 72 | +} |
| 73 | + |
| 74 | +/* RMGR API implementation */ |
| 75 | + |
| 76 | +/* |
| 77 | + * Redo is just a noop for this module, because we aren't testing recovery of |
| 78 | + * any real structure. |
| 79 | + */ |
| 80 | +void |
| 81 | +testcustomrmgrs_redo(XLogReaderState *record) |
| 82 | +{ |
| 83 | + uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK; |
| 84 | + |
| 85 | + if (info != XLOG_TEST_CUSTOM_RMGRS_MESSAGE) |
| 86 | + elog(PANIC, "testcustomrmgrs_redo: unknown op code %u", info); |
| 87 | +} |
| 88 | + |
| 89 | +void |
| 90 | +testcustomrmgrs_desc(StringInfo buf, XLogReaderState *record) |
| 91 | +{ |
| 92 | + char *rec = XLogRecGetData(record); |
| 93 | + uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK; |
| 94 | + |
| 95 | + if (info == XLOG_TEST_CUSTOM_RMGRS_MESSAGE) |
| 96 | + { |
| 97 | + xl_testcustomrmgrs_message *xlrec = (xl_testcustomrmgrs_message *) rec; |
| 98 | + |
| 99 | + appendStringInfo(buf, "payload (%zu bytes): ", xlrec->message_size); |
| 100 | + appendBinaryStringInfo(buf, xlrec->message, xlrec->message_size); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +const char * |
| 105 | +testcustomrmgrs_identify(uint8 info) |
| 106 | +{ |
| 107 | + if ((info & ~XLR_INFO_MASK) == XLOG_TEST_CUSTOM_RMGRS_MESSAGE) |
| 108 | + return "TEST_CUSTOM_RMGRS_MESSAGE"; |
| 109 | + |
| 110 | + return NULL; |
| 111 | +} |
| 112 | + |
| 113 | +/* |
| 114 | + * SQL function for writing a simple message into WAL with the help of custom |
| 115 | + * WAL resource manager. |
| 116 | + */ |
| 117 | +PG_FUNCTION_INFO_V1(test_custom_rmgrs_insert_wal_record); |
| 118 | +Datum |
| 119 | +test_custom_rmgrs_insert_wal_record(PG_FUNCTION_ARGS) |
| 120 | +{ |
| 121 | + text *arg = PG_GETARG_TEXT_PP(0); |
| 122 | + char *payload = VARDATA_ANY(arg); |
| 123 | + Size len = VARSIZE_ANY_EXHDR(arg); |
| 124 | + XLogRecPtr lsn; |
| 125 | + xl_testcustomrmgrs_message xlrec; |
| 126 | + |
| 127 | + xlrec.message_size = len; |
| 128 | + |
| 129 | + XLogBeginInsert(); |
| 130 | + XLogRegisterData((char *) &xlrec, SizeOfTestCustomRmgrsMessage); |
| 131 | + XLogRegisterData((char *) payload, len); |
| 132 | + |
| 133 | + /* Let's mark this record as unimportant, just in case. */ |
| 134 | + XLogSetRecordFlags(XLOG_MARK_UNIMPORTANT); |
| 135 | + |
| 136 | + lsn = XLogInsert(RM_TESTCUSTOMRMGRS_ID, XLOG_TEST_CUSTOM_RMGRS_MESSAGE); |
| 137 | + |
| 138 | + PG_RETURN_LSN(lsn); |
| 139 | +} |
0 commit comments