0Pricing
SQL Academy · Lesson

PL/pgSQL Function Basics

Write PL/pgSQL functions with parameters, RETURNS TABLE, control flow (IF, LOOP, FOREACH), and exception handling.

What Is PL/pgSQL?

PostgreSQL's built-in procedural language. SQL extended with variables, control flow, exceptions, and the ability to call queries dynamically. Used for stored procedures and trigger functions.

Function Skeleton

A simple function:

CREATE OR REPLACE FUNCTION add(a INT, b INT)
RETURNS INT AS $$
BEGIN
  RETURN a + b;
END;
$$ LANGUAGE plpgsql IMMUTABLE;

SELECT add(2, 3);   -- 5

All lessons in this course

  1. Trigger Anatomy: BEFORE/AFTER, FOR EACH ROW
  2. PL/pgSQL Function Basics
  3. DO Blocks and Anonymous Code
  4. Auditing Tables with Triggers
← Back to SQL Academy