← Back to SQL, page 13

sql-0243 · Medium mid

What is a stored procedure, and how does a PostgreSQL procedure differ from a function?

A stored procedure is code stored and run inside the database, called by name. In PostgreSQL, a FUNCTION returns a value and can be used inside queries, but cannot commit or roll back. A PROCEDURE (PostgreSQL 11+) is invoked with CALL, returns no value in the usual way, and can control transactions with COMMIT inside its body. SQLite has no stored procedures; logic lives in the application or in application-defined functions.

Full answer

A function used in a query:

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');
sql
CREATE FUNCTION annual_bonus(salary int) RETURNS int LANGUAGE sql IMMUTABLE AS $$ SELECT salary / 10 $$;
SELECT name, annual_bonus(salary) AS bonus FROM employees WHERE id <= 2 ORDER BY id;
namebonus
Asha12000
Ravi9500

A procedure that applies a raise, called with CALL:

sql
CREATE PROCEDURE give_raise(dept int, pct int) LANGUAGE plpgsql AS $$
BEGIN
  UPDATE employees SET salary = salary + salary * pct / 100 WHERE dept_id = dept;
END $$;
CALL give_raise(2, 10);
SELECT name, salary FROM employees WHERE dept_id = 2 ORDER BY id;
namesalary
Tom77000
Sara68200

Reasons teams use them: fewer round trips for multi-step operations, permissions that let users run the procedure without direct table access (with SECURITY DEFINER), and logic shared by several applications.

Reasons to be careful: they are harder to version, test and debug than application code, and they tie the system to one engine's language. Many teams keep only data-heavy operations, such as batch maintenance, in the database.

SQLite instead lets the host program register functions (for example Python's create_function), which run in the application process.

sql
CREATE TABLE departments (
  id   INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);
CREATE TABLE employees (
  id         INTEGER PRIMARY KEY,
  name       TEXT NOT NULL,
  dept_id    INTEGER REFERENCES departments(id),
  manager_id INTEGER REFERENCES employees(id),
  salary     INTEGER,
  hire_date  TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
  (1,'Asha', 1,NULL,120000,'2019-03-01'),
  (2,'Ravi', 1,1,    95000,'2020-07-15'),
  (3,'Mei',  1,1,    95000,'2021-01-10'),
  (4,'Tom',  2,1,    70000,'2022-05-20'),
  (5,'Sara', 2,4,    62000,'2023-02-01'),
  (6,'Diego',3,1,    55000,'2021-11-30'),
  (7,'Lena', NULL,4, NULL,  '2024-06-03'),
  (8,'Omar', 3,6,    48000,'2024-09-09');

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • Stored procedures are named code run in the database
  • PostgreSQL functions return values and work inside queries
  • Procedures use CALL and can COMMIT inside
  • SQLite has no stored procedures

Common mistakes

  • Putting every business rule in stored code without tests
  • Expecting a function to COMMIT in PostgreSQL

Follow-ups

  • What does SECURITY DEFINER do?
  • When would a procedure beat application code?

Tips

  • Contrast function and procedure clearly