Update FSM on WAL replay. This is a bit limited; the FSM is only updated
[PostgreSQL.git] / src / include / utils / elog.h
blob112af9f2011d29f98cd84eadc6f8f3ce7bac82c5
1 /*-------------------------------------------------------------------------
3 * elog.h
4 * POSTGRES error reporting/logging definitions.
7 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL$
12 *-------------------------------------------------------------------------
14 #ifndef ELOG_H
15 #define ELOG_H
17 #include <setjmp.h>
19 /* Error level codes */
20 #define DEBUG5 10 /* Debugging messages, in categories of
21 * decreasing detail. */
22 #define DEBUG4 11
23 #define DEBUG3 12
24 #define DEBUG2 13
25 #define DEBUG1 14 /* used by GUC debug_* variables */
26 #define LOG 15 /* Server operational messages; sent only to
27 * server log by default. */
28 #define COMMERROR 16 /* Client communication problems; same as LOG
29 * for server reporting, but never sent to
30 * client. */
31 #define INFO 17 /* Informative messages that are always sent
32 * to client; is not affected by
33 * client_min_messages */
34 #define NOTICE 18 /* Helpful messages to users about query
35 * operation; sent to client and server log
36 * by default. */
37 #define WARNING 19 /* Warnings. NOTICE is for expected messages
38 * like implicit sequence creation by SERIAL.
39 * WARNING is for unexpected messages. */
40 #define ERROR 20 /* user error - abort transaction; return to
41 * known state */
42 /* Save ERROR value in PGERROR so it can be restored when Win32 includes
43 * modify it. We have to use a constant rather than ERROR because macros
44 * are expanded only when referenced outside macros.
46 #ifdef WIN32
47 #define PGERROR 20
48 #endif
49 #define FATAL 21 /* fatal error - abort process */
50 #define PANIC 22 /* take down the other backends with me */
52 /* #define DEBUG DEBUG1 */ /* Backward compatibility with pre-7.3 */
55 /* macros for representing SQLSTATE strings compactly */
56 #define PGSIXBIT(ch) (((ch) - '0') & 0x3F)
57 #define PGUNSIXBIT(val) (((val) & 0x3F) + '0')
59 #define MAKE_SQLSTATE(ch1,ch2,ch3,ch4,ch5) \
60 (PGSIXBIT(ch1) + (PGSIXBIT(ch2) << 6) + (PGSIXBIT(ch3) << 12) + \
61 (PGSIXBIT(ch4) << 18) + (PGSIXBIT(ch5) << 24))
63 /* These macros depend on the fact that '0' becomes a zero in SIXBIT */
64 #define ERRCODE_TO_CATEGORY(ec) ((ec) & ((1 << 12) - 1))
65 #define ERRCODE_IS_CATEGORY(ec) (((ec) & ~((1 << 12) - 1)) == 0)
67 /* SQLSTATE codes for errors are defined in a separate file */
68 #include "utils/errcodes.h"
71 /* Which __func__ symbol do we have, if any? */
72 #ifdef HAVE_FUNCNAME__FUNC
73 #define PG_FUNCNAME_MACRO __func__
74 #else
75 #ifdef HAVE_FUNCNAME__FUNCTION
76 #define PG_FUNCNAME_MACRO __FUNCTION__
77 #else
78 #define PG_FUNCNAME_MACRO NULL
79 #endif
80 #endif
83 /*----------
84 * New-style error reporting API: to be used in this way:
85 * ereport(ERROR,
86 * (errcode(ERRCODE_UNDEFINED_CURSOR),
87 * errmsg("portal \"%s\" not found", stmt->portalname),
88 * ... other errxxx() fields as needed ...));
90 * The error level is required, and so is a primary error message (errmsg
91 * or errmsg_internal). All else is optional. errcode() defaults to
92 * ERRCODE_INTERNAL_ERROR if elevel is ERROR or more, ERRCODE_WARNING
93 * if elevel is WARNING, or ERRCODE_SUCCESSFUL_COMPLETION if elevel is
94 * NOTICE or below.
96 * ereport_domain() allows a message domain to be specified, for modules that
97 * wish to use a different message catalog from the backend's. To avoid having
98 * one copy of the default text domain per .o file, we define it as NULL here
99 * and have errstart insert the default text domain. Modules can either use
100 * ereport_domain() directly, or preferably they can override the TEXTDOMAIN
101 * macro.
102 *----------
104 #define ereport_domain(elevel, domain, rest) \
105 (errstart(elevel, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain) ? \
106 (errfinish rest) : (void) 0)
108 #define ereport(level, rest) \
109 ereport_domain(level, TEXTDOMAIN, rest)
111 #define TEXTDOMAIN NULL
113 extern bool errstart(int elevel, const char *filename, int lineno,
114 const char *funcname, const char *domain);
115 extern void errfinish(int dummy,...);
117 extern int errcode(int sqlerrcode);
119 extern int errcode_for_file_access(void);
120 extern int errcode_for_socket_access(void);
122 extern int
123 errmsg(const char *fmt,...)
124 /* This extension allows gcc to check the format string for consistency with
125 the supplied arguments. */
126 __attribute__((format(printf, 1, 2)));
128 extern int
129 errmsg_internal(const char *fmt,...)
130 /* This extension allows gcc to check the format string for consistency with
131 the supplied arguments. */
132 __attribute__((format(printf, 1, 2)));
134 extern int
135 errdetail(const char *fmt,...)
136 /* This extension allows gcc to check the format string for consistency with
137 the supplied arguments. */
138 __attribute__((format(printf, 1, 2)));
140 extern int
141 errdetail_log(const char *fmt,...)
142 /* This extension allows gcc to check the format string for consistency with
143 the supplied arguments. */
144 __attribute__((format(printf, 1, 2)));
146 extern int
147 errhint(const char *fmt,...)
148 /* This extension allows gcc to check the format string for consistency with
149 the supplied arguments. */
150 __attribute__((format(printf, 1, 2)));
152 extern int
153 errcontext(const char *fmt,...)
154 /* This extension allows gcc to check the format string for consistency with
155 the supplied arguments. */
156 __attribute__((format(printf, 1, 2)));
158 extern int errhidestmt(bool hide_stmt);
160 extern int errfunction(const char *funcname);
161 extern int errposition(int cursorpos);
163 extern int internalerrposition(int cursorpos);
164 extern int internalerrquery(const char *query);
166 extern int geterrcode(void);
167 extern int geterrposition(void);
168 extern int getinternalerrposition(void);
171 /*----------
172 * Old-style error reporting API: to be used in this way:
173 * elog(ERROR, "portal \"%s\" not found", stmt->portalname);
174 *----------
176 #define elog elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO), elog_finish
178 extern void elog_start(const char *filename, int lineno, const char *funcname);
179 extern void
180 elog_finish(int elevel, const char *fmt,...)
181 /* This extension allows gcc to check the format string for consistency with
182 the supplied arguments. */
183 __attribute__((format(printf, 2, 3)));
186 /* Support for attaching context information to error reports */
188 typedef struct ErrorContextCallback
190 struct ErrorContextCallback *previous;
191 void (*callback) (void *arg);
192 void *arg;
193 } ErrorContextCallback;
195 extern PGDLLIMPORT ErrorContextCallback *error_context_stack;
198 /*----------
199 * API for catching ereport(ERROR) exits. Use these macros like so:
201 * PG_TRY();
203 * ... code that might throw ereport(ERROR) ...
205 * PG_CATCH();
207 * ... error recovery code ...
209 * PG_END_TRY();
211 * (The braces are not actually necessary, but are recommended so that
212 * pg_indent will indent the construct nicely.) The error recovery code
213 * can optionally do PG_RE_THROW() to propagate the same error outwards.
215 * Note: while the system will correctly propagate any new ereport(ERROR)
216 * occurring in the recovery section, there is a small limit on the number
217 * of levels this will work for. It's best to keep the error recovery
218 * section simple enough that it can't generate any new errors, at least
219 * not before popping the error stack.
221 * Note: an ereport(FATAL) will not be caught by this construct; control will
222 * exit straight through proc_exit(). Therefore, do NOT put any cleanup
223 * of non-process-local resources into the error recovery section, at least
224 * not without taking thought for what will happen during ereport(FATAL).
225 * The PG_ENSURE_ERROR_CLEANUP macros provided by storage/ipc.h may be
226 * helpful in such cases.
227 *----------
229 #define PG_TRY() \
230 do { \
231 sigjmp_buf *save_exception_stack = PG_exception_stack; \
232 ErrorContextCallback *save_context_stack = error_context_stack; \
233 sigjmp_buf local_sigjmp_buf; \
234 if (sigsetjmp(local_sigjmp_buf, 0) == 0) \
236 PG_exception_stack = &local_sigjmp_buf
238 #define PG_CATCH() \
240 else \
242 PG_exception_stack = save_exception_stack; \
243 error_context_stack = save_context_stack
245 #define PG_END_TRY() \
247 PG_exception_stack = save_exception_stack; \
248 error_context_stack = save_context_stack; \
249 } while (0)
252 * gcc understands __attribute__((noreturn)); for other compilers, insert
253 * a useless exit() call so that the compiler gets the point.
255 #ifdef __GNUC__
256 #define PG_RE_THROW() \
257 pg_re_throw()
258 #else
259 #define PG_RE_THROW() \
260 (pg_re_throw(), exit(1))
261 #endif
263 extern PGDLLIMPORT sigjmp_buf *PG_exception_stack;
266 /* Stuff that error handlers might want to use */
269 * ErrorData holds the data accumulated during any one ereport() cycle.
270 * Any non-NULL pointers must point to palloc'd data.
271 * (The const pointers are an exception; we assume they point at non-freeable
272 * constant strings.)
274 typedef struct ErrorData
276 int elevel; /* error level */
277 bool output_to_server; /* will report to server log? */
278 bool output_to_client; /* will report to client? */
279 bool show_funcname; /* true to force funcname inclusion */
280 bool hide_stmt; /* true to prevent STATEMENT: inclusion */
281 const char *filename; /* __FILE__ of ereport() call */
282 int lineno; /* __LINE__ of ereport() call */
283 const char *funcname; /* __func__ of ereport() call */
284 const char *domain; /* message domain */
285 int sqlerrcode; /* encoded ERRSTATE */
286 char *message; /* primary error message */
287 char *detail; /* detail error message */
288 char *detail_log; /* detail error message for server log only */
289 char *hint; /* hint message */
290 char *context; /* context message */
291 int cursorpos; /* cursor index into query string */
292 int internalpos; /* cursor index into internalquery */
293 char *internalquery; /* text of internally-generated query */
294 int saved_errno; /* errno at entry */
295 } ErrorData;
297 extern void EmitErrorReport(void);
298 extern ErrorData *CopyErrorData(void);
299 extern void FreeErrorData(ErrorData *edata);
300 extern void FlushErrorState(void);
301 extern void ReThrowError(ErrorData *edata);
302 extern void pg_re_throw(void) __attribute__((noreturn));
305 /* GUC-configurable parameters */
307 typedef enum
309 PGERROR_TERSE, /* single-line error messages */
310 PGERROR_DEFAULT, /* recommended style */
311 PGERROR_VERBOSE /* all the facts, ma'am */
312 } PGErrorVerbosity;
314 extern int Log_error_verbosity;
315 extern char *Log_line_prefix;
316 extern int Log_destination;
318 /* Log destination bitmap */
319 #define LOG_DESTINATION_STDERR 1
320 #define LOG_DESTINATION_SYSLOG 2
321 #define LOG_DESTINATION_EVENTLOG 4
322 #define LOG_DESTINATION_CSVLOG 8
324 /* Other exported functions */
325 extern void DebugFileOpen(void);
326 extern char *unpack_sql_state(int sql_state);
327 extern bool in_error_recursion_trouble(void);
329 #ifdef HAVE_SYSLOG
330 extern void set_syslog_parameters(const char *ident, int facility);
331 #endif
334 * Write errors to stderr (or by equal means when stderr is
335 * not available). Used before ereport/elog can be used
336 * safely (memory context, GUC load etc)
338 extern void
339 write_stderr(const char *fmt,...)
340 /* This extension allows gcc to check the format string for consistency with
341 the supplied arguments. */
342 __attribute__((format(printf, 1, 2)));
344 #endif /* ELOG_H */