Define reusable functions in SQL or PL/pgSQL. Call them from any query. Functions are stored in the catalog and visible across sessions.
A function body is any SQL expression; the result is the return type you declare
Define a function as a single SQL expression. The optimizer can inline it at query planning time.
CREATE FUNCTION price_with_tax(p DECIMAL, r DECIMAL)
RETURNS DECIMAL AS p * (1 + r);
Arguments are automatically coerced to the declared parameter types following standard SQL rules.
Functions belong to a schema. GRANT and REVOKE control which roles can call them.
Multiple functions with the same name but different parameter types resolve by the best-matching signature.
Procedural control flow over Delta tables using the PostgreSQL procedural language
IF/ELSIF/ELSE, FOR loops, WHILE, CASE, and RAISE. Full procedural logic in the same file as your SQL.
Anonymous PL/pgSQL blocks with DO $$ BEGIN ... END $$; for one-off procedural execution.
CREATE PROCEDURE ... LANGUAGE plpgsql stores the body in the catalog. Call with CALL schema.proc();
IN, OUT, and INOUT parameters. Procedures can return result sets or write state back through OUT parameters.
SQL expressions and PL/pgSQL procedures, stored in the catalog, callable from any query.