-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.mq-pgmq-dml.sql
More file actions
401 lines (323 loc) · 9.92 KB
/
16.mq-pgmq-dml.sql
File metadata and controls
401 lines (323 loc) · 9.92 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
-- =====================================================
-- PGMQ EXTENSION SETUP
-- Demonstrates: Installing and enabling the extension
-- =====================================================
CREATE EXTENSION IF NOT EXISTS pgmq;
-- =====================================================
-- QUEUE MANAGEMENT
-- Demonstrates: Creating, listing, and dropping queues
-- =====================================================
-- Create a standard queue
SELECT pgmq.create('my_queue');
-- Create a partitioned queue (requires pg_partman)
SELECT pgmq.create_partitioned('my_partitioned_queue');
-- Create an unlogged queue (skips WAL, faster writes, not crash-safe, does not replicate)
SELECT pgmq.create_unlogged('my_unlogged_queue');
-- List all queues
SELECT *
FROM pgmq.list_queues();
-- Drop a queue (and all its messages, archived messages, partitions, etc)
SELECT pgmq.drop_queue('my_unlogged_queue');
-- =====================================================
-- SENDING MESSAGES
-- Demonstrates: Enqueuing single and batch messages
-- =====================================================
-- Send a single message
SELECT *
FROM pgmq.send(
queue_name => 'my_queue',
msg => '{"event": "user_signup", "user_id": 42}'
);
-- Send a message with a delay (visible after 10 seconds)
SELECT *
FROM pgmq.send(
queue_name => 'my_queue',
msg => '{"event": "send_email", "to": "user@example.com"}',
delay => 10
);
-- Send a batch of messages at once
SELECT *
FROM pgmq.send_batch(
queue_name => 'my_queue',
msgs => ARRAY [
'{
"event": "job_1",
"payload": "a"
}'::jsonb,
'{
"event": "job_2",
"payload": "b"
}'::jsonb,
'{
"event": "job_3",
"payload": "c"
}'::jsonb
]
);
-- Send a batch with delay
SELECT *
FROM pgmq.send_batch(
queue_name => 'my_queue',
msgs => ARRAY [
'{
"event": "scheduled_1"
}'::jsonb,
'{
"event": "scheduled_2"
}'::jsonb
],
delay => 30
);
-- =====================================================
-- BASIC QUEUE CONSUMPTION
-- Demonstrates: Pull-based message retrieval
-- =====================================================
-- Read 1 message (visibility timeout = 30s)
SELECT *
FROM pgmq.read(
queue_name => 'my_queue',
vt => 30,
qty => 1
);
-- Read up to 5 messages (visibility timeout = 60s)
SELECT *
FROM pgmq.read(
queue_name => 'my_queue',
vt => 60,
qty => 5
);
-- Read with polling (waits up to 5s for a message to arrive)
SELECT *
FROM pgmq.read_with_poll(
queue_name => 'my_queue',
vt => 30,
qty => 1,
max_poll_seconds => 5
);
-- Pop: read + delete atomically (no need to delete manually)
SELECT *
FROM pgmq.pop('my_queue');
-- =====================================================
-- MESSAGE LIFECYCLE
-- Demonstrates: Deleting and archiving consumed messages
-- =====================================================
-- Delete a single message after processing
SELECT pgmq.delete('my_queue', 1);
-- Delete multiple messages by ID
SELECT pgmq.delete('my_queue', ARRAY [2, 3, 4]);
-- Archive a message (moves to pgmq.a_my_queue for audit trail)
SELECT pgmq.archive(
queue_name => 'my_queue',
msg_id => 5
);
-- Archive multiple messages
SELECT pgmq.archive(
queue_name => 'my_queue',
msg_ids => ARRAY [6, 7, 8]
);
-- View archived messages
SELECT *
FROM pgmq.a_my_queue;
-- =====================================================
-- VISIBILITY TIMEOUT MANAGEMENT
-- Demonstrates: Extending or resetting message locks
-- =====================================================
-- Extend visibility timeout for a message being processed
-- (resets the clock by another 60 seconds)
SELECT pgmq.set_vt(
queue_name => 'my_queue',
msg_id => 9,
vt => 60
);
-- =====================================================
-- QUEUE INSPECTION
-- Demonstrates: Monitoring queue depth and message state
-- =====================================================
-- Get queue metrics (depth, hidden count, oldest message age)
SELECT *
FROM pgmq.metrics('my_queue');
-- Get metrics for all queues at once
SELECT *
FROM pgmq.metrics_all();
-- Peek at the next N messages without locking them
SELECT *
FROM pgmq.read(
queue_name => 'my_queue',
vt => 0, -- vt=0 means immediately visible again
qty => 10
);
-- View all messages currently in the queue table directly
SELECT *
FROM pgmq.q_my_queue;
-- =====================================================
-- PGMQ DOCKER DEMO
-- =====================================================
-- =====================================================
-- STEP 1: CREATE QUEUES
-- Demonstrates: Different queue types
-- =====================================================
-- Standard durable queue
SELECT pgmq.create('orders');
-- Unlogged queue (faster, not crash-safe)
SELECT pgmq.create_unlogged('notifications');
-- Confirm both queues exist
SELECT queue_name, is_unlogged, created_at
FROM pgmq.list_queues();
-- =====================================================
-- STEP 3: SEND MESSAGES
-- Demonstrates: Enqueuing single, delayed, batch messages
-- =====================================================
-- Send a single order event
SELECT *
FROM pgmq.send(
queue_name => 'orders',
msg => '{"order_id": 1001, "item": "keyboard", "qty": 1}',
headers => '{"trace_id": "D123456789"}'
);
-- Send another
SELECT *
FROM pgmq.send(
queue_name => 'orders',
msg => '{"order_id": 1002, "item": "monitor", "qty": 2}'
);
-- Send a delayed message (visible after 15 seconds)
SELECT *
FROM pgmq.send(
queue_name => 'orders',
msg => '{"order_id": 1003, "item": "mouse", "qty": 1}',
delay => 15
);
-- Send a batch of notifications
SELECT *
FROM pgmq.send_batch(
queue_name => 'notifications',
msgs => ARRAY [
'{
"user_id": 1,
"type": "email",
"body": "Your order shipped!"
}'::jsonb,
'{
"user_id": 2,
"type": "sms",
"body": "Flash sale starts now!"
}'::jsonb,
'{
"user_id": 3,
"type": "push",
"body": "You have a new message."
}'::jsonb
]
);
-- Check queue depth after sending
SELECT *
FROM pgmq.metrics('orders');
SELECT *
FROM pgmq.metrics('notifications');
-- =====================================================
-- STEP 4: CONSUME MESSAGES
-- Demonstrates: Reading with visibility timeout
-- =====================================================
-- Read 1 order (locked for 30 seconds)
SELECT *
FROM pgmq.read(
queue_name => 'orders',
vt => 30,
qty => 1
);
-- Read up to 3 notifications
SELECT *
FROM pgmq.read(
queue_name => 'notifications',
vt => 30,
qty => 3
);
-- Read with polling — waits up to 5s for a message
SELECT *
FROM pgmq.read_with_poll(
queue_name => 'orders',
vt => 30,
qty => 1,
max_poll_seconds => 5
);
-- =====================================================
-- STEP 5: ACK / DELETE MESSAGES
-- Demonstrates: Acknowledging processed messages
-- =====================================================
-- Delete message with msg_id = 1 (replace with actual id from step 4)
SELECT pgmq.delete('orders', 1);
-- Delete multiple messages at once
SELECT pgmq.delete('notifications', ARRAY [1, 2, 3]);
-- Pop: read + delete in one atomic call (no manual delete needed)
SELECT *
FROM pgmq.pop('orders');
-- =====================================================
-- STEP 6: ARCHIVE MESSAGES
-- Demonstrates: Keeping a processed message audit trail
-- =====================================================
-- Send a fresh message to archive
SELECT *
FROM pgmq.send(
queue_name => 'orders',
msg => '{"order_id": 1004, "item": "desk", "qty": 1}'
);
-- Read it to get the msg_id
SELECT *
FROM pgmq.read(
queue_name => 'orders',
vt => 30,
qty => 1
);
-- Archive it instead of deleting (replace 4 with actual msg_id)
SELECT pgmq.archive(queue_name => 'orders', msg_id => 4);
-- View the archive
SELECT *
FROM pgmq.a_orders;
-- =====================================================
-- STEP 7: VISIBILITY TIMEOUT EXTENSION
-- Demonstrates: Extending lock on a slow-processing message
-- =====================================================
-- Send a message
SELECT *
FROM pgmq.send(
queue_name => 'orders',
msg => '{"order_id": 1005, "item": "chair", "qty": 3}'
);
-- Read it (locked for 10 seconds)
SELECT *
FROM pgmq.read(
queue_name => 'orders',
vt => 10,
qty => 1
);
-- Extend by another 60 seconds before it expires (replace 5 with actual msg_id)
SELECT pgmq.set_vt(
queue_name => 'orders',
msg_id => 5,
vt => 60
);
-- =====================================================
-- STEP 8: INSPECT & MONITOR
-- Demonstrates: Metrics and raw table inspection
-- =====================================================
-- Metrics for a specific queue
SELECT *
FROM pgmq.metrics('orders');
-- Metrics for all queues
SELECT *
FROM pgmq.metrics_all();
-- Peek directly at the raw queue table
SELECT *
FROM pgmq.q_orders;
SELECT *
FROM pgmq.q_notifications;
-- =====================================================
-- STEP 9: CLEANUP
-- Demonstrates: Dropping queues
-- =====================================================
-- Drop queue and archive
SELECT pgmq.drop_queue('orders', true);
SELECT pgmq.drop_queue('notifications', true);
-- Confirm all gone
SELECT queue_name
FROM pgmq.list_queues();