PostgreSQL Error 39P01: Trigger Protocol Violated
In this article
PostgreSQL error 39P01 means a trigger function returned a value in a context where the trigger protocol forbids it. The fix is almost always to make the trigger function return NULL (or, for row-level BEFORE triggers, the row itself) and to ensure the function is declared to return trigger. This guide walks through diagnosing the cause and correcting the function so the error stops.
What You'll Need
- Access to the database with permission to view and alter the trigger function (typically the owner or a superuser).
- A SQL client such as psql, pgAdmin, or any tool that can run queries and show function definitions.
- The name of the table or view where the error occurs, and the trigger name if known.
- Basic familiarity with PL/pgSQL or whichever language the trigger function is written in.
Step-by-Step
- Reproduce the error and note the context. Run the statement that fails (an INSERT, UPDATE, or DELETE) and read the full error message. PostgreSQL usually names the trigger and the function. Write down both, plus the operation (INSERT/UPDATE/DELETE) and whether it is a row-level or statement-level trigger.
- List the triggers on the table. Connect with psql or your client and run:
SELECT tgname, tgrelid::regclass, tgfoid::regproc, tgtype FROM pg_trigger WHERE NOT tgisinternal;This shows trigger names, the table they belong to, and the function they call. Match the trigger from the error message. - Inspect the trigger function body. Use
\df+ schema.function_namein psql, or querypg_procfor the source. Look at everyRETURNstatement. The trigger protocol requires: a row-level BEFORE trigger must return the row to be inserted or updated (or NULL to skip the operation); a row-level AFTER trigger's return value is ignored but should be NULL; a statement-level trigger's return value is ignored but should be NULL. Any function that returns something else—especially a value from an INSERT/UPDATE/DELETE inside the function—can trigger 39P01. - Check the function's declared return type. It must be
trigger(orevent_triggerfor event triggers). If it was created asRETURNS integerorRETURNS void, it cannot be used as a trigger function and may produce this or a related error. Recreate it withRETURNS trigger. - Fix the return statements. For a typical row-level BEFORE trigger, end with
RETURN NEW;for INSERT/UPDATE andRETURN OLD;for DELETE. For AFTER or statement-level triggers, useRETURN NULL;. Remove anyRETURNthat returns a scalar, a query result, or a value from a nested statement. If the function usesPERFORMorEXECUTE, those do not need returns; just ensure the final return is correct. - Check for early returns. A
RETURNinside an IF block must follow the same rules. A common mistake is returning a boolean or an integer from a validation branch. Change those toRETURN NULL;(to skip) orRETURN NEW;(to proceed). - Verify the trigger definition. Confirm the trigger is defined as
FOR EACH ROWorFOR EACH STATEMENTand that the function's return logic matches. A function written for a row-level trigger used as a statement-level trigger (or vice versa) can violate the protocol. - Recreate or replace the function. Use
CREATE OR REPLACE FUNCTION ... RETURNS trigger AS $$ ... $$ LANGUAGE plpgsql;to update it without dropping the trigger. If the return type changes, you must drop and recreate the function and then recreate the trigger. - Test with a rolled-back transaction. In psql, run
BEGIN;then the failing statement, check the result, andROLLBACK;. This avoids leaving test data behind. If it succeeds, commit a real change and confirm. - Check for nested or cascading triggers. If the trigger fires another trigger, the error may come from the inner function. Repeat steps 2–5 for each trigger in the chain. Use
SET session_replication_role = replica;only for diagnosis, not as a permanent fix, and revert it immediately.
Common Mistakes
- Returning the result of an INSERT/UPDATE/DELETE statement instead of
NEW,OLD, orNULL. - Declaring the function as
RETURNS voidorRETURNS integerand expecting it to work as a trigger. - Using
RETURN TRUEorRETURN 1in a validation branch. - Forgetting that a row-level BEFORE trigger must return the row; returning NULL silently skips the operation, which may look like data loss.
- Mixing up row-level and statement-level logic when copying a function between triggers.
- Editing the function but not running
CREATE OR REPLACE, so the old definition remains.
Tips
- Keep trigger functions small and single-purpose; a function that both validates and modifies data is harder to debug.
- Use
RAISE NOTICEinside the function to log which branch runs during testing. - Name triggers and functions consistently (for example,
trg_table_actionandfn_table_action) so error messages are easy to trace. - Document the expected return value in a comment at the top of the function.
- When migrating from another database, review trigger functions manually; return conventions differ.
Troubleshooting
If the error persists after fixing the return statements, check whether another trigger on the same table fires first and returns an invalid value. Disable triggers one at a time with ALTER TABLE ... DISABLE TRIGGER trigger_name; (requires table owner) and test after each change; re-enable them immediately. If the function is written in a language other than PL/pgSQL, confirm that language's trigger API returns the correct value—for example, in PL/Python the function must return a row or None according to the trigger type. If you cannot alter the function because of permissions, ask the object owner or a superuser. If the error appears only under load, look for a code path that calls the trigger function directly (outside a trigger); that is not allowed and will also produce protocol errors. Finally, check the PostgreSQL server log for the full statement and context; it often names the exact line in the function.
Frequently Asked Questions
What does error 39P01 mean exactly?
It is the SQLSTATE for "trigger protocol violated": a trigger function returned a value that the trigger protocol does not permit for that trigger type.
Can I fix it without dropping the trigger?
Yes. Use CREATE OR REPLACE FUNCTION to correct the return statements, as long as the return type stays trigger. If the return type changes, you must drop and recreate both function and trigger.
Why does my AFTER trigger fail when it returns a row?
AFTER triggers ignore the return value, but some PostgreSQL versions and languages still enforce the protocol. Return NULL from AFTER and statement-level triggers to be safe.
Does this error affect data integrity?
The statement that fired the trigger is rolled back, so no partial change is committed. Fix the function before retrying.
Can a view trigger cause 39P01?
Yes. INSTEAD OF triggers on views follow the same protocol; the function must return NEW for row-level operations or NULL where appropriate.
Is 39P01 the same as 39P02?
No. 39P02 is "srf protocol violated" (set-returning function protocol). 39P01 is specific to triggers.