# 54001 — statement_too_complex

> Source-backed reference for PostgreSQL SQLSTATE 54001.
---

# 54001

## At a glance {#at-a-glance}
`54001` covers a statement that exceeds a planner, parser, or execution-stack limit. Fixed paths include the 4096 grouping-set ceiling and `max_stack_depth` enforcement.

<!-- BEGIN SQLSTATE FACTS: generated by scripts/generate.py; do not edit -->

| Field | Value |
| --- | --- |
| SQLSTATE | `54001` |
| Condition | `statement_too_complex` |
| Status | `active` |
| Known present by | `7.4` |
| Locked snapshots | `9.0.23, 9.1.24, 9.2.24, 9.3.25, 9.4.26, 9.5.25, 9.6.24, 10.23, 11.22, 12.22, 13.23, 14.24, 15.19, 16.15, 17.11, 18.6, 19beta3` |
| Macros | `ERRCODE_STATEMENT_TOO_COMPLEX` |
| Aliases | `—` |

<!-- source facts: data/errcodes/54001.json -->
<!-- END SQLSTATE FACTS -->

## Meaning {#meaning}
The source separates a structural query limit (`too many grouping sets present (maximum 4096)`) from recursive execution depth (`stack depth limit exceeded`) with a hint naming `max_stack_depth`. They need different repairs: reducing query structure is not the same as changing a stack setting.

## Diagnosis {#diagnosis}
Preserve the message and phase. For grouping sets, count the generated grouping sets and simplify the query. For stack depth, inspect recursive SQL/function expansion and compare the setting with the platform stack limit before changing it.

## Response {#response}
Rewrite or decompose a structurally excessive statement. For stack depth, remove unintended recursion or reduce nesting; change `max_stack_depth` only after checking the operating-system stack allowance and operational policy. If this `ERROR` occurs inside an explicit transaction, use `ROLLBACK` or `ROLLBACK TO SAVEPOINT` before retrying; in autocommit, retry only after the statement or stack setting is corrected. The GUC rlimit check validates a `SET` or configuration value against the platform allowance; a failed `SET` still follows the transaction context that issued it. Validate the rewritten statement rather than automatically replaying it.

## Versions {#versions}
The locked catalogue records this condition from 7.4; cited parser and stack-depth paths are PostgreSQL 18.6 source. No complexity limit was forced.

## Related {#related}
[`54000`](../54000/), [`54011`](../54011/), [`42601`](../42601/)

## Sources {#sources}
[`src/backend/parser/parse_agg.c#L1166-L1172`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_agg.c#L1166)

[`src/backend/utils/misc/stack_depth.c#L100-L105`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/misc/stack_depth.c#L100)

[`src/backend/utils/misc/stack_depth.c#L156-L168`](https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/misc/stack_depth.c#L156)

The structured [evidence record](../data/evidence/54001.json) records fixed messages and the source/runtime boundary.
