Re: Fwd: Trigger on VIEW not firing

From: Massimo Costantini <massimo(dot)costantini(at)gmail(dot)com>
To: Beena Emerson <memissemerson(at)gmail(dot)com>
Cc: Ian Lawrence Barwick <barwick(at)gmail(dot)com>, PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Fwd: Trigger on VIEW not firing
Date: 2013-07-30 13:54:53
Message-ID: CAO_q5tvVtNHmnXZ2n_93UHt242jQSnku3U19q=UbtsiJc4-9Tw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgeu-general pgsql-general

ok, thank to all

On Tue, Jul 30, 2013 at 3:30 PM, Beena Emerson <memissemerson(at)gmail(dot)com>wrote:

> Hi again,
>
> IIUC you want to update the alarm table only when the speed limit is above
> 100. You cannot achieve it by the view and triggers you have written here
> because the trigger will be fired even for values < 100
>
> =# INSERT INTO speedv VALUES (1, 'test', 10);
> INSERT 0 1
>
> =# SELECT * FROM speedv;
> id | type | speed
> ----+------+-------
> (0 rows)
>
> =# SELECT * FROM car;
> id | type | speed
> ----+------+-------
> (0 rows)
>
> =# SELECT * FROM alarm;
> name | id | type | init | fired | t_end | t_user
> ------+----+-------+----------------------------+-------+-------+--------
> test | 0 | SPEED | 2013-07-30 18:08:01.006979 | | |
> test | 0 | SPEED | 2013-07-30 18:20:00.73507 | | |
> (2 rows)
>
> If you want to update the alarm table for speed > 100 then use an if else
> clause in the trigger function:
>
> CREATE OR REPLACE FUNCTION update_alarm_view() RETURNS TRIGGER AS
> $alarm_tg$
> BEGIN
> IF (new.speed > 100) THEN
> IF (TG_OP = 'UPDATE') THEN
> INSERT INTO alarm VALUES(NEW.type,
> 0,'SPEED',now(),NULL,NULL,'');
> ELSEIF (TG_OP = 'INSERT') THEN
> INSERT INTO alarm VALUES(NEW.type,
> 0,'SPEED',now(),NULL,NULL,'');
> END IF;
> END IF;
> RETURN new;
> END;
> $alarm_tg$ LANGUAGE plpgsql;
>
> And write the trigger on the car table.
>
>
>
> --
>
> Beena Emerson
>
>

In response to

Browse pgeu-general by date

  From Date Subject
Next Message Wan Mohd Hashim Wan Yusoff 2013-08-02 08:02:02 Streaming replication
Previous Message Beena Emerson 2013-07-30 13:30:41 Re: Fwd: Trigger on VIEW not firing

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2013-07-30 14:42:40 Re: to_char with locale decimal separator
Previous Message Beena Emerson 2013-07-30 13:30:41 Re: Fwd: Trigger on VIEW not firing