1 /*-------------------------------------------------------------------------
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
12 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
15 * $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.153 2004/02/10 03:42:45 tgl Exp $
18 * some of the information in this file should be moved to
21 *-------------------------------------------------------------------------
27 /*****************************************************************************
28 * System interrupt and critical section handling
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.
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.
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.
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.
60 *****************************************************************************/
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;
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;
74 extern void ProcessInterrupts(void);
77 #define CHECK_FOR_INTERRUPTS() \
79 if (InterruptPending) \
80 ProcessInterrupts(); \
83 #define CHECK_FOR_INTERRUPTS() \
85 WaitForSingleObjectEx(GetCurrentThread(),0,TRUE); \
86 if (InterruptPending) \
87 ProcessInterrupts(); \
92 #define HOLD_INTERRUPTS() (InterruptHoldoffCount++)
94 #define RESUME_INTERRUPTS() \
96 Assert(InterruptHoldoffCount > 0); \
97 InterruptHoldoffCount--; \
100 #define START_CRIT_SECTION() (CritSectionCount++)
102 #define END_CRIT_SECTION() \
104 Assert(CritSectionCount > 0); \
105 CritSectionCount--; \
109 /*****************************************************************************
111 *****************************************************************************/
114 * from postmaster/postmaster.c
116 extern bool ClientAuthInProgress;
117 extern const bool ExecBackend;
119 extern int PostmasterMain(int argc, char *argv[]);
121 extern void SubPostmasterMain(int argc, char* argv[]);
123 extern void ClosePostmasterPorts(bool pgstat_too);
126 * from utils/init/globals.c
128 extern pid_t PostmasterPid;
129 extern bool IsPostmasterEnvironment;
130 extern bool IsUnderPostmaster;
132 extern bool ExitOnAnyError;
134 extern char *DataDir;
136 extern DLLIMPORT int MyProcPid;
137 extern struct Port *MyProcPort;
138 extern long MyCancelKey;
140 extern char OutputFileName[];
141 extern char pg_pathname[];
144 * done in storage/backendid.h for now.
146 * extern BackendId MyBackendId;
148 extern DLLIMPORT Oid MyDatabaseId;
151 * Date/Time Configuration
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
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)
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.
169 * The user-visible "DateStyle" run-time parameter subsumes both of these.
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
178 /* valid DateOrder values */
179 #define DATEORDER_YMD 0
180 #define DATEORDER_DMY 1
181 #define DATEORDER_MDY 2
183 extern int DateStyle;
184 extern int DateOrder;
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).
192 extern bool HasCTZSet;
193 extern int CTimeZone;
195 #define MAXTZLEN 10 /* max TZ name len, not counting tr. null */
197 extern bool enableFsync;
198 extern bool allowSystemTableMods;
199 extern DLLIMPORT int work_mem;
200 extern DLLIMPORT int maintenance_work_mem;
202 extern int VacuumCostPageHit;
203 extern int VacuumCostPageMiss;
204 extern int VacuumCostPageDirty;
205 extern int VacuumCostLimit;
206 extern int VacuumCostNaptime;
208 extern int VacuumCostBalance;
209 extern bool VacuumCostActive;
212 * A few postmaster startup options are exported here so the
213 * configuration file processor can access them.
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;
228 /*****************************************************************************
230 * POSTGRES directory path definitions. *
231 *****************************************************************************/
233 extern char *DatabasePath;
235 /* in utils/misc/database.c */
236 extern void GetRawDatabaseInfo(const char *name, Oid *db_id, char *path);
238 /* now in utils/init/miscinit.c */
239 extern void SetDatabasePath(const char *path);
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);
250 extern void SetDataDir(const char *dir);
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);
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 */
261 /*****************************************************************************
263 * POSTGRES processing mode definitions. *
264 *****************************************************************************/
268 * There are three processing modes in POSTGRES. They are
269 * BootstrapProcessing or "bootstrap," InitProcessing or
270 * "initialization," and NormalProcessing or "normal."
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.
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.
281 * If a POSTGRES binary is in normal mode, then all code may be executed
285 typedef enum ProcessingMode
287 BootstrapProcessing, /* bootstrap creation of template database */
288 InitProcessing, /* initializing system */
289 NormalProcessing /* normal processing */
292 extern ProcessingMode Mode;
294 #define IsBootstrapProcessingMode() ((bool)(Mode == BootstrapProcessing))
295 #define IsInitProcessingMode() ((bool)(Mode == InitProcessing))
296 #define IsNormalProcessingMode() ((bool)(Mode == NormalProcessing))
298 #define SetProcessingMode(mode) \
300 AssertArg((mode) == BootstrapProcessing || \
301 (mode) == InitProcessing || \
302 (mode) == NormalProcessing); \
306 #define GetProcessingMode() Mode
309 /*****************************************************************************
311 * POSTGRES initialization and cleanup definitions. *
312 *****************************************************************************/
314 /* in utils/init/postinit.c */
315 extern void InitPostgres(const char *dbname, const char *username);
316 extern void BaseInit(void);
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,
329 extern void ValidatePgVersion(const char *path);
330 extern void process_preload_libraries(char *preload_libraries_string);
332 #endif /* MISCADMIN_H */