I am trying to make simple trigger function (Postgresql) but I am getting same error 'function did not return any row' in all these cases (just simple examples):
New:
UPDATE somewhere SET something = something - 1;
RETURN NEW;
Old:
UPDATE somewhere SET something = something - 1;
RETURN OLD;
What should I return when I call this function "before delete"? ("after insert/update" works well)
Tyvm for tips!
Full code as requested:
Function:
CREATE OR REPLACE FUNCTION pictogram_frequency_on_delete()
RETURNS trigger AS
$BODY$
DECLARE
new_frequency RECORD;
target_unit RECORD;
current_row RECORD;
units_with_same_type RECORD;
what RECORD;
BEGIN
SET search_path TO 'myScheme';
CASE TG_OP
WHEN 'DELETED' THEN what := OLD;
ELSE what:= OLD;
END CASE;
SELECT unit_type_uid INTO STRICT target_unit
FROM unit
WHERE unit_uid = what.unit_uid;
SELECT count(*) AS exists INTO STRICT current_row
FROM unit_type_pictogram utp
WHERE utp.pictogram_uid = what.pictogram_uid
AND utp.unit_type_uid = target_unit.unit_type_uid;
IF (current_row.exists = 0) THEN
RETURN what; /* return new/old doesnt work too */
END IF;
UPDATE unit_type_pictogram utp
SET frequency = frequency - 1
WHERE utp.pictogram_uid = what.pictogram_uid
AND utp.unit_type_uid = target_unit.unit_type_uid;
RETURN what; /* return new/old doesnt work too */
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Trigger:
CREATE TRIGGER on_delete_frequency
BEFORE DELETE
ON unit_pictogram
FOR EACH ROW
EXECUTE PROCEDURE pictogram_frequency_on_delete();
From documentation:
Trigger functions invoked by per-statement triggers should always
return NULL. Trigger functions invoked by per-row triggers can return
a table row (a value of type HeapTuple) to the calling executor, if
they choose. A row-level trigger fired before an operation has the
following choices:
It can return NULL to skip the operation for the current row. This instructs the executor to not perform the row-level operation that
invoked the trigger (the insertion, modification, or deletion of a
particular table row).
For row-level INSERT and UPDATE triggers only, the returned row becomes the row that will be inserted or will replace the row being
updated. This allows the trigger function to modify the row being
inserted or updated.
A row-level BEFORE trigger that does not intend to cause either of
these behaviors must be careful to return as its result the same row
that was passed in (that is, the NEW row for INSERT and UPDATE
triggers, the OLD row for DELETE triggers).
EDIT
Try something like this:
CREATE OR REPLACE FUNCTION pictogram_frequency_on_delete()
RETURNS trigger AS
$BODY$
BEGIN
UPDATE unit_type_pictogram AS utp
SET frequency = frequency - 1
FROM unit
WHERE utp.pictogram_uid = OLD.pictogram_uid
AND unit_uid = OLD.unit_uid
AND utp.unit_type_uid = unit.unit_type_uid;
RETURN OLD;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
You should RETURN OLD;.
Your function must be defined as RETURNS trigger.
Related
I am looking for an elegant solution to this situation:
I have created a trigger function that updates the table supply with the sum of some detail rows, whenever a row is inserted or updated on warehouse_supplies.
PostgreSQL insert or update syntax allowed me to share the same function sync_supply_stock() for the insert and update conditions.
However, when I try to wire the after delete condition to the function it cannot be reused (although it is logically valid), for the returning object must be old instead of new.
-- The function I want to use for the 3 conditions (insert, update, delete)
create or replace function sync_supply_stock ()
returns trigger
as $$
begin
-- update the supply whose stock just changed in warehouse_supply with
-- the sum its stocks on all the warehouses.
update supply
set stock = (select sum(stock) from warehouse_supplies where supply_id = new.supply_id)
where supply_id = new.supply_id;
return new;
end;
$$ language plpgsql;
-- The (probably) unnecessary copy of the previous function, this time returning old.
create or replace function sync_supply_stock2 ()
returns trigger
as $$
begin
-- update the supply whose stock just changed in warehouse_supply with
-- the sum its stocks on all the warehouses.
update supply
set stock = (select sum(stock) from warehouse_supplies where supply_id = old.supply_id)
where supply_id = old.supply_id;
return old;
end;
$$ language plpgsql;
-- The after insert/update trigger
create trigger on_warehouse_supplies__after_upsert after insert or update
on warehouse_supplies for each row
execute procedure sync_supply_stock ();
-- The after delete trigger
create trigger on_warehouse_supplies__after_delete after delete
on warehouse_supplies for each row
execute procedure sync_supply_stock2 ();
Am I missing something or is there any fixing to duplicating sync_supply_stock2() as sync_supply_stock2()?
EDIT
For the benefit of future readers, following #bergi answer and discusion, this is a possible factorized solution
create or replace function sync_supply_stock ()
returns trigger
as $$
declare
_supply_id int;
begin
-- read the supply_id column from `new` on insert/update conditions and from `old` on delete conditions
_supply_id = coalesce(new.supply_id, old.supply_id);
-- update the supply whose stock just changed in of_warehouse_supply with
-- the sum its stocks on all the warehouses.
update of_supply
set stock = (select sum(stock) from of_warehouse_supplies where supply_id = _supply_id)
where supply_id = _supply_id;
-- returns `new` on insert/update conditions and `old` on delete conditions
return coalesce(new, old);
end;
$$ language plpgsql;
create trigger on_warehouse_supplies__after_upsert after insert or update
on of_warehouse_supplies for each row
execute procedure sync_supply_stock ();
create trigger on_warehouse_supplies__after_delete after delete
on of_warehouse_supplies for each row
execute procedure sync_supply_stock ();
for the returning object must be old instead of new.
No. The return value is only relevant for BEFORE ROW or INSTEAD OF triggers. From the docs: "The return value of a row-level trigger fired AFTER or a statement-level trigger fired BEFORE or AFTER is always ignored; it might as well be null".
So you can just make your sync_supply_stock trigger function RETURN NULL and it can be used on all operations.
Below I have implemented a simple postgres notify trigger function, which
publishes the OLD and NEW record as a custom json object after an insert or update transaction.
CREATE OR REPLACE FUNCTION test()
RETURNS trigger AS
$BODY$
BEGIN
PERFORM pg_notify('test_chanel', json_build_object('new',row_to_json(NEW),'old',row_to_json(OLD))::text );
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
should emit
{
"old":{...},
"new":{...}
}
Problem is that OLD obviously does not exists for an insert. How can I make it optional?
You can use automatic variable TG_OP inside trigger. With this variable your code should to look like
BEGIN
IF TG_OP = 'INSERT' THEN
PERFORM pg_notify('test_chanel', json_build_object('new',row_to_json(NEW));
ELSEIF TG_OP = 'UPDATE' THEN
PERFORM pg_notify('test_chanel', json_build_object('new',row_to_json(NEW),'old',row_to_json(OLD))::text );
ELSE
PERFORM pg_notify('test_chanel', json_build_object(row_to_json(OLD))::text );
END IF;
END;
I have a function and a trigger as below:
CREATE FUNCTION update() RETURNS TRIGGER AS $logupdate$
DECLARE
judge boolean;
BEGIN
judge := EXECUTE ('SELECT starttime,endtime,NEW.starttime,NEW.endtime FROM reserves WHERE bid = NEW.bid AND startdate = NEW.startdate AND (starttime,endtime) overlaps(NEW.starttime,NEW.endtime) IS NULL');
IF judge = f THEN RETURN false;
END IF;
RETURN NEW;
END;
$logupdate$ LANGUAGE plpgsql;
CREATE TRIGGER logupdate
before UPDATE ON reserves
FOR EACH ROW
EXECUTE PROCEDURE update();
The trigger should detect if the new input has overlapping with previous time, for example if there is an old time in my table [11:00 - 13:00], and a new input is [12:00 - 14:00] then the trigger should fire at it and stop insertion.
However, it doesn't work in my computer, what's wrong here?
Your trigger gets executed for every row. You don't need to query the tables. You already have access to the old and new rows.
CREATE FUNCTION update() RETURNS TRIGGER AS $logupdate$
BEGIN
IF (NEW.starttime, NEW.endtime) OVERLAPS (OLD.starttime, OLD.endtime) THEN
RETURN false;
END IF;
RETURN NEW;
END;
$logupdate$ LANGUAGE plpgsql;
However, this is super inefficient. You should consider using an exclusion constraint http://www.postgresql.org/docs/9.0/static/ddl-constraints.html
I'm pretty sure there's an exclusion constraint that will work with OVERLAPS
How to apply an update after an insert or update in POSTGRESQL; I have got a table which has a field lastupdate; I want that field to be set up whenever the row is updated or when it was inserted.
I tried this trigger, but It is not working! HELP!!
CREATE OR REPLACE FUNCTION fn_update_profile()
RETURNS TRIGGER AS $update_profile$
BEGIN
IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE' ) THEN
UPDATE profile SET lastupdate=now() where oid=OLD.oid;
RETURN NULL;
ELSEIF (TG_OP = 'DELETE') THEN
RETURN NULL;
END IF;
RETURN NULL; -- result is ignored since this is an AFTER trigger
END;
$update_profile$ LANGUAGE plpgsql;
Your trigger function can be a lot easier than you had. Keep in mind that PG will do the update or the insert on the original table, you only have to deal with keeping the profile table up-to-date:
CREATE OR REPLACE FUNCTION fn_update_profile()
RETURNS TRIGGER AS $update_profile$
BEGIN
UPDATE profile SET lastupdate = now() WHERE oid = NEW.oid;
RETURN NEW;
END;
$update_profile$ LANGUAGE plpgsql;
The INSERT and UPDATE trigger functions both use the NEW parameter; the INSERT trigger function does not have the OLD parameter. You should always return NEW from the trigger function if successful (or OLD from a DELETE trigger), even if it is an AFTER INSERT OR UPDATE trigger; the whole operation will be rolled back if NULL is returned. If you then define the actual trigger to fire after the insert or update, you should be good:
CREATE TRIGGER tr_update_profile
AFTER INSERT OR UPDATE ON my_table
FOR EACH ROW EXECUTE PROCEDURE fn_update_profile();
I am using a function in PostgreSQL 9.1:
CREATE FUNCTION myfunc() RETURNS trigger AS $$ ... $$ LANGUAGE plpgsql;
with a trigger:
CREATE TRIGGER mycheck
BEFORE INSERT OR UPDATE ON t
FOR EACH ROW EXECUTE PROCEDURE myfunc();
My problem now is to express a condition about events in the body of that function like (pseudocode):
IF TRIGGER_EVENT_WAS_INSERT THEN ...doThis... END IF;
How to express this condition?
(Note BEFORE INSERT OR UPDATE in the trigger!)
Yes, TG_OP. The manual:
TG_OP
Data type text; a string of INSERT, UPDATE, DELETE, or TRUNCATE telling for which operation the trigger was fired.
Careful what you return in each case. Sometimes you want to RETURN NEW, which is not defined in case of a DELETE or vice versa. If it gets too complex, rather split into multiple triggers, called on separate events.
Example:
IF TG_OP = 'DELETE' THEN
-- do something
RETURN OLD; -- depends!
ELSIF TG_OP = 'UPDATE' THEN
-- do something
RETURN NEW; -- depends!
END IF;
More code examples in related answers.