create function some_trigger() returns trigger as $$
begin
if TG_OP = 'DELETE' then
insert into audits values (OLD.value);
else
insert into audits values (NEW.value);
end if;
return NULL;
end
$$ language plpgsql;
create trigger some_trigger after insert on products
for each row execute procedure some_trigger();
I wish I could do:
create trigger some_trigger after insert on products
execute procedure do $$ begin
insert into audits values (CHANGED.value);
end $$ language plpgsql;
Changes/improvements:
1. Triggers default to 'for each row'
2. Triggers can use anonymous functions
3. Triggers can access a special CHANGED value that's either NEW for insert or updates, or OLD for deletes.
4. Default for 'after insert' triggers is to return null, as I believe it doesn't matter what you return here.
5. Way less repetitive typing.
Thoughts? Is this a terrible idea?