miscadmin.h 17 KB

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