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/Tcl triggers


If you want to write trigger functions using Tcl, then PL/PTcl allows you to do all the good stuff that you have learned so far, using PL/pgSQL, PL/Perl, and PL/Python. Let's rewrite an example we demonstrated in Chapter 5, PL/pgSQL Trigger Functions. Recall the simple, "Hey, I am called" trigger. This is how the PL/Tcl version of the example looks:

CREATE OR REPLACE FUNCTION notify_trigger_pltcl() RETURNS TRIGGER 
AS $$ 
  set result [format "Hi, I got %s invoked FOR %s %s %s on %s" $TG_name $TG_level $TG_when $TG_op $TG_table_name]
  if {$TG_op == "UPDATE"} { 
    append result [format " OLD = %s AND NEW=%s" $OLD(i) $NEW(i)]
      set NEW(i) [expr $OLD(i) + $NEW(i)]
      elog NOTICE $result
      return [array get NEW]
  } elseif {$TG_op == "DELETE"} {
      elog NOTICE "DELETE"
      return SKIP
    } elog NOTICE $result
  return OK
$$ LANGUAGE pltcl;

CREATE TABLE notify_test_pltcl(i int);

CREATE  TRIGGER notify_insert_pltcl_trigger
  BEFORE INSERT OR UPDATE...