-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.mq-cron.sql
More file actions
94 lines (78 loc) · 2.74 KB
/
14.mq-cron.sql
File metadata and controls
94 lines (78 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
-- =====================================================
-- INSTALL PG Cron extension (https://access.crunchydata.com/documentation/pg_cron/latest/, https://github.com/citusdata/pg_cron)
--
-- RUN
-- apt-get update && apt-get install -y postgresql-18-cron
-- Update postgresql.conf:
-- - Add setting cron.database_name = 'postgres'
-- - Update setting shared_preload_libraries and add 'pg_cron'
-- Restart postgresql
-- =====================================================
-- =====================================================
-- CREATE EXTENSION
-- =====================================================
CREATE EXTENSION IF NOT EXISTS pg_cron;
-- =====================================================
-- Cron configuration
-- =====================================================
SELECT *
FROM pg_settings
WHERE name LIKE 'cron%';
-- =====================================================
-- JOB SCHEDULING
-- Demonstrates: Create a new job to run every minute
-- =====================================================
SELECT cron.schedule('my-job', '* * * * *', 'NOTIFY heartbeat, ''ping''');
-- =====================================================
-- JOB SHECULING
-- Demonstrates: Create a job combined with NOTIFY
-- =====================================================
SELECT cron.schedule(
'mq_worker_notify',
'*/1 * * * *',
$$
WITH batch AS (
SELECT id, event_type
FROM mq.queue
WHERE processed = FALSE
ORDER BY created_at ASC
FOR UPDATE SKIP LOCKED
LIMIT 5
),
updated AS (
UPDATE mq.queue
SET processed = TRUE
WHERE id IN (SELECT id FROM batch)
RETURNING id, event_type
)
SELECT pg_notify(
'mq_channel',
json_build_object('processed', json_agg(updated.*))::text
) FROM updated;
$$
);
-- =====================================================
-- JOB LISTING
-- Demonstrates: Show scheduled jobs
-- =====================================================
SELECT *
FROM cron.job;
-- =====================================================
-- JOB LISTING
-- Demonstrates: Show scheduled jobs run history
-- =====================================================
SELECT *
FROM cron.job_run_details
ORDER BY start_time DESC
LIMIT 100;
-- =====================================================
-- MODIFY JOB SCHEDULE
-- Demonstrates: Change existing job's schedule
-- =====================================================
SELECT cron.alter_job(4, '*/2 * * * *');
-- =====================================================
-- JOB REMOVAL
-- Demonstrates: Remove a job from schedule
-- =====================================================
SELECT cron.unschedule('my-job');
SELECT cron.unschedule('mq_worker_notify');