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