Re: update in triggers - Mailing list pgsql-general

From Alban Hertroys
Subject Re: update in triggers
Date
Msg-id 41EE5338.8000909@magproductions.nl
Whole thread Raw
In response to update in triggers  (Jamie Deppeler <jamie@doitonce.net.au>)
List pgsql-general
Jamie Deppeler wrote:
> Trigger
> CREATE TRIGGER "new_trigger" AFTER INSERT OR UPDATE
> ON "chargeratetest" FOR EACH ROW
> EXECUTE PROCEDURE "chargeratetest"();
>
>
> function
>
> CREATE OR REPLACE FUNCTION "chargeratetest" () RETURNS trigger AS'
> begin
>
>  UPDATE chargeratetest
>  set notes=''hello''
>  where new."primary" = chargeratetest."primary";
>
>  return null;
> end;
> 'LANGUAGE 'plpgsql' IMMUTABLE CALLED ON NULL INPUT SECURITY INVOKER;

If you're only going to modify the updated/inserted record, you should
definitely take a look at RULEs (Chapter 34). They RULE for this kind of
thing ;)

I haven't used them yet, as I only knew about triggers until recently,
but you could do something like this:

CREATE RULE new_rule AS ON UPDATE
     TO chargeratetest
     DO INSTEAD
    UPDATE chargeratetest
       SET notes = 'hello'
     WHERE primary = NEW.primary;

Alban.

pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: Postgres crashed when adding a sequence column
Next
From: "Marc G. Fournier"
Date:
Subject: PostgreSQL 8.0.0 Released