Re: Simple row serialization?

From: "Adam Rich" <adam(dot)r(at)sbcglobal(dot)net>
To: "'Ivan Voras'" <ivoras(at)freebsd(dot)org>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Simple row serialization?
Date: 2008-01-26 20:14:17
Message-ID: 011601c86058$0a862090$1f9261b0$@r@sbcglobal.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> I'd like to implement some simple data logging via triggers on a small
> number of infrequently updated tables and I'm wondering if there are
> some helpful functions, plugins or idioms that would serialize a row

If you're familiar with perl, you can try PL/Perl.

http://www.postgresql.org/docs/8.2/interactive/plperl-triggers.html

Here's an example (untested). If you're using quotes and colons as delimeters,
you may also need to escape those in your data.

CREATE OR REPLACE FUNCTION log_change() RETURNS trigger AS $$
my ($old_serialized, $new_serialized);

foreach my $col (keys %{$_TD->{old}}) {
$old_serialized .= "'" . $col ."':'" . $_TD->{old}{$col} . "',";
}
foreach my $col (keys %{$_TD->{new}}) {
$new_serialized .= "'" . $col ."':'" . $_TD->{new}{$col} . "',";
}

my $qry = spi_prepare('insert into log_tbl values ($1,$2)', VARCHAR, VARCHAR);
spi_exec_prepared($qry, $old_serialized, $new_serialized);
spi_freeplan($qry);

return;
$$ LANGUAGE plperl;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ivan Voras 2008-01-26 20:21:11 Re: Simple row serialization?
Previous Message Ivan Voras 2008-01-26 18:39:28 Simple row serialization?