Re: Simple row serialization?

Поиск
Список
Период
Сортировка
От Adam Rich
Тема Re: Simple row serialization?
Дата
Msg-id 011601c86058$0a862090$1f9261b0$@r@sbcglobal.net
обсуждение исходный текст
Ответ на Simple row serialization?  (Ivan Voras <ivoras@freebsd.org>)
Список 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;




В списке pgsql-general по дате отправления:

Предыдущее
От: Ivan Voras
Дата:
Сообщение: Simple row serialization?
Следующее
От: Ivan Voras
Дата:
Сообщение: Re: Simple row serialization?