Stored Procedure
ELI5 — The Vibe Check
A stored procedure is a named program you write in SQL (and sometimes a procedural language) that lives inside the database. Instead of sending multiple queries from your app, you call one procedure and the database handles the logic. Like a function, but lives in the database.
Real Talk
A stored procedure is a precompiled collection of SQL statements stored in the database. It can accept parameters, contain conditional logic, loops, and error handling. Procedures are called by name and can encapsulate complex business logic, reducing network round-trips and improving security.
Show Me The Code
CREATE PROCEDURE transfer_funds(
from_account INT,
to_account INT,
amount DECIMAL
)
LANGUAGE plpgsql AS $$
BEGIN
UPDATE accounts SET balance = balance - amount WHERE id = from_account;
UPDATE accounts SET balance = balance + amount WHERE id = to_account;
END;
$$;
When You'll Hear This
"The payment logic lives in a stored procedure." / "Stored procedures reduce the number of round-trips between the app and database."
Related Terms
Function
A function is a reusable recipe. You write the steps once, give it a name, and call it whenever you need those steps done.
Transaction
A transaction groups multiple database operations into one all-or-nothing bundle. Either ALL of them succeed, or NONE of them happen.
Trigger
A trigger is code that the database runs automatically when something happens — like automatically updating an 'updated_at' timestamp whenever a row change...
View
A view is a saved query that looks and acts like a table.