]> granicus.if.org Git - postgresql/blob - src/include/miscadmin.h
Do stack-depth checking in all postmaster children.
[postgresql] / src / include / miscadmin.h
1 /*-------------------------------------------------------------------------
2  *
3  * miscadmin.h
4  *        This file contains general postgres administration and initialization
5  *        stuff that used to be spread out between the following files:
6  *              globals.h                                               global variables
7  *              pdir.h                                                  directory path crud
8  *              pinit.h                                                 postgres initialization
9  *              pmod.h                                                  processing modes
10  *        Over time, this has also become the preferred place for widely known
11  *        resource-limitation stuff, such as work_mem and check_stack_depth().
12  *
13  * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
14  * Portions Copyright (c) 1994, Regents of the University of California
15  *
16  * src/include/miscadmin.h
17  *
18  * NOTES
19  *        some of the information in this file should be moved to other files.
20  *
21  *-------------------------------------------------------------------------
22  */
23 #ifndef MISCADMIN_H
24 #define MISCADMIN_H
25
26 #include "pgtime.h"                             /* for pg_time_t */
27
28
29 #define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
30
31
32 /*****************************************************************************
33  *        System interrupt and critical section handling
34  *
35  * There are two types of interrupts that a running backend needs to accept
36  * without messing up its state: QueryCancel (SIGINT) and ProcDie (SIGTERM).
37  * In both cases, we need to be able to clean up the current transaction
38  * gracefully, so we can't respond to the interrupt instantaneously ---
39  * there's no guarantee that internal data structures would be self-consistent
40  * if the code is interrupted at an arbitrary instant.  Instead, the signal
41  * handlers set flags that are checked periodically during execution.
42  *
43  * The CHECK_FOR_INTERRUPTS() macro is called at strategically located spots
44  * where it is normally safe to accept a cancel or die interrupt.  In some
45  * cases, we invoke CHECK_FOR_INTERRUPTS() inside low-level subroutines that
46  * might sometimes be called in contexts that do *not* want to allow a cancel
47  * or die interrupt.  The HOLD_INTERRUPTS() and RESUME_INTERRUPTS() macros
48  * allow code to ensure that no cancel or die interrupt will be accepted,
49  * even if CHECK_FOR_INTERRUPTS() gets called in a subroutine.  The interrupt
50  * will be held off until CHECK_FOR_INTERRUPTS() is done outside any
51  * HOLD_INTERRUPTS() ... RESUME_INTERRUPTS() section.
52  *
53  * Special mechanisms are used to let an interrupt be accepted when we are
54  * waiting for a lock or when we are waiting for command input (but, of
55  * course, only if the interrupt holdoff counter is zero).      See the
56  * related code for details.
57  *
58  * A lost connection is handled similarly, although the loss of connection
59  * does not raise a signal, but is detected when we fail to write to the
60  * socket. If there was a signal for a broken connection, we could make use of
61  * it by setting ClientConnectionLost in the signal handler.
62  *
63  * A related, but conceptually distinct, mechanism is the "critical section"
64  * mechanism.  A critical section not only holds off cancel/die interrupts,
65  * but causes any ereport(ERROR) or ereport(FATAL) to become ereport(PANIC)
66  * --- that is, a system-wide reset is forced.  Needless to say, only really
67  * *critical* code should be marked as a critical section!      Currently, this
68  * mechanism is only used for XLOG-related code.
69  *
70  *****************************************************************************/
71
72 /* in globals.c */
73 /* these are marked volatile because they are set by signal handlers: */
74 extern PGDLLIMPORT volatile bool InterruptPending;
75 extern volatile bool QueryCancelPending;
76 extern volatile bool ProcDiePending;
77
78 extern volatile bool ClientConnectionLost;
79
80 /* these are marked volatile because they are examined by signal handlers: */
81 extern volatile bool ImmediateInterruptOK;
82 extern PGDLLIMPORT volatile uint32 InterruptHoldoffCount;
83 extern PGDLLIMPORT volatile uint32 CritSectionCount;
84
85 /* in tcop/postgres.c */
86 extern void ProcessInterrupts(void);
87
88 #ifndef WIN32
89
90 #define CHECK_FOR_INTERRUPTS() \
91 do { \
92         if (InterruptPending) \
93                 ProcessInterrupts(); \
94 } while(0)
95 #else                                                   /* WIN32 */
96
97 #define CHECK_FOR_INTERRUPTS() \
98 do { \
99         if (UNBLOCKED_SIGNAL_QUEUE()) \
100                 pgwin32_dispatch_queued_signals(); \
101         if (InterruptPending) \
102                 ProcessInterrupts(); \
103 } while(0)
104 #endif   /* WIN32 */
105
106
107 #define HOLD_INTERRUPTS()  (InterruptHoldoffCount++)
108
109 #define RESUME_INTERRUPTS() \
110 do { \
111         Assert(InterruptHoldoffCount > 0); \
112         InterruptHoldoffCount--; \
113 } while(0)
114
115 #define START_CRIT_SECTION()  (CritSectionCount++)
116
117 #define END_CRIT_SECTION() \
118 do { \
119         Assert(CritSectionCount > 0); \
120         CritSectionCount--; \
121 } while(0)
122
123
124 /*****************************************************************************
125  *        globals.h --                                                                                                                   *
126  *****************************************************************************/
127
128 /*
129  * from utils/init/globals.c
130  */
131 extern pid_t PostmasterPid;
132 extern bool IsPostmasterEnvironment;
133 extern PGDLLIMPORT bool IsUnderPostmaster;
134 extern bool IsBinaryUpgrade;
135
136 extern bool ExitOnAnyError;
137
138 extern PGDLLIMPORT char *DataDir;
139
140 extern PGDLLIMPORT int NBuffers;
141 extern int      MaxBackends;
142 extern int      MaxConnections;
143
144 extern PGDLLIMPORT int MyProcPid;
145 extern PGDLLIMPORT pg_time_t MyStartTime;
146 extern PGDLLIMPORT struct Port *MyProcPort;
147 extern long MyCancelKey;
148 extern int      MyPMChildSlot;
149
150 extern char OutputFileName[];
151 extern PGDLLIMPORT char my_exec_path[];
152 extern char pkglib_path[];
153
154 #ifdef EXEC_BACKEND
155 extern char postgres_exec_path[];
156 #endif
157
158 /*
159  * done in storage/backendid.h for now.
160  *
161  * extern BackendId    MyBackendId;
162  */
163 extern PGDLLIMPORT Oid MyDatabaseId;
164
165 extern PGDLLIMPORT Oid MyDatabaseTableSpace;
166
167 /*
168  * Date/Time Configuration
169  *
170  * DateStyle defines the output formatting choice for date/time types:
171  *      USE_POSTGRES_DATES specifies traditional Postgres format
172  *      USE_ISO_DATES specifies ISO-compliant format
173  *      USE_SQL_DATES specifies Oracle/Ingres-compliant format
174  *      USE_GERMAN_DATES specifies German-style dd.mm/yyyy
175  *
176  * DateOrder defines the field order to be assumed when reading an
177  * ambiguous date (anything not in YYYY-MM-DD format, with a four-digit
178  * year field first, is taken to be ambiguous):
179  *      DATEORDER_YMD specifies field order yy-mm-dd
180  *      DATEORDER_DMY specifies field order dd-mm-yy ("European" convention)
181  *      DATEORDER_MDY specifies field order mm-dd-yy ("US" convention)
182  *
183  * In the Postgres and SQL DateStyles, DateOrder also selects output field
184  * order: day comes before month in DMY style, else month comes before day.
185  *
186  * The user-visible "DateStyle" run-time parameter subsumes both of these.
187  */
188
189 /* valid DateStyle values */
190 #define USE_POSTGRES_DATES              0
191 #define USE_ISO_DATES                   1
192 #define USE_SQL_DATES                   2
193 #define USE_GERMAN_DATES                3
194 #define USE_XSD_DATES                   4
195
196 /* valid DateOrder values */
197 #define DATEORDER_YMD                   0
198 #define DATEORDER_DMY                   1
199 #define DATEORDER_MDY                   2
200
201 extern int      DateStyle;
202 extern int      DateOrder;
203
204 /*
205  * IntervalStyles
206  *       INTSTYLE_POSTGRES                         Like Postgres < 8.4 when DateStyle = 'iso'
207  *       INTSTYLE_POSTGRES_VERBOSE         Like Postgres < 8.4 when DateStyle != 'iso'
208  *       INTSTYLE_SQL_STANDARD             SQL standard interval literals
209  *       INTSTYLE_ISO_8601                         ISO-8601-basic formatted intervals
210  */
211 #define INTSTYLE_POSTGRES                       0
212 #define INTSTYLE_POSTGRES_VERBOSE       1
213 #define INTSTYLE_SQL_STANDARD           2
214 #define INTSTYLE_ISO_8601                       3
215
216 extern int      IntervalStyle;
217
218 /*
219  * HasCTZSet is true if user has set timezone as a numeric offset from UTC.
220  * If so, CTimeZone is the timezone offset in seconds (using the Unix-ish
221  * sign convention, ie, positive offset is west of UTC, rather than the
222  * SQL-ish convention that positive is east of UTC).
223  */
224 extern bool HasCTZSet;
225 extern int      CTimeZone;
226
227 #define MAXTZLEN                10              /* max TZ name len, not counting tr. null */
228
229 extern bool enableFsync;
230 extern bool allowSystemTableMods;
231 extern PGDLLIMPORT int work_mem;
232 extern PGDLLIMPORT int maintenance_work_mem;
233
234 extern int      VacuumCostPageHit;
235 extern int      VacuumCostPageMiss;
236 extern int      VacuumCostPageDirty;
237 extern int      VacuumCostLimit;
238 extern int      VacuumCostDelay;
239
240 extern int      VacuumPageHit;
241 extern int      VacuumPageMiss;
242 extern int      VacuumPageDirty;
243
244 extern int      VacuumCostBalance;
245 extern bool VacuumCostActive;
246
247
248 /* in tcop/postgres.c */
249
250 #if defined(__ia64__) || defined(__ia64)
251 typedef struct
252 {
253         char       *stack_base_ptr;
254         char       *register_stack_base_ptr;
255 } pg_stack_base_t;
256 #else
257 typedef char *pg_stack_base_t;
258 #endif
259
260 extern pg_stack_base_t set_stack_base(void);
261 extern void restore_stack_base(pg_stack_base_t base);
262 extern void check_stack_depth(void);
263
264 /* in tcop/utility.c */
265 extern void PreventCommandIfReadOnly(const char *cmdname);
266 extern void PreventCommandDuringRecovery(const char *cmdname);
267
268 /* in utils/misc/guc.c */
269 extern int      trace_recovery_messages;
270 extern int      trace_recovery(int trace_level);
271
272 /*****************************************************************************
273  *        pdir.h --                                                                                                                              *
274  *                      POSTGRES directory path definitions.                                                     *
275  *****************************************************************************/
276
277 /* flags to be OR'd to form sec_context */
278 #define SECURITY_LOCAL_USERID_CHANGE    0x0001
279 #define SECURITY_RESTRICTED_OPERATION   0x0002
280
281 extern char *DatabasePath;
282
283 /* now in utils/init/miscinit.c */
284 extern void SetDatabasePath(const char *path);
285
286 extern char *GetUserNameFromId(Oid roleid);
287 extern Oid      GetUserId(void);
288 extern Oid      GetOuterUserId(void);
289 extern Oid      GetSessionUserId(void);
290 extern void GetUserIdAndSecContext(Oid *userid, int *sec_context);
291 extern void SetUserIdAndSecContext(Oid userid, int sec_context);
292 extern bool InLocalUserIdChange(void);
293 extern bool InSecurityRestrictedOperation(void);
294 extern void GetUserIdAndContext(Oid *userid, bool *sec_def_context);
295 extern void SetUserIdAndContext(Oid userid, bool sec_def_context);
296 extern void InitializeSessionUserId(const char *rolename);
297 extern void InitializeSessionUserIdStandalone(void);
298 extern void SetSessionAuthorization(Oid userid, bool is_superuser);
299 extern Oid      GetCurrentRoleId(void);
300 extern void SetCurrentRoleId(Oid roleid, bool is_superuser);
301
302 extern void SetDataDir(const char *dir);
303 extern void ChangeToDataDir(void);
304 extern char *make_absolute_path(const char *path);
305
306 /* in utils/misc/superuser.c */
307 extern bool superuser(void);    /* current user is superuser */
308 extern bool superuser_arg(Oid roleid);  /* given user is superuser */
309
310
311 /*****************************************************************************
312  *        pmod.h --                                                                                                                              *
313  *                      POSTGRES processing mode definitions.                                                    *
314  *****************************************************************************/
315
316 /*
317  * Description:
318  *              There are three processing modes in POSTGRES.  They are
319  * BootstrapProcessing or "bootstrap," InitProcessing or
320  * "initialization," and NormalProcessing or "normal."
321  *
322  * The first two processing modes are used during special times. When the
323  * system state indicates bootstrap processing, transactions are all given
324  * transaction id "one" and are consequently guaranteed to commit. This mode
325  * is used during the initial generation of template databases.
326  *
327  * Initialization mode: used while starting a backend, until all normal
328  * initialization is complete.  Some code behaves differently when executed
329  * in this mode to enable system bootstrapping.
330  *
331  * If a POSTGRES binary is in normal mode, then all code may be executed
332  * normally.
333  */
334
335 typedef enum ProcessingMode
336 {
337         BootstrapProcessing,            /* bootstrap creation of template database */
338         InitProcessing,                         /* initializing system */
339         NormalProcessing                        /* normal processing */
340 } ProcessingMode;
341
342 extern ProcessingMode Mode;
343
344 #define IsBootstrapProcessingMode() ((bool)(Mode == BootstrapProcessing))
345 #define IsInitProcessingMode() ((bool)(Mode == InitProcessing))
346 #define IsNormalProcessingMode() ((bool)(Mode == NormalProcessing))
347
348 #define SetProcessingMode(mode) \
349         do { \
350                 AssertArg((mode) == BootstrapProcessing || \
351                                   (mode) == InitProcessing || \
352                                   (mode) == NormalProcessing); \
353                 Mode = (mode); \
354         } while(0)
355
356 #define GetProcessingMode() Mode
357
358
359 /*****************************************************************************
360  *        pinit.h --                                                                                                                     *
361  *                      POSTGRES initialization and cleanup definitions.                                 *
362  *****************************************************************************/
363
364 /* in utils/init/postinit.c */
365 extern void pg_split_opts(char **argv, int *argcp, char *optstr);
366 extern void InitPostgres(const char *in_dbname, Oid dboid, const char *username,
367                          char *out_dbname);
368 extern void BaseInit(void);
369
370 /* in utils/init/miscinit.c */
371 extern bool IgnoreSystemIndexes;
372 extern PGDLLIMPORT bool process_shared_preload_libraries_in_progress;
373 extern char *shared_preload_libraries_string;
374 extern char *local_preload_libraries_string;
375
376 /*
377  * As of 9.1, the contents of the data-directory lock file are:
378  *
379  * line #
380  *              1       postmaster PID (or negative of a standalone backend's PID)
381  *              2       data directory path
382  *              3       postmaster start timestamp (time_t representation)
383  *              4       port number
384  *              5       socket directory path (empty on Windows)
385  *              6       first listen_address (IP address or "*"; empty if no TCP port)
386  *              7       shared memory key (not present on Windows)
387  *
388  * Lines 6 and up are added via AddToDataDirLockFile() after initial file
389  * creation; they have to be ordered according to time of addition.
390  *
391  * The socket lock file, if used, has the same contents as lines 1-5.
392  */
393 #define LOCK_FILE_LINE_PID                      1
394 #define LOCK_FILE_LINE_DATA_DIR         2
395 #define LOCK_FILE_LINE_START_TIME       3
396 #define LOCK_FILE_LINE_PORT                     4
397 #define LOCK_FILE_LINE_SOCKET_DIR       5
398 #define LOCK_FILE_LINE_LISTEN_ADDR      6
399 #define LOCK_FILE_LINE_SHMEM_KEY        7
400
401 extern void CreateDataDirLockFile(bool amPostmaster);
402 extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster);
403 extern void TouchSocketLockFile(void);
404 extern void AddToDataDirLockFile(int target_line, const char *str);
405 extern void ValidatePgVersion(const char *path);
406 extern void process_shared_preload_libraries(void);
407 extern void process_local_preload_libraries(void);
408 extern void pg_bindtextdomain(const char *domain);
409 extern bool is_authenticated_user_replication_role(void);
410
411 /* in access/transam/xlog.c */
412 extern bool BackupInProgress(void);
413 extern void CancelBackup(void);
414
415 #endif   /* MISCADMIN_H */