pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedTriggers, plpgsql, etc.
2+ messages / 2 participants
[nested] [flat]
* Triggers, plpgsql, etc.
@ 1999-01-15 12:10 Christophe Labouisse <labouiss@club-internet.fr>
0 siblings, 1 reply; 2+ messages in thread
From: Christophe Labouisse @ 1999-01-15 12:10 UTC (permalink / raw)
To: pgsql-sql
I want to have a « date of the last update » field in a table. So I
create a table with datetime field (liens_maj) and a default value to
'now'.
As shown below it works fine.
lumiere=> insert into liens (liens_nom,liens_url) values ('Ga','GAbuzo');
INSERT 1009600 1
lumiere=> select * from liens;
liens_id|liens_nom|liens_url|liens_commentaire|liens_maj
--------+---------+---------+-----------------+----------------------------
1|Ga |GAbuzo | |Thu Jan 14 13:29:35 1999 CET
(1 row)
To update automatically this field when the row is updated I thought
of a trigger calling a plpgsql function :
create trigger liens_maj_trig after update
on liens for each row execute procedure liens_maj_fun();
create function liens_maj_fun () returns opaque as '
begin
update liens set liens_maj=''now'' where liens_id=old.liens_id;
return new;
end;
' language 'plpgsql';
When I try to update a record I get the following error :
lumiere=> update liens set liens_nom='zzz' where liens_id=1;
ERROR: There is no operator '=$' for types 'int4' and 'int4'
You will either have to retype this query using an explicit cast,
or you will have to define the operator using CREATE OPERATOR
Any idea ?
Christophe Labouisse : Cinéma, typographie, Unix
labouiss@club-internet.fr http://gabuzo.home.ml.org/
Le cinéma en Lumière : http://www.lumiere.org/
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: [SQL] Triggers, plpgsql, etc.
@ 1999-01-15 17:36 Jan Wieck <jwieck@debis.com>
parent: Christophe Labouisse <labouiss@club-internet.fr>
0 siblings, 0 replies; 2+ messages in thread
From: jwieck@debis.com @ 1999-01-15 17:36 UTC (permalink / raw)
To: labouiss@club-internet.fr; +Cc: pgsql-sql
Christophe Labouisse wrote:
>
> I want to have a =AB date of the last update =BB field in a table. So I
> create a table with datetime field (liens_maj) and a default value to
> 'now'.
>
> As shown below it works fine.
>
> lumiere=3D> insert into liens (liens_nom,liens_url) values ('Ga','GAbuzo'=
> );
> INSERT 1009600 1
> lumiere=3D> select * from liens;
> liens_id|liens_nom|liens_url|liens_commentaire|liens_maj
> --------+---------+---------+-----------------+--------------------------=
> --
> 1|Ga |GAbuzo | |Thu Jan 14 13:29:35 1999 C=
> ET
> (1 row)
>
>
> To update automatically this field when the row is updated I thought
> of a trigger calling a plpgsql function :
Good idea! Maybe you want to force the initial value to the
actual time too, so it will allways be the time of the insert
and not only the fallback 'default' and the user isn't able
to insert anything else.
Also the job of a trigger.
>
> create trigger liens_maj_trig after update
> on liens for each row execute procedure liens_maj_fun();
>
>
> create function liens_maj_fun () returns opaque as '
> begin
> update liens set liens_maj=3D''now'' where liens_id=3Dold.liens_id;
> return new;
> end;
> ' language 'plpgsql';
>
> When I try to update a record I get the following error :
>
> lumiere=3D> update liens set liens_nom=3D'zzz' where liens_id=3D1;
> ERROR: There is no operator '=3D$' for types 'int4' and 'int4'
> You will either have to retype this query using an explicit cast,
> or you will have to define the operator using CREATE OPERATOR
Lucky you :-)
I expected a final crash of the backend in this case (and got
it on test), because the trigger procedure itself does
exactly the operation that triggers it. This is an endless
recursion!
The following is the right solution:
CREATE FUNCTION liens_maj_fun() RETURNS opaque AS '
BEGIN
new.liens_maj := ''now'';
RETURN new;
END;
' LANGUAGE 'plpgsql';
CREATE TRIGGER liens_maj_trig BEFORE [INSERT OR] UPDATE TO liens
FOR EACH ROW EXECUTE PROCEDURE liens_maj_fun();
Why? A trigger procedure that is fired BEFORE can replace
values in NEW just before it is put into the database.
Jan
--
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#======================================== jwieck@debis.com (Jan Wieck) #
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~1999-01-15 17:36 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-01-15 12:10 Triggers, plpgsql, etc. Christophe Labouisse <labouiss@club-internet.fr>
1999-01-15 17:36 ` Jan Wieck <jwieck@debis.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox