views:

299

answers:

7

I have a procedure written in PLJava that sends out updates over JMS in my postgres database.

What I would like to do is have that function called on an interval (every 15 seconds) internally in the database (preferably not from an outside process). Is this possible? Any ideas?

+1  A: 

As far as I know postgresql doesn't support scheduled tasks. You'll need to use a script with cron or at (depending on your operating system.)

Ben S
+2  A: 

Strict answer: it is not possible. Since you don't want outside process, and PostgreSQL doesn't support jobs - you are out of luck.

If you'll reconsider using outside processes, then you're most likely want something like cron, or better yet pgagent.

On absolutely other hand - what do you need to do that has to happen every 30 seconds? this seems like a problem with design.

depesz
I am flushing out database tables (updates) as java objects using a stored proc written in PL/Java every 15 seconds
Grasper
+2  A: 

If you need no external access, you are presumably able to modify the database design so that you don't need the update at all. Can you explain more about what the update is doing?

As depesz said, you could use either cron or pgAgent, but they are only able to go down to a one minute granularity, not 15 seconds. Considering sleeping inside the stored procedure until the next iteration is not a good idea, because you will have an open transaction for all that time which is a really bad idea.

alvherre
You can do something every 15 seconds with cron by running a job every minute which will (dosomething: sleep 15; dosomething: sleep 15; dosomething: sleep 15;)
bortzmeyer
A: 

Sounds like you're doing sort of replication? Every 15s sounds like a lot of updates. Could you setup a trigger (or a number of triggers) instead of polling?

Draemon
+2  A: 

First, you'll get minimal work if you just go with a cron job.

However, if you were starting from scracth: You are trying to periodically replicate rows from your database. I think you are looking at a replication queue.

The PGQ project (used for Londiste replication, both from Skype's SkyTools) has a queue that you can use independently. When configuring it, you set a maximum event count, and a loop delay, before batched events are generated. You can get batches spaced by no more than 15 seconds that way. You now have to produce the events that will be batched, using a trigger that calls pgq.insert_event; and consume the queues. The consumer can call your PL/Java stored proc; you'll have to rewrite the procedure to send everything in the batch instead of scanning the base table for new events.

Tobu
"you'll get minimal work" is unclear to me.
Michael Easter
“Very little work besides what's already done”, is that better?
Tobu
How about "You won't need to do much work, if you just go with a cron job"
Michael Easter
A: 

If you are using JMS why not just have th task wait for input on the queue?

James Anderson
A: 

Per your depesz comment, you have a PL/Java stored procedure that "flushes out database tables (updates) as java objects". Since you want it to run in 15 second intervals, it must be processing a batch of updates each time. Rather than processing a batch of updates in a stored procedure every 15 seconds, why not process them one at a time when they happen via an after update trigger and eliminate the need for a timed interval. If you are aggregrating data from multiple tables to build your objects than add the triggers to you upper most tables only.

fupsduck