Loading Depth Study
Preparing your workspace
PostgreSQL supports functions, procedures, and triggers for reusable or automatic database-side behavior.
PostgreSQL functions can encapsulate reusable database logic and may return values or result sets. Procedures support procedural operations that can be invoked separately. Triggers automatically run when specific events occur on tables or other supported objects. These capabilities can be useful for enforcing low-level database behavior, auditing, or reusable calculations. They should be used carefully because hidden database-side logic can make a system harder to understand if overused.
A database may automatically write an audit record when a sensitive row changes.
CREATE FUNCTION active_course_count() RETURNS integer LANGUAGE sql AS $$ SELECT COUNT(*) FROM courses WHERE published = true; $$; SELECT active_course_count();
The PostgreSQL function returns the number of published courses.