How can we schedule auto vacuum between specific times, such as 10 PM and 6 AM?

2024-01-20T17:05:10.3233333+00:00

How can we schedule auto vacuum in Azure Database for PostgreSQL flexible server in between specific times, such as 10 PM and 6 AM?

Azure Database for PostgreSQL
{count} votes

Accepted answer
  1. SSingh-MSFT 16,371 Reputation points Moderator
    2024-01-23T04:59:59.15+00:00

    Hi Naveen Kumar Kakarla (Quadrant Resource),

    Welcome to Micrsoft Q&A forum.

    As I understand, you want to schedule auto vacuum between specific times, such as 10 PM and 6 AM.

    As mentioned in the above answer by Dillon, make use of pg_cron extension to achieve the same.

    The commands would look like below:

    SELECT cron.schedule('0 10 * * *', 'VACUUM');

    SELECT cron.schedule('0 18 * * *', 'VACUUM');

    Reference: https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/concepts-extensions#pg_cron

    Let us know for further queries.

    Thanks

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dillon Silzer 57,831 Reputation points Volunteer Moderator
    2024-01-20T17:44:00.2666667+00:00

    Hi Naveen,

    Have a look at using pg_cron extension to schedule this:

    https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/concepts-extensions#pg_cron

    pg_cron is a simple, cron-based job scheduler for PostgreSQL that runs inside the database as an extension. The pg_cron extension can be used to run scheduled maintenance tasks within a PostgreSQL database. For example, you can run periodic vacuum of a table or removing old data jobs.

    Some examples:

    To run vacuum every day at 10:00am (GMT) in default database 'postgres'

    SELECT cron.schedule('0 10 * * *', 'VACUUM');
    

    If this is helpful please accept answer.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.