]> granicus.if.org Git - postgresql/blob - src/include/miscadmin.h
Centralize implementation of delay code by creating a pg_usleep()
[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  *
11  *
12  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.153 2004/02/10 03:42:45 tgl Exp $
16  *
17  * NOTES
18  *        some of the information in this file should be moved to
19  *        other files.
20  *
21  *-------------------------------------------------------------------------
22  */
23 #ifndef MISCADMIN_H
24 #define MISCADMIN_H
25
26
27 /*****************************************************************************
28  *        System interrupt and critical section handling
29  *
30  * There are two types of interrupts that a running backend needs to accept
31  * without messing up its state: QueryCancel (SIGINT) and ProcDie (SIGTERM).
32  * In both cases, we need to be able to clean up the current transaction
33  * gracefully, so we can't respond to the interrupt instantaneously ---
34  * there's no guarantee that internal data structures would be self-consistent
35  * if the code is interrupted at an arbitrary instant.  Instead, the signal
36  * handlers set flags that are checked periodically during execution.
37  *
38  * The CHECK_FOR_INTERRUPTS() macro is called at strategically located spots
39  * where it is normally safe to accept a cancel or die interrupt.  In some
40  * cases, we invoke CHECK_FOR_INTERRUPTS() inside low-level subroutines that
41  * might sometimes be called in contexts that do *not* want to allow a cancel
42  * or die interrupt.  The HOLD_INTERRUPTS() and RESUME_INTERRUPTS() macros
43  * allow code to ensure that no cancel or die interrupt will be accepted,
44  * even if CHECK_FOR_INTERRUPTS() gets called in a subroutine.  The interrupt
45  * will be held off until CHECK_FOR_INTERRUPTS() is done outside any
46  * HOLD_INTERRUPTS() ... RESUME_INTERRUPTS() section.
47  *
48  * Special mechanisms are used to let an interrupt be accepted when we are
49  * waiting for a lock or when we are waiting for command input (but, of
50  * course, only if the interrupt holdoff counter is zero).      See the
51  * related code for details.
52  *
53  * A related, but conceptually distinct, mechanism is the "critical section"
54  * mechanism.  A critical section not only holds off cancel/die interrupts,
55  * but causes any ereport(ERROR) or ereport(FATAL) to become ereport(PANIC)
56  * --- that is, a system-wide reset is forced.  Needless to say, only really
57  * *critical* code should be marked as a critical section!      Currently, this
58  * mechanism is only used for XLOG-related code.
59  *
60  *****************************************************************************/
61
62 /* in globals.c */
63 /* these are marked volatile because they are set by signal handlers: */
64 extern DLLIMPORT volatile bool InterruptPending;
65 extern volatile bool QueryCancelPending;
66 extern volatile bool ProcDiePending;
67
68 /* these are marked volatile because they are examined by signal handlers: */
69 extern volatile bool ImmediateInterruptOK;
70 extern volatile uint32 InterruptHoldoffCount;
71 extern volatile uint32 CritSectionCount;
72
73 /* in postgres.c */
74 extern void ProcessInterrupts(void);
75
76 #ifndef WIN32
77 #define CHECK_FOR_INTERRUPTS() \
78 do { \
79         if (InterruptPending) \
80                 ProcessInterrupts(); \
81 } while(0)
82 #else
83 #define CHECK_FOR_INTERRUPTS() \
84 do { \
85         WaitForSingleObjectEx(GetCurrentThread(),0,TRUE); \
86         if (InterruptPending) \
87                 ProcessInterrupts(); \
88 } while(0)
89 #endif
90
91
92 #define HOLD_INTERRUPTS()  (InterruptHoldoffCount++)
93
94 #define RESUME_INTERRUPTS() \
95 do { \
96         Assert(InterruptHoldoffCount > 0); \
97         InterruptHoldoffCount--; \
98 } while(0)
99
100 #define START_CRIT_SECTION()  (CritSectionCount++)
101
102 #define END_CRIT_SECTION() \
103 do { \
104         Assert(CritSectionCount > 0); \
105         CritSectionCount--; \
106 } while(0)
107
108
109 /*****************************************************************************
110  *        globals.h --                                                                                                                   *
111  *****************************************************************************/
112
113 /*
114  * from postmaster/postmaster.c
115  */
116 extern bool ClientAuthInProgress;
117 extern const bool ExecBackend;
118
119 extern int      PostmasterMain(int argc, char *argv[]);
120 #ifdef EXEC_BACKEND
121 extern void SubPostmasterMain(int argc, char* argv[]);
122 #endif
123 extern void ClosePostmasterPorts(bool pgstat_too);
124
125 /*
126  * from utils/init/globals.c
127  */
128 extern pid_t PostmasterPid;
129 extern bool IsPostmasterEnvironment;
130 extern bool IsUnderPostmaster;
131
132 extern bool ExitOnAnyError;
133
134 extern char *DataDir;
135
136 extern DLLIMPORT int MyProcPid;
137 extern struct Port *MyProcPort;
138 extern long MyCancelKey;
139
140 extern char OutputFileName[];
141 extern char pg_pathname[];
142
143 /*
144  * done in storage/backendid.h for now.
145  *
146  * extern BackendId    MyBackendId;
147  */
148 extern DLLIMPORT Oid MyDatabaseId;
149
150 /*
151  * Date/Time Configuration
152  *
153  * DateStyle defines the output formatting choice for date/time types:
154  *      USE_POSTGRES_DATES specifies traditional Postgres format
155  *      USE_ISO_DATES specifies ISO-compliant format
156  *      USE_SQL_DATES specifies Oracle/Ingres-compliant format
157  *      USE_GERMAN_DATES specifies German-style dd.mm/yyyy
158  *
159  * DateOrder defines the field order to be assumed when reading an
160  * ambiguous date (anything not in YYYY-MM-DD format, with a four-digit
161  * year field first, is taken to be ambiguous):
162  *      DATEORDER_YMD specifies field order yy-mm-dd
163  *      DATEORDER_DMY specifies field order dd-mm-yy ("European" convention)
164  *      DATEORDER_MDY specifies field order mm-dd-yy ("US" convention)
165  *
166  * In the Postgres and SQL DateStyles, DateOrder also selects output field
167  * order: day comes before month in DMY style, else month comes before day.
168  *
169  * The user-visible "DateStyle" run-time parameter subsumes both of these.
170  */
171
172 /* valid DateStyle values */
173 #define USE_POSTGRES_DATES              0
174 #define USE_ISO_DATES                   1
175 #define USE_SQL_DATES                   2
176 #define USE_GERMAN_DATES                3
177
178 /* valid DateOrder values */
179 #define DATEORDER_YMD                   0
180 #define DATEORDER_DMY                   1
181 #define DATEORDER_MDY                   2
182
183 extern int      DateStyle;
184 extern int      DateOrder;
185
186 /*
187  * HasCTZSet is true if user has set timezone as a numeric offset from UTC.
188  * If so, CTimeZone is the timezone offset in seconds (using the Unix-ish
189  * sign convention, ie, positive offset is west of UTC, rather than the
190  * SQL-ish convention that positive is east of UTC).
191  */
192 extern bool HasCTZSet;
193 extern int      CTimeZone;
194
195 #define MAXTZLEN                10              /* max TZ name len, not counting tr. null */
196
197 extern bool enableFsync;
198 extern bool allowSystemTableMods;
199 extern DLLIMPORT int work_mem;
200 extern DLLIMPORT int maintenance_work_mem;
201
202 extern int      VacuumCostPageHit;
203 extern int      VacuumCostPageMiss;
204 extern int      VacuumCostPageDirty;
205 extern int      VacuumCostLimit;
206 extern int      VacuumCostNaptime;
207
208 extern int      VacuumCostBalance;
209 extern bool     VacuumCostActive;
210
211 /*
212  *      A few postmaster startup options are exported here so the
213  *      configuration file processor can access them.
214  */
215 extern bool NetServer;
216 extern bool EnableSSL;
217 extern bool SilentMode;
218 extern int      MaxBackends;
219 extern int      ReservedBackends;
220 extern DLLIMPORT int NBuffers;
221 extern int      PostPortNumber;
222 extern int      Unix_socket_permissions;
223 extern char *Unix_socket_group;
224 extern char *UnixSocketDir;
225 extern char *VirtualHost;
226
227
228 /*****************************************************************************
229  *        pdir.h --                                                                                                                              *
230  *                      POSTGRES directory path definitions.                                                     *
231  *****************************************************************************/
232
233 extern char *DatabasePath;
234
235 /* in utils/misc/database.c */
236 extern void GetRawDatabaseInfo(const char *name, Oid *db_id, char *path);
237
238 /* now in utils/init/miscinit.c */
239 extern void SetDatabasePath(const char *path);
240
241 extern char *GetUserNameFromId(AclId userid);
242 extern AclId GetUserId(void);
243 extern void SetUserId(AclId userid);
244 extern AclId GetSessionUserId(void);
245 extern void SetSessionUserId(AclId userid);
246 extern void InitializeSessionUserId(const char *username);
247 extern void InitializeSessionUserIdStandalone(void);
248 extern void SetSessionAuthorization(AclId userid, bool is_superuser);
249
250 extern void SetDataDir(const char *dir);
251
252 extern int FindExec(char *full_path, const char *argv0,
253                  const char *binary_name);
254 extern int      CheckPathAccess(char *path, char *name, int open_mode);
255
256 /* in utils/misc/superuser.c */
257 extern bool superuser(void);    /* current user is superuser */
258 extern bool superuser_arg(AclId userid);                /* given user is superuser */
259
260
261 /*****************************************************************************
262  *        pmod.h --                                                                                                                              *
263  *                      POSTGRES processing mode definitions.                                                    *
264  *****************************************************************************/
265
266 /*
267  * Description:
268  *              There are three processing modes in POSTGRES.  They are
269  * BootstrapProcessing or "bootstrap," InitProcessing or
270  * "initialization," and NormalProcessing or "normal."
271  *
272  * The first two processing modes are used during special times. When the
273  * system state indicates bootstrap processing, transactions are all given
274  * transaction id "one" and are consequently guaranteed to commit. This mode
275  * is used during the initial generation of template databases.
276  *
277  * Initialization mode: used while starting a backend, until all normal
278  * initialization is complete.  Some code behaves differently when executed
279  * in this mode to enable system bootstrapping.
280  *
281  * If a POSTGRES binary is in normal mode, then all code may be executed
282  * normally.
283  */
284
285 typedef enum ProcessingMode
286 {
287         BootstrapProcessing,            /* bootstrap creation of template database */
288         InitProcessing,                         /* initializing system */
289         NormalProcessing                        /* normal processing */
290 } ProcessingMode;
291
292 extern ProcessingMode Mode;
293
294 #define IsBootstrapProcessingMode() ((bool)(Mode == BootstrapProcessing))
295 #define IsInitProcessingMode() ((bool)(Mode == InitProcessing))
296 #define IsNormalProcessingMode() ((bool)(Mode == NormalProcessing))
297
298 #define SetProcessingMode(mode) \
299         do { \
300                 AssertArg((mode) == BootstrapProcessing || \
301                                   (mode) == InitProcessing || \
302                                   (mode) == NormalProcessing); \
303                 Mode = (mode); \
304         } while(0)
305
306 #define GetProcessingMode() Mode
307
308
309 /*****************************************************************************
310  *        pinit.h --                                                                                                                     *
311  *                      POSTGRES initialization and cleanup definitions.                                 *
312  *****************************************************************************/
313
314 /* in utils/init/postinit.c */
315 extern void InitPostgres(const char *dbname, const char *username);
316 extern void BaseInit(void);
317
318 /* in utils/init/miscinit.c */
319 extern void IgnoreSystemIndexes(bool mode);
320 extern bool IsIgnoringSystemIndexes(void);
321 extern void SetReindexProcessing(Oid heapOid, Oid indexOid);
322 extern bool ReindexIsProcessingHeap(Oid heapOid);
323 extern bool ReindexIsProcessingIndex(Oid indexOid);
324 extern void CreateDataDirLockFile(const char *datadir, bool amPostmaster);
325 extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster);
326 extern void TouchSocketLockFile(void);
327 extern void RecordSharedMemoryInLockFile(unsigned long id1,
328                                                          unsigned long id2);
329 extern void ValidatePgVersion(const char *path);
330 extern void process_preload_libraries(char *preload_libraries_string);
331
332 #endif   /* MISCADMIN_H */