December 18th, 2013 by depesz | Tags: , , , , , , | No comments »
Did it help? If yes - maybe you can help me? Donate BTC to 19zPa5diT2LZqGtTi8f8bfApLn8rw9zBHx

On 11th of December, Peter Eisentraut committed patch:

PL/Perl: Add event trigger support
 
From: Dimitri Fontaine <dimitri@2ndQuadrant.fr>

In PostgreSQL 9.3 we got EVENT triggers.

To remind – these are triggers that get executed, not on change to table, but on specific event – related to general schema. Creation of new object. Altering state of the object. Dropping it.

Let's see how that works. First I need to have the language:

$ create extension plperlu;
CREATE EXTENSION

Now, I can write some simple, testing trigger:

$ create function event_test_pl() returns event_trigger as $$
>> use Data::Dumper;
>> elog( NOTICE, Dumper($_TD) );
>> $$ language plperlu;
CREATE FUNCTION
 
$ create event trigger test_before on ddl_command_start execute procedure event_test_pl();
CREATE EVENT TRIGGER
 
$ create event trigger test_after on ddl_command_end execute procedure event_test_pl();
CREATE EVENT TRIGGER

And now for the test:

$ create table z (i int4);
NOTICE:  $VAR1 = {
          'tag' => 'CREATE TABLE',
          'event' => 'ddl_command_start'
        };
 
CONTEXT:  PL/Perl function "event_test_pl"
NOTICE:  $VAR1 = {
          'tag' => 'CREATE TABLE',
          'event' => 'ddl_command_end'
        };
 
CONTEXT:  PL/Perl function "event_test_pl"
CREATE TABLE

and dropping:

$ drop table z;
NOTICE:  $VAR1 = {
          'tag' => 'DROP TABLE',
          'event' => 'ddl_command_start'
        };
 
CONTEXT:  PL/Perl function "event_test_pl"
NOTICE:  $VAR1 = {
          'tag' => 'DROP TABLE',
          'event' => 'ddl_command_end'
        };
 
CONTEXT:  PL/Perl function "event_test_pl"
DROP TABLE

OK. Works. Just like with PL/pgSQL triggers – there is not enough information passed to trigger function to be able to do something really cool, but that's just the beginning.

Of course even now we can detect what is added/dropped, by simply checking catalogs state in “ddl_command_start" and “ddl_command_end" triggers, and comparing results. Since PL/Perl has %_SHARED, it shouldn't be all that problematic:

create or replace function event_test_pl() returns event_trigger as $$
use Data::Dumper;
my $rv     = spi_exec_query("SELECT relname FROM pg_class where relkind = 'r'");
my $tables = [ map { $_->{'relname'} } @{ $rv->{'rows'} } ];
 
if ( $_TD->{'event'} eq 'ddl_command_start' ) {
    $_SHARED{'ddl_before'} = $tables;
    return 1;
}
 
my $before_as_hash = {};
$before_as_hash->{$_} = 1 for @{ $_SHARED{'ddl_before'} };
my $after_as_hash = {};
$after_as_hash->{$_} = 1 for @{ $tables };
 
for my $table ( @{ $_SHARED{'ddl_before'} } ) {
    elog( NOTICE, 'Dropped table: ' . $table ) unless $after_as_hash->{ $table };
}
for my $table ( @{ $tables } ) {
    elog( NOTICE, 'Created table: ' . $table ) unless $before_as_hash->{ $table };
}
$$ language plperlu;

This trigger can detect table creation/dropping:

$ create table depesz_test (whatever text);
NOTICE:  Created table: depesz_test
CONTEXT:  PL/Perl function "event_test_pl"
CREATE TABLE
 
$ drop table depesz_test ;
NOTICE:  Dropped table: depesz_test
CONTEXT:  PL/Perl function "event_test_pl"
DROP TABLE

Of course it's not beautiful, but it works. And, in time, we'll get more features.

Leave a comment