Views, Functions, and Stored Procedures
Table of Contents
Introduction: The Copy-Paste Query Problem
Views: Named, Reusable Queries
Creating a View
Modifying Views
Updatable Views
Views with SECURITY DEFINER
Materialised Views: Cached Query Results
Refreshing Materialised Views
Scheduling Refreshes
PostgreSQL Functions (User-Defined Functions)
Simple SQL Function
Function That Returns a Table
Function Volatility
Marker
Meaning
Can Be Cached?
Functions with OUT Parameters
Stored Procedures
Triggers: Automatic Database Reactions
Types of Triggers
Auto-Update updated_at Timestamp
updated_at TimestampAudit Log Trigger
Validation Trigger
PL/pgSQL: Writing Procedural Code in PostgreSQL
Variables and Control Flow
Looping
Exception Handling
Common Patterns and Use Cases
Soft Delete Pattern
Slug Generation
Performance Considerations
Object
Performance Notes
Managing Database Code
What I Learned About Database Logic
Next Steps
Last updated