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

Controlling when a trigger is called


While it is relatively easy to perform trigger actions conditionally inside the PL/pgSQL trigger function, it is often more efficient to skip invoking the trigger altogether. The performance effects of firing a trigger are not generally noticed when only a few events are fired. However, if you are bulk loading data or updating large portions of your table, the cumulative effects can certainly be felt. To avoid the overhead, it's best to call the trigger function only when it is actually needed.

There are two ways to narrow down when a trigger will be called in the CREATE TRIGGER command itself.

So, use the same syntax once more but with all the options this time:

CREATE TRIGGER name
    { BEFORE | AFTER | INSTEAD OF } { event [ OR event ... ] }
    [ OF column_name  [ OR column_name ... ] ] ON table_name
    [ FOR [ EACH ] { ROW | STATEMENT } ]
    [ WHEN ( condition ) ]
    EXECUTE PROCEDURE function_name ( arguments )

You can use the WHEN clause to only...