PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 54001: stack depth limit exceeded

A trigger or function kept calling itself with no end, until PostgreSQL ran out of stack.

PostgreSQL 17.12
54001: stack depth limit exceeded

Jump to a cause

  1. 01An AFTER UPDATE trigger that runs UPDATE on the same table
  2. 02A recursive function walking data that loops (parent of A is B, parent of B is A)

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

Each nested call (a function calling itself, or a trigger running an UPDATE that fires the same trigger again) uses some stack. When the nesting never stops, PostgreSQL ends the statement with 54001.

The HINT suggests raising max_stack_depth (2048kB here). We did not change it: in both cases below the calls would never end, so the fix is to break the loop. psql also prints one CONTEXT line per nesting level, hundreds of lines, so we hid them with \set SHOW_CONTEXT never. Both cases were run on PostgreSQL 17.

01An AFTER UPDATE trigger that runs UPDATE on the same table

The trigger function on products runs UPDATE products SET updated_at = now() WHERE id = NEW.id. That UPDATE fires the same AFTER UPDATE trigger, which updates again, and so on. A single UPDATE products SET price = 1200 WHERE id = 1 ended with 54001.

The fix is not to update the table again: make it a BEFORE UPDATE trigger and set the value on the row being written, NEW.updated_at := now(); RETURN NEW;. The same UPDATE then returned UPDATE 1, and updated_at was filled (touched = t).

Setup
Setuprun as lab_a
CREATE TABLE products (id int PRIMARY KEY, price int, updated_at timestamptz);
INSERT INTO products VALUES (1, 1000, NULL);
CREATE FUNCTION touch_product() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
  UPDATE products SET updated_at = now() WHERE id = NEW.id;
  RETURN NEW;
END $$;
CREATE TRIGGER trg_touch AFTER UPDATE ON products FOR EACH ROW EXECUTE FUNCTION touch_product();
OUTPUT
CREATE TABLE
INSERT 0 1
CREATE FUNCTION
CREATE TRIGGER
Reproducerun as lab_a
\set SHOW_CONTEXT never
UPDATE products SET price = 1200 WHERE id = 1;
✕ ERROR
ERROR:  54001: stack depth limit exceeded
HINT:  Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.
Fixrun as lab_a
DROP TRIGGER trg_touch ON products;
CREATE OR REPLACE FUNCTION touch_product() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
  NEW.updated_at := now();
  RETURN NEW;
END $$;
CREATE TRIGGER trg_touch BEFORE UPDATE ON products FOR EACH ROW EXECUTE FUNCTION touch_product();
OUTPUT
DROP TRIGGER
CREATE FUNCTION
CREATE TRIGGER

To change columns of the row being updated, use a BEFORE trigger and assign to NEW. Do not UPDATE the same table from its own trigger.

After the fixrun as lab_a
UPDATE products SET price = 1200 WHERE id = 1;
SELECT id, price, updated_at IS NOT NULL AS touched FROM products;
✓ SUCCESS
UPDATE 1
 id | price | touched
----+-------+---------
  1 |  1200 | t
(1 row)

02A recursive function walking data that loops (parent of A is B, parent of B is A)

dept_path(p_id) calls itself on parent_id until it reaches a row whose parent_id is NULL. The data had no such row: HQ (1) pointed to Sales (2), and Sales pointed back to HQ. SELECT dept_path(3) recursed forever and failed with 54001.

Listing the table showed the cycle (1 → 2, 2 → 1). After UPDATE dept SET parent_id = NULL WHERE id = 1 made HQ the top, the same call returned HQ > Sales > Team A.

Setup
Setuprun as lab_a
CREATE TABLE dept (id int PRIMARY KEY, parent_id int, name text);
INSERT INTO dept VALUES (1, 2, 'HQ'), (2, 1, 'Sales'), (3, 2, 'Team A');
CREATE FUNCTION dept_path(p_id int) RETURNS text LANGUAGE plpgsql AS $$
DECLARE r dept;
BEGIN
  SELECT * INTO r FROM dept WHERE id = p_id;
  IF r.parent_id IS NULL THEN
    RETURN r.name;
  END IF;
  RETURN dept_path(r.parent_id) || ' > ' || r.name;
END $$;
OUTPUT
CREATE TABLE
INSERT 0 3
CREATE FUNCTION
Reproducerun as lab_a
\set SHOW_CONTEXT never
SELECT dept_path(3);
✕ ERROR
ERROR:  54001: stack depth limit exceeded
HINT:  Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate.
Checkrun as lab_a
SELECT id, parent_id, name FROM dept ORDER BY id;
OUTPUT
 id | parent_id |  name
----+-----------+--------
  1 |         2 | HQ
  2 |         1 | Sales
  3 |         2 | Team A
(3 rows)
Fixrun as lab_a
UPDATE dept SET parent_id = NULL WHERE id = 1;
OUTPUT
UPDATE 1

For hierarchies, check the data for cycles first. The code was fine here; the data was not.

After the fixrun as lab_a
SELECT dept_path(3);
✓ SUCCESS
      dept_path
---------------------
 HQ > Sales > Team A
(1 row)

FAQ

Should I increase max_stack_depth to fix stack depth limit exceeded?

Not when the recursion never ends, as in both cases on this page: any limit would be reached. Find the trigger or function that calls itself and break the loop. We did not change max_stack_depth here.

How do I set updated_at in a trigger without infinite recursion?

Use a BEFORE UPDATE trigger with NEW.updated_at := now(); RETURN NEW; instead of an UPDATE statement. On this page that finished with a single UPDATE 1.

Why does psql print hundreds of CONTEXT lines with this error?

psql shows one CONTEXT line per nested call. Run \set SHOW_CONTEXT never in psql to hide them; the error and HINT still show.

My recursive function worked before. Why does it fail now?

Check the data. On this page the function was fine, but two rows pointed to each other as parents, so it never reached a top row. Setting one parent_id to NULL fixed it.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12