Stored Procedure

A stored procedure (or function) is logic stored and executed inside the database, letting you run complex operations close to the data in a single call.

Stored procedures and functions encapsulate multi-step logic — validation, calculations, multiple writes — on the database server. They run in one round trip and can enforce invariants centrally.

In PostgreSQL, functions can be written in SQL or PL/pgSQL and called from queries or APIs. They're useful for atomic operations and for logic that must always run regardless of the client.

Used judiciously, they reduce network chatter and keep critical rules next to the data; overused, they can scatter business logic, so balance is key.

Related terms

See it in practice

Kolaybase gives you PostgreSQL, auth, storage, and a REST API in minutes.

Get started