Database Management
PostgreSQL
Subjective
Sep 25, 2025
What are PostgreSQL stored procedures and how do they differ from functions?
Detailed Explanation
Stored Procedures (PostgreSQL 11+):
• Can manage transactions
• Support COMMIT/ROLLBACK
• Called with CALL statement
• Cannot return values directly
Functions:
• Cannot manage transactions
• Return values or tables
• Called in SELECT statements
• More flexible for calculations
Example Procedure:
CREATE PROCEDURE transfer_funds(
sender_id INT,
receiver_id INT,
amount DECIMAL
)
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE accounts SET balance = balance - amount WHERE id = sender_id;
UPDATE accounts SET balance = balance + amount WHERE id = receiver_id;
COMMIT;
END;
$$;Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts