1/*-------------------------------------------------------------------------
4 * Functions for monitoring and limiting process stack depth
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/utils/misc/stack_depth.c
13 *-------------------------------------------------------------------------
25/* GUC variable for maximum stack depth (measured in kilobytes) */
28/* max_stack_depth converted to bytes for speed of checking */
32 * Stack base pointer -- initialized by set_stack_base(), which
33 * should be called from main().
39 * set_stack_base: set up reference point for stack depth checking
41 * Returns the old reference point, if any.
46#ifndef HAVE__BUILTIN_FRAME_ADDRESS
54 * Set up reference point for stack depth checking. On recent gcc we use
55 * __builtin_frame_address() to avoid a warning about storing a local
56 * variable's address in a long-lived variable.
58#ifdef HAVE__BUILTIN_FRAME_ADDRESS
68 * restore_stack_base: restore reference point for stack depth checking
70 * This can be used after set_stack_base() to restore the old value. This
71 * is currently only used in PL/Java. When PL/Java calls a backend function
72 * from different thread, the thread's stack is at a different location than
73 * the main thread's stack, so it sets the base pointer before the call, and
74 * restores it afterwards.
84 * check_stack_depth/stack_is_too_deep: check for excessively deep recursion
86 * This should be called someplace in any recursive routine that might possibly
87 * recurse deep enough to overflow the stack. Most Unixen treat stack
88 * overflow as an unrecoverable SIGSEGV, so we want to error out ourselves
89 * before hitting the hardware limit.
91 * check_stack_depth() just throws an error summarily. stack_is_too_deep()
92 * can be used by code that wants to handle the error condition itself.
100 (
errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
101 errmsg(
"stack depth limit exceeded"),
102 errhint(
"Increase the configuration parameter \"max_stack_depth\" (currently %dkB), "
103 "after ensuring the platform's stack depth limit is adequate.",
115 * Compute distance from reference point to my local variables
120 * Take abs value, since stacks grow up on some machines, down on others
123 stack_depth = -stack_depth;
128 * The test on stack_base_ptr prevents us from erroring out if called
129 * before that's been set. Logically it should be done first, but putting
130 * it last avoids wasting cycles during normal cases.
140/* GUC check hook for max_stack_depth */
144 ssize_t newval_bytes = *
newval * (ssize_t) 1024;
151 GUC_check_errhint(
"Increase the platform's stack depth limit via \"ulimit -s\" or local equivalent.");
157/* GUC assign hook for max_stack_depth */
161 ssize_t newval_bytes =
newval * (ssize_t) 1024;
167 * Obtain platform stack depth limit (in bytes)
169 * Return -1 if unknown
171 * Note: we choose to use ssize_t not size_t as the result type because
172 * callers compute values that could theoretically go negative,
173 * such as "result - STACK_DEPTH_SLOP".
178#if defined(HAVE_GETRLIMIT)
179 static ssize_t
val = 0;
181 /* This won't change after process launch, so check just once */
186 if (getrlimit(RLIMIT_STACK, &rlim) < 0)
188 else if (rlim.rlim_cur == RLIM_INFINITY)
190 /* rlim_cur is probably of an unsigned type, so check for overflow */
191 else if (rlim.rlim_cur >= SSIZE_MAX)
198 /* On Windows we set the backend stack size in src/backend/Makefile */
199 return WIN32_STACK_RLIMIT;
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define GUC_check_errdetail
#define GUC_check_errhint
static rewind_source * source
void restore_stack_base(pg_stack_base_t base)
ssize_t get_stack_depth_rlimit(void)
bool check_max_stack_depth(int *newval, void **extra, GucSource source)
bool stack_is_too_deep(void)
void assign_max_stack_depth(int newval, void *extra)
static char * stack_base_ptr
static ssize_t max_stack_depth_bytes
void check_stack_depth(void)
pg_stack_base_t set_stack_base(void)