Stored Procedures, User-Defined Functions (UDFs) & Triggers
Push logic into the database carefully with procedures, UDFs, and triggers - knowing the ops trade-offs.
In plain words
Pure expression, transaction, or invariant. Next, avoid row loops and network calls. Finally, migration, timings, and side-effect counts.
Think of it like…
UDFs are calculation tools, procedures are controlled maintenance scripts, and triggers are automatic interlocks: powerful inside the engine, dangerous when they secretly call the outside world.
Where you’ve seen it
A BEFORE INSERT/UPDATE trigger sets updated_at and a CHECK-like audit field; a bulk ETL remains set-based, while a procedure owns an explicitly logged multi-statement archival transaction.
Words you’ll hear
- Procedures
- Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support.
- UDFs
- User functions with scalar or set inputs and declared volatility that can participate in queries and indexes.
- Triggers
- Callbacks fired before, after, or instead of INSERT/UPDATE/DELETE at row or statement level inside a transaction.
- Operational boundary
- Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic. Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.
Visual diagram
Theory & deep dive
Procedures: the working mechanism
Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction.
Pure expression, transaction, or invariant. Avoid row loops and network calls, then know commit and exception boundaries; this exposes the state governed by Triggers.
A BEFORE INSERT/UPDATE trigger sets updated_at and a CHECK-like audit field; a bulk ETL remains set-based, while a procedure owns an explicitly logged multi-statement archival transaction.
Stored Procedures, User-Defined Functions & Triggers: failure mode and production response
Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution. Track Rows per call as "prefer set-based processing to row-by-row PL loops", and make Volatility visible to the dataset owner.
How it works, step by step
Step 1 of 41. Choose scope
Pure expression, transaction, or invariant. Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction.
- Rows per call
- prefer set-based processing to row-by-row PL loops
- UDFs
- User functions with scalar or set inputs and declared volatility that can participate in queries and indexes.
When to use it & trade-offs
What it is
Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction. A BEFORE INSERT/UPDATE trigger sets updated_at and a CHECK-like audit field; a bulk ETL remains set-based, while a procedure owns an explicitly logged multi-statement archival transaction.
Why it matters: Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic. Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.
When to reach for it
- Push logic into the database carefully with procedures, UDFs, and triggers - knowing the ops trade-offs.
- Choose Procedures because it provides this capability: Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support.
- Keep evidence for Rows per call at "prefer set-based processing to row-by-row PL loops" so the last production stage-Version and observe - migration, timings, and side-effect counts-remains testable.
When to avoid or be careful
- Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
- Do not conflate UDFs with Procedures. The second concept contributes different behavior: User functions with scalar or set inputs and declared volatility that can participate in queries and indexes.
- Release only after this guardrail is in place: Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.
Procedures / UDFs / Triggers - roles, limits, and operating cost
Strengths: Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support. Stored procedures can manage transactions and multi-step writes, UDFs compute a return value in SQL or another language, and triggers react to row or statement events inside the database transaction.
Watch out: Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
Strengths: User functions with scalar or set inputs and declared volatility that can participate in queries and indexes. In this workflow it supports: Control transaction - know commit and exception boundaries.
Watch out: The operating cost is real: declare IMMUTABLE/STABLE/VOLATILE correctly.
Strengths: Callbacks fired before, after, or instead of INSERT/UPDATE/DELETE at row or statement level inside a transaction. Its output can be checked with Trigger side effects.
Watch out: Operating guardrail: Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.
Interview signals & numbers to know
When this topic comes up
- “Where is the failure boundary for Procedures, and how would Rows per call reveal it?”
- “Why is UDFs distinct from this behavior: callbacks fired before, after, or instead of INSERT/UPDATE/DELETE at row or statement level inside a transaction?”
- Production clue: Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
- Design probe: Keep functions deterministic and set-based, limit triggers to invariants/audit records, version migrations with the schema, test transaction rollback behavior, and leave orchestration and external calls outside trigger execution.
Benchmarks & rules of thumb
- Rows per call
- prefer set-based processing to row-by-row PL loops
- Volatility
- declare IMMUTABLE/STABLE/VOLATILE correctly
- Trigger side effects
- one source event should not create unbounded downstream work
Common mistakes & follow-ups
Common interview mistakes
- Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
- Reporting success from a job exit code without checking Rows per call and Volatility.
- Treating this example as complete without replay, rollback, or access boundaries: A BEFORE INSERT/UPDATE trigger sets
updated_atand a CHECK-like audit field; a bulk ETL remains set-based, while a procedure owns an explicitly logged multi-statement archival transaction.
Likely interviewer follow-ups
- How would you prove this contract instead of only checking that the command ran: Server-side control-flow blocks that may manage transaction boundaries and several statements depending on database support.
- What does the recovery plan do when this occurs? Trigger side effects multiply hidden writes, a procedural loop operates one row at a time, deployment versions drift from application code, and a database outage can become the failure domain for all business logic.
- When is Procedures insufficient for Volatility, and what does adding UDFs cost in Rows per call?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Stored Procedures, User-Defined Functions (UDFs) & Triggers with data engineer interview Q&A and hands-on practice.