I have two tables foo
and bar
, and an after trigger
on foo
which changes something on bar
table.
create table foo(id serial primary key, _key char(128) not null);
create table bar(id bigserial primary key, _key_p char(256) not null);
So when I have a transaction, such as invoking a function like below, the trigger is fired after the transaction, not the statement or related dml action.
The trigger for foo
table:
create or replace function foo_trg_func()returns trigger as $$
declare k_p char(256);begin
k_p:=(select res from prepare_pa(new.key));
insert into bar(_key_p) values(k_p);--insert it to the bar
end $$ language plpgsql;
--
create trigger foo_trg
after insert on foo
for each row execute procedure foo_trg_func();
The sample function/transaction
create or replace function `bas`(int,character(128))returns int as $$
-- some commands
with res as (select res as "d" from c_key($1,$2)),
-- attemp to insert into foo and expect the insertion to bar too
ins as (insert into foo(_key) select d from res returning 1) --line[5]
-- check the effect of the foo_trg
select _key_p from bar,res where _key_p=res.d; --line[7]
$$ language sql
The trigger is called and data is inserted to the bar
table by foo
trigger, but after the function call, and I cannot get the result of trigger insert at line 7.
How may I do this now?
I also have to mention that it's possible to mark the trigger as instead or before, but it will cause many changes, so I wondered if it's possible to do with after
triggers.