Database

Triggers

Execute a function automatically when rows change. Triggers fire on INSERT, UPDATE, DELETE, or TRUNCATE, before or after the event, per row or per statement.

The shape of a trigger

ChoiceValuesNotes
TimingBEFORE, AFTERBEFORE can modify the row being written; AFTER sees the final state
EventsINSERT, UPDATE, DELETE, TRUNCATECombine several events in one trigger
LevelROW, STATEMENTROW fires per affected row; STATEMENT once per query
Update ofcolumn listFire only when specific columns are updated
WHENboolean expressionA row-level condition, e.g. NEW.plan <> OLD.plan
Functiona public trigger functionMust return trigger
  • TRUNCATE triggers must be FOR EACH STATEMENT — there are no rows to iterate.
  • WHEN conditions require FOR EACH ROW.
  • INSTEAD OF triggers apply to views and are not offered on tables.

The trigger function

A trigger calls a function that returns trigger. Create it on the Functions screen (there is a trigger template), then pick it here. Inside the body, NEW is the row being written and OLD the row being replaced or removed.

Example · stamp updated_at on every update
CREATE FUNCTION public.set_updated_at()
RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
  NEW.updated_at := now();
  RETURN NEW;
END;
$$;
SQL · what the Console creates
CREATE TRIGGER customers_set_updated_at
BEFORE UPDATE ON public.customers
FOR EACH ROW
EXECUTE FUNCTION public.set_updated_at();

Enabling, disabling, dropping

  • The Triggers screen groups triggers by table and shows timing, events, level, function, and enabled state.
  • Disabling a trigger keeps its definition but stops it firing — useful while backfilling data.
  • Dropping the trigger does not drop the function; other triggers may still use it.
  • Triggers run inside the writing transaction: if the function raises, the whole write is rolled back.

Additional resources