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