Thread: Can I create working trigger on view

Can I create working trigger on view

From
Акулов Александр
Date:
Hello!
Please, help me!

Can I create working trigger on view?

The problem is: I need a plpgsql function that execute on insert (or update, or delete) into view and knows the *OLD*
and*NEW*. (Number of fields can be more than 16)   
 
Something like this:
 Create Sequence id; Create Table t1(id1 int4, last_name varchar(32), time_create_t1); Create Table t2(id2 int4,
first_namevarchar(32), time_create_t2); Create View name as   Select * from t1 a, t2 b where a.id1=b.id2; Create Rule
rl_ins_nameason INSERT to name do instead nothing; -- without it trigger is not allowed Create Function fn_ins_name()
returnsopaque as ' Declare   v_time_create timestamp; Begin   v_tm_create:=current_timestamp;   Insert into t1(id1,
last_name,time_create_t1)           values(nextval(''id''), new.last_name, v_time_create);   Insert into t2(id2,
first_name,time_create_t2)           values(currval(''id''), new.first_name, v_time_create);   return null; End; '
Language'plpgsql'; Create Trigger tr_ins_name before INSERT on now_cis_user for each row execute procedure fn_ins_name;
This example is very simple. The trigger is not fired.
 

Sorry for my English.
--
Thanks for everybody.
Acue.



Re: Can I create working trigger on view

From
"Josh Berkus"
Date:
Acue,

> Can I create working trigger on view?
> 
> The problem is:
>   I need a plpgsql function that execute on insert (or update,
>   or delete) into view and knows the *OLD* and *NEW*.
>   (Number of fields can be more than 16)

No.   Create a RULE instead, which can be created on a view:
http://www.us.postgresql.org/users-lounge/docs/7.2/postgres/rules.html

BTW, you can increase the number of parameters accepted by functions by
re-compiling postgres.  Also, 7.3 will have 32 as the default.

-Josh Berkus


Re: Can I create working trigger on view

From
Tom Lane
Date:
"Acue" <akulov@ien.ru> writes:
> Can I create working trigger on view?

Not usefully.  No tuple will ever actually be inserted into the view,
therefore the trigger will never fire.
        regards, tom lane