Sub topic 17
PostgreSQL Triggers: Performance Costs and Alternatives
Performance costs and alternatives to triggers
This chapter assumes you know basic SQL, transactions, and simple PL/pgSQL. A trigger runs inside the transaction that changes a table. That makes it useful, but it also adds work to every matching statement.
A row-level trigger runs once for every affected row. An UPDATE that changes 100,000 rows can therefore call the trigger function 100,000 times. Each call can add expression evaluation, queries, logging, locks, and network-independent database work. The original statement waits for all of that work to finish.
Statement-level triggers run once per statement, so they are often cheaper for set-based work. A transition table can expose all affected rows to one trigger invocation. This is usually better than querying the base table once for every row.
Measure trigger cost with representative data. Compare execution plans and timings before and after adding the trigger. Also test multi-row statements, because a trigger that seems cheap for one row may be expensive for a bulk update.
EXPLAIN (ANALYZE, BUFFERS)
UPDATE accounts
SET balance = balance + 10
WHERE id <= 100000;Common alternatives include:
- Use a
CHECK,NOT NULL,UNIQUE, or foreign-key constraint for a rule that only describes valid data. - Use a generated column for a deterministic value derived from columns in the same row.
- Use one set-based SQL statement instead of a row-by-row trigger.
- Use application code when the behavior is part of a user workflow and does not need database-wide enforcement.
- Use an asynchronous job or message queue when audit processing or notifications do not need to delay the transaction.
- Use explicit stored procedures when callers should invoke a business operation intentionally.
A trigger is still appropriate when every writer must receive the same enforcement, including scripts and other applications. Avoid hiding expensive queries, external calls, or unpredictable work in a trigger.
Constraint triggers and deferred checks
A constraint trigger is a special AFTER ROW trigger that behaves like a constraint. It can be declared DEFERRABLE, allowing PostgreSQL to postpone its check until transaction commit. This helps when a temporary intermediate state is invalid but the final state is valid.
For example, a deferred trigger can check that an account balance is not negative after a transaction performs several related changes:
CREATE CONSTRAINT TRIGGER accounts_balance_check
AFTER INSERT OR UPDATE OF balance ON accounts
DEFERRABLE INITIALLY DEFERRED
FOR EACH ROW
EXECUTE FUNCTION forbid_negative_balance();INITIALLY DEFERRED makes the check run at commit by default. A transaction can change the timing with:
SET CONSTRAINTS accounts_balance_check IMMEDIATE;When the check becomes immediate, PostgreSQL runs pending checks and reports any violation. Deferred checks do not remove work. They move the work later, and a failure at commit can roll back the whole transaction. Use them for cross-row or cross-table consistency that cannot be expressed with ordinary constraints. Prefer a standard constraint when one can express the rule clearly and efficiently.
Exercise
A statement updates 50,000 rows, and a row-level trigger runs a query for each row. What are two ways to reduce the cost?
Show answer
Replace the per-row query with set-based work, possibly in a statement-level trigger using a transition table. If the behavior is not required synchronously, move it to an asynchronous job.