miscadmin.h 16 KB

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