Last Updated : 15 Jul, 2025
PostgreSQL allows the users to extend the database functionality with the help of user-defined functions and stored procedures through various procedural language elements, which are often referred to as stored procedures.The store procedures define functions for creating triggers or custom aggregate functions.
In this article, we will explain how PostgreSQL stored procedures work, their benefits, and disadvantages, and also go through some examples of creating and using stored procedures. Let's understand the world of PostgreSQL stored procedures in depth to enhance our database functionality.
PostgreSQL Stored ProceduresStored procedures provide a means to execute multiple SQL statements in a structured and reusable way. They introduce advanced control structures and allow us to perform complex calculations, making it easier to build complex applications directly within the database. By default, PostgreSQL supports three main procedural languages: SQL, PL/pgSQL, and C. We can also add other languages, such as Perl, Python, and TCL, through extensions.
PostgreSQL divides procedural languages into two main categories:
To understand how stored procedures work in PostgreSQL, let's walk through an example. We'll create a simple stored procedure that transfers funds from one account to another in a banking system.
Step 1: Create an Accounts TableFirst, create a table called accounts
with columns for id
, name
, and balance
. This table will serve as the foundation for our stored procedure.
Query:
drop table if exists accounts;create table accounts (
id int generated by default as identity,
name varchar(100) not null,
balance dec(15, 2) not null,
primary key(id)
);insert into accounts(name, balance)
values('Raju', 10000);
insert into accounts(name, balance)
values('Nikhil', 10000);
The following query will show the table data:
select * from accounts;
Output
Stored Procedure result Step 2: Create a Stored Procedure to Transfer FundsThe following query creates a stored procedure named transfer that transfers a specified amount of money from one account to another.
create or replace procedure transfer(Step 3: Call the Stored Procedure
sender int,
receiver int,
amount dec
)
language plpgsql
as $$
begin
-- subtracting the amount from the sender's account
update accounts
set balance = balance - amount
where id = sender;-- adding the amount to the receiver's account
update accounts
set balance = balance + amount
where id = receiver;commit;
end;$$;
To call a stored procedure, you use the CALL statement as follows:
call stored_procedure_name(argument_list);
Example:
The below statement invokes the transfer stored procedure to transfer $1, 000 from Raju's account to Nikhil's account:
call transfer(1, 2, 1000);Step 4: Verify the Data
The following statement verifies the data in the accounts table after the transfer:
SELECT * FROM accounts;
Output
Stored Procedure result1 Stored Procedures vs. Functions in PostgreSQLStored procedures are often compared to functions in PostgreSQL. Here’s a quick comparison:
For example, a function in PostgreSQL would return a value, while a procedure, as demonstrated above, does not return a value but rather executes a task.
ConclusionPostgreSQL stored procedures provide a powerful tool for database administrators and developers, enabling them to perform complex operations, reuse code, and improve the efficiency of applications. By centralizing the logic on the database server, stored procedures help reduce network traffic and boost application performance. Whether we are performing fund transfers, calculating payroll, or processing orders, PostgreSQL stored procedures offer the flexibility and performance needed to streamline our operations.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4