arkhipov / temporal_tables

Temporal Tables PostgreSQL Extension
BSD 2-Clause "Simplified" License
927 stars 46 forks source link

Calling inside another function #7

Closed thaumant closed 9 years ago

thaumant commented 9 years ago

When I create trigger as in your README it works just fine, I update a row and see result in the history table. Trigger looks like this:

create trigger test_versioning_trigger1
before update on testtable
for each row execute procedure versioning('sys_valid', 'testtable_history', true);

But when I try to execute versioning function within another function I get an error:

Kernel error: ERROR:  function versioning(unknown, unknown, boolean) does not exist

Procedure and trigger look like this:

create or replace function test_versioning() returns trigger as
$$
begin
    return versioning('sys_valid', 'testtable_history', true);
end;
$$
language plpgsql;

create trigger test_versioning_trigger2
before update on testtable
for each row execute procedure test_versioning();

I suppose the problem is not in the temporal_tables extension but I'm new to plpgsql and would appreciate any help.

arkhipov commented 9 years ago

versioning is not a usual function. It is a trigger function. So, the proper way to call it will be return versioning(), but this will not work since trigger functions cannot be called outside triggers (at least you cannot do this from a PL/pgSQL function).

If you are trying to add the versioning logic in addition to other things, just add another trigger, they will be fired in alphabetic order by name.

thaumant commented 9 years ago

trigger functions cannot be called outside triggers

That "function does not exist" message was confusing :-) Anyway, that's the piece I was missing. Thank you!