I try to work with trigger here. I have a relation like this :
salary(salaryid, userid, netsalary, reward, totalsalary)
So I want to update totalsalary
everytime I insert and update (netsalary
or reward
), it will recount : totalsalary = netsalary + reward
.
To do that, I made a function and a trigger :
CREATE FUNCTION reCount()
RETURNS TRIGGER AS $function$
BEGIN
UPDATE salary SET totalsalary = netsalary + reward;
RETURN NEW;
END;
CREATE TRIGGER updateTotalsalary
AFTER INSERT OR UPDATE
ON salary
FOR EACH ROW
EXECUTE PROCEDURE reCount();
Finally, I try to test by a query insert like this :
INSERT INTO salary(salaryid,userid,netsalary,reward,totalsalary)
VALUES (1234,123, 30,2,30);
but it run for a long time and it seem never stop. So when a try to stop it with, I got many rows of :
SQL statement "UPDATE salary SET totalsalary = netsalary + reward"
PL/pgSQL function "reCount()" line 3 at SQL statement
So what is the problem. Hope you guys can give me some suggestion?