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-2018, 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 extern PGDLLIMPORT int data_directory_mode; 169 170 extern PGDLLIMPORT int NBuffers; 171 extern PGDLLIMPORT int MaxBackends; 172 extern PGDLLIMPORT int MaxConnections; 173 extern PGDLLIMPORT int max_worker_processes; 174 extern PGDLLIMPORT int max_parallel_workers; 175 176 extern PGDLLIMPORT int MyProcPid; 177 extern PGDLLIMPORT pg_time_t MyStartTime; 178 extern PGDLLIMPORT struct Port *MyProcPort; 179 extern PGDLLIMPORT struct Latch *MyLatch; 180 extern int32 MyCancelKey; 181 extern int MyPMChildSlot; 182 183 extern char OutputFileName[]; 184 extern PGDLLIMPORT char my_exec_path[]; 185 extern char pkglib_path[]; 186 187 #ifdef EXEC_BACKEND 188 extern char postgres_exec_path[]; 189 #endif 190 191 /* 192 * done in storage/backendid.h for now. 193 * 194 * extern BackendId MyBackendId; 195 */ 196 extern PGDLLIMPORT Oid MyDatabaseId; 197 198 extern PGDLLIMPORT Oid MyDatabaseTableSpace; 199 200 /* 201 * Date/Time Configuration 202 * 203 * DateStyle defines the output formatting choice for date/time types: 204 * USE_POSTGRES_DATES specifies traditional Postgres format 205 * USE_ISO_DATES specifies ISO-compliant format 206 * USE_SQL_DATES specifies Oracle/Ingres-compliant format 207 * USE_GERMAN_DATES specifies German-style dd.mm/yyyy 208 * 209 * DateOrder defines the field order to be assumed when reading an 210 * ambiguous date (anything not in YYYY-MM-DD format, with a four-digit 211 * year field first, is taken to be ambiguous): 212 * DATEORDER_YMD specifies field order yy-mm-dd 213 * DATEORDER_DMY specifies field order dd-mm-yy ("European" convention) 214 * DATEORDER_MDY specifies field order mm-dd-yy ("US" convention) 215 * 216 * In the Postgres and SQL DateStyles, DateOrder also selects output field 217 * order: day comes before month in DMY style, else month comes before day. 218 * 219 * The user-visible "DateStyle" run-time parameter subsumes both of these. 220 */ 221 222 /* valid DateStyle values */ 223 #define USE_POSTGRES_DATES 0 224 #define USE_ISO_DATES 1 225 #define USE_SQL_DATES 2 226 #define USE_GERMAN_DATES 3 227 #define USE_XSD_DATES 4 228 229 /* valid DateOrder values */ 230 #define DATEORDER_YMD 0 231 #define DATEORDER_DMY 1 232 #define DATEORDER_MDY 2 233 234 extern PGDLLIMPORT int DateStyle; 235 extern PGDLLIMPORT int DateOrder; 236 237 /* 238 * IntervalStyles 239 * INTSTYLE_POSTGRES Like Postgres < 8.4 when DateStyle = 'iso' 240 * INTSTYLE_POSTGRES_VERBOSE Like Postgres < 8.4 when DateStyle != 'iso' 241 * INTSTYLE_SQL_STANDARD SQL standard interval literals 242 * INTSTYLE_ISO_8601 ISO-8601-basic formatted intervals 243 */ 244 #define INTSTYLE_POSTGRES 0 245 #define INTSTYLE_POSTGRES_VERBOSE 1 246 #define INTSTYLE_SQL_STANDARD 2 247 #define INTSTYLE_ISO_8601 3 248 249 extern PGDLLIMPORT int IntervalStyle; 250 251 #define MAXTZLEN 10 /* max TZ name len, not counting tr. null */ 252 253 extern bool enableFsync; 254 extern PGDLLIMPORT bool allowSystemTableMods; 255 extern PGDLLIMPORT int work_mem; 256 extern PGDLLIMPORT int maintenance_work_mem; 257 extern PGDLLIMPORT int max_parallel_maintenance_workers; 258 259 extern int VacuumCostPageHit; 260 extern int VacuumCostPageMiss; 261 extern int VacuumCostPageDirty; 262 extern int VacuumCostLimit; 263 extern int VacuumCostDelay; 264 265 extern int VacuumPageHit; 266 extern int VacuumPageMiss; 267 extern int VacuumPageDirty; 268 269 extern int VacuumCostBalance; 270 extern bool VacuumCostActive; 271 272 extern double vacuum_cleanup_index_scale_factor; 273 274 275 /* in tcop/postgres.c */ 276 277 #if defined(__ia64__) || defined(__ia64) 278 typedef struct 279 { 280 char *stack_base_ptr; 281 char *register_stack_base_ptr; 282 } pg_stack_base_t; 283 #else 284 typedef char *pg_stack_base_t; 285 #endif 286 287 extern pg_stack_base_t set_stack_base(void); 288 extern void restore_stack_base(pg_stack_base_t base); 289 extern void check_stack_depth(void); 290 extern bool stack_is_too_deep(void); 291 292 extern void PostgresSigHupHandler(SIGNAL_ARGS); 293 294 /* in tcop/utility.c */ 295 extern void PreventCommandIfReadOnly(const char *cmdname); 296 extern void PreventCommandIfParallelMode(const char *cmdname); 297 extern void PreventCommandDuringRecovery(const char *cmdname); 298 299 /* in utils/misc/guc.c */ 300 extern int trace_recovery_messages; 301 extern int trace_recovery(int trace_level); 302 303 /***************************************************************************** 304 * pdir.h -- * 305 * POSTGRES directory path definitions. * 306 *****************************************************************************/ 307 308 /* flags to be OR'd to form sec_context */ 309 #define SECURITY_LOCAL_USERID_CHANGE 0x0001 310 #define SECURITY_RESTRICTED_OPERATION 0x0002 311 #define SECURITY_NOFORCE_RLS 0x0004 312 313 extern char *DatabasePath; 314 315 /* now in utils/init/miscinit.c */ 316 extern void InitPostmasterChild(void); 317 extern void InitStandaloneProcess(const char *argv0); 318 319 extern void SetDatabasePath(const char *path); 320 321 extern char *GetUserNameFromId(Oid roleid, bool noerr); 322 extern Oid GetUserId(void); 323 extern Oid GetOuterUserId(void); 324 extern Oid GetSessionUserId(void); 325 extern Oid GetAuthenticatedUserId(void); 326 extern void GetUserIdAndSecContext(Oid *userid, int *sec_context); 327 extern void SetUserIdAndSecContext(Oid userid, int sec_context); 328 extern bool InLocalUserIdChange(void); 329 extern bool InSecurityRestrictedOperation(void); 330 extern bool InNoForceRLSOperation(void); 331 extern void GetUserIdAndContext(Oid *userid, bool *sec_def_context); 332 extern void SetUserIdAndContext(Oid userid, bool sec_def_context); 333 extern void InitializeSessionUserId(const char *rolename, Oid useroid); 334 extern void InitializeSessionUserIdStandalone(void); 335 extern void SetSessionAuthorization(Oid userid, bool is_superuser); 336 extern Oid GetCurrentRoleId(void); 337 extern void SetCurrentRoleId(Oid roleid, bool is_superuser); 338 339 extern void checkDataDir(void); 340 extern void SetDataDir(const char *dir); 341 extern void ChangeToDataDir(void); 342 343 extern void SwitchToSharedLatch(void); 344 extern void SwitchBackToLocalLatch(void); 345 346 /* in utils/misc/superuser.c */ 347 extern bool superuser(void); /* current user is superuser */ 348 extern bool superuser_arg(Oid roleid); /* given user is superuser */ 349 350 351 /***************************************************************************** 352 * pmod.h -- * 353 * POSTGRES processing mode definitions. * 354 *****************************************************************************/ 355 356 /* 357 * Description: 358 * There are three processing modes in POSTGRES. They are 359 * BootstrapProcessing or "bootstrap," InitProcessing or 360 * "initialization," and NormalProcessing or "normal." 361 * 362 * The first two processing modes are used during special times. When the 363 * system state indicates bootstrap processing, transactions are all given 364 * transaction id "one" and are consequently guaranteed to commit. This mode 365 * is used during the initial generation of template databases. 366 * 367 * Initialization mode: used while starting a backend, until all normal 368 * initialization is complete. Some code behaves differently when executed 369 * in this mode to enable system bootstrapping. 370 * 371 * If a POSTGRES backend process is in normal mode, then all code may be 372 * executed normally. 373 */ 374 375 typedef enum ProcessingMode 376 { 377 BootstrapProcessing, /* bootstrap creation of template database */ 378 InitProcessing, /* initializing system */ 379 NormalProcessing /* normal processing */ 380 } ProcessingMode; 381 382 extern ProcessingMode Mode; 383 384 #define IsBootstrapProcessingMode() (Mode == BootstrapProcessing) 385 #define IsInitProcessingMode() (Mode == InitProcessing) 386 #define IsNormalProcessingMode() (Mode == NormalProcessing) 387 388 #define GetProcessingMode() Mode 389 390 #define SetProcessingMode(mode) \ 391 do { \ 392 AssertArg((mode) == BootstrapProcessing || \ 393 (mode) == InitProcessing || \ 394 (mode) == NormalProcessing); \ 395 Mode = (mode); \ 396 } while(0) 397 398 399 /* 400 * Auxiliary-process type identifiers. These used to be in bootstrap.h 401 * but it seems saner to have them here, with the ProcessingMode stuff. 402 * The MyAuxProcType global is defined and set in bootstrap.c. 403 */ 404 405 typedef enum 406 { 407 NotAnAuxProcess = -1, 408 CheckerProcess = 0, 409 BootstrapProcess, 410 StartupProcess, 411 BgWriterProcess, 412 CheckpointerProcess, 413 WalWriterProcess, 414 WalReceiverProcess, 415 416 NUM_AUXPROCTYPES /* Must be last! */ 417 } AuxProcType; 418 419 extern AuxProcType MyAuxProcType; 420 421 #define AmBootstrapProcess() (MyAuxProcType == BootstrapProcess) 422 #define AmStartupProcess() (MyAuxProcType == StartupProcess) 423 #define AmBackgroundWriterProcess() (MyAuxProcType == BgWriterProcess) 424 #define AmCheckpointerProcess() (MyAuxProcType == CheckpointerProcess) 425 #define AmWalWriterProcess() (MyAuxProcType == WalWriterProcess) 426 #define AmWalReceiverProcess() (MyAuxProcType == WalReceiverProcess) 427 428 429 /***************************************************************************** 430 * pinit.h -- * 431 * POSTGRES initialization and cleanup definitions. * 432 *****************************************************************************/ 433 434 /* in utils/init/postinit.c */ 435 extern void pg_split_opts(char **argv, int *argcp, const char *optstr); 436 extern void InitializeMaxBackends(void); 437 extern void InitPostgres(const char *in_dbname, Oid dboid, const char *username, 438 Oid useroid, char *out_dbname, bool override_allow_connections); 439 extern void BaseInit(void); 440 441 /* in utils/init/miscinit.c */ 442 extern bool IgnoreSystemIndexes; 443 extern PGDLLIMPORT bool process_shared_preload_libraries_in_progress; 444 extern char *session_preload_libraries_string; 445 extern char *shared_preload_libraries_string; 446 extern char *local_preload_libraries_string; 447 448 extern void CreateDataDirLockFile(bool amPostmaster); 449 extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster, 450 const char *socketDir); 451 extern void TouchSocketLockFiles(void); 452 extern void AddToDataDirLockFile(int target_line, const char *str); 453 extern bool RecheckDataDirLockFile(void); 454 extern void ValidatePgVersion(const char *path); 455 extern void process_shared_preload_libraries(void); 456 extern void process_session_preload_libraries(void); 457 extern void pg_bindtextdomain(const char *domain); 458 extern bool has_rolreplication(Oid roleid); 459 460 /* in access/transam/xlog.c */ 461 extern bool BackupInProgress(void); 462 extern void CancelBackup(void); 463 464 #endif /* MISCADMIN_H */ 465