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

Writing PL/Perl triggers


If you want to write trigger functions using Perl, then PL/Perl allows you to do all the good stuff that you have learned so far using PL/PgSQL. Let's rewrite an example we demonstrated in Chapter 5, PL/pgSQL Trigger Functions, in PL/Perl. Recall the simple, "Hey, I am called" trigger. The PL/Perl version of the example looks as shown in the following code. We probably don't need to provide a more complex example, as this simple example demonstrates the PL/Perl syntax in a sufficient way:

CREATE OR REPLACE FUNCTION notify_trigger_plperl() RETURNS TRIGGER 
AS $$ 
  $result = sprintf('Hi, I got %s invoked FOR %s %s %s on %s', 
                    $_TD->{name}, 
                    $_TD->{level}, 
                    $_TD->{when}, 
                    $_TD->{event}, 
                    $_TD->{table_name}
                   );
    if(($_TD->{event} cmp 'UPDATE') == 0){
      $result .= sprintf(' OLD = %s AND NEW=%s', $_TD->{old}{i}, $_TD->{new...