Skip to content

Commit 86fbb41

Browse files
fix(rdb): cron jobs pt3
1 parent ada661e commit 86fbb41

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

pages/managed-databases-for-postgresql-and-mysql/api-cli/using-pgcron.mdx

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ CREATE EXTENSION
3535

3636
## Configuring pg_cron
3737

38+
To fully use the extension, you must grant read/write rights to the user who will be running the `pg_cron` functions to manage jobs on the database.
39+
3840
<Message type="note">
39-
To fully use the extension, you must grant read/write rights to the user who will be running the `pg_cron` functions to manage jobs on the database. Refer to the [How to add users to a PostgreSQL or MySQL Database Instance](/managed-databases-for-postgresql-and-mysql/how-to/add-users) documentation for more information.
41+
Refer to the [How to add users to a PostgreSQL or MySQL Database Instance](/managed-databases-for-postgresql-and-mysql/how-to/add-users) documentation for more information.
4042
</Message>
4143

4244
## Scheduling jobs
@@ -49,37 +51,52 @@ SELECT cron.schedule(
4951
'${JOB_NAME}',
5052
'${SCHEDULE_SPEC}',
5153
$$
52-
DELETE FROM ${TABLE_NAME}
53-
WHERE ${CONDITION_COLUMN} < now() - interval '${TIME_INTERVAL}'
54+
${SQL_COMMAND}
5455
$$
5556
);
5657
```
5758

5859
Replace the variables with the corresponding information:
5960

6061
- `${JOB_NAME}` - set a name for the job
61-
- `${SCHEDULE_SPEC}` - the schedule specification in cron format (e.g. 0 0 * * * for daily at midnight)
62-
- `${TABLE_NAME}` - the name of the table to delete from
63-
- `${CONDITION_COLUMN}` - the column to use for the deletion condition
64-
- `${TIME_INTERVAL}` - the time interval to use for the deletion condition (e.g. 1 week, 1 month, etc.)
62+
- `${SCHEDULE_SPEC}` - the schedule specification in [cron format](/serverless-jobs/reference-content/cron-schedules/)
63+
- `${SQL_COMMAND}` - the SQL command to be executed. Depending on the command, you might need to specify other parameters.
64+
65+
### Job examples
66+
67+
**Example 1 - Deleting old data**
6568

69+
You can run the command below to delete old data from the `events` table every Saturday at 3:30am:
6670

6771
```sql
68-
-- Delete old data on Saturday at 3:30am (GMT)
6972
SELECT cron.schedule('30 3 * * 6', $$DELETE FROM events WHERE event_time < now() - interval '1 week'$$);
7073
```
7174

75+
**Example 2 - Scheduling a VACUUM job**
76+
77+
You can run the command below to execute the VACUUM task every day at 10:00am.
78+
7279
```sql
73-
-- Change to vacuum at 3:00am (GMT)
7480
SELECT cron.schedule('nightly-vacuum', '0 10 * * *', 'VACUUM');
7581
```
7682

77-
**Listing Jobs**
83+
## Listing Jobs
7884

7985
To list all scheduled jobs, you can run the following command:
8086

8187
```sql
8288
SELECT * FROM cron.job;
89+
```
90+
Each job is represented by a record. You can see the following information in the response:
91+
92+
- `jobid` - a unique job ID
93+
- `schedule` - the schedule specification in cron format
94+
- `command` - the SQL command
95+
- `database`, `username`, `nodename`, `nodeport` - connection details
96+
- `active` - whether the job is active or not
97+
- `jobname` - the name of the job
98+
99+
```sql
83100
-[ RECORD 1 ]-------------------------------------------------------------
84101
jobid | 1
85102
schedule | 30 3 * * 6
@@ -102,32 +119,37 @@ active | t
102119
jobname | nightly-vacuum
103120
```
104121

105-
**Unscheduling Jobs**
122+
## Unscheduling Jobs
106123

107124
To unschedule a job, you can run the following command:
108125

126+
```sql
127+
SELECT cron.unschedule('${JOB_ID}');
128+
```
129+
Replace ${JOB_ID} with the ID of the job you want to unschedule.
130+
131+
### Examples
132+
133+
To unschedule the jobs set in the previous section, you can run:
134+
109135
```sql
110136
SELECT cron.unschedule(1);
111-
unschedule
112-
------------
113-
t
114-
(1 row)
115137
```
116138

117139
or
118140

119141
```sql
120142
SELECT cron.unschedule('nightly-vacuum');
121-
unschedule
122-
------------
123-
t
124-
(1 row)
125143
```
126144

127-
**Scheduling Jobs in Other Databases**
145+
## Scheduling jobs in other databases
128146

129147
To schedule a job in another database, you can use the `schedule_in_database` function:
130148

149+
```sql
150+
SELECT cron.unschedule('${JOB_ID}');
151+
```
152+
131153
```sql
132154
SELECT cron.schedule_in_database('job-in-another-db', '0 12 * * *', 'INSERT INTO public.another_db_table values (now())', 'mydb');
133155
schedule

0 commit comments

Comments
 (0)