Don't hard-code the input file name in gen_tabcomplete.pl's output.
[pgsql.git] / src / include / miscadmin.h
blobe26d108a470b4bbe6574ab630730a82f9963fa8b
1 /*-------------------------------------------------------------------------
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().
13 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
14 * Portions Copyright (c) 1994, Regents of the University of California
16 * src/include/miscadmin.h
18 * NOTES
19 * some of the information in this file should be moved to other files.
21 *-------------------------------------------------------------------------
23 #ifndef MISCADMIN_H
24 #define MISCADMIN_H
26 #include <signal.h>
28 #include "datatype/timestamp.h" /* for TimestampTz */
29 #include "pgtime.h" /* for pg_time_t */
32 #define InvalidPid (-1)
35 /*****************************************************************************
36 * System interrupt and critical section handling
38 * There are two types of interrupts that a running backend needs to accept
39 * without messing up its state: QueryCancel (SIGINT) and ProcDie (SIGTERM).
40 * In both cases, we need to be able to clean up the current transaction
41 * gracefully, so we can't respond to the interrupt instantaneously ---
42 * there's no guarantee that internal data structures would be self-consistent
43 * if the code is interrupted at an arbitrary instant. Instead, the signal
44 * handlers set flags that are checked periodically during execution.
46 * The CHECK_FOR_INTERRUPTS() macro is called at strategically located spots
47 * where it is normally safe to accept a cancel or die interrupt. In some
48 * cases, we invoke CHECK_FOR_INTERRUPTS() inside low-level subroutines that
49 * might sometimes be called in contexts that do *not* want to allow a cancel
50 * or die interrupt. The HOLD_INTERRUPTS() and RESUME_INTERRUPTS() macros
51 * allow code to ensure that no cancel or die interrupt will be accepted,
52 * even if CHECK_FOR_INTERRUPTS() gets called in a subroutine. The interrupt
53 * will be held off until CHECK_FOR_INTERRUPTS() is done outside any
54 * HOLD_INTERRUPTS() ... RESUME_INTERRUPTS() section.
56 * There is also a mechanism to prevent query cancel interrupts, while still
57 * allowing die interrupts: HOLD_CANCEL_INTERRUPTS() and
58 * RESUME_CANCEL_INTERRUPTS().
60 * Note that ProcessInterrupts() has also acquired a number of tasks that
61 * do not necessarily cause a query-cancel-or-die response. Hence, it's
62 * possible that it will just clear InterruptPending and return.
64 * INTERRUPTS_PENDING_CONDITION() can be checked to see whether an
65 * interrupt needs to be serviced, without trying to do so immediately.
66 * Some callers are also interested in INTERRUPTS_CAN_BE_PROCESSED(),
67 * which tells whether ProcessInterrupts is sure to clear the interrupt.
69 * Special mechanisms are used to let an interrupt be accepted when we are
70 * waiting for a lock or when we are waiting for command input (but, of
71 * course, only if the interrupt holdoff counter is zero). See the
72 * related code for details.
74 * A lost connection is handled similarly, although the loss of connection
75 * does not raise a signal, but is detected when we fail to write to the
76 * socket. If there was a signal for a broken connection, we could make use of
77 * it by setting ClientConnectionLost in the signal handler.
79 * A related, but conceptually distinct, mechanism is the "critical section"
80 * mechanism. A critical section not only holds off cancel/die interrupts,
81 * but causes any ereport(ERROR) or ereport(FATAL) to become ereport(PANIC)
82 * --- that is, a system-wide reset is forced. Needless to say, only really
83 * *critical* code should be marked as a critical section! Currently, this
84 * mechanism is only used for XLOG-related code.
86 *****************************************************************************/
88 /* in globals.c */
89 /* these are marked volatile because they are set by signal handlers: */
90 extern PGDLLIMPORT volatile sig_atomic_t InterruptPending;
91 extern PGDLLIMPORT volatile sig_atomic_t QueryCancelPending;
92 extern PGDLLIMPORT volatile sig_atomic_t ProcDiePending;
93 extern PGDLLIMPORT volatile sig_atomic_t IdleInTransactionSessionTimeoutPending;
94 extern PGDLLIMPORT volatile sig_atomic_t TransactionTimeoutPending;
95 extern PGDLLIMPORT volatile sig_atomic_t IdleSessionTimeoutPending;
96 extern PGDLLIMPORT volatile sig_atomic_t ProcSignalBarrierPending;
97 extern PGDLLIMPORT volatile sig_atomic_t LogMemoryContextPending;
98 extern PGDLLIMPORT volatile sig_atomic_t IdleStatsUpdateTimeoutPending;
100 extern PGDLLIMPORT volatile sig_atomic_t CheckClientConnectionPending;
101 extern PGDLLIMPORT volatile sig_atomic_t ClientConnectionLost;
103 /* these are marked volatile because they are examined by signal handlers: */
104 extern PGDLLIMPORT volatile uint32 InterruptHoldoffCount;
105 extern PGDLLIMPORT volatile uint32 QueryCancelHoldoffCount;
106 extern PGDLLIMPORT volatile uint32 CritSectionCount;
108 /* in tcop/postgres.c */
109 extern void ProcessInterrupts(void);
111 /* Test whether an interrupt is pending */
112 #ifndef WIN32
113 #define INTERRUPTS_PENDING_CONDITION() \
114 (unlikely(InterruptPending))
115 #else
116 #define INTERRUPTS_PENDING_CONDITION() \
117 (unlikely(UNBLOCKED_SIGNAL_QUEUE()) ? pgwin32_dispatch_queued_signals() : 0, \
118 unlikely(InterruptPending))
119 #endif
121 /* Service interrupt, if one is pending and it's safe to service it now */
122 #define CHECK_FOR_INTERRUPTS() \
123 do { \
124 if (INTERRUPTS_PENDING_CONDITION()) \
125 ProcessInterrupts(); \
126 } while(0)
128 /* Is ProcessInterrupts() guaranteed to clear InterruptPending? */
129 #define INTERRUPTS_CAN_BE_PROCESSED() \
130 (InterruptHoldoffCount == 0 && CritSectionCount == 0 && \
131 QueryCancelHoldoffCount == 0)
133 #define HOLD_INTERRUPTS() (InterruptHoldoffCount++)
135 #define RESUME_INTERRUPTS() \
136 do { \
137 Assert(InterruptHoldoffCount > 0); \
138 InterruptHoldoffCount--; \
139 } while(0)
141 #define HOLD_CANCEL_INTERRUPTS() (QueryCancelHoldoffCount++)
143 #define RESUME_CANCEL_INTERRUPTS() \
144 do { \
145 Assert(QueryCancelHoldoffCount > 0); \
146 QueryCancelHoldoffCount--; \
147 } while(0)
149 #define START_CRIT_SECTION() (CritSectionCount++)
151 #define END_CRIT_SECTION() \
152 do { \
153 Assert(CritSectionCount > 0); \
154 CritSectionCount--; \
155 } while(0)
158 /*****************************************************************************
159 * globals.h -- *
160 *****************************************************************************/
163 * from utils/init/globals.c
165 extern PGDLLIMPORT pid_t PostmasterPid;
166 extern PGDLLIMPORT bool IsPostmasterEnvironment;
167 extern PGDLLIMPORT bool IsUnderPostmaster;
168 extern PGDLLIMPORT bool IsBinaryUpgrade;
170 extern PGDLLIMPORT bool ExitOnAnyError;
172 extern PGDLLIMPORT char *DataDir;
173 extern PGDLLIMPORT int data_directory_mode;
175 extern PGDLLIMPORT int NBuffers;
176 extern PGDLLIMPORT int MaxBackends;
177 extern PGDLLIMPORT int MaxConnections;
178 extern PGDLLIMPORT int max_worker_processes;
179 extern PGDLLIMPORT int max_parallel_workers;
181 extern PGDLLIMPORT int commit_timestamp_buffers;
182 extern PGDLLIMPORT int multixact_member_buffers;
183 extern PGDLLIMPORT int multixact_offset_buffers;
184 extern PGDLLIMPORT int notify_buffers;
185 extern PGDLLIMPORT int serializable_buffers;
186 extern PGDLLIMPORT int subtransaction_buffers;
187 extern PGDLLIMPORT int transaction_buffers;
189 extern PGDLLIMPORT int MyProcPid;
190 extern PGDLLIMPORT pg_time_t MyStartTime;
191 extern PGDLLIMPORT TimestampTz MyStartTimestamp;
192 extern PGDLLIMPORT struct Port *MyProcPort;
193 extern PGDLLIMPORT struct Latch *MyLatch;
194 extern PGDLLIMPORT bool MyCancelKeyValid;
195 extern PGDLLIMPORT int32 MyCancelKey;
196 extern PGDLLIMPORT int MyPMChildSlot;
198 extern PGDLLIMPORT char OutputFileName[];
199 extern PGDLLIMPORT char my_exec_path[];
200 extern PGDLLIMPORT char pkglib_path[];
202 #ifdef EXEC_BACKEND
203 extern PGDLLIMPORT char postgres_exec_path[];
204 #endif
206 extern PGDLLIMPORT Oid MyDatabaseId;
208 extern PGDLLIMPORT Oid MyDatabaseTableSpace;
210 extern PGDLLIMPORT bool MyDatabaseHasLoginEventTriggers;
213 * Date/Time Configuration
215 * DateStyle defines the output formatting choice for date/time types:
216 * USE_POSTGRES_DATES specifies traditional Postgres format
217 * USE_ISO_DATES specifies ISO-compliant format
218 * USE_SQL_DATES specifies Oracle/Ingres-compliant format
219 * USE_GERMAN_DATES specifies German-style dd.mm/yyyy
221 * DateOrder defines the field order to be assumed when reading an
222 * ambiguous date (anything not in YYYY-MM-DD format, with a four-digit
223 * year field first, is taken to be ambiguous):
224 * DATEORDER_YMD specifies field order yy-mm-dd
225 * DATEORDER_DMY specifies field order dd-mm-yy ("European" convention)
226 * DATEORDER_MDY specifies field order mm-dd-yy ("US" convention)
228 * In the Postgres and SQL DateStyles, DateOrder also selects output field
229 * order: day comes before month in DMY style, else month comes before day.
231 * The user-visible "DateStyle" run-time parameter subsumes both of these.
234 /* valid DateStyle values */
235 #define USE_POSTGRES_DATES 0
236 #define USE_ISO_DATES 1
237 #define USE_SQL_DATES 2
238 #define USE_GERMAN_DATES 3
239 #define USE_XSD_DATES 4
241 /* valid DateOrder values */
242 #define DATEORDER_YMD 0
243 #define DATEORDER_DMY 1
244 #define DATEORDER_MDY 2
246 extern PGDLLIMPORT int DateStyle;
247 extern PGDLLIMPORT int DateOrder;
250 * IntervalStyles
251 * INTSTYLE_POSTGRES Like Postgres < 8.4 when DateStyle = 'iso'
252 * INTSTYLE_POSTGRES_VERBOSE Like Postgres < 8.4 when DateStyle != 'iso'
253 * INTSTYLE_SQL_STANDARD SQL standard interval literals
254 * INTSTYLE_ISO_8601 ISO-8601-basic formatted intervals
256 #define INTSTYLE_POSTGRES 0
257 #define INTSTYLE_POSTGRES_VERBOSE 1
258 #define INTSTYLE_SQL_STANDARD 2
259 #define INTSTYLE_ISO_8601 3
261 extern PGDLLIMPORT int IntervalStyle;
263 #define MAXTZLEN 10 /* max TZ name len, not counting tr. null */
265 extern PGDLLIMPORT bool enableFsync;
266 extern PGDLLIMPORT bool allowSystemTableMods;
267 extern PGDLLIMPORT int work_mem;
268 extern PGDLLIMPORT double hash_mem_multiplier;
269 extern PGDLLIMPORT int maintenance_work_mem;
270 extern PGDLLIMPORT int max_parallel_maintenance_workers;
273 * Upper and lower hard limits for the buffer access strategy ring size
274 * specified by the VacuumBufferUsageLimit GUC and BUFFER_USAGE_LIMIT option
275 * to VACUUM and ANALYZE.
277 #define MIN_BAS_VAC_RING_SIZE_KB 128
278 #define MAX_BAS_VAC_RING_SIZE_KB (16 * 1024 * 1024)
280 extern PGDLLIMPORT int VacuumBufferUsageLimit;
281 extern PGDLLIMPORT int VacuumCostPageHit;
282 extern PGDLLIMPORT int VacuumCostPageMiss;
283 extern PGDLLIMPORT int VacuumCostPageDirty;
284 extern PGDLLIMPORT int VacuumCostLimit;
285 extern PGDLLIMPORT double VacuumCostDelay;
287 extern PGDLLIMPORT int VacuumCostBalance;
288 extern PGDLLIMPORT bool VacuumCostActive;
291 /* in tcop/postgres.c */
293 typedef char *pg_stack_base_t;
295 extern pg_stack_base_t set_stack_base(void);
296 extern void restore_stack_base(pg_stack_base_t base);
297 extern void check_stack_depth(void);
298 extern bool stack_is_too_deep(void);
300 /* in tcop/utility.c */
301 extern void PreventCommandIfReadOnly(const char *cmdname);
302 extern void PreventCommandIfParallelMode(const char *cmdname);
303 extern void PreventCommandDuringRecovery(const char *cmdname);
305 /*****************************************************************************
306 * pdir.h -- *
307 * POSTGRES directory path definitions. *
308 *****************************************************************************/
310 /* flags to be OR'd to form sec_context */
311 #define SECURITY_LOCAL_USERID_CHANGE 0x0001
312 #define SECURITY_RESTRICTED_OPERATION 0x0002
313 #define SECURITY_NOFORCE_RLS 0x0004
315 extern PGDLLIMPORT char *DatabasePath;
317 /* now in utils/init/miscinit.c */
318 extern void InitPostmasterChild(void);
319 extern void InitStandaloneProcess(const char *argv0);
320 extern void InitProcessLocalLatch(void);
321 extern void SwitchToSharedLatch(void);
322 extern void SwitchBackToLocalLatch(void);
325 * MyBackendType indicates what kind of a backend this is.
327 * If you add entries, please also update the child_process_kinds array in
328 * launch_backend.c.
330 typedef enum BackendType
332 B_INVALID = 0,
334 /* Backends and other backend-like processes */
335 B_BACKEND,
336 B_AUTOVAC_LAUNCHER,
337 B_AUTOVAC_WORKER,
338 B_BG_WORKER,
339 B_WAL_SENDER,
340 B_SLOTSYNC_WORKER,
342 B_STANDALONE_BACKEND,
345 * Auxiliary processes. These have PGPROC entries, but they are not
346 * attached to any particular database. There can be only one of each of
347 * these running at a time.
349 * If you modify these, make sure to update NUM_AUXILIARY_PROCS and the
350 * glossary in the docs.
352 B_ARCHIVER,
353 B_BG_WRITER,
354 B_CHECKPOINTER,
355 B_STARTUP,
356 B_WAL_RECEIVER,
357 B_WAL_SUMMARIZER,
358 B_WAL_WRITER,
361 * Logger is not connected to shared memory and does not have a PGPROC
362 * entry.
364 B_LOGGER,
365 } BackendType;
367 #define BACKEND_NUM_TYPES (B_LOGGER + 1)
369 extern PGDLLIMPORT BackendType MyBackendType;
371 #define AmAutoVacuumLauncherProcess() (MyBackendType == B_AUTOVAC_LAUNCHER)
372 #define AmAutoVacuumWorkerProcess() (MyBackendType == B_AUTOVAC_WORKER)
373 #define AmBackgroundWorkerProcess() (MyBackendType == B_BG_WORKER)
374 #define AmWalSenderProcess() (MyBackendType == B_WAL_SENDER)
375 #define AmLogicalSlotSyncWorkerProcess() (MyBackendType == B_SLOTSYNC_WORKER)
376 #define AmArchiverProcess() (MyBackendType == B_ARCHIVER)
377 #define AmBackgroundWriterProcess() (MyBackendType == B_BG_WRITER)
378 #define AmCheckpointerProcess() (MyBackendType == B_CHECKPOINTER)
379 #define AmStartupProcess() (MyBackendType == B_STARTUP)
380 #define AmWalReceiverProcess() (MyBackendType == B_WAL_RECEIVER)
381 #define AmWalSummarizerProcess() (MyBackendType == B_WAL_SUMMARIZER)
382 #define AmWalWriterProcess() (MyBackendType == B_WAL_WRITER)
384 extern const char *GetBackendTypeDesc(BackendType backendType);
386 extern void SetDatabasePath(const char *path);
387 extern void checkDataDir(void);
388 extern void SetDataDir(const char *dir);
389 extern void ChangeToDataDir(void);
391 extern char *GetUserNameFromId(Oid roleid, bool noerr);
392 extern Oid GetUserId(void);
393 extern Oid GetOuterUserId(void);
394 extern Oid GetSessionUserId(void);
395 extern Oid GetAuthenticatedUserId(void);
396 extern void GetUserIdAndSecContext(Oid *userid, int *sec_context);
397 extern void SetUserIdAndSecContext(Oid userid, int sec_context);
398 extern bool InLocalUserIdChange(void);
399 extern bool InSecurityRestrictedOperation(void);
400 extern bool InNoForceRLSOperation(void);
401 extern void GetUserIdAndContext(Oid *userid, bool *sec_def_context);
402 extern void SetUserIdAndContext(Oid userid, bool sec_def_context);
403 extern void InitializeSessionUserId(const char *rolename, Oid roleid,
404 bool bypass_login_check);
405 extern void InitializeSessionUserIdStandalone(void);
406 extern void SetSessionAuthorization(Oid userid, bool is_superuser);
407 extern Oid GetCurrentRoleId(void);
408 extern void SetCurrentRoleId(Oid roleid, bool is_superuser);
409 extern void InitializeSystemUser(const char *authn_id,
410 const char *auth_method);
411 extern const char *GetSystemUser(void);
413 /* in utils/misc/superuser.c */
414 extern bool superuser(void); /* current user is superuser */
415 extern bool superuser_arg(Oid roleid); /* given user is superuser */
418 /*****************************************************************************
419 * pmod.h -- *
420 * POSTGRES processing mode definitions. *
421 *****************************************************************************/
424 * Description:
425 * There are three processing modes in POSTGRES. They are
426 * BootstrapProcessing or "bootstrap," InitProcessing or
427 * "initialization," and NormalProcessing or "normal."
429 * The first two processing modes are used during special times. When the
430 * system state indicates bootstrap processing, transactions are all given
431 * transaction id "one" and are consequently guaranteed to commit. This mode
432 * is used during the initial generation of template databases.
434 * Initialization mode: used while starting a backend, until all normal
435 * initialization is complete. Some code behaves differently when executed
436 * in this mode to enable system bootstrapping.
438 * If a POSTGRES backend process is in normal mode, then all code may be
439 * executed normally.
442 typedef enum ProcessingMode
444 BootstrapProcessing, /* bootstrap creation of template database */
445 InitProcessing, /* initializing system */
446 NormalProcessing, /* normal processing */
447 } ProcessingMode;
449 extern PGDLLIMPORT ProcessingMode Mode;
451 #define IsBootstrapProcessingMode() (Mode == BootstrapProcessing)
452 #define IsInitProcessingMode() (Mode == InitProcessing)
453 #define IsNormalProcessingMode() (Mode == NormalProcessing)
455 #define GetProcessingMode() Mode
457 #define SetProcessingMode(mode) \
458 do { \
459 Assert((mode) == BootstrapProcessing || \
460 (mode) == InitProcessing || \
461 (mode) == NormalProcessing); \
462 Mode = (mode); \
463 } while(0)
466 /*****************************************************************************
467 * pinit.h -- *
468 * POSTGRES initialization and cleanup definitions. *
469 *****************************************************************************/
471 /* in utils/init/postinit.c */
472 /* flags for InitPostgres() */
473 #define INIT_PG_LOAD_SESSION_LIBS 0x0001
474 #define INIT_PG_OVERRIDE_ALLOW_CONNS 0x0002
475 #define INIT_PG_OVERRIDE_ROLE_LOGIN 0x0004
476 extern void pg_split_opts(char **argv, int *argcp, const char *optstr);
477 extern void InitializeMaxBackends(void);
478 extern void InitializeFastPathLocks(void);
479 extern void InitPostgres(const char *in_dbname, Oid dboid,
480 const char *username, Oid useroid,
481 bits32 flags,
482 char *out_dbname);
483 extern void BaseInit(void);
485 /* in utils/init/miscinit.c */
486 extern PGDLLIMPORT bool IgnoreSystemIndexes;
487 extern PGDLLIMPORT bool process_shared_preload_libraries_in_progress;
488 extern PGDLLIMPORT bool process_shared_preload_libraries_done;
489 extern PGDLLIMPORT bool process_shmem_requests_in_progress;
490 extern PGDLLIMPORT char *session_preload_libraries_string;
491 extern PGDLLIMPORT char *shared_preload_libraries_string;
492 extern PGDLLIMPORT char *local_preload_libraries_string;
494 extern void CreateDataDirLockFile(bool amPostmaster);
495 extern void CreateSocketLockFile(const char *socketfile, bool amPostmaster,
496 const char *socketDir);
497 extern void TouchSocketLockFiles(void);
498 extern void AddToDataDirLockFile(int target_line, const char *str);
499 extern bool RecheckDataDirLockFile(void);
500 extern void ValidatePgVersion(const char *path);
501 extern void process_shared_preload_libraries(void);
502 extern void process_session_preload_libraries(void);
503 extern void process_shmem_requests(void);
504 extern void pg_bindtextdomain(const char *domain);
505 extern bool has_rolreplication(Oid roleid);
507 typedef void (*shmem_request_hook_type) (void);
508 extern PGDLLIMPORT shmem_request_hook_type shmem_request_hook;
510 extern Size EstimateClientConnectionInfoSpace(void);
511 extern void SerializeClientConnectionInfo(Size maxsize, char *start_address);
512 extern void RestoreClientConnectionInfo(char *conninfo);
514 /* in executor/nodeHash.c */
515 extern size_t get_hash_memory_limit(void);
517 #endif /* MISCADMIN_H */