Supabase Database Functions Guide — Serverless Logic with RPC and PL/pgSQL Supabase Database Functions let you move complex business logic into PostgreSQL and call it from Flutter via rpc() — no Edge Function deployment needed. Why Use Database Functions Atomicity : Multi-table updates in a single transaction Performance : Fewer network round-trips, lower latency Security : Encapsulate logic without bypassing RLS Simplicity : Skip Deno deployment entirely Basic Function CREATE OR REPLACE FUNCTION calculate_streak ( user_id UUID ) RETURNS INTEGER LANGUAGE plpgsql SECURITY DEFINER AS $$ DECLARE streak_count INTEGER : = 0 ; check_date DATE : = CURRENT_DATE ; has_entry BOOLEAN ; BEGIN LOOP SELECT EXISTS ( SELECT 1 FROM journal_entries WHERE user_id = $ 1 AND DATE ( created_at ) = check_date ) INTO has_entry ; EXIT WHEN NOT has_entry ; streak_count : = streak_count + 1 ; check_date : = check_date - INTERVAL '1 day' ; END LOOP ; RETURN streak_count ; END ; $$ ; Enter fullscreen mode Exit fullscreen mode Call from…