PostgreSQL Source Code  git master
rmgr.c
Go to the documentation of this file.
1 /*
2  * rmgr.c
3  *
4  * Resource managers definition
5  *
6  * src/backend/access/transam/rmgr.c
7  */
8 #include "postgres.h"
9 
10 #include "access/brin_xlog.h"
11 #include "access/clog.h"
12 #include "access/commit_ts.h"
13 #include "access/generic_xlog.h"
14 #include "access/ginxlog.h"
15 #include "access/gistxlog.h"
16 #include "access/hash_xlog.h"
17 #include "access/heapam_xlog.h"
18 #include "access/multixact.h"
19 #include "access/nbtxlog.h"
20 #include "access/spgxlog.h"
21 #include "access/xact.h"
22 #include "access/xlog_internal.h"
23 #include "catalog/storage_xlog.h"
25 #include "commands/sequence.h"
26 #include "commands/tablespace.h"
27 #include "fmgr.h"
28 #include "funcapi.h"
29 #include "miscadmin.h"
30 #include "replication/decode.h"
31 #include "replication/message.h"
32 #include "replication/origin.h"
33 #include "storage/standby.h"
34 #include "utils/builtins.h"
35 #include "utils/relmapper.h"
36 
37 /* must be kept in sync with RmgrData definition in xlog_internal.h */
38 #define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode) \
39  { name, redo, desc, identify, startup, cleanup, mask, decode },
40 
42 #include "access/rmgrlist.h"
43 };
44 
45 /*
46  * Start up all resource managers.
47  */
48 void
50 {
51  for (int rmid = 0; rmid <= RM_MAX_ID; rmid++)
52  {
53  if (!RmgrIdExists(rmid))
54  continue;
55 
56  if (RmgrTable[rmid].rm_startup != NULL)
57  RmgrTable[rmid].rm_startup();
58  }
59 }
60 
61 /*
62  * Clean up all resource managers.
63  */
64 void
66 {
67  for (int rmid = 0; rmid <= RM_MAX_ID; rmid++)
68  {
69  if (!RmgrIdExists(rmid))
70  continue;
71 
72  if (RmgrTable[rmid].rm_cleanup != NULL)
73  RmgrTable[rmid].rm_cleanup();
74  }
75 }
76 
77 /*
78  * Emit ERROR when we encounter a record with an RmgrId we don't
79  * recognize.
80  */
81 void
83 {
84  ereport(ERROR, (errmsg("resource manager with ID %d not registered", rmid),
85  errhint("Include the extension module that implements this resource manager in shared_preload_libraries.")));
86 }
87 
88 /*
89  * Register a new custom WAL resource manager.
90  *
91  * Resource manager IDs must be globally unique across all extensions. Refer
92  * to https://wiki.postgresql.org/wiki/CustomWALResourceManager to reserve a
93  * unique RmgrId for your extension, to avoid conflicts with other extension
94  * developers. During development, use RM_EXPERIMENTAL_ID to avoid needlessly
95  * reserving a new ID.
96  */
97 void
99 {
100  if (rmgr->rm_name == NULL || strlen(rmgr->rm_name) == 0)
101  ereport(ERROR, (errmsg("custom resource manager name is invalid"),
102  errhint("Provide a non-empty name for the custom resource manager.")));
103 
104  if (!RmgrIdIsCustom(rmid))
105  ereport(ERROR, (errmsg("custom resource manager ID %d is out of range", rmid),
106  errhint("Provide a custom resource manager ID between %d and %d.",
108 
110  ereport(ERROR,
111  (errmsg("failed to register custom resource manager \"%s\" with ID %d", rmgr->rm_name, rmid),
112  errdetail("Custom resource manager must be registered while initializing modules in shared_preload_libraries.")));
113 
114  if (RmgrTable[rmid].rm_name != NULL)
115  ereport(ERROR,
116  (errmsg("failed to register custom resource manager \"%s\" with ID %d", rmgr->rm_name, rmid),
117  errdetail("Custom resource manager \"%s\" already registered with the same ID.",
118  RmgrTable[rmid].rm_name)));
119 
120  /* check for existing rmgr with the same name */
121  for (int existing_rmid = 0; existing_rmid <= RM_MAX_ID; existing_rmid++)
122  {
123  if (!RmgrIdExists(existing_rmid))
124  continue;
125 
126  if (!pg_strcasecmp(RmgrTable[existing_rmid].rm_name, rmgr->rm_name))
127  ereport(ERROR,
128  (errmsg("failed to register custom resource manager \"%s\" with ID %d", rmgr->rm_name, rmid),
129  errdetail("Existing resource manager with ID %d has the same name.", existing_rmid)));
130  }
131 
132  /* register it */
133  RmgrTable[rmid] = *rmgr;
134  ereport(LOG,
135  (errmsg("registered custom resource manager \"%s\" with ID %d",
136  rmgr->rm_name, rmid)));
137 }
138 
139 /* SQL SRF showing loaded resource managers */
140 Datum
142 {
143 #define PG_GET_RESOURCE_MANAGERS_COLS 3
144  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
146  bool nulls[PG_GET_RESOURCE_MANAGERS_COLS] = {0};
147 
148  SetSingleFuncCall(fcinfo, 0);
149 
150  for (int rmid = 0; rmid <= RM_MAX_ID; rmid++)
151  {
152  if (!RmgrIdExists(rmid))
153  continue;
154  values[0] = Int32GetDatum(rmid);
155  values[1] = CStringGetTextDatum(GetRmgr(rmid).rm_name);
156  values[2] = BoolGetDatum(RmgrIdIsBuiltin(rmid));
157  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
158  }
159 
160  return (Datum) 0;
161 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:85
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errhint(const char *fmt,...)
Definition: elog.c:1151
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define LOG
Definition: elog.h:25
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
void SetSingleFuncCall(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
bool process_shared_preload_libraries_in_progress
Definition: miscinit.c:1618
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
uintptr_t Datum
Definition: postgres.h:411
#define BoolGetDatum(X)
Definition: postgres.h:446
#define Int32GetDatum(X)
Definition: postgres.h:523
void RmgrStartup(void)
Definition: rmgr.c:49
RmgrData RmgrTable[RM_MAX_ID+1]
Definition: rmgr.c:41
#define PG_GET_RESOURCE_MANAGERS_COLS
void RmgrCleanup(void)
Definition: rmgr.c:65
void RmgrNotFound(RmgrId rmid)
Definition: rmgr.c:82
void RegisterCustomRmgr(RmgrId rmid, RmgrData *rmgr)
Definition: rmgr.c:98
Datum pg_get_wal_resource_managers(PG_FUNCTION_ARGS)
Definition: rmgr.c:141
static bool RmgrIdIsCustom(int rmid)
Definition: rmgr.h:48
#define RM_MAX_CUSTOM_ID
Definition: rmgr.h:36
#define RM_MAX_ID
Definition: rmgr.h:33
uint8 RmgrId
Definition: rmgr.h:11
static bool RmgrIdIsBuiltin(int rmid)
Definition: rmgr.h:42
#define RM_MIN_CUSTOM_ID
Definition: rmgr.h:35
void(* rm_cleanup)(void)
const char * rm_name
void(* rm_startup)(void)
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
static RmgrData GetRmgr(RmgrId rmid)
static bool RmgrIdExists(RmgrId rmid)