13. Stored Procedures and Functions
PostgreSQL supports server-side logic using functions and procedures.
Decision Guide
| Need | Prefer | Notes |
|---|---|---|
| Use in SELECT / computed columns | Function | Functions return values and can be used in queries |
| Run multi-step admin workflows | Procedure (CALL) | Procedures can be used for operational tasks |
| Pure SQL transformation | SQL-language function | Often faster and simpler than PL/pgSQL |
| Complex control flow | PL/pgSQL | IF/LOOP/EXCEPTION handling |
Lessons
What's Next
- Continue to 14. Triggers