Problem while logging primary key of updated field.

From: "Gurunandan R(dot) Bhat" <grbhat(at)exocore(dot)com>
To:
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Problem while logging primary key of updated field.
Date: 2001-09-21 20:19:01
Message-ID: Pine.LNX.4.33.0109220139470.1326-100000@suman.greenfields.universe
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Hi,

I want to log the primary key of a table whenever a new row is inserted.
To achieve this, I created the following function as trigger on update.
However I get the following error on inserts:

ERROR: NEW used in non-rule query

Here is my function:
The first select inside the function body gives me the name of the primary
key field.

-----------------------------------------------------------------------
create function log_insert() returns opaque as '
declare
currtime timestamp := ''now'';
pkeyname name;
query text;
begin
select into pkeyname c.attname
from pg_class a,
pg_index b,
pg_attribute c
where
a.relname = TG_RELNAME and
a.oid = b.indrelid and
a.oid = c.attrelid and
b.indkey[0] = c.attnum and
b.indisprimary=''t'';
query := ''insert into logtable (pkvalue, tablename, time)
values (NEW.'' ||
quote_ident(pkeyname) ||
'', '' ||
quote_ident(TG_RELNAME) ||
'', '' ||
quote_ident(currtime) ||
'');'';
execute query;
return null;
end;
' language 'plpgsql';
----------------------------------------------------------------------

I would be extremely grateful for any help. I might add that when I print
the dynamic query with a "raise notice" the query looks fine.

Thanks in advance

Gurunandan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nick Fankhauser 2001-09-21 20:41:14 Re: connection fails
Previous Message paula 2001-09-21 19:25:25 connection fails