]> granicus.if.org Git - postgresql/blob - src/include/miscadmin.h
Make standard maintenance operations (including VACUUM, ANALYZE, REINDEX,
[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-2008, PostgreSQL Global Development Group
14  * Portions Copyright (c) 1994, Regents of the University of California
15  *
16  * $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.199 2008/01/03 21:23:15 tgl Exp $
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 <time.h>                               /* for time_t */
27
28
29 #define PG_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 related, but conceptually distinct, mechanism is the "critical section"
59  * mechanism.  A critical section not only holds off cancel/die interrupts,
60  * but causes any ereport(ERROR) or ereport(FATAL) to become ereport(PANIC)
61  * --- that is, a system-wide reset is forced.  Needless to say, only really
62  * *critical* code should be marked as a critical section!      Currently, this
63  * mechanism is only used for XLOG-related code.
64  *
65  *****************************************************************************/
66
67 /* in globals.c */
68 /* these are marked volatile because they are set by signal handlers: */
69 extern PGDLLIMPORT volatile bool InterruptPending;
70 extern volatile bool QueryCancelPending;
71 extern volatile bool ProcDiePending;
72
73 /* these are marked volatile because they are examined by signal handlers: */
74 extern volatile bool ImmediateInterruptOK;
75 extern PGDLLIMPORT volatile uint32 InterruptHoldoffCount;
76 extern PGDLLIMPORT volatile uint32 CritSectionCount;
77
78 /* in tcop/postgres.c */
79 extern void ProcessInterrupts(void);
80
81 #ifndef WIN32
82
83 #define CHECK_FOR_INTERRUPTS() \
84 do { \
85         if (InterruptPending) \
86                 ProcessInterrupts(); \
87 } while(0)
88 #else                                                   /* WIN32 */
89
90 #define CHECK_FOR_INTERRUPTS() \
91 do { \
92         if (UNBLOCKED_SIGNAL_QUEUE()) \
93                 pgwin32_dispatch_queued_signals(); \
94         if (InterruptPending) \
95                 ProcessInterrupts(); \
96 } while(0)
97 #endif   /* WIN32 */
98
99
100 #define HOLD_INTERRUPTS()  (InterruptHoldoffCount++)
101
102 #define RESUME_INTERRUPTS() \
103 do { \
104         Assert(InterruptHoldoffCount > 0); \
105         InterruptHoldoffCount--; \
106 } while(0)
107
108 #define START_CRIT_SECTION()  (CritSectionCount++)
109
110 #define END_CRIT_SECTION() \
111 do { \
112         Assert(CritSectionCount > 0); \
113         CritSectionCount--; \
114 } while(0)
115
116
117 /*****************************************************************************
118  *        globals.h --                                                                                                                   *
119  *****************************************************************************/
120
121 /*
122  * from utils/init/globals.c
123  */
124 extern pid_t PostmasterPid;
125 extern bool IsPostmasterEnvironment;
126 extern bool IsUnderPostmaster;
127
128 extern bool ExitOnAnyError;
129
130 extern PGDLLIMPORT char *DataDir;
131
132 extern PGDLLIMPORT int NBuffers;
133 extern int      MaxBackends;
134 extern int      MaxConnections;
135
136 extern PGDLLIMPORT int MyProcPid;
137 extern PGDLLIMPORT time_t MyStartTime;
138 extern PGDLLIMPORT struct Port *MyProcPort;
139 extern long MyCancelKey;
140
141 extern char OutputFileName[];
142 extern PGDLLIMPORT char my_exec_path[];
143 extern char pkglib_path[];
144
145 #ifdef EXEC_BACKEND
146 extern char postgres_exec_path[];
147 #endif
148
149 /*
150  * done in storage/backendid.h for now.
151  *
152  * extern BackendId    MyBackendId;
153  */
154 extern PGDLLIMPORT Oid MyDatabaseId;
155
156 extern PGDLLIMPORT Oid MyDatabaseTableSpace;
157
158 /*
159  * Date/Time Configuration
160  *
161  * DateStyle defines the output formatting choice for date/time types:
162  *      USE_POSTGRES_DATES specifies traditional Postgres format
163  *      USE_ISO_DATES specifies ISO-compliant format
164  *      USE_SQL_DATES specifies Oracle/Ingres-compliant format
165  *      USE_GERMAN_DATES specifies German-style dd.mm/yyyy
166  *
167  * DateOrder defines the field order to be assumed when reading an
168  * ambiguous date (anything not in YYYY-MM-DD format, with a four-digit
169  * year field first, is taken to be ambiguous):
170  *      DATEORDER_YMD specifies field order yy-mm-dd
171  *      DATEORDER_DMY specifies field order dd-mm-yy ("European" convention)
172  *      DATEORDER_MDY specifies field order mm-dd-yy ("US" convention)
173  *
174  * In the Postgres and SQL DateStyles, DateOrder also selects output field
175  * order: day comes before month in DMY style, else month comes before day.
176  *
177  * The user-visible "DateStyle" run-time parameter subsumes both of these.
178  */
179
180 /* valid DateStyle values */
181 #define USE_POSTGRES_DATES              0
182 #define USE_ISO_DATES                   1
183 #define USE_SQL_DATES                   2
184 #define USE_GERMAN_DATES                3
185 #define USE_XSD_DATES                   4
186
187 /* valid DateOrder values */
188 #define DATEORDER_YMD                   0
189 #define DATEORDER_DMY                   1
190 #define DATEORDER_MDY                   2
191
192 extern int      DateStyle;
193 extern int      DateOrder;
194
195 /*
196  * HasCTZSet is true if user has set timezone as a numeric offset from UTC.
197  * If so, CTimeZone is the timezone offset in seconds (using the Unix-ish
198  * sign convention, ie, positive offset is west of UTC, rather than the
199  * SQL-ish convention that positive is east of UTC).
200  */
201 extern bool HasCTZSet;
202 extern int      CTimeZone;
203
204 #define MAXTZLEN                10              /* max TZ name len, not counting tr. null */
205
206 extern bool enableFsync;
207 extern bool allowSystemTableMods;
208 extern PGDLLIMPORT int work_mem;
209 extern PGDLLIMPORT int maintenance_work_mem;
210
211 extern int      VacuumCostPageHit;
212 extern int      VacuumCostPageMiss;
213 extern int      VacuumCostPageDirty;
214 extern int      VacuumCostLimit;
215 extern int      VacuumCostDelay;
216
217 extern int      VacuumCostBalance;
218 extern bool VacuumCostActive;
219
220
221 /* in tcop/postgres.c */
222 extern void check_stack_depth(void);
223
224
225 /*****************************************************************************
226  *        pdir.h --                                                                                                                              *
227  *                      POSTGRES directory path definitions.                                                     *
228  *****************************************************************************/
229
230 extern char *DatabasePath;
231
232 /* now in utils/init/miscinit.c */
233 extern void SetDatabasePath(const char *path);
234
235 extern char *GetUserNameFromId(Oid roleid);
236 extern Oid      GetUserId(void);
237 extern Oid      GetOuterUserId(void);
238 extern Oid      GetSessionUserId(void);
239 extern void GetUserIdAndContext(Oid *userid, bool *sec_def_context);
240 extern void SetUserIdAndContext(Oid userid, bool sec_def_context);
241 extern bool InSecurityDefinerContext(void);
242 extern void InitializeSessionUserId(const char *rolename);
243 extern void InitializeSessionUserIdStandalone(void);
244 extern void SetSessionAuthorization(Oid userid, bool is_superuser);
245 extern Oid      GetCurrentRoleId(void);
246 extern void SetCurrentRoleId(Oid roleid, bool is_superuser);
247
248 extern void SetDataDir(const char *dir);
249 extern void ChangeToDataDir(void);
250 extern char *make_absolute_path(const char *path);
251
252 /* in utils/misc/superuser.c */
253 extern bool superuser(void);    /* current user is superuser */
254 extern bool superuser_arg(Oid roleid);  /* given user is superuser */
255
256
257 /*****************************************************************************
258  *        pmod.h --                                                                                                                              *
259  *                      POSTGRES processing mode definitions.                                                    *
260  *****************************************************************************/
261
262 /*
263  * Description:
264  *              There are three processing modes in POSTGRES.  They are
265  * BootstrapProcessing or "bootstrap," InitProcessing or
266  * "initialization," and NormalProcessing or "normal."
267  *
268  * The first two processing modes are used during special times. When the
269  * system state indicates bootstrap processing, transactions are all given
270  * transaction id "one" and are consequently guaranteed to commit. This mode
271  * is used during the initial generation of template databases.
272  *
273  * Initialization mode: used while starting a backend, until all normal
274  * initialization is complete.  Some code behaves differently when executed
275  * in this mode to enable system bootstrapping.
276  *
277  * If a POSTGRES binary is in normal mode, then all code may be executed
278  * normally.
279  */
280
281 typedef enum ProcessingMode
282 {
283         BootstrapProcessing,            /* bootstrap creation of template database */
284         InitProcessing,                         /* initializing system */
285         NormalProcessing                        /* normal processing */
286 } ProcessingMode;
287
288 extern ProcessingMode Mode;
289
290 #define IsBootstrapProcessingMode() ((bool)(Mode == BootstrapProcessing))
291 #define IsInitProcessingMode() ((bool)(Mode == InitProcessing))
292 #define IsNormalProcessingMode() ((bool)(Mode == NormalProcessing))
293
294 #define SetProcessingMode(mode) \
295         do { \
296                 AssertArg((mode) == BootstrapProcessing || \
297                                   (mode) == InitProcessing || \
298                                   (mode) == NormalProcessing); \
299                 Mode = (mode); \
300         } while(0)
301
302 #define GetProcessingMode() Mode
303
304
305 /*****************************************************************************
306  *        pinit.h --                                                                                                                     *
307  *                      POSTGRES initialization and cleanup definitions.                                 *
308  *****************************************************************************/
309
310 /* in utils/init/postinit.c */
311 extern bool InitPostgres(const char *in_dbname, Oid dboid, const char *username,
312                          char **out_dbname);
313 extern void BaseInit(void);
314
315 /* in utils/init/miscinit.c */
316 extern bool IgnoreSystemIndexes;
317 extern char *shared_preload_libraries_string;
318 extern char *local_preload_libraries_string;
319
320 extern void SetReindexProcessing(Oid heapOid, Oid indexOid);
321 extern void ResetReindexProcessing(void);
322 extern bool ReindexIsProcessingHeap(Oid heapOid);
323 extern bool ReindexIsProcessingIndex(Oid indexOid);
324 extern void CreateDataDirLockFile(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_shared_preload_libraries(void);
331 extern void process_local_preload_libraries(void);
332
333 #endif   /* MISCADMIN_H */