Senior LevelPostgreSQL
How does the pg_cron extension run background cron jobs inside PostgreSQL memory?
❓ Interview Prompt: Explain `pg_cron` extension, running cron background workers inside PostgreSQL daemon, `cron.schedule()`, and executing SQL maintenance jobs.
'pg_cron' is an extension that runs a background worker process inside PostgreSQL, executing scheduled SQL queries directly using standard cron syntax:
Benefits:
Runs directly in database memory without external OS crontabs or Airflow workers!
Code Implementation Example
sql
CREATE EXTENSION IF NOT EXISTS pg_cron;
-- Schedule nightly vacuum maintenance job at 3:00 AM
SELECT cron.schedule('nightly-vacuum', '0 3 * * *', 'VACUUM ANALYZE orders;');
-- View active scheduled jobs
SELECT * FROM cron.job;💡Scheduling database maintenance tasks using the pg_cron extension.
🎯 Interview Key Takeaway
Use pg_cron to manage scheduled database maintenance and materialized view refreshes directly inside PostgreSQL.
#pg_cron#Cron#Scheduling#Background Worker#Senior
Back to all PostgreSQL Questions→