Database Functions and Triggers
Encapsulate logic in Postgres functions and automate reactions to data changes with triggers, all callable from Supabase.
Database Functions and Triggers is a free Supabase Backend as a Service lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Supabase Backend as a Service learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Logic Inside the Database
Postgres lets you store reusable logic as functions and run code automatically on data changes with triggers. This keeps critical rules close to the data.
What Is a Database Function?
A function is named, reusable SQL or PL/pgSQL that returns a value. You can call it from queries or from the Supabase client via RPC.
Writing a Simple Function
This function returns the count of active users.
create or replace function active_user_count()
returns integer
language sql
as 'select count(*)::int from users where active';Calling It from Supabase
Use rpc to invoke a database function from the client.
const { data, error } = await supabase
.rpc('active_user_count');
console.log(data);Functions With Parameters
Functions can accept arguments, enabling parameterized server-side logic.
create or replace function posts_by(author_id bigint)
returns setof posts
language sql
as 'select * from posts where user_id = author_id';What Is a Trigger?
A trigger runs a function automatically BEFORE or AFTER an INSERT, UPDATE, or DELETE on a table.
A Trigger Function
Trigger functions return trigger and access the new row via NEW.
create or replace function set_updated_at()
returns trigger
language plpgsql
as 'begin new.updated_at = now(); return new; end;';Attaching the Trigger
Bind the function to a table and event.
create trigger touch_updated
before update on posts
for each row
execute function set_updated_at();Common Use Cases
Triggers shine for:
- Auto-setting timestamps
- Maintaining denormalized counts
- Writing audit log rows
- Creating a profile row when a user signs up
Keep Triggers Lightweight
Triggers run inside the transaction, so heavy work slows every write. Offload long tasks to Edge Functions or queues instead.
function isLightweight(ms) { return ms < 10; }
console.log(isLightweight(4));Putting It Together
Use functions for reusable, callable logic and triggers for automatic reactions to changes, while keeping trigger work small and fast.
Quick Check
Test your understanding of functions and triggers.
Recap
You learned to write callable database functions, invoke them with rpc, and automate logic with triggers using NEW, while keeping trigger work lightweight.
Frequently asked questions
Is the “Database Functions and Triggers” lesson free?
Yes — the full text of “Database Functions and Triggers” is free to read here on the web, and the Supabase Backend as a Service course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Supabase Backend as a Service course, upgrade to CoddyKit PRO.
What will I learn in “Database Functions and Triggers”?
Encapsulate logic in Postgres functions and automate reactions to data changes with triggers, all callable from Supabase. You practise Supabase Backend as a Service with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Supabase Backend as a Service?
No prior experience is required. Supabase Backend as a Service on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Database Functions and Triggers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Supabase Backend as a Service lesson?
Yes. Every Supabase Backend as a Service lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Advanced SQL Queries & Joins
- Database Indexing for Performance
- Database Functions and Triggers