I need to write a plpgsql function which executes update statement in an infinite loop:
create function change_type() returns void as $$
begin
loop
update table a set type = 1 where date < now();
end loop;
end;
$$ LANGUAGE plpgsql;
When I call this function the update statement is not executed, although I can see that the loop is running. I ran the update statement as a single query and it works. How can I solve this problem?
Thanks
I guess what you are looking for is a cronjob to schedule a task at certain times (repeatedly). Try
man crontabon a on UNIX / LINUX system. Preferably as system user postgres.To run a query every 5 minutes, enter a line like this in your cron table. I use
crontab -eunder LINUX to edit my cron jobs:If you have more complex jobs, create a shell script with multiple SQL commands and call it like:
Or create a plpgsql function and call it:
Set your system up so that the superuser postgres can log in without password (is default).
Or have a look at pgAgent that is shipped with pgAdmin.