PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
Stored Procedures in PostgreSQL are part of the SQL standard and have been introduced in PostgreSQL 11. Prior to PostgreSQL 11, there were only functions, and many developers used them similarly to how stored procedures are used in other RDBMS systems. The main distinction is that stored procedures support transaction control, meaning they can manage their own transactions by using commit and rollback.
Transaction Control: Stored procedures can manage their own transactions using COMMIT
, ROLLBACK
, and other transaction control statements, which isn't allowed in functions.
CALL Keyword: Unlike functions that are invoked using SELECT
, stored procedures are invoked using the CALL
keyword.
Return Value: Functions always return a value, while stored procedures do not have a return value.
The basic syntax for creating a stored procedure is:
CREATE OR REPLACE PROCEDURE procedure_name (parameter_list) LANGUAGE plpgsql AS $$ BEGIN -- procedure body END; $$;
Suppose we have a table called users
with columns id
, name
, and email
. We can create a stored procedure to insert a new user:
CREATE OR REPLACE PROCEDURE insert_user(p_name VARCHAR, p_email VARCHAR) LANGUAGE plpgsql AS $$ BEGIN INSERT INTO users(name, email) VALUES (p_name, p_email); COMMIT; END; $$;
To call this procedure:
CALL insert_user('John Doe', 'johndoe@example.com');
Stored procedures can be used to perform operations that modify the database data. Inside the stored procedure, you can use INSERT, UPDATE, DELETE, and other DML operations.
You can use exception handling in stored procedures to manage errors:
CREATE OR REPLACE PROCEDURE some_procedure() LANGUAGE plpgsql AS $$ BEGIN -- some operations EXCEPTION WHEN some_exception THEN -- handle exception END; $$;
Stored procedures in PostgreSQL provide a way to encapsulate business logic, enhance security, and improve performance by reducing the need to send multiple queries from the client to the server. When combined with roles and privileges, they can be a powerful tool in managing and controlling access to data in the database.
How to define a stored procedure in PostgreSQL:
CREATE PROCEDURE
statement.CREATE OR REPLACE PROCEDURE procedure_name(parameters) AS $$ -- SQL statements $$ LANGUAGE plpgsql;
PL/pgSQL stored procedures in PostgreSQL:
CREATE OR REPLACE PROCEDURE plpgsql_procedure() AS $$ BEGIN -- PL/pgSQL statements END; $$ LANGUAGE plpgsql;
Calling stored procedures in PostgreSQL:
CALL
statement.CALL procedure_name(parameters);
Parameters and arguments in PostgreSQL stored procedures:
CREATE OR REPLACE PROCEDURE param_procedure(param1 INTEGER, param2 VARCHAR) AS $$ BEGIN -- Use parameters in SQL statements END; $$ LANGUAGE plpgsql;
Returning values from stored procedures in PostgreSQL:
OUT
parameters to return values from stored procedures.CREATE OR REPLACE PROCEDURE return_procedure(IN param1 INTEGER, OUT result VARCHAR) AS $$ BEGIN -- Perform operations result := 'Value'; END; $$ LANGUAGE plpgsql;
Handling errors in PostgreSQL stored procedures:
EXCEPTION
blocks.CREATE OR REPLACE PROCEDURE error_handling_procedure() AS $$ BEGIN -- Code with error handling EXCEPTION WHEN others THEN -- Handle exception END; $$ LANGUAGE plpgsql;
Conditional logic in stored procedures in PostgreSQL:
IF
, CASE
, and other conditional constructs in stored procedures.CREATE OR REPLACE PROCEDURE conditional_procedure(IN param BOOLEAN) AS $$ BEGIN IF param THEN -- Execute if true ELSE -- Execute if false END IF; END; $$ LANGUAGE plpgsql;
Transaction management in PostgreSQL stored procedures:
BEGIN
, COMMIT
, and ROLLBACK
statements.CREATE OR REPLACE PROCEDURE transaction_procedure() AS $$ BEGIN -- Start transaction BEGIN -- SQL statements EXCEPTION WHEN others THEN -- Rollback on error ROLLBACK; END; -- Commit transaction COMMIT; END; $$ LANGUAGE plpgsql;
Debugging stored procedures in PostgreSQL:
RAISE NOTICE
and logging for debugging stored procedures.CREATE OR REPLACE PROCEDURE debug_procedure() AS $$ BEGIN -- Debugging statements RAISE NOTICE 'Debugging information'; END; $$ LANGUAGE plpgsql;
Dynamic SQL in PostgreSQL stored procedures:
CREATE OR REPLACE PROCEDURE dynamic_sql_procedure(IN table_name VARCHAR) AS $$ BEGIN EXECUTE 'SELECT * FROM ' || table_name; END; $$ LANGUAGE plpgsql;
Stored procedures and triggers in PostgreSQL:
CREATE OR REPLACE FUNCTION trigger_function() RETURNS TRIGGER AS $$ BEGIN -- Trigger logic END; $$ LANGUAGE plpgsql; CREATE TRIGGER example_trigger AFTER INSERT ON example_table FOR EACH ROW EXECUTE FUNCTION trigger_function();
Using stored procedures for batch processing in PostgreSQL:
CREATE OR REPLACE PROCEDURE batch_process_procedure() AS $$ BEGIN -- Batch processing logic END; $$ LANGUAGE plpgsql;