Glossary · Databases

Database Trigger

A database trigger is code that runs automatically when a row is inserted, updated, or deleted, used for auditing, validation, and syncing derived data.

Updated October 6, 2026

A database trigger is a function attached to a table that the database runs automatically in response to an INSERT, UPDATE, or DELETE event, either before or after the change.

Why it matters

Triggers enforce rules no matter which application touches the data, which makes them useful for audit logs, timestamps, and denormalized counters. Because they run invisibly, they can surprise developers, slow down writes, and make debugging difficult. Use them sparingly and document them. Triggers are best for small, fast, deterministic actions that must always happen, and application code is usually better for anything involving external systems.

Example

-- PostgreSQL style
CREATE FUNCTION set_updated() RETURNS trigger AS $$
BEGIN
  NEW.updated_at = now();
  RETURN NEW;
END; $$ LANGUAGE plpgsql;

CREATE TRIGGER trg_users_updated
BEFORE UPDATE ON users
FOR EACH ROW EXECUTE FUNCTION set_updated();

Common mistakes

  • Chaining triggers so that one change silently fires many others.
  • Putting slow work, such as heavy queries, inside a trigger and stalling every write.
  • Forgetting triggers exist when bulk-loading data or debugging unexpected changes.

Get notified when this term updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.