You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/managed-databases-for-postgresql-and-mysql/api-cli/using-pgcron.mdx
+23-3Lines changed: 23 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ categories:
14
14
- postgresql-and-mysql
15
15
---
16
16
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.
18
18
19
19
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.
20
20
@@ -26,7 +26,6 @@ The `pgcron` extension is available with Scaleway Managed Databases for PostgreS
26
26
27
27
## Installing pgcron
28
28
29
-
30
29
1. Run the following command to install the extension:
31
30
32
31
```sql
@@ -42,7 +41,28 @@ CREATE EXTENSION
42
41
43
42
## Scheduling jobs
44
43
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
+
SELECTcron.schedule(
49
+
'${JOB_NAME}',
50
+
'${SCHEDULE_SPEC}',
51
+
$$
52
+
DELETEFROM ${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.)
0 commit comments