Skip to content
uencoder

Trigger

A trigger is database code that executes automatically when specific events (INSERT/UPDATE/DELETE) occur, used for auditing, cascading, and constraints.

Purpose

To execute logic automatically on data changes without explicit application calls.

Mechanism

BEFORE/AFTER × row-level/statement-level triggers. When the event fires, the trigger function runs automatically. Commonly used for audit logs, cascading updates, and complex constraint checks. But implicit execution is hard to debug and may cascade unexpectedly.

Use cases

  • Data-change audit logs
  • Cascading updates and sync
  • Complex constraint checks
  • Automatic materialized-view refresh

Examples

  • Input
    AFTER UPDATE ON users → log change
    Output
    Change recorded automatically

    Audit trigger

Common errors

Trigger cascades cause recursion and deadlocks
Limit trigger depth and avoid cycles

Related terms

References