Skip to content

Commit ada661e

Browse files
fix(rdb): pgcron pt2
1 parent 19548dd commit ada661e

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ categories:
1414
- postgresql-and-mysql
1515
---
1616

17-
The pg_cron extension for PostgreSQL is used to execute periodic tasks. You can schedule SQL tasks, such as queries and data imports, to run at regular intervals. This allows you to automate the tasks to be run regularly, on a daily, weekly or monthly basis, for example.
17+
The pg_cron extension for PostgreSQL is used to execute periodic tasks. You can schedule SQL tasks, such as queries and data imports, using jobs that run at the intervals you set. This allows you to automate the tasks and run them regularly, on a daily, weekly or monthly basis, for example.
1818

1919
The `pgcron` extension is available with Scaleway Managed Databases for PostgreSQL. The extension is natively loaded in the `shared_preload_libraries` of the Database Instances by default.
2020

@@ -26,7 +26,6 @@ The `pgcron` extension is available with Scaleway Managed Databases for PostgreS
2626

2727
## Installing pgcron
2828

29-
3029
1. Run the following command to install the extension:
3130

3231
```sql
@@ -42,7 +41,28 @@ CREATE EXTENSION
4241

4342
## Scheduling jobs
4443

45-
To schedule jobs, you can run the following commands:
44+
Jobs allow you to define the SQL command or task you want to run based on a cron schedule.
45+
46+
To schedule jobs, you can run the following command in the SQL client:
47+
```sql
48+
SELECT cron.schedule(
49+
'${JOB_NAME}',
50+
'${SCHEDULE_SPEC}',
51+
$$
52+
DELETE FROM ${TABLE_NAME}
53+
WHERE ${CONDITION_COLUMN} < now() - interval '${TIME_INTERVAL}'
54+
$$
55+
);
56+
```
57+
58+
Replace the variables with the corresponding information:
59+
60+
- `${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.)
65+
4666

4767
```sql
4868
-- Delete old data on Saturday at 3:30am (GMT)

0 commit comments

Comments
 (0)