Summary
This chapter covers functions and stored procedures in PostgreSQL. They should be used to create modular code that runs close to the data, reduces roundtrips between the application and the database, and isolates the application from the database layout details. Functions and procedures also define transactional units of work, and PostgreSQL provides various constructs to initiate, commit, or cancel transactions. PostgreSQL supports several procedural languages, and we focus on the two most popular ones, using multiple detailed examples: PL/Python and PL/pgSQL.The chapter concludes with advice for developers choosing between these two procedural languages, setting the foundation for discussing high-transaction-volume designs and guidelines for writing efficient transactional code.