Re: Dynamic Log tigger (plpgsql)

Lists: pgsql-general
From: Noah Heusser <noah(at)heussers(dot)ch>
To: pgsql-general(at)postgresql(dot)org
Subject: Dynamic Log tigger (plpgsql)
Date: 2007-06-16 11:26:57
Message-ID: 4673C901.2000806@heussers.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi

I want to implement a trigger-function witch can fill the following table.
Each data manipulation (INSERT, UPDATE or DELETE) gets logged.
The function should work as trigger on diffrent tables.

CREATE TABLE logtable (
operation CHAR(6) CHECK (change_type IN ('DELETE', 'INSERT', 'UPDATE')),
tablename VARCHAR,
rowid INTEGER, -
touched_columns VARCHAR[]
);

My Problem is in the last Column (touched_columns).
If it was an UPDATE Operation, I just need to know witch columns changed. (I am not iterrestet in the old or new value)
=> IF OLD.columnName != NEW.columnName, it has changed.

My Question:
How can I do "OLD.columnName != NEW.columnName" if I don't know what the
columnNames are at Compile Time?
I have the columnName in a variable.

Thx for help.
Noah


From: "Sibte Abbas" <sibtay(at)gmail(dot)com>
To: "Noah Heusser" <noah(at)heussers(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic Log tigger (plpgsql)
Date: 2007-06-19 14:10:23
Message-ID: bd6a35510706190710m76fa9e9eh661f591c527bc796@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 6/16/07, Noah Heusser <noah(at)heussers(dot)ch> wrote:
> Hi
>
> I want to implement a trigger-function witch can fill the following table.
> Each data manipulation (INSERT, UPDATE or DELETE) gets logged.
> The function should work as trigger on diffrent tables.
>
> CREATE TABLE logtable (
> operation CHAR(6) CHECK (change_type IN ('DELETE', 'INSERT', 'UPDATE')),
> tablename VARCHAR,
> rowid INTEGER, -
> touched_columns VARCHAR[]
> );
>
> My Problem is in the last Column (touched_columns).
> If it was an UPDATE Operation, I just need to know witch columns changed. (I am not iterrestet in the old or new value)
> => IF OLD.columnName != NEW.columnName, it has changed.
>
>
>
> My Question:
> How can I do "OLD.columnName != NEW.columnName" if I don't know what the
> columnNames are at Compile Time?
> I have the columnName in a variable.
>
>
> Thx for help.
> Noah
>

Are you trying to do this from a plpgsql function? If so then I think
you should try to do this from a C function.

With C functions you will get more control over the new and old
versions of the tuple since you get their pointers via
TriggerData->tg_trigtuple (old tuple) and TriggerData->tg_newtuple
(new tuple).

--
Sibte Abbas
EnterpriseDB http://www.enterprisedb.com


From: Noah Heusser <noah(at)heussers(dot)ch>
To: Sibte Abbas <sibtay(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic Log tigger (plpgsql)
Date: 2007-06-19 15:55:30
Message-ID: 4677FC72.7040107@heussers.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


>> How can I do "OLD.columnName != NEW.columnName" if I don't know what the
>> columnNames are at Compile Time?
>> I have the columnName in a variable.
>
> Are you trying to do this from a plpgsql function? If so then I think
> you should try to do this from a C function.
>
> With C functions you will get more control over the new and old
> versions of the tuple since you get their pointers via
> TriggerData->tg_trigtuple (old tuple) and TriggerData->tg_newtuple
> (new tuple).
>
I think this would work. And if there is no other possibility i will do
that.
But as I saw, it is necessary to have Sysadmin rights, if you want to
add a C function.
If possible i want to do it as a regular DB-Owner.


From: Noah Heusser <pgsql-general(at)heussers(dot)ch>
To: Sibte Abbas <sibtay(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic Log tigger (plpgsql)
Date: 2007-06-19 15:58:24
Message-ID: 4677FD20.6090207@heussers.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


>> How can I do "OLD.columnName != NEW.columnName" if I don't know what the
>> columnNames are at Compile Time?
>> I have the columnName in a variable.
>
> Are you trying to do this from a plpgsql function? If so then I think
> you should try to do this from a C function.
>
> With C functions you will get more control over the new and old
> versions of the tuple since you get their pointers via
> TriggerData->tg_trigtuple (old tuple) and TriggerData->tg_newtuple
> (new tuple).
>
I think this would work. And if there is no other possibility i will do
that.
But as I saw, it is necessary to have Sysadmin rights, if you want to
add a C function.
If possible i want to do it as a regular DB-Owner.


From: "Sergey Konoplev" <gray(dot)ru(at)gmail(dot)com>
To: "Noah Heusser" <noah(at)heussers(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic Log tigger (plpgsql)
Date: 2007-06-20 07:04:04
Message-ID: c3a7de1f0706200004y6f43e600reba765590872bec3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> My Question:
> How can I do "OLD.columnName != NEW.columnName" if I don't know what the
> columnNames are at Compile Time?
> I have the columnName in a variable.
>

I suggest you use plpython. In this case you'll be able to do it.

TD['old'][colNameVar] != TD['new'][colNameVar]

--
Regards,
Sergey Konoplev


From: Noah Heusser <pgsql-general(at)heussers(dot)ch>
To: Sergey Konoplev <gray(dot)ru(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic Log tigger (plpgsql)
Date: 2007-06-21 22:50:11
Message-ID: 467B00A3.1010802@heussers.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Sergey Konoplev schrieb:
>> My Question:
>> How can I do "OLD.columnName != NEW.columnName" if I don't know what the
>> columnNames are at Compile Time?
>> I have the columnName in a variable.
>>
>
> I suggest you use plpython. In this case you'll be able to do it.
>
> TD['old'][colNameVar] != TD['new'][colNameVar]
>
thx, you are right. These Languages are trusted like pgsql,

Did it in Perl:

foreach $key (keys %{$_TD->{old}}) {
if($_TD->{old}{$key} ne $_TD->{new}{$key}){

}
}


From: Jim Nasby <decibel(at)decibel(dot)org>
To: Noah Heusser <noah(at)heussers(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic Log tigger (plpgsql)
Date: 2007-06-22 21:29:28
Message-ID: 09B1FAEB-3361-4601-B3B7-A8B985EA7246@decibel.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Jun 16, 2007, at 6:26 AM, Noah Heusser wrote:
> I want to implement a trigger-function witch can fill the following
> table.
> Each data manipulation (INSERT, UPDATE or DELETE) gets logged.
> The function should work as trigger on diffrent tables.
>
> CREATE TABLE logtable (
> operation CHAR(6) CHECK (change_type IN ('DELETE',
> 'INSERT', 'UPDATE')),

Note that that field will take 12 bytes in 8.2, and assuming that
varvarlena is in 8.3, 8 bytes there (or is varvarlena byte-aligned?)

You might be better going with "char" (with the double-quotes) and
'D', 'I', and 'U'.
--
Jim Nasby jim(at)nasby(dot)net
EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)