39P03
PostgreSQLERRORNotableExternal Routine Invocation ExceptionHIGH confidence

event trigger protocol violated

What this means

SQLSTATE 39P03 is a Postgres-specific error raised when an event trigger function violates the event trigger calling protocol — for example, returning a non-VOID value from an event trigger function.

Why it happens
  1. 1An event trigger function returns a non-VOID value (event triggers must return VOID)
  2. 2An event trigger function does not follow the event trigger calling conventions
How to reproduce

Event trigger function returning a non-VOID value.

trigger — this will error
trigger — this will error
CREATE OR REPLACE FUNCTION bad_event_trigger() RETURNS event_trigger AS $
BEGIN
  -- implementation
END;
$ LANGUAGE plpgsql;
-- If it returns a value instead of nothing, 39P03 fires

expected output

ERROR:  event trigger protocol violated

Fix

Ensure event trigger functions return VOID

WHEN When writing event trigger functions.

Ensure event trigger functions return VOID
CREATE OR REPLACE FUNCTION ddl_event_fn() RETURNS event_trigger AS $
DECLARE obj RECORD;
BEGIN
  FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands() LOOP
    RAISE NOTICE 'DDL: % on %', obj.command_tag, obj.object_identity;
  END LOOP;
  -- no RETURN value needed
END;
$ LANGUAGE plpgsql;

Why this works

Event trigger functions declared as RETURNS event_trigger must not return a value. The function must complete normally (RETURN; implicitly) or by falling off the end.

Version notes
Postgres 9.3+

Event triggers introduced in Postgres 9.3.

Sources
Official documentation ↗

Class 39 — External Routine Invocation Exception (Postgres-specific)

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors