// PostgreSQL error reference

PostgreSQL P0001: raise exception

A PL/pgSQL RAISE EXCEPTION without its own SQLSTATE. The message is whatever text the function or trigger author wrote.

SQLSTATE
P0001
Name
raise_exception
Category
PL/pgSQL Error
Error message
insufficient funds for account 42

What it means

P0001 (raise_exception) is the default code for RAISE EXCEPTION 'message' in PL/pgSQL. It isn't a PostgreSQL error as such: a function, procedure or trigger in your database chose to fail, and the message text comes from its code. The CONTEXT lines show which function and line raised it.

Function authors can give a specific SQLSTATE with USING ERRCODE, plus DETAIL and HINT, which makes the error easier for applications to handle.

Common causes

  • A business rule enforced in a trigger or function, such as a balance check, rejected the change.
  • A validation function called by the application raised an error for bad input.
  • An ASSERT failing reports a different code, P0004 (assert_failure).

How to fix it

  1. Read the CONTEXT lines to find the function and line, then look at its source.

    SELECT pg_get_functiondef('debit_account'::regproc);
  2. Fix the data or the call so the rule is satisfied, or change the rule if it is wrong.

  3. When writing such checks, give them a meaningful code and detail so callers can tell them apart.

    RAISE EXCEPTION 'insufficient funds for account %', p_account
        USING ERRCODE = 'check_violation',
              DETAIL = format('balance %s, requested %s', v_balance, p_amount);

Reproduce it safely

DO $$ BEGIN RAISE EXCEPTION 'something went wrong'; END $$;  -- ERROR: something went wrong (SQLSTATE P0001)

Related errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.