Book Image

PostgreSQL Server Programming - Second Edition

Book Image

PostgreSQL Server Programming - Second Edition

Overview of this book

Table of Contents (21 chapters)
PostgreSQL Server Programming Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Chapter 5. PL/pgSQL Trigger Functions

While it is generally a good practice to keep related code together and avoid hidden actions as part of the main application code workflows, there are valid cases where it is a good practice to add some kind of general or cross-application functionality to the database using automated actions, which happen each and every time a table is modified. That is, actions are part of your data model and not your application code and you want to be sure that it is not possible to forget or bypass them, which is similar to how constraints make it impossible to insert invalid data.

The method used to add automated function calls to a table modifying event is called a trigger. Triggers are especially useful for the cases where there are multiple different client applications—possibly from different sources and using different programming styles—accessing the same data using multiple different functions or simple SQL.

From a standard's perspective, triggers are standardized...