Navigation

pg_cron Extension

Postgres

Overview

A simple cron-based job scheduler for PostgreSQL. It allows you to run SQL commands on a schedule, similar to how cron jobs work in Unix-like systems.

Dashboard Configuration

This extension requires activation via the PlanetScale Dashboard before it can be used. It must be enabled through shared libraries and requires a database restart.

To enable pg_cron:

  1. From the PlanetScale organization dashboard, select the desired database
  2. Navigate to the Cluster configuration page from the menu on the left
  3. Choose the branch whose extensions you'd like to configure in the "Branch" dropdown
  4. Select the Extensions tab
  5. Enable pg_cron and configure its parameters
  6. Click Queue extension changes to apply the configuration
  7. Once you're ready to apply the changes, click "Apply changes"

Parameters

cron.database_name

  • Type: String
  • Default: postgres
  • Description: Name of the database where pg_cron is installed (via CREATE EXTENSION).

cron.launch_active_jobs

  • Type: Boolean
  • Default: true
  • Description: Switch to enable/disable running cron jobs - applies to all jobs.

cron.log_min_messages

  • Type: Select
  • Options: error, warning, notice, info, log, debug
  • Default: warning
  • Description: Lowest severity messages to log from the launcher background worker.

cron.log_run

  • Type: Boolean
  • Default: true
  • Description: Log all cron runs in the cron.job_run_details table.

cron.log_statement

  • Type: Boolean
  • Default: true
  • Description: Log all cron statements before running them.

cron.max_running_jobs

  • Type: Integer
  • Default: 1
  • Minimum: 1
  • Description: Maximum number of jobs that can run at once. Must be less than or equal to cluster-level max_worker_processes.

Usage

After enabling the extension through the dashboard, you can install it in your database:

CREATE EXTENSION IF NOT EXISTS pg_cron;

Then you can schedule jobs using the cron.schedule function:

-- Schedule a job to run every minute
SELECT cron.schedule('job-name', '* * * * *', 'SELECT 1;');

External Documentation

For more detailed information about pg_cron usage and functionality, see the official pg_cron documentation.

Need help?

Get help from the PlanetScale Support team, or join our GitHub discussion board to see how others are using PlanetScale.