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"} ...

Get PostgreSQL Server Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.