Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a python script on postgresql database which is fetching records from sql server database and inserting it to postgresql DB after some filtering. this jobs is running by cron every two minutes. I want to know when there is maintenance job such as autovacuum is running and if running forget fetching records. Is it possible to detect in python?

share|improve this question
Don't bother. There is no problem inserting into the table at the same time as vacuum is running. – a_horse_with_no_name May 7 '12 at 12:04
up vote 2 down vote accepted

In current versions of PostgreSQL, you can look at the *pg_stat_activity* view to find autovacuum tasks. They will have *current_query* fields that start with "Mark autovacuum entries in pg_stat_activity that look like this:

autovacuum: VACUUM t

A query to count how many of those you have might look like this:

SELECT count(*) FROM pg_stat_activity WHERE position('autovacuum:' IN current_query) = 1;

And you can have your Python code check if the value returned is >0; if so, autovacuum is running. You might want to track down and avoid running concurrently with manual VACUUM calls, too.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.