Sub topic 9
Procedures and CALL
Assume you know basic SQL statements, PostgreSQL data types, tables, schemas, and roles. This chapter focuses on PostgreSQL procedures written in the SQL language.
A procedure is a named database routine that performs actions. A function usually computes and returns a value. You call a function inside an expression, such as SELECT calculate_total(). You call a procedure as a statement with CALL.
Create a procedure
The following procedure moves old orders to an archive table and then deletes them from the active table:
CREATE TABLE orders (
id integer PRIMARY KEY,
ordered_at date NOT NULL,
total numeric NOT NULL
);
CREATE TABLE orders_archive (
id integer PRIMARY KEY,
ordered_at date NOT NULL,
total numeric NOT NULL
);
CREATE PROCEDURE archive_old_orders(cutoff date)
LANGUAGE sql
AS $$
INSERT INTO orders_archive (id, ordered_at, total)
SELECT id, ordered_at, total
FROM orders
WHERE ordered_at < cutoff;
DELETE FROM orders
WHERE ordered_at < cutoff;
$$;The parameter named cutoff is available to both SQL statements in the body. The dollar-quoted string keeps the procedure body readable without requiring a separate quote around every line.
Run the procedure with CALL:
CALL archive_old_orders(date '2025-01-01');CALL does not produce a function-style return value. A procedure can have OUT or INOUT parameters, however. PostgreSQL returns those parameter values as a result row from CALL.
Transaction control
A procedure can contain transaction control statements such as COMMIT and ROLLBACK. This is one important difference from a function. Transaction control is allowed only when the procedure is called in a context that permits it, such as a top-level CALL:
CREATE PROCEDURE record_event(event_text text)
LANGUAGE sql
AS $$
INSERT INTO events (message) VALUES (event_text);
COMMIT;
$$;This example assumes that an events table already exists. Calling record_event from inside an explicit transaction block causes an error because the procedure cannot commit part of that surrounding transaction:
BEGIN;
CALL record_event('maintenance finished');
COMMIT;A procedure that does not contain COMMIT or ROLLBACK can usually be called inside a larger transaction. The archive procedure is often used this way so that its insert and delete succeed or fail together.
Procedures versus functions
A procedure cannot be used directly in SELECT, WHERE, JOIN, or an index expression. A function can be used in those locations when its return type and behavior allow it.
Procedures are useful for administrative or business operations such as archiving records, rebuilding derived data, or applying a controlled batch. Functions are a better fit for calculations, reusable queries, and data-modifying actions whose result must be consumed by SQL.
PostgreSQL identifies a procedure by its name and input parameter types. You can create overloaded procedures with the same name when their input signatures differ. Use DROP PROCEDURE with the signature when removing one:
DROP PROCEDURE archive_old_orders(date);Exercise
Why is CALL archiveoldorders(date '2025-01-01') correct, while SELECT archiveoldorders(date '2025-01-01') is not?
Show answer
archiveoldorders is a procedure, so PostgreSQL invokes it with CALL. SELECT is the invocation form for functions that return values.