Creating a pgAgent Job¶
pgAgent is a scheduling agent that runs and manages jobs; each job consists of steps and schedules.
To create or manage a job, use the Browser tree control to browse to the server on which the pgAgent database objects were created. The tree control will display a pgAgent Jobs node, under which currently defined jobs are displayed. To add a new job, right click on the pgAgent Jobs node, and select Create pgAgent Job... from the context menu.
When the pgAgent dialog opens, use the tabs on the pgAgent Job dialog to define the steps and schedule that make up a pgAgent job.
Create pgAgent Job dialog - General tab¶
Use the fields on the General tab to provide general information about a job:
Provide a name for the job in the
Namefield.Move the
Enabledswitch to theYesposition to enable a job, orNoto disable a job.Use the
Job Classdrop-down to select a class (for job categorization).Use the
Host Agentfield to specify the name of a machine that is running pgAgent to indicate that only that machine may execute the job. Leave the field blank to specify that any machine may perform the job.Note: It is not always obvious what value to specify for the Host Agent in order to target a job step to a specific machine. With pgAgent running on the required machines and connected to the scheduler database, you can use the following query to view the hostnames as reported by each agent:
SELECT jagstation FROM pgagent.pga_jobagentUse the hostname exactly as reported by the query in the Host Agent field.
Use the
Commentfield to store notes about the job.
Create pgAgent Job dialog - Steps tab¶
Use the Steps tab to define and manage the steps that the job will perform. Click the Add icon (+) to add a new step; then click the compose icon (located at the left side of the header) to open the step definition dialog:
Create pgAgent Job dialog - Steps tab - General tab¶
Use fields on the step definition dialog to define the step:
Provide a name for the step in the
Namefield; please note that steps will be performed in alphanumeric order by name.Use the
Enabledswitch to include the step when executing the job (True) or to disable the step (False).Use the
Kindswitch to indicate if the job step invokes SQL code (SQL) or a batch script (Batch).
If you select
SQL, use theCodetab to provide SQL code for the step.If you select
Batch, use theCodetab to provide the batch script that will be executed during the step.
Note
The fields Connection type, Database and Connection string are only
applicable when SQL is selected because Batch cannot be run on remote servers.
Use the
Connection typeswitch to indicate if the step is performed on a local server (Local) or on a remote host (Remote). If you specify a remote connection should be used for the step, theConnection stringfield will be enabled, and you must provide a libpq-style connection string.Use the
Databasedrop-down to select the database on which the job step will be performed.Use the
Connection stringfield to specify a libpq-style connection string to the remote server on which the step will be performed. For more information about writing a connection string, please see the PostgreSQL documentation.Use the
On errordrop-down to specify the behavior of pgAgent if it encounters an error while executing the step. Select from:Fail- Stop the job if you encounter an error while processing this step.Success- Mark the step as completing successfully, and continue.Ignore- Ignore the error, and continue.
Use the
Commentfield to provide a comment about the step.
Create pgAgent Job dialog - Steps tab - Code tab¶
Use the context-sensitive field on the step definition dialog’s Code tab to provide the SQL code or batch script that will be executed during the step:
If the step invokes SQL code, provide one or more SQL statements in the
SQL queryfield.If the step performs a batch script, provide the script in the
Scriptfield. If you are running on a Windows server, standard batch file syntax must be used. When running on a Linux server, any shell script may be used, provided that a suitable interpreter is specified on the first line (e.g.#!/bin/sh).
When you’ve provided all of the information required by the step, click the compose icon to close the step definition dialog. Click the add icon (+) to add each additional step, or select the Schedules tab to define the job schedule.
Create pgAgent Job dialog - Schedules tab¶
Click the Add icon (+) to add a schedule for the job; then click the compose icon (located at the left side of the header) to open the schedule definition dialog:
Create pgAgent Job dialog - Schedules tab - General tab¶
Use the fields on the schedule definition tab to specify the days and times at which the job will execute.
Provide a name for the schedule in the
Namefield.Use the
Enabledswitch to indicate that pgAgent should use the schedule (Yes) or to disable the schedule (No).Use the calendar selector in the
Startfield to specify the starting date and time for the schedule.Use the calendar selector in the
Endfield to specify the ending date and time for the schedule.Use the
Commentfield to provide a comment about the schedule.
Select the Repeat tab to define the days on which the schedule will execute.
Create pgAgent Job dialog - Schedules tab - Repeat tab¶
Use the fields on the Repeat tab to specify the details about the schedule in a cron-style format. The job will execute on each date or time element selected on the Repeat tab.
Click within a field to open a list of valid values for that field; click on a specific value to add that value to the list of selected values for the field. To clear the values from a field, click the X located at the right-side of the field.
Use the fields within the Days box to specify the days on which the job will execute:
Use the
Week Daysfield to select the days on which the job will execute.Use the
Month Daysfield to select the numeric days on which the job will execute. Specify theLast Dayto indicate that the job should be performed on the last day of the month, irregardless of the date.Use the
Monthsfield to select the months in which the job will execute.
Use the fields within the Times box to specify the times at which the job will execute:
Use the
Hoursfield to select the hour at which the job will execute.Use the
Minutesfield to select the minute at which the job will execute.Use the
Timezonedrop-down to select the timezone to be used in the next job run.
Select the Exceptions tab to specify any days on which the schedule will not execute.
Create pgAgent Job dialog - Schedules tab - Exceptions tab¶
Use the fields on the Exceptions tab to specify days on which you wish the job to not execute; for example, you may wish for jobs to not execute on national holidays.
Click the Add icon (+) to add a row to the exception table, then:
Click within the
Datecolumn to open a calendar selector, and select a date on which the job will not execute. Specify<Any>in theDatecolumn to indicate that the job should not execute on any day at the time selected.Click within the
Timecolumn to open a time selector, and specify a time on which the job will not execute. Specify<Any>in theTimecolumn to indicate that the job should not execute at any time on the day selected.
When you’ve finished defining the schedule, you can use the SQL tab to review the code that will create or modify your job.
Create pgAgent Job dialog - SQL tab¶
Click the Save button to save the job definition, or Cancel to exit the job without saving. Use the Reset button to remove your unsaved entries from the dialog.
After saving a job, the job will be listed under the pgAgent Jobs node of the browser tree control of the server on which it was defined. The Properties tab in the main PEM window will display a high-level overview of the selected job, and the Statistics tab will show the details of each run of the job.
pgAgent Object Properties tab¶
To modify an existing job or to review detailed information about a job, right-click on a job name, and select Properties from the context menu.