Header And Logo

PostgreSQL
| The world's most advanced open source database.

miscadmin.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * miscadmin.h
00004  *    This file contains general postgres administration and initialization
00005  *    stuff that used to be spread out between the following files:
00006  *      globals.h                       global variables
00007  *      pdir.h                          directory path crud
00008  *      pinit.h                         postgres initialization
00009  *      pmod.h                          processing modes
00010  *    Over time, this has also become the preferred place for widely known
00011  *    resource-limitation stuff, such as work_mem and check_stack_depth().
00012  *
00013  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00014  * Portions Copyright (c) 1994, Regents of the University of California
00015  *
00016  * src/include/miscadmin.h
00017  *
00018  * NOTES
00019  *    some of the information in this file should be moved to other files.
00020  *
00021  *-------------------------------------------------------------------------
00022  */
00023 #ifndef MISCADMIN_H
00024 #define MISCADMIN_H
00025 
00026 #include "pgtime.h"             /* for pg_time_t */
00027 
00028 
00029 #define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
00030 
00031 
00032 /*****************************************************************************
00033  *    System interrupt and critical section handling
00034  *
00035  * There are two types of interrupts that a running backend needs to accept
00036  * without messing up its state: QueryCancel (SIGINT) and ProcDie (SIGTERM).
00037  * In both cases, we need to be able to clean up the current transaction
00038  * gracefully, so we can't respond to the interrupt instantaneously ---
00039  * there's no guarantee that internal data structures would be self-consistent
00040  * if the code is interrupted at an arbitrary instant.  Instead, the signal
00041  * handlers set flags that are checked periodically during execution.
00042  *
00043  * The CHECK_FOR_INTERRUPTS() macro is called at strategically located spots
00044  * where it is normally safe to accept a cancel or die interrupt.  In some
00045  * cases, we invoke CHECK_FOR_INTERRUPTS() inside low-level subroutines that
00046  * might sometimes be called in contexts that do *not* want to allow a cancel
00047  * or die interrupt.  The HOLD_INTERRUPTS() and RESUME_INTERRUPTS() macros
00048  * allow code to ensure that no cancel or die interrupt will be accepted,
00049  * even if CHECK_FOR_INTERRUPTS() gets called in a subroutine.  The interrupt
00050  * will be held off until CHECK_FOR_INTERRUPTS() is done outside any
00051  * HOLD_INTERRUPTS() ... RESUME_INTERRUPTS() section.
00052  *
00053  * Special mechanisms are used to let an interrupt be accepted when we are
00054  * waiting for a lock or when we are waiting for command input (but, of
00055  * course, only if the interrupt holdoff counter is zero).  See the
00056  * related code for details.
00057  *
00058  * A lost connection is handled similarly, although the loss of connection
00059  * does not raise a signal, but is detected when we fail to write to the
00060  * socket. If there was a signal for a broken connection, we could make use of
00061  * it by setting ClientConnectionLost in the signal handler.
00062  *
00063  * A related, but conceptually distinct, mechanism is the "critical section"
00064  * mechanism.  A critical section not only holds off cancel/die interrupts,
00065  * but causes any ereport(ERROR) or ereport(FATAL) to become ereport(PANIC)
00066  * --- that is, a system-wide reset is forced.  Needless to say, only really
00067  * *critical* code should be marked as a critical section!  Currently, this
00068  * mechanism is only used for XLOG-related code.
00069  *
00070  *****************************************************************************/
00071 
00072 /* in globals.c */
00073 /* these are marked volatile because they are set by signal handlers: */
00074 extern PGDLLIMPORT volatile bool InterruptPending;
00075 extern volatile bool QueryCancelPending;
00076 extern volatile bool ProcDiePending;
00077 
00078 extern volatile bool ClientConnectionLost;
00079 
00080 /* these are marked volatile because they are examined by signal handlers: */
00081 extern volatile bool ImmediateInterruptOK;
00082 extern PGDLLIMPORT volatile uint32 InterruptHoldoffCount;
00083 extern PGDLLIMPORT volatile uint32 CritSectionCount;
00084 
00085 /* in tcop/postgres.c */
00086 extern void ProcessInterrupts(void);
00087 
00088 #ifndef WIN32
00089 
00090 #define CHECK_FOR_INTERRUPTS() \
00091 do { \
00092     if (InterruptPending) \
00093         ProcessInterrupts(); \
00094 } while(0)
00095 #else                           /* WIN32 */
00096 
00097 #define CHECK_FOR_INTERRUPTS() \
00098 do { \
00099     if (UNBLOCKED_SIGNAL_QUEUE()) \
00100         pgwin32_dispatch_queued_signals(); \
00101     if (InterruptPending) \
00102         ProcessInterrupts(); \
00103 } while(0)
00104 #endif   /* WIN32 */
00105 
00106 
00107 #define HOLD_INTERRUPTS()  (InterruptHoldoffCount++)
00108 
00109 #define RESUME_INTERRUPTS() \
00110 do { \
00111     Assert(InterruptHoldoffCount > 0); \
00112     InterruptHoldoffCount--; \
00113 } while(0)
00114 
00115 #define START_CRIT_SECTION()  (CritSectionCount++)
00116 
00117 #define END_CRIT_SECTION() \
00118 do { \
00119     Assert(CritSectionCount > 0); \
00120     CritSectionCount--; \
00121 } while(0)
00122 
00123 
00124 /*****************************************************************************
00125  *    globals.h --                                                           *
00126  *****************************************************************************/
00127 
00128 /*
00129  * from utils/init/globals.c
00130  */
00131 extern pid_t PostmasterPid;
00132 extern bool IsPostmasterEnvironment;
00133 extern PGDLLIMPORT bool IsUnderPostmaster;
00134 extern bool IsBackgroundWorker;
00135 extern bool IsBinaryUpgrade;
00136 
00137 extern bool ExitOnAnyError;
00138 
00139 extern PGDLLIMPORT char *DataDir;
00140 
00141 extern PGDLLIMPORT int NBuffers;
00142 extern int  MaxBackends;
00143 extern int  MaxConnections;
00144 
00145 extern PGDLLIMPORT int MyProcPid;
00146 extern PGDLLIMPORT pg_time_t MyStartTime;
00147 extern PGDLLIMPORT struct Port *MyProcPort;
00148 extern long MyCancelKey;
00149 extern int  MyPMChildSlot;
00150 
00151 extern char OutputFileName[];
00152 extern PGDLLIMPORT char my_exec_path[];
00153 extern char pkglib_path[];
00154 
00155 #ifdef EXEC_BACKEND
00156 extern char postgres_exec_path[];
00157 #endif
00158 
00159 /*
00160  * done in storage/backendid.h for now.
00161  *
00162  * extern BackendId    MyBackendId;
00163  */
00164 extern PGDLLIMPORT Oid MyDatabaseId;
00165 
00166 extern PGDLLIMPORT Oid MyDatabaseTableSpace;
00167 
00168 /*
00169  * Date/Time Configuration
00170  *
00171  * DateStyle defines the output formatting choice for date/time types:
00172  *  USE_POSTGRES_DATES specifies traditional Postgres format
00173  *  USE_ISO_DATES specifies ISO-compliant format
00174  *  USE_SQL_DATES specifies Oracle/Ingres-compliant format
00175  *  USE_GERMAN_DATES specifies German-style dd.mm/yyyy
00176  *
00177  * DateOrder defines the field order to be assumed when reading an
00178  * ambiguous date (anything not in YYYY-MM-DD format, with a four-digit
00179  * year field first, is taken to be ambiguous):
00180  *  DATEORDER_YMD specifies field order yy-mm-dd
00181  *  DATEORDER_DMY specifies field order dd-mm-yy ("European" convention)
00182  *  DATEORDER_MDY specifies field order mm-dd-yy ("US" convention)
00183  *
00184  * In the Postgres and SQL DateStyles, DateOrder also selects output field
00185  * order: day comes before month in DMY style, else month comes before day.
00186  *
00187  * The user-visible "DateStyle" run-time parameter subsumes both of these.
00188  */
00189 
00190 /* valid DateStyle values */
00191 #define USE_POSTGRES_DATES      0
00192 #define USE_ISO_DATES           1
00193 #define USE_SQL_DATES           2
00194 #define USE_GERMAN_DATES        3
00195 #define USE_XSD_DATES           4
00196 
00197 /* valid DateOrder values */
00198 #define DATEORDER_YMD           0
00199 #define DATEORDER_DMY           1
00200 #define DATEORDER_MDY           2
00201 
00202 extern int  DateStyle;
00203 extern int  DateOrder;
00204 
00205 /*
00206  * IntervalStyles
00207  *   INTSTYLE_POSTGRES             Like Postgres < 8.4 when DateStyle = 'iso'
00208  *   INTSTYLE_POSTGRES_VERBOSE     Like Postgres < 8.4 when DateStyle != 'iso'
00209  *   INTSTYLE_SQL_STANDARD         SQL standard interval literals
00210  *   INTSTYLE_ISO_8601             ISO-8601-basic formatted intervals
00211  */
00212 #define INTSTYLE_POSTGRES           0
00213 #define INTSTYLE_POSTGRES_VERBOSE   1
00214 #define INTSTYLE_SQL_STANDARD       2
00215 #define INTSTYLE_ISO_8601           3
00216 
00217 extern int  IntervalStyle;
00218 
00219 /*
00220  * HasCTZSet is true if user has set timezone as a numeric offset from UTC.
00221  * If so, CTimeZone is the timezone offset in seconds (using the Unix-ish
00222  * sign convention, ie, positive offset is west of UTC, rather than the
00223  * SQL-ish convention that positive is east of UTC).
00224  */
00225 extern bool HasCTZSet;
00226 extern int  CTimeZone;
00227 
00228 #define MAXTZLEN        10      /* max TZ name len, not counting tr. null */
00229 
00230 extern bool enableFsync;
00231 extern bool allowSystemTableMods;
00232 extern PGDLLIMPORT int work_mem;
00233 extern PGDLLIMPORT int maintenance_work_mem;
00234 
00235 extern int  VacuumCostPageHit;
00236 extern int  VacuumCostPageMiss;
00237 extern int  VacuumCostPageDirty;
00238 extern int  VacuumCostLimit;
00239 extern int  VacuumCostDelay;
00240 
00241 extern int  VacuumPageHit;
00242 extern int  VacuumPageMiss;
00243 extern int  VacuumPageDirty;
00244 
00245 extern int  VacuumCostBalance;
00246 extern bool VacuumCostActive;
00247 
00248 
00249 /* in tcop/postgres.c */
00250 
00251 #if defined(__ia64__) || defined(__ia64)
00252 typedef struct
00253 {
00254     char       *stack_base_ptr;
00255     char       *register_stack_base_ptr;
00256 } pg_stack_base_t;
00257 #else
00258 typedef char *pg_stack_base_t;
00259 #endif
00260 
00261 extern pg_stack_base_t set_stack_base(void);
00262 extern void restore_stack_base(pg_stack_base_t base);
00263 extern void check_stack_depth(void);
00264 
00265 /* in tcop/utility.c */
00266 extern void PreventCommandIfReadOnly(const char *cmdname);
00267 extern void PreventCommandDuringRecovery(const char *cmdname);
00268 
00269 /* in utils/misc/guc.c */
00270 extern int  trace_recovery_messages;
00271 extern int  trace_recovery(int trace_level);
00272 
00273 /*****************************************************************************
00274  *    pdir.h --                                                              *
00275  *          POSTGRES directory path definitions.                             *
00276  *****************************************************************************/
00277 
00278 /* flags to be OR'd to form sec_context */
00279 #define SECURITY_LOCAL_USERID_CHANGE    0x0001
00280 #define SECURITY_RESTRICTED_OPERATION   0x0002
00281 
00282 extern char *DatabasePath;
00283 
00284 /* now in utils/init/miscinit.c */
00285 extern void SetDatabasePath(const char *path);
00286 
00287 extern char *GetUserNameFromId(Oid roleid);
00288 extern Oid  GetUserId(void);
00289 extern Oid  GetOuterUserId(void);
00290 extern Oid  GetSessionUserId(void);
00291 extern void GetUserIdAndSecContext(Oid *userid, int *sec_context);
00292 extern void SetUserIdAndSecContext(Oid userid, int sec_context);
00293 extern bool InLocalUserIdChange(void);
00294 extern bool InSecurityRestrictedOperation(void);
00295 extern void GetUserIdAndContext(Oid *userid, bool *sec_def_context);
00296 extern void SetUserIdAndContext(Oid userid, bool sec_def_context);
00297 extern void InitializeSessionUserId(const char *rolename);
00298 extern void InitializeSessionUserIdStandalone(void);
00299 extern void SetSessionAuthorization(Oid userid, bool is_superuser);
00300 extern Oid  GetCurrentRoleId(void);
00301 extern void SetCurrentRoleId(Oid roleid, bool is_superuser);
00302 
00303 extern void SetDataDir(const char *dir);
00304 extern void ChangeToDataDir(void);
00305 extern char *make_absolute_path(const char *path);
00306 
00307 /* in utils/misc/superuser.c */
00308 extern bool superuser(void);    /* current user is superuser */
00309 extern bool superuser_arg(Oid roleid);  /* given user is superuser */
00310 
00311 
00312 /*****************************************************************************
00313  *    pmod.h --                                                              *
00314  *          POSTGRES processing mode definitions.                            *
00315  *****************************************************************************/
00316 
00317 /*
00318  * Description:
00319  *      There are three processing modes in POSTGRES.  They are
00320  * BootstrapProcessing or "bootstrap," InitProcessing or
00321  * "initialization," and NormalProcessing or "normal."
00322  *
00323  * The first two processing modes are used during special times. When the
00324  * system state indicates bootstrap processing, transactions are all given
00325  * transaction id "one" and are consequently guaranteed to commit. This mode
00326  * is used during the initial generation of template databases.
00327  *
00328  * Initialization mode: used while starting a backend, until all normal
00329  * initialization is complete.  Some code behaves differently when executed
00330  * in this mode to enable system bootstrapping.
00331  *
00332  * If a POSTGRES backend process is in normal mode, then all code may be
00333  * executed normally.
00334  */
00335 
00336 typedef enum ProcessingMode
00337 {
00338     BootstrapProcessing,        /* bootstrap creation of template database */
00339     InitProcessing,             /* initializing system */
00340     NormalProcessing            /* normal processing */
00341 } ProcessingMode;
00342 
00343 extern ProcessingMode Mode;
00344 
00345 #define IsBootstrapProcessingMode() (Mode == BootstrapProcessing)
00346 #define IsInitProcessingMode()      (Mode == InitProcessing)
00347 #define IsNormalProcessingMode()    (Mode == NormalProcessing)
00348 
00349 #define GetProcessingMode() Mode
00350 
00351 #define SetProcessingMode(mode) \
00352     do { \
00353         AssertArg((mode) == BootstrapProcessing || \
00354                   (mode) == InitProcessing || \
00355                   (mode) == NormalProcessing); \
00356         Mode = (mode); \
00357     } while(0)
00358 
00359 
00360 /*
00361  * Auxiliary-process type identifiers.  These used to be in bootstrap.h
00362  * but it seems saner to have them here, with the ProcessingMode stuff.
00363  * The MyAuxProcType global is defined and set in bootstrap.c.
00364  */
00365 
00366 typedef enum
00367 {
00368     NotAnAuxProcess = -1,
00369     CheckerProcess = 0,
00370     BootstrapProcess,
00371     StartupProcess,
00372     BgWriterProcess,
00373     CheckpointerProcess,
00374     WalWriterProcess,
00375     WalReceiverProcess,
00376 
00377     NUM_AUXPROCTYPES            /* Must be last! */
00378 } AuxProcType;
00379 
00380 extern AuxProcType MyAuxProcType;
00381 
00382 #define AmBootstrapProcess()        (MyAuxProcType == BootstrapProcess)
00383 #define AmStartupProcess()          (MyAuxProcType == StartupProcess)
00384 #define AmBackgroundWriterProcess() (MyAuxProcType == BgWriterProcess)
00385 #define AmCheckpointerProcess()     (MyAuxProcType == CheckpointerProcess)
00386 #define AmWalWriterProcess()        (MyAuxProcType == WalWriterProcess)
00387 #define AmWalReceiverProcess()      (MyAuxProcType == WalReceiverProcess)
00388 
00389 
00390 /*****************************************************************************
00391  *    pinit.h --                                                             *
00392  *          POSTGRES initialization and cleanup definitions.                 *
00393  *****************************************************************************/
00394 
00395 /* in utils/init/postinit.c */
00396 extern void pg_split_opts(char **argv, int *argcp, char *optstr);
00397 extern void InitializeMaxBackends(void);
00398 extern void InitPostgres(const char *in_dbname, Oid dboid, const char *username,
00399              char *out_dbname);
00400 extern void BaseInit(void);
00401 
00402 /* in utils/init/miscinit.c */
00403 extern bool IgnoreSystemIndexes;
00404 extern PGDLLIMPORT bool process_shared_preload_libraries_in_progress;
00405 extern char *shared_preload_libraries_string;
00406 extern char *local_preload_libraries_string;
00407 
00408 /*
00409  * As of 9.1, the contents of the data-directory lock file are:
00410  *
00411  * line #
00412  *      1   postmaster PID (or negative of a standalone backend's PID)
00413  *      2   data directory path
00414  *      3   postmaster start timestamp (time_t representation)
00415  *      4   port number
00416  *      5   first Unix socket directory path (empty if none)
00417  *      6   first listen_address (IP address or "*"; empty if no TCP port)
00418  *      7   shared memory key (not present on Windows)
00419  *
00420  * Lines 6 and up are added via AddToDataDirLockFile() after initial file
00421  * creation.
00422  *
00423  * The socket lock file, if used, has the same contents as lines 1-5.
00424  */
00425 #define LOCK_FILE_LINE_PID          1
00426 #define LOCK_FILE_LINE_DATA_DIR     2
00427 #define LOCK_FILE_LINE_START_TIME   3
00428 #define LOCK_FILE_LINE_PORT         4
00429 #define LOCK_FILE_LINE_SOCKET_DIR   5
00430 #define LOCK_FILE_LINE_LISTEN_ADDR  6
00431 #define LOCK_FILE_LINE_SHMEM_KEY    7
00432 
00433 extern void CreateDataDirLockFile(bool amPostmaster);
00434 extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster,
00435                      const char *socketDir);
00436 extern void TouchSocketLockFiles(void);
00437 extern void AddToDataDirLockFile(int target_line, const char *str);
00438 extern void ValidatePgVersion(const char *path);
00439 extern void process_shared_preload_libraries(void);
00440 extern void process_local_preload_libraries(void);
00441 extern void pg_bindtextdomain(const char *domain);
00442 extern bool has_rolreplication(Oid roleid);
00443 
00444 /* in access/transam/xlog.c */
00445 extern bool BackupInProgress(void);
00446 extern void CancelBackup(void);
00447 
00448 #endif   /* MISCADMIN_H */