The idea · Lab 2.7.6A PL/pgSQL FUNCTION and a PROCEDURE look almost identical on the page — both have DECLARE, BEGIN, variables, loops — but they differ in exactly the way that matters for an order-processing operation: a function always runs inside whatever transaction the caller already has open, with no COMMIT or ROLLBACK of its own allowed anywhere inside it. A procedure, called with CALL instead of SELECT, can manage its own transaction boundaries completely.
This lab builds process_order as a real procedure: it validates the order exists, decrements stock for every line item, and logs the event — and if any single line item does not have enough stock, the entire operation aborts with zero partial changes, not a half-decremented mess. That all-or-nothing guarantee is proven directly here, not just described: an order that fails partway through is checked afterward to confirm nothing at all was actually changed.
A Function: Runs Inside the Caller's Transaction
CREATE OR REPLACE FUNCTION safe_divide(a numeric, b numeric) RETURNS numeric
LANGUAGE plpgsql AS $$
BEGIN
RETURN a / b;
EXCEPTION WHEN division_by_zero THEN
RAISE NOTICE 'division by zero caught, returning NULL';
RETURN NULL;
END;
$$;
A function can 't COMMIT or ROLLBACK — whatever transaction called it is still the one in charge when the function returns.
A Procedure: Manages Its Own Transaction
CREATE OR REPLACE PROCEDURE process_order(p_order_id int)
LANGUAGE plpgsql AS $$
DECLARE r record;
BEGIN
IF NOT EXISTS (SELECT 1 FROM order_items WHERE order_id = p_order_id) THEN
RAISE EXCEPTION 'order % does not exist', p_order_id;
END IF;
FOR r IN SELECT product_id, qty FROM order_items WHERE order_id = p_order_id LOOP
UPDATE stock SET qty = qty - r.qty WHERE product_id = r.product_id AND qty >= r.qty;
IF NOT FOUND THEN
RAISE EXCEPTION 'insufficient stock for product %', r.product_id;
END IF;
END LOOP;
INSERT INTO event_log (msg) VALUES ('order ' || p_order_id || ' processed');
COMMIT;
END;
$$;
CALL process_order(1);
Called with CALL, not SELECT — procedures are not expressions, they are statements in their own right.
Why NOT FOUND Is the Right Check Here
UPDATE ... WHERE product_id = r.product_id AND qty >= r.qty either updates exactly one row (there was enough stock) or zero rows (there was not) — it can never fail with an error either way. NOT FOUND after an UPDATE is PL/pgSQL's way of asking "did that statement actually touch a row," which is exactly the signal needed to notice zero rows changed and raise a real exception instead of silently proceeding as if the decrement had succeeded.
Key concepts
Function vs Procedure
A FUNCTION is called as part of an expression (SELECT my_func(...)) and always executes inside whatever transaction is already open — it cannot COMMIT or ROLLBACK. A PROCEDURE is called as its own statement (CALL my_proc(...)) and, when it has no OUT parameters and is called outside an existing multi-statement transaction, can manage transaction boundaries directly with COMMIT and ROLLBACK inside its own body.
RAISE EXCEPTION and Automatic Rollback
An uncaught RAISE EXCEPTION inside a function or procedure propagates up like any PostgreSQL error, aborting the entire current transaction unless something further up the call chain specifically catches it with EXCEPTION WHEN. Every change made earlier in that same transaction — including earlier iterations of a loop that already succeeded — is rolled back along with it, which is exactly what gives a multi-step procedure its all-or-nothing guarantee.
What to remember
- A FUNCTION always runs inside the caller's transaction and can never COMMIT or ROLLBACK — a PostgreSQL compile-time restriction, not a style choice
- A PROCEDURE, called with CALL rather than SELECT, can manage its own transaction boundaries with COMMIT and ROLLBACK directly in its body
- NOT FOUND after an UPDATE detects zero rows changed without the statement itself erroring — the right signal to notice a failed conditional update
- An uncaught RAISE EXCEPTION aborts the entire current transaction, rolling back every earlier change in the same call — proven here against a real insufficient-stock order, not just described
- EXCEPTION WHEN inside a function catches a specific error locally, preventing it from propagating up and aborting a larger transaction that called it