DB Internals AtlasPostgreSQL 18.6 Home About

Parameter · Resource Usage / Memory

max_stack_depth

Sets the maximum stack depth, in kilobytes.

  • Default: 100kB
  • Type: integer (kB)
  • Range: 100kB … max (2147483647)
  • Applies: Session (superuser)

In plain words

Every function call inside the server uses some stack memory. A runaway recursive function, or a gigantic nested expression, could overflow the stack and crash the process. PostgreSQL checks the depth and stops the query with an error before that happens.

For DBAs and operators

The built-in default shown in pg_settings is 100 kB, but the server raises it at startup to 2 MB when the kernel stack limit allows (SHOW max_stack_depth normally returns 2MB). Set it to the kernel limit (ulimit -s, typically 8 MB) minus about 1 MB of safety margin; the server refuses values above what it detects as safe: DETAIL: "max_stack_depth" must not exceed 7680kB.

When the limit is hit:

ERROR: stack depth limit exceeded
HINT: Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.

Common causes: unbounded recursive SQL/PL functions, queries generated with thousands of OR/IN elements or deeply nested JSON, recursive triggers. Fix the query first; raise the setting (superuser) only if the recursion is legitimate. If the systemd unit sets a smaller LimitSTACK, the safe maximum shrinks accordingly.

How to change it

A superuser (or a role granted SET) can change it per session with SET.

See it in context

Local (per-backend) memory Client backend

Related parameters

work_mem

All Resource Usage parameters