gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

455 行
15KB

  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-2019, 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. * 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 sig_atomic_t InterruptPending;
  75. extern PGDLLIMPORT volatile sig_atomic_t QueryCancelPending;
  76. extern PGDLLIMPORT volatile sig_atomic_t ProcDiePending;
  77. extern PGDLLIMPORT volatile sig_atomic_t IdleInTransactionSessionTimeoutPending;
  78. extern PGDLLIMPORT volatile sig_atomic_t ConfigReloadPending;
  79. extern PGDLLIMPORT volatile sig_atomic_t 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 (unlikely(InterruptPending)) \
  90. ProcessInterrupts(); \
  91. } while(0)
  92. #else /* WIN32 */
  93. #define CHECK_FOR_INTERRUPTS() \
  94. do { \
  95. if (unlikely(UNBLOCKED_SIGNAL_QUEUE())) \
  96. pgwin32_dispatch_queued_signals(); \
  97. if (unlikely(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 PGDLLIMPORT bool IsPostmasterEnvironment;
  127. extern PGDLLIMPORT bool IsUnderPostmaster;
  128. extern PGDLLIMPORT bool IsBackgroundWorker;
  129. extern PGDLLIMPORT bool IsBinaryUpgrade;
  130. extern PGDLLIMPORT bool ExitOnAnyError;
  131. extern PGDLLIMPORT char *DataDir;
  132. extern PGDLLIMPORT int data_directory_mode;
  133. extern PGDLLIMPORT int NBuffers;
  134. extern PGDLLIMPORT int MaxBackends;
  135. extern PGDLLIMPORT int MaxConnections;
  136. extern PGDLLIMPORT int max_worker_processes;
  137. extern PGDLLIMPORT int max_parallel_workers;
  138. extern PGDLLIMPORT int MyProcPid;
  139. extern PGDLLIMPORT pg_time_t MyStartTime;
  140. extern PGDLLIMPORT TimestampTz MyStartTimestamp;
  141. extern PGDLLIMPORT struct Port *MyProcPort;
  142. extern PGDLLIMPORT struct Latch *MyLatch;
  143. extern int32 MyCancelKey;
  144. extern int MyPMChildSlot;
  145. extern char OutputFileName[];
  146. extern PGDLLIMPORT char my_exec_path[];
  147. extern char pkglib_path[];
  148. #ifdef EXEC_BACKEND
  149. extern char postgres_exec_path[];
  150. #endif
  151. /*
  152. * done in storage/backendid.h for now.
  153. *
  154. * extern BackendId MyBackendId;
  155. */
  156. extern PGDLLIMPORT Oid MyDatabaseId;
  157. extern PGDLLIMPORT Oid MyDatabaseTableSpace;
  158. /*
  159. * Date/Time Configuration
  160. *
  161. * DateStyle defines the output formatting choice for date/time types:
  162. * USE_POSTGRES_DATES specifies traditional Postgres format
  163. * USE_ISO_DATES specifies ISO-compliant format
  164. * USE_SQL_DATES specifies Oracle/Ingres-compliant format
  165. * USE_GERMAN_DATES specifies German-style dd.mm/yyyy
  166. *
  167. * DateOrder defines the field order to be assumed when reading an
  168. * ambiguous date (anything not in YYYY-MM-DD format, with a four-digit
  169. * year field first, is taken to be ambiguous):
  170. * DATEORDER_YMD specifies field order yy-mm-dd
  171. * DATEORDER_DMY specifies field order dd-mm-yy ("European" convention)
  172. * DATEORDER_MDY specifies field order mm-dd-yy ("US" convention)
  173. *
  174. * In the Postgres and SQL DateStyles, DateOrder also selects output field
  175. * order: day comes before month in DMY style, else month comes before day.
  176. *
  177. * The user-visible "DateStyle" run-time parameter subsumes both of these.
  178. */
  179. /* valid DateStyle values */
  180. #define USE_POSTGRES_DATES 0
  181. #define USE_ISO_DATES 1
  182. #define USE_SQL_DATES 2
  183. #define USE_GERMAN_DATES 3
  184. #define USE_XSD_DATES 4
  185. /* valid DateOrder values */
  186. #define DATEORDER_YMD 0
  187. #define DATEORDER_DMY 1
  188. #define DATEORDER_MDY 2
  189. extern PGDLLIMPORT int DateStyle;
  190. extern PGDLLIMPORT int DateOrder;
  191. /*
  192. * IntervalStyles
  193. * INTSTYLE_POSTGRES Like Postgres < 8.4 when DateStyle = 'iso'
  194. * INTSTYLE_POSTGRES_VERBOSE Like Postgres < 8.4 when DateStyle != 'iso'
  195. * INTSTYLE_SQL_STANDARD SQL standard interval literals
  196. * INTSTYLE_ISO_8601 ISO-8601-basic formatted intervals
  197. */
  198. #define INTSTYLE_POSTGRES 0
  199. #define INTSTYLE_POSTGRES_VERBOSE 1
  200. #define INTSTYLE_SQL_STANDARD 2
  201. #define INTSTYLE_ISO_8601 3
  202. extern PGDLLIMPORT int IntervalStyle;
  203. #define MAXTZLEN 10 /* max TZ name len, not counting tr. null */
  204. extern bool enableFsync;
  205. extern PGDLLIMPORT bool allowSystemTableMods;
  206. extern PGDLLIMPORT int work_mem;
  207. extern PGDLLIMPORT int maintenance_work_mem;
  208. extern PGDLLIMPORT int max_parallel_maintenance_workers;
  209. extern int VacuumCostPageHit;
  210. extern int VacuumCostPageMiss;
  211. extern int VacuumCostPageDirty;
  212. extern int VacuumCostLimit;
  213. extern double VacuumCostDelay;
  214. extern int VacuumPageHit;
  215. extern int VacuumPageMiss;
  216. extern int VacuumPageDirty;
  217. extern int VacuumCostBalance;
  218. extern bool VacuumCostActive;
  219. extern double vacuum_cleanup_index_scale_factor;
  220. /* in tcop/postgres.c */
  221. #if defined(__ia64__) || defined(__ia64)
  222. typedef struct
  223. {
  224. char *stack_base_ptr;
  225. char *register_stack_base_ptr;
  226. } pg_stack_base_t;
  227. #else
  228. typedef char *pg_stack_base_t;
  229. #endif
  230. extern pg_stack_base_t set_stack_base(void);
  231. extern void restore_stack_base(pg_stack_base_t base);
  232. extern void check_stack_depth(void);
  233. extern bool stack_is_too_deep(void);
  234. extern void PostgresSigHupHandler(SIGNAL_ARGS);
  235. /* in tcop/utility.c */
  236. extern void PreventCommandIfReadOnly(const char *cmdname);
  237. extern void PreventCommandIfParallelMode(const char *cmdname);
  238. extern void PreventCommandDuringRecovery(const char *cmdname);
  239. /* in utils/misc/guc.c */
  240. extern int trace_recovery_messages;
  241. extern int trace_recovery(int trace_level);
  242. /*****************************************************************************
  243. * pdir.h -- *
  244. * POSTGRES directory path definitions. *
  245. *****************************************************************************/
  246. /* flags to be OR'd to form sec_context */
  247. #define SECURITY_LOCAL_USERID_CHANGE 0x0001
  248. #define SECURITY_RESTRICTED_OPERATION 0x0002
  249. #define SECURITY_NOFORCE_RLS 0x0004
  250. extern char *DatabasePath;
  251. /* now in utils/init/miscinit.c */
  252. extern void InitPostmasterChild(void);
  253. extern void InitStandaloneProcess(const char *argv0);
  254. extern void SetDatabasePath(const char *path);
  255. extern char *GetUserNameFromId(Oid roleid, bool noerr);
  256. extern Oid GetUserId(void);
  257. extern Oid GetOuterUserId(void);
  258. extern Oid GetSessionUserId(void);
  259. extern Oid GetAuthenticatedUserId(void);
  260. extern void GetUserIdAndSecContext(Oid *userid, int *sec_context);
  261. extern void SetUserIdAndSecContext(Oid userid, int sec_context);
  262. extern bool InLocalUserIdChange(void);
  263. extern bool InSecurityRestrictedOperation(void);
  264. extern bool InNoForceRLSOperation(void);
  265. extern void GetUserIdAndContext(Oid *userid, bool *sec_def_context);
  266. extern void SetUserIdAndContext(Oid userid, bool sec_def_context);
  267. extern void InitializeSessionUserId(const char *rolename, Oid useroid);
  268. extern void InitializeSessionUserIdStandalone(void);
  269. extern void SetSessionAuthorization(Oid userid, bool is_superuser);
  270. extern Oid GetCurrentRoleId(void);
  271. extern void SetCurrentRoleId(Oid roleid, bool is_superuser);
  272. extern void checkDataDir(void);
  273. extern void SetDataDir(const char *dir);
  274. extern void ChangeToDataDir(void);
  275. extern void SwitchToSharedLatch(void);
  276. extern void SwitchBackToLocalLatch(void);
  277. /* in utils/misc/superuser.c */
  278. extern bool superuser(void); /* current user is superuser */
  279. extern bool superuser_arg(Oid roleid); /* given user is superuser */
  280. /*****************************************************************************
  281. * pmod.h -- *
  282. * POSTGRES processing mode definitions. *
  283. *****************************************************************************/
  284. /*
  285. * Description:
  286. * There are three processing modes in POSTGRES. They are
  287. * BootstrapProcessing or "bootstrap," InitProcessing or
  288. * "initialization," and NormalProcessing or "normal."
  289. *
  290. * The first two processing modes are used during special times. When the
  291. * system state indicates bootstrap processing, transactions are all given
  292. * transaction id "one" and are consequently guaranteed to commit. This mode
  293. * is used during the initial generation of template databases.
  294. *
  295. * Initialization mode: used while starting a backend, until all normal
  296. * initialization is complete. Some code behaves differently when executed
  297. * in this mode to enable system bootstrapping.
  298. *
  299. * If a POSTGRES backend process is in normal mode, then all code may be
  300. * executed normally.
  301. */
  302. typedef enum ProcessingMode
  303. {
  304. BootstrapProcessing, /* bootstrap creation of template database */
  305. InitProcessing, /* initializing system */
  306. NormalProcessing /* normal processing */
  307. } ProcessingMode;
  308. extern ProcessingMode Mode;
  309. #define IsBootstrapProcessingMode() (Mode == BootstrapProcessing)
  310. #define IsInitProcessingMode() (Mode == InitProcessing)
  311. #define IsNormalProcessingMode() (Mode == NormalProcessing)
  312. #define GetProcessingMode() Mode
  313. #define SetProcessingMode(mode) \
  314. do { \
  315. AssertArg((mode) == BootstrapProcessing || \
  316. (mode) == InitProcessing || \
  317. (mode) == NormalProcessing); \
  318. Mode = (mode); \
  319. } while(0)
  320. /*
  321. * Auxiliary-process type identifiers. These used to be in bootstrap.h
  322. * but it seems saner to have them here, with the ProcessingMode stuff.
  323. * The MyAuxProcType global is defined and set in bootstrap.c.
  324. */
  325. typedef enum
  326. {
  327. NotAnAuxProcess = -1,
  328. CheckerProcess = 0,
  329. BootstrapProcess,
  330. StartupProcess,
  331. BgWriterProcess,
  332. CheckpointerProcess,
  333. WalWriterProcess,
  334. WalReceiverProcess,
  335. NUM_AUXPROCTYPES /* Must be last! */
  336. } AuxProcType;
  337. extern AuxProcType MyAuxProcType;
  338. #define AmBootstrapProcess() (MyAuxProcType == BootstrapProcess)
  339. #define AmStartupProcess() (MyAuxProcType == StartupProcess)
  340. #define AmBackgroundWriterProcess() (MyAuxProcType == BgWriterProcess)
  341. #define AmCheckpointerProcess() (MyAuxProcType == CheckpointerProcess)
  342. #define AmWalWriterProcess() (MyAuxProcType == WalWriterProcess)
  343. #define AmWalReceiverProcess() (MyAuxProcType == WalReceiverProcess)
  344. /*****************************************************************************
  345. * pinit.h -- *
  346. * POSTGRES initialization and cleanup definitions. *
  347. *****************************************************************************/
  348. /* in utils/init/postinit.c */
  349. extern void pg_split_opts(char **argv, int *argcp, const char *optstr);
  350. extern void InitializeMaxBackends(void);
  351. extern void InitPostgres(const char *in_dbname, Oid dboid, const char *username,
  352. Oid useroid, char *out_dbname, bool override_allow_connections);
  353. extern void BaseInit(void);
  354. /* in utils/init/miscinit.c */
  355. extern bool IgnoreSystemIndexes;
  356. extern PGDLLIMPORT bool process_shared_preload_libraries_in_progress;
  357. extern char *session_preload_libraries_string;
  358. extern char *shared_preload_libraries_string;
  359. extern char *local_preload_libraries_string;
  360. extern void CreateDataDirLockFile(bool amPostmaster);
  361. extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster,
  362. const char *socketDir);
  363. extern void TouchSocketLockFiles(void);
  364. extern void AddToDataDirLockFile(int target_line, const char *str);
  365. extern bool RecheckDataDirLockFile(void);
  366. extern void ValidatePgVersion(const char *path);
  367. extern void process_shared_preload_libraries(void);
  368. extern void process_session_preload_libraries(void);
  369. extern void pg_bindtextdomain(const char *domain);
  370. extern bool has_rolreplication(Oid roleid);
  371. /* in access/transam/xlog.c */
  372. extern bool BackupInProgress(void);
  373. extern void CancelBackup(void);
  374. #endif /* MISCADMIN_H */
上海开阖软件有限公司 沪ICP备12045867号-1