1

我开发的一个程序正在使用 postgresql。该程序正在运行一个 plpgsql 函数,它需要很长时间(数小时或数天)。我想确保该功能在很长一段时间内都在运行。

我怎么知道?我不想在函数循环中使用“引发通知”,因为这会延长运行时间。

4

1 回答 1

2

You can see if it's running by examining pg_stat_activity for the process. However, this won't tell you if the function is progressing.

You can check to see whether that backend is blocked on any locks by joining pg_stat_activity against pg_locks to see if there are any open (granted = False) locks for that table. Again, this won't tell you if it's progressing, just that if it isn't it's not stuck on a lock.

If you want to monitor a function's progress you will need to emit log messages or use one of the other hacks for monitoring progress. You can (ab)use NOTIFY with payload to LISTEN for progress messages. Alternately, you could create a sequence that you call nextval on each time you process an item in your procedure; you can then SELECT * FROM the_sequence_name; in another transaction to see the approximate progress.

In general I'd recommend setting client_min_messages to notice or above then RAISE LOG so you record messages that appear only in the logs, without being sent to the client. To reduce overhead, keep a counter and log every 100 or 1000 or whatever iterations of your loop so you only log occasionally. There's a cost to updating the counter, for sure, but it's pretty low compared to the cost of a big, slow PL/PgSQL procedure like this.

于 2013-07-22T07:22:28.377 回答