Difficulty: Intermediate
What are stored procedures, and what are the pros and cons of putting business logic in them instead of in your application code?
A stored procedure is a named block of SQL plus procedural logic (variables, loops, conditionals) that is saved inside the database and executed there with a single call, such as CALL transfer_funds(...). The main benefits are fewer network round trips, a single enforced path for critical operations, and tighter permissions. The main costs are logic that is harder to test, version and scale, and code that ties you to one database vendor.
The client sends one statement, like CALL place_order(42, 'SKU-9'). The database parses it, runs every statement inside the procedure on the server, and returns only the final result. If the same work were done from the application, each SELECT, UPDATE and INSERT would be a separate round trip over the network.
Each database has its own language:
CREATE FUNCTION returns a value; CREATE PROCEDURE (added in PostgreSQL 11) is invoked with CALL and can run COMMIT or ROLLBACK inside its body.CREATE PROCEDURE with its own SQL/PSM style syntax.debit_wallet() instead of writing to the wallets table directly, rules like "balance can never go negative" cannot be bypassed by a buggy microservice.GRANT EXECUTE on a procedure while denying direct table access. In PostgreSQL a SECURITY DEFINER function runs with the owner's rights, so the caller never needs table permissions.| Good fit | Poor fit |
|---|---|
| Atomic multi-row money movements | General CRUD behind a REST API |
| Bulk data maintenance jobs | Logic that changes every sprint |
| Legacy enterprise systems built around them | Logic that calls external APIs |
| Exposing a narrow, audited API to reporting users | Teams with no DBA or migration discipline |
Most modern product companies keep business rules in the application and use procedures selectively, for example a single function that atomically moves money between two ledger rows. Banks and older enterprise systems often still run large parts of their logic as procedures.
EXECUTE (PostgreSQL) or sp_executesql without parameters is still injectable.-- Amounts stored in paise to avoid floating point errors
CREATE OR REPLACE PROCEDURE transfer_funds(
p_from BIGINT, p_to BIGINT, p_amount_paise BIGINT
)
LANGUAGE plpgsql
AS $
BEGIN
-- Lock both rows in a fixed order to avoid deadlocks
PERFORM 1 FROM wallets
WHERE id IN (p_from, p_to)
ORDER BY id
FOR UPDATE;
UPDATE wallets SET balance_paise = balance_paise - p_amount_paise
WHERE id = p_from AND balance_paise >= p_amount_paise;
IF NOT FOUND THEN
RAISE EXCEPTION 'Insufficient balance in wallet %', p_from;
END IF;
UPDATE wallets SET balance_paise = balance_paise + p_amount_paise
WHERE id = p_to;
END;
$;
-- One round trip from the app
CALL transfer_funds(101, 202, 50000); -- Rs 500
The debit, credit and balance check run as one server-side unit. If the balance is too low, the exception rolls back the whole call.
REVOKE ALL ON wallets FROM app_user;
GRANT EXECUTE ON PROCEDURE transfer_funds(BIGINT, BIGINT, BIGINT) TO app_user;
-- For this to work without table rights, the routine
-- must run with its owner's privileges:
ALTER PROCEDURE transfer_funds(BIGINT, BIGINT, BIGINT) SECURITY DEFINER;
ALTER PROCEDURE transfer_funds(BIGINT, BIGINT, BIGINT) SET search_path = public;
The app role can only move money through the procedure. Pinning search_path is the standard hardening step for SECURITY DEFINER routines.
Stored Procedures, PL/pgSQL, Network Round Trips, SECURITY DEFINER, Vendor Lock-in