pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: jwieck@debis.com
To: labouiss@club-internet.fr
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Triggers, plpgsql, etc.
Date: Fri, 15 Jan 1999 18:36:15 +0100 (MET)
Message-ID: <m101DAO-000EBQC@orion.SAPserv.Hamburg.dsh.de> (raw)
In-Reply-To: <m3hftsg2i5.fsf@gabuzo.meunet>

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) #





view thread (2+ messages)

Message-ID: <m101DAO-000EBQC@orion.SAPserv.Hamburg.dsh.de>
Permalink:  ../m101DAO-000EBQC@orion.SAPserv.Hamburg.dsh.de/
Also on:    postgresql.org/message-id/m101DAO-000EBQC@orion.SAPserv.Hamburg.dsh.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: jwieck@debis.com, labouiss@club-internet.fr
  Subject: Re: [SQL] Triggers, plpgsql, etc.
  In-Reply-To: <m101DAO-000EBQC@orion.SAPserv.Hamburg.dsh.de>
  References: <m3hftsg2i5.fsf@gabuzo.meunet> <m101DAO-000EBQC@orion.SAPserv.Hamburg.dsh.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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