-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathpostgres-schema.sql
More file actions
2591 lines (1909 loc) · 78.8 KB
/
Copy pathpostgres-schema.sql
File metadata and controls
2591 lines (1909 loc) · 78.8 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- automatically generated with `make postgres-schema`
------------------------------------------------------------------------------------------
-- Database: backendA
--
-- PostgreSQL database dump
--
\restrict 79bbfb4630959c48307653a5cd3d83f2582b3c2210f75f10d79e3ebf0015620
-- Dumped from database version 17.10
-- Dumped by pg_dump version 17.10
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: arbiter; Type: SCHEMA; Schema: -; Owner: wire-server
--
CREATE SCHEMA arbiter;
ALTER SCHEMA arbiter OWNER TO "wire-server";
--
-- Name: public; Type: SCHEMA; Schema: -; Owner: wire-server
--
-- *not* creating schema, since initdb creates it
ALTER SCHEMA public OWNER TO "wire-server";
--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: wire-server
--
COMMENT ON SCHEMA public IS '';
--
-- Name: recurrence_frequency; Type: TYPE; Schema: public; Owner: wire-server
--
CREATE TYPE public.recurrence_frequency AS ENUM (
'daily',
'weekly',
'monthly',
'yearly'
);
ALTER TYPE public.recurrence_frequency OWNER TO "wire-server";
--
-- Name: maintain_conversations_groups_delete(); Type: FUNCTION; Schema: arbiter; Owner: wire-server
--
CREATE FUNCTION arbiter.maintain_conversations_groups_delete() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM old_table WHERE group_key IS NOT NULL LIMIT 1) THEN
RETURN NULL;
END IF;
-- Lock group rows in group_key order to avoid deadlock with concurrent triggers.
PERFORM 1 FROM "arbiter"."conversations_groups" g
WHERE g.group_key IN (SELECT group_key FROM old_table WHERE group_key IS NOT NULL)
ORDER BY g.group_key
FOR UPDATE;
UPDATE "arbiter"."conversations_groups" g
SET job_count = g.job_count - sub.removed_count,
min_priority = COALESCE(sub.new_min_priority, g.min_priority),
min_id = COALESCE(sub.new_min_id, g.min_id),
ready_count = GREATEST(0, g.ready_count - sub.removed_ready_count),
next_due = sub.new_next_due,
in_flight_until = CASE
WHEN sub.had_inflight THEN sub.surviving_ift
ELSE g.in_flight_until
END
FROM (
SELECT d.group_key, d.removed_count, d.removed_ready_count, d.had_inflight,
MIN(t.priority) AS new_min_priority,
MIN(t.id) AS new_min_id,
MIN(t.not_visible_until) FILTER (WHERE t.not_visible_until IS NOT NULL AND NOT t.suspended) AS new_next_due,
MAX(t.not_visible_until) FILTER (WHERE t.not_visible_until > NOW() AND NOT t.suspended AND (t.attempts > 0 OR t.throttled_until > NOW())) AS surviving_ift
FROM (
SELECT group_key, COUNT(*) AS removed_count,
COUNT(*) FILTER (WHERE not_visible_until IS NULL AND NOT suspended) AS removed_ready_count,
bool_or(not_visible_until > NOW() AND NOT suspended AND (attempts > 0 OR throttled_until > NOW())) AS had_inflight
FROM old_table
WHERE group_key IS NOT NULL
GROUP BY group_key
) d
LEFT JOIN "arbiter"."conversations" t ON t.group_key = d.group_key
GROUP BY d.group_key, d.removed_count, d.removed_ready_count, d.had_inflight
) sub
WHERE g.group_key = sub.group_key;
DELETE FROM "arbiter"."conversations_groups"
WHERE job_count <= 0
AND group_key IN (SELECT group_key FROM old_table WHERE group_key IS NOT NULL);
RETURN NULL;
END;
$$;
ALTER FUNCTION arbiter.maintain_conversations_groups_delete() OWNER TO "wire-server";
--
-- Name: maintain_conversations_groups_insert(); Type: FUNCTION; Schema: arbiter; Owner: wire-server
--
CREATE FUNCTION arbiter.maintain_conversations_groups_insert() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM new_table WHERE group_key IS NOT NULL LIMIT 1) THEN
RETURN NULL;
END IF;
-- Lock group rows in group_key order to avoid deadlock with concurrent triggers.
PERFORM 1 FROM "arbiter"."conversations_groups" g
WHERE g.group_key IN (SELECT group_key FROM new_table WHERE group_key IS NOT NULL)
ORDER BY g.group_key
FOR UPDATE;
INSERT INTO "arbiter"."conversations_groups" (group_key, min_priority, min_id, job_count, ready_count, next_due)
SELECT group_key,
MIN(priority),
MIN(id),
COUNT(*),
COUNT(*) FILTER (WHERE not_visible_until IS NULL AND NOT suspended),
MIN(not_visible_until) FILTER (WHERE not_visible_until IS NOT NULL AND NOT suspended)
FROM new_table
WHERE group_key IS NOT NULL
GROUP BY group_key
ORDER BY group_key
ON CONFLICT (group_key) DO UPDATE SET
min_priority = LEAST("arbiter"."conversations_groups".min_priority, EXCLUDED.min_priority),
min_id = LEAST("arbiter"."conversations_groups".min_id, EXCLUDED.min_id),
job_count = "arbiter"."conversations_groups".job_count + EXCLUDED.job_count,
ready_count = "arbiter"."conversations_groups".ready_count + EXCLUDED.ready_count,
next_due = LEAST("arbiter"."conversations_groups".next_due, EXCLUDED.next_due),
in_flight_until = CASE WHEN "arbiter"."conversations_groups".in_flight_until <= NOW()
THEN NULL ELSE "arbiter"."conversations_groups".in_flight_until END;
RETURN NULL;
END;
$$;
ALTER FUNCTION arbiter.maintain_conversations_groups_insert() OWNER TO "wire-server";
--
-- Name: maintain_conversations_groups_update(); Type: FUNCTION; Schema: arbiter; Owner: wire-server
--
CREATE FUNCTION arbiter.maintain_conversations_groups_update() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM new_table WHERE group_key IS NOT NULL LIMIT 1
) AND NOT EXISTS (
SELECT 1 FROM old_table WHERE group_key IS NOT NULL LIMIT 1
) THEN
RETURN NULL;
END IF;
-- Lock group rows (old and new) in group_key order to avoid deadlock with concurrent triggers.
PERFORM 1 FROM "arbiter"."conversations_groups" g
WHERE g.group_key IN (
SELECT group_key FROM new_table WHERE group_key IS NOT NULL
UNION
SELECT group_key FROM old_table WHERE group_key IS NOT NULL
)
ORDER BY g.group_key
FOR UPDATE;
-- Step 1: Full rescan - recompute in_flight_until when not_visible_until decreases or suspended changes
UPDATE "arbiter"."conversations_groups" g
SET in_flight_until = sub.new_ift
FROM (
SELECT t.group_key,
MAX(t.not_visible_until) FILTER (
WHERE t.not_visible_until > NOW() AND NOT t.suspended AND (t.attempts > 0 OR t.throttled_until > NOW())
) AS new_ift
FROM "arbiter"."conversations" t
WHERE t.group_key IN (
SELECT n.group_key FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND (o.not_visible_until IS DISTINCT FROM n.not_visible_until
OR o.suspended IS DISTINCT FROM n.suspended
OR o.attempts IS DISTINCT FROM n.attempts)
AND (
n.not_visible_until > NOW() AND NOT n.suspended AND n.attempts > 0
AND (o.not_visible_until IS NULL OR o.not_visible_until <= NOW()
OR n.not_visible_until > o.not_visible_until)
) IS NOT TRUE
)
GROUP BY t.group_key
) sub
WHERE g.group_key = sub.group_key
AND g.in_flight_until IS DISTINCT FROM sub.new_ift;
-- Step 2: group_key change (dedup replace) - remove from old group
UPDATE "arbiter"."conversations_groups" g
SET job_count = g.job_count - sub.cnt,
min_priority = COALESCE(sub.new_min_priority, g.min_priority),
min_id = COALESCE(sub.new_min_id, g.min_id),
ready_count = GREATEST(0, g.ready_count - sub.removed_ready_count),
next_due = sub.new_next_due,
in_flight_until = CASE
WHEN sub.had_inflight THEN sub.surviving_ift
ELSE g.in_flight_until
END
FROM (
SELECT d.group_key, d.cnt, d.removed_ready_count, d.had_inflight,
MIN(t.priority) AS new_min_priority, MIN(t.id) AS new_min_id,
MIN(t.not_visible_until) FILTER (WHERE t.not_visible_until IS NOT NULL AND NOT t.suspended) AS new_next_due,
MAX(t.not_visible_until) FILTER (WHERE t.not_visible_until > NOW() AND NOT t.suspended AND (t.attempts > 0 OR t.throttled_until > NOW())) AS surviving_ift
FROM (
SELECT o.group_key, COUNT(*) AS cnt,
COUNT(*) FILTER (WHERE o.not_visible_until IS NULL AND NOT o.suspended) AS removed_ready_count,
bool_or(o.not_visible_until > NOW() AND NOT o.suspended AND (o.attempts > 0 OR o.throttled_until > NOW())) AS had_inflight
FROM old_table o
JOIN new_table n ON o.id = n.id
WHERE o.group_key IS NOT NULL
AND o.group_key IS DISTINCT FROM n.group_key
GROUP BY o.group_key
) d
LEFT JOIN "arbiter"."conversations" t ON t.group_key = d.group_key
GROUP BY d.group_key, d.cnt, d.removed_ready_count, d.had_inflight
) sub
WHERE g.group_key = sub.group_key;
DELETE FROM "arbiter"."conversations_groups"
WHERE job_count <= 0
AND group_key IN (
SELECT o.group_key FROM old_table o
JOIN new_table n ON o.id = n.id
WHERE o.group_key IS NOT NULL
AND o.group_key IS DISTINCT FROM n.group_key
);
-- Step 3: group_key change - add to new group
INSERT INTO "arbiter"."conversations_groups" (group_key, min_priority, min_id, job_count, ready_count, next_due)
SELECT n.group_key, MIN(n.priority), MIN(n.id), COUNT(*),
COUNT(*) FILTER (WHERE n.not_visible_until IS NULL AND NOT n.suspended),
MIN(n.not_visible_until) FILTER (WHERE n.not_visible_until IS NOT NULL AND NOT n.suspended)
FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND o.group_key IS DISTINCT FROM n.group_key
GROUP BY n.group_key
ORDER BY n.group_key
ON CONFLICT (group_key) DO UPDATE SET
min_priority = LEAST("arbiter"."conversations_groups".min_priority, EXCLUDED.min_priority),
min_id = LEAST("arbiter"."conversations_groups".min_id, EXCLUDED.min_id),
job_count = "arbiter"."conversations_groups".job_count + EXCLUDED.job_count,
ready_count = "arbiter"."conversations_groups".ready_count + EXCLUDED.ready_count,
next_due = LEAST("arbiter"."conversations_groups".next_due, EXCLUDED.next_due);
-- Step 4: same-group ordering/visibility change - recompute min and next_due.
UPDATE "arbiter"."conversations_groups" g
SET min_priority = sub.new_min_priority,
min_id = sub.new_min_id,
next_due = sub.new_next_due
FROM (
SELECT d.group_key,
MIN(t.priority) AS new_min_priority,
MIN(t.id) AS new_min_id,
MIN(t.not_visible_until) FILTER (WHERE t.not_visible_until IS NOT NULL AND NOT t.suspended) AS new_next_due
FROM (
SELECT DISTINCT n.group_key
FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND n.group_key IS NOT DISTINCT FROM o.group_key
AND (n.priority IS DISTINCT FROM o.priority
OR o.not_visible_until IS DISTINCT FROM n.not_visible_until
OR o.suspended IS DISTINCT FROM n.suspended)
) d
LEFT JOIN "arbiter"."conversations" t ON t.group_key = d.group_key
GROUP BY d.group_key
) sub
WHERE g.group_key = sub.group_key
AND (g.min_priority IS DISTINCT FROM sub.new_min_priority
OR g.min_id IS DISTINCT FROM sub.new_min_id
OR g.next_due IS DISTINCT FROM sub.new_next_due);
-- Step 5: commutative in_flight_until extend and ready_count delta in one write.
UPDATE "arbiter"."conversations_groups" g
SET in_flight_until = GREATEST(g.in_flight_until, s.new_ift),
ready_count = GREATEST(0, g.ready_count + COALESCE(s.delta, 0))
FROM (
SELECT COALESCE(ift.group_key, rc.group_key) AS group_key, ift.new_ift, rc.delta
FROM (
SELECT n.group_key, MAX(n.not_visible_until) AS new_ift
FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND n.not_visible_until > NOW()
AND NOT n.suspended
AND n.attempts > 0
AND (o.not_visible_until IS NULL OR o.not_visible_until <= NOW()
OR n.not_visible_until > o.not_visible_until)
GROUP BY n.group_key
) ift
FULL OUTER JOIN (
SELECT group_key, delta FROM (
SELECT n.group_key,
SUM(
(CASE WHEN n.not_visible_until IS NULL AND NOT n.suspended THEN 1 ELSE 0 END)
- (CASE WHEN o.not_visible_until IS NULL AND NOT o.suspended THEN 1 ELSE 0 END)
)::int AS delta
FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND n.group_key IS NOT DISTINCT FROM o.group_key
GROUP BY n.group_key
) z
WHERE delta <> 0
) rc ON ift.group_key = rc.group_key
) s
WHERE g.group_key = s.group_key;
RETURN NULL;
END;
$$;
ALTER FUNCTION arbiter.maintain_conversations_groups_update() OWNER TO "wire-server";
--
-- Name: maintain_meetings_groups_delete(); Type: FUNCTION; Schema: arbiter; Owner: wire-server
--
CREATE FUNCTION arbiter.maintain_meetings_groups_delete() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM old_table WHERE group_key IS NOT NULL LIMIT 1) THEN
RETURN NULL;
END IF;
-- Lock group rows in group_key order to avoid deadlock with concurrent triggers.
PERFORM 1 FROM "arbiter"."meetings_groups" g
WHERE g.group_key IN (SELECT group_key FROM old_table WHERE group_key IS NOT NULL)
ORDER BY g.group_key
FOR UPDATE;
UPDATE "arbiter"."meetings_groups" g
SET job_count = g.job_count - sub.removed_count,
min_priority = COALESCE(sub.new_min_priority, g.min_priority),
min_id = COALESCE(sub.new_min_id, g.min_id),
ready_count = GREATEST(0, g.ready_count - sub.removed_ready_count),
next_due = sub.new_next_due,
in_flight_until = CASE
WHEN sub.had_inflight THEN sub.surviving_ift
ELSE g.in_flight_until
END
FROM (
SELECT d.group_key, d.removed_count, d.removed_ready_count, d.had_inflight,
MIN(t.priority) AS new_min_priority,
MIN(t.id) AS new_min_id,
MIN(t.not_visible_until) FILTER (WHERE t.not_visible_until IS NOT NULL AND NOT t.suspended) AS new_next_due,
MAX(t.not_visible_until) FILTER (WHERE t.not_visible_until > NOW() AND NOT t.suspended AND (t.attempts > 0 OR t.throttled_until > NOW())) AS surviving_ift
FROM (
SELECT group_key, COUNT(*) AS removed_count,
COUNT(*) FILTER (WHERE not_visible_until IS NULL AND NOT suspended) AS removed_ready_count,
bool_or(not_visible_until > NOW() AND NOT suspended AND (attempts > 0 OR throttled_until > NOW())) AS had_inflight
FROM old_table
WHERE group_key IS NOT NULL
GROUP BY group_key
) d
LEFT JOIN "arbiter"."meetings" t ON t.group_key = d.group_key
GROUP BY d.group_key, d.removed_count, d.removed_ready_count, d.had_inflight
) sub
WHERE g.group_key = sub.group_key;
DELETE FROM "arbiter"."meetings_groups"
WHERE job_count <= 0
AND group_key IN (SELECT group_key FROM old_table WHERE group_key IS NOT NULL);
RETURN NULL;
END;
$$;
ALTER FUNCTION arbiter.maintain_meetings_groups_delete() OWNER TO "wire-server";
--
-- Name: maintain_meetings_groups_insert(); Type: FUNCTION; Schema: arbiter; Owner: wire-server
--
CREATE FUNCTION arbiter.maintain_meetings_groups_insert() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM new_table WHERE group_key IS NOT NULL LIMIT 1) THEN
RETURN NULL;
END IF;
-- Lock group rows in group_key order to avoid deadlock with concurrent triggers.
PERFORM 1 FROM "arbiter"."meetings_groups" g
WHERE g.group_key IN (SELECT group_key FROM new_table WHERE group_key IS NOT NULL)
ORDER BY g.group_key
FOR UPDATE;
INSERT INTO "arbiter"."meetings_groups" (group_key, min_priority, min_id, job_count, ready_count, next_due)
SELECT group_key,
MIN(priority),
MIN(id),
COUNT(*),
COUNT(*) FILTER (WHERE not_visible_until IS NULL AND NOT suspended),
MIN(not_visible_until) FILTER (WHERE not_visible_until IS NOT NULL AND NOT suspended)
FROM new_table
WHERE group_key IS NOT NULL
GROUP BY group_key
ORDER BY group_key
ON CONFLICT (group_key) DO UPDATE SET
min_priority = LEAST("arbiter"."meetings_groups".min_priority, EXCLUDED.min_priority),
min_id = LEAST("arbiter"."meetings_groups".min_id, EXCLUDED.min_id),
job_count = "arbiter"."meetings_groups".job_count + EXCLUDED.job_count,
ready_count = "arbiter"."meetings_groups".ready_count + EXCLUDED.ready_count,
next_due = LEAST("arbiter"."meetings_groups".next_due, EXCLUDED.next_due),
in_flight_until = CASE WHEN "arbiter"."meetings_groups".in_flight_until <= NOW()
THEN NULL ELSE "arbiter"."meetings_groups".in_flight_until END;
RETURN NULL;
END;
$$;
ALTER FUNCTION arbiter.maintain_meetings_groups_insert() OWNER TO "wire-server";
--
-- Name: maintain_meetings_groups_update(); Type: FUNCTION; Schema: arbiter; Owner: wire-server
--
CREATE FUNCTION arbiter.maintain_meetings_groups_update() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM new_table WHERE group_key IS NOT NULL LIMIT 1
) AND NOT EXISTS (
SELECT 1 FROM old_table WHERE group_key IS NOT NULL LIMIT 1
) THEN
RETURN NULL;
END IF;
-- Lock group rows (old and new) in group_key order to avoid deadlock with concurrent triggers.
PERFORM 1 FROM "arbiter"."meetings_groups" g
WHERE g.group_key IN (
SELECT group_key FROM new_table WHERE group_key IS NOT NULL
UNION
SELECT group_key FROM old_table WHERE group_key IS NOT NULL
)
ORDER BY g.group_key
FOR UPDATE;
-- Step 1: Full rescan - recompute in_flight_until when not_visible_until decreases or suspended changes
UPDATE "arbiter"."meetings_groups" g
SET in_flight_until = sub.new_ift
FROM (
SELECT t.group_key,
MAX(t.not_visible_until) FILTER (
WHERE t.not_visible_until > NOW() AND NOT t.suspended AND (t.attempts > 0 OR t.throttled_until > NOW())
) AS new_ift
FROM "arbiter"."meetings" t
WHERE t.group_key IN (
SELECT n.group_key FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND (o.not_visible_until IS DISTINCT FROM n.not_visible_until
OR o.suspended IS DISTINCT FROM n.suspended
OR o.attempts IS DISTINCT FROM n.attempts)
AND (
n.not_visible_until > NOW() AND NOT n.suspended AND n.attempts > 0
AND (o.not_visible_until IS NULL OR o.not_visible_until <= NOW()
OR n.not_visible_until > o.not_visible_until)
) IS NOT TRUE
)
GROUP BY t.group_key
) sub
WHERE g.group_key = sub.group_key
AND g.in_flight_until IS DISTINCT FROM sub.new_ift;
-- Step 2: group_key change (dedup replace) - remove from old group
UPDATE "arbiter"."meetings_groups" g
SET job_count = g.job_count - sub.cnt,
min_priority = COALESCE(sub.new_min_priority, g.min_priority),
min_id = COALESCE(sub.new_min_id, g.min_id),
ready_count = GREATEST(0, g.ready_count - sub.removed_ready_count),
next_due = sub.new_next_due,
in_flight_until = CASE
WHEN sub.had_inflight THEN sub.surviving_ift
ELSE g.in_flight_until
END
FROM (
SELECT d.group_key, d.cnt, d.removed_ready_count, d.had_inflight,
MIN(t.priority) AS new_min_priority, MIN(t.id) AS new_min_id,
MIN(t.not_visible_until) FILTER (WHERE t.not_visible_until IS NOT NULL AND NOT t.suspended) AS new_next_due,
MAX(t.not_visible_until) FILTER (WHERE t.not_visible_until > NOW() AND NOT t.suspended AND (t.attempts > 0 OR t.throttled_until > NOW())) AS surviving_ift
FROM (
SELECT o.group_key, COUNT(*) AS cnt,
COUNT(*) FILTER (WHERE o.not_visible_until IS NULL AND NOT o.suspended) AS removed_ready_count,
bool_or(o.not_visible_until > NOW() AND NOT o.suspended AND (o.attempts > 0 OR o.throttled_until > NOW())) AS had_inflight
FROM old_table o
JOIN new_table n ON o.id = n.id
WHERE o.group_key IS NOT NULL
AND o.group_key IS DISTINCT FROM n.group_key
GROUP BY o.group_key
) d
LEFT JOIN "arbiter"."meetings" t ON t.group_key = d.group_key
GROUP BY d.group_key, d.cnt, d.removed_ready_count, d.had_inflight
) sub
WHERE g.group_key = sub.group_key;
DELETE FROM "arbiter"."meetings_groups"
WHERE job_count <= 0
AND group_key IN (
SELECT o.group_key FROM old_table o
JOIN new_table n ON o.id = n.id
WHERE o.group_key IS NOT NULL
AND o.group_key IS DISTINCT FROM n.group_key
);
-- Step 3: group_key change - add to new group
INSERT INTO "arbiter"."meetings_groups" (group_key, min_priority, min_id, job_count, ready_count, next_due)
SELECT n.group_key, MIN(n.priority), MIN(n.id), COUNT(*),
COUNT(*) FILTER (WHERE n.not_visible_until IS NULL AND NOT n.suspended),
MIN(n.not_visible_until) FILTER (WHERE n.not_visible_until IS NOT NULL AND NOT n.suspended)
FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND o.group_key IS DISTINCT FROM n.group_key
GROUP BY n.group_key
ORDER BY n.group_key
ON CONFLICT (group_key) DO UPDATE SET
min_priority = LEAST("arbiter"."meetings_groups".min_priority, EXCLUDED.min_priority),
min_id = LEAST("arbiter"."meetings_groups".min_id, EXCLUDED.min_id),
job_count = "arbiter"."meetings_groups".job_count + EXCLUDED.job_count,
ready_count = "arbiter"."meetings_groups".ready_count + EXCLUDED.ready_count,
next_due = LEAST("arbiter"."meetings_groups".next_due, EXCLUDED.next_due);
-- Step 4: same-group ordering/visibility change - recompute min and next_due.
UPDATE "arbiter"."meetings_groups" g
SET min_priority = sub.new_min_priority,
min_id = sub.new_min_id,
next_due = sub.new_next_due
FROM (
SELECT d.group_key,
MIN(t.priority) AS new_min_priority,
MIN(t.id) AS new_min_id,
MIN(t.not_visible_until) FILTER (WHERE t.not_visible_until IS NOT NULL AND NOT t.suspended) AS new_next_due
FROM (
SELECT DISTINCT n.group_key
FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND n.group_key IS NOT DISTINCT FROM o.group_key
AND (n.priority IS DISTINCT FROM o.priority
OR o.not_visible_until IS DISTINCT FROM n.not_visible_until
OR o.suspended IS DISTINCT FROM n.suspended)
) d
LEFT JOIN "arbiter"."meetings" t ON t.group_key = d.group_key
GROUP BY d.group_key
) sub
WHERE g.group_key = sub.group_key
AND (g.min_priority IS DISTINCT FROM sub.new_min_priority
OR g.min_id IS DISTINCT FROM sub.new_min_id
OR g.next_due IS DISTINCT FROM sub.new_next_due);
-- Step 5: commutative in_flight_until extend and ready_count delta in one write.
UPDATE "arbiter"."meetings_groups" g
SET in_flight_until = GREATEST(g.in_flight_until, s.new_ift),
ready_count = GREATEST(0, g.ready_count + COALESCE(s.delta, 0))
FROM (
SELECT COALESCE(ift.group_key, rc.group_key) AS group_key, ift.new_ift, rc.delta
FROM (
SELECT n.group_key, MAX(n.not_visible_until) AS new_ift
FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND n.not_visible_until > NOW()
AND NOT n.suspended
AND n.attempts > 0
AND (o.not_visible_until IS NULL OR o.not_visible_until <= NOW()
OR n.not_visible_until > o.not_visible_until)
GROUP BY n.group_key
) ift
FULL OUTER JOIN (
SELECT group_key, delta FROM (
SELECT n.group_key,
SUM(
(CASE WHEN n.not_visible_until IS NULL AND NOT n.suspended THEN 1 ELSE 0 END)
- (CASE WHEN o.not_visible_until IS NULL AND NOT o.suspended THEN 1 ELSE 0 END)
)::int AS delta
FROM new_table n
JOIN old_table o ON o.id = n.id
WHERE n.group_key IS NOT NULL
AND n.group_key IS NOT DISTINCT FROM o.group_key
GROUP BY n.group_key
) z
WHERE delta <> 0
) rc ON ift.group_key = rc.group_key
) s
WHERE g.group_key = s.group_key;
RETURN NULL;
END;
$$;
ALTER FUNCTION arbiter.maintain_meetings_groups_update() OWNER TO "wire-server";
--
-- Name: notify_conversations_created(); Type: FUNCTION; Schema: arbiter; Owner: wire-server
--
CREATE FUNCTION arbiter.notify_conversations_created() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('conversations_created', '');
RETURN NEW;
END;
$$;
ALTER FUNCTION arbiter.notify_conversations_created() OWNER TO "wire-server";
--
-- Name: notify_meetings_created(); Type: FUNCTION; Schema: arbiter; Owner: wire-server
--
CREATE FUNCTION arbiter.notify_meetings_created() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('meetings_created', '');
RETURN NEW;
END;
$$;
ALTER FUNCTION arbiter.notify_meetings_created() OWNER TO "wire-server";
--
-- Name: update_updated_at(); Type: FUNCTION; Schema: public; Owner: wire-server
--
CREATE FUNCTION public.update_updated_at() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$;
ALTER FUNCTION public.update_updated_at() OWNER TO "wire-server";
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: arbiter_concurrency; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE UNLOGGED TABLE arbiter.arbiter_concurrency (
concurrency_key text NOT NULL,
concurrency_prefix text NOT NULL,
in_flight integer DEFAULT 0 NOT NULL
)
WITH (fillfactor='80');
ALTER TABLE arbiter.arbiter_concurrency OWNER TO "wire-server";
--
-- Name: arbiter_concurrency_policies; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.arbiter_concurrency_policies (
prefix_id text NOT NULL,
default_limit integer NOT NULL,
override_limit integer,
CONSTRAINT arbiter_concurrency_policies_default_limit_check CHECK ((default_limit > 0)),
CONSTRAINT arbiter_concurrency_policies_override_limit_check CHECK ((override_limit >= 0))
);
ALTER TABLE arbiter.arbiter_concurrency_policies OWNER TO "wire-server";
--
-- Name: arbiter_gates; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.arbiter_gates (
task_name text NOT NULL,
last_run_at timestamp with time zone DEFAULT '1970-01-01 00:00:00+00'::timestamp with time zone NOT NULL
);
ALTER TABLE arbiter.arbiter_gates OWNER TO "wire-server";
--
-- Name: arbiter_queues; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.arbiter_queues (
queue_name text NOT NULL,
paused boolean DEFAULT false NOT NULL,
paused_at timestamp with time zone,
metadata jsonb,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
ALTER TABLE arbiter.arbiter_queues OWNER TO "wire-server";
--
-- Name: arbiter_rate_limit_policies; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.arbiter_rate_limit_policies (
prefix_id text NOT NULL,
default_max_tokens double precision NOT NULL,
default_refill_amount double precision NOT NULL,
default_interval double precision NOT NULL,
override_max_tokens double precision,
override_refill_amount double precision,
override_interval double precision,
CONSTRAINT arbiter_rate_limit_policies_default_interval_check CHECK ((default_interval > (0)::double precision)),
CONSTRAINT arbiter_rate_limit_policies_default_max_tokens_check CHECK ((default_max_tokens >= (0)::double precision)),
CONSTRAINT arbiter_rate_limit_policies_default_refill_amount_check CHECK ((default_refill_amount >= (0)::double precision)),
CONSTRAINT arbiter_rate_limit_policies_override_interval_check CHECK ((override_interval > (0)::double precision)),
CONSTRAINT arbiter_rate_limit_policies_override_max_tokens_check CHECK ((override_max_tokens >= (0)::double precision)),
CONSTRAINT arbiter_rate_limit_policies_override_refill_amount_check CHECK ((override_refill_amount >= (0)::double precision))
);
ALTER TABLE arbiter.arbiter_rate_limit_policies OWNER TO "wire-server";
--
-- Name: arbiter_rate_limits; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE UNLOGGED TABLE arbiter.arbiter_rate_limits (
rate_limit_key text NOT NULL,
policy_prefix text NOT NULL,
tokens double precision NOT NULL,
last_refill timestamp with time zone NOT NULL
)
WITH (fillfactor='80');
ALTER TABLE arbiter.arbiter_rate_limits OWNER TO "wire-server";
--
-- Name: arbiter_workers; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.arbiter_workers (
worker_id uuid NOT NULL,
queue_name text NOT NULL,
host_name text,
worker_count integer,
started_at timestamp with time zone DEFAULT now() NOT NULL,
last_heartbeat timestamp with time zone DEFAULT now() NOT NULL,
shutting_down boolean DEFAULT false NOT NULL,
paused boolean DEFAULT false NOT NULL,
stale_threshold_secs double precision DEFAULT 300 NOT NULL,
metadata jsonb
);
ALTER TABLE arbiter.arbiter_workers OWNER TO "wire-server";
--
-- Name: conversations; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.conversations (
id bigint NOT NULL,
payload jsonb NOT NULL,
group_key text,
inserted_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone,
last_attempted_at timestamp with time zone,
not_visible_until timestamp with time zone,
attempts integer DEFAULT 0 NOT NULL,
last_error text,
priority integer DEFAULT 0 NOT NULL,
dedup_key text,
dedup_strategy text,
max_attempts integer DEFAULT 10,
parent_id bigint,
parent_state jsonb,
suspended boolean DEFAULT false NOT NULL,
claimed_by uuid,
rate_limit_key text,
rate_limit_prefix text,
throttled_until timestamp with time zone,
concurrency_key text,
concurrency_prefix text,
rate_limit_cost double precision DEFAULT 1 NOT NULL,
cancel_requested_at timestamp with time zone,
archive_for integer
)
WITH (fillfactor='100');
ALTER TABLE arbiter.conversations OWNER TO "wire-server";
--
-- Name: conversations_archive; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.conversations_archive (
id bigint NOT NULL,
completed_at timestamp with time zone DEFAULT now() NOT NULL,
archive_expires_at timestamp with time zone NOT NULL,
job_id bigint NOT NULL,
claimed_by uuid,
archive_for integer,
rate_limit_key text,
rate_limit_prefix text,
rate_limit_cost double precision,
concurrency_key text,
concurrency_prefix text,
result jsonb,
payload jsonb NOT NULL,
group_key text,
inserted_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone,
last_attempted_at timestamp with time zone,
not_visible_until timestamp with time zone,
attempts integer DEFAULT 0 NOT NULL,
last_error text,
priority integer DEFAULT 0 NOT NULL,
dedup_key text,
dedup_strategy text,
max_attempts integer,
parent_id bigint,
parent_state jsonb,
suspended boolean DEFAULT false NOT NULL
);
ALTER TABLE arbiter.conversations_archive OWNER TO "wire-server";
--
-- Name: conversations_archive_id_seq; Type: SEQUENCE; Schema: arbiter; Owner: wire-server
--
CREATE SEQUENCE arbiter.conversations_archive_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE arbiter.conversations_archive_id_seq OWNER TO "wire-server";
--
-- Name: conversations_archive_id_seq; Type: SEQUENCE OWNED BY; Schema: arbiter; Owner: wire-server
--
ALTER SEQUENCE arbiter.conversations_archive_id_seq OWNED BY arbiter.conversations_archive.id;
--
-- Name: conversations_dlq; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.conversations_dlq (
id bigint NOT NULL,
failed_at timestamp with time zone DEFAULT now() NOT NULL,
job_id bigint NOT NULL,
payload jsonb NOT NULL,
group_key text,
inserted_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone,
last_attempted_at timestamp with time zone,
not_visible_until timestamp with time zone,
attempts integer DEFAULT 0 NOT NULL,
last_error text,
priority integer DEFAULT 0 NOT NULL,
dedup_key text,
dedup_strategy text,
max_attempts integer,
parent_id bigint,
parent_state jsonb,
suspended boolean DEFAULT false NOT NULL,
claimed_by uuid,
rate_limit_key text,
rate_limit_prefix text,
concurrency_key text,
concurrency_prefix text,
rate_limit_cost double precision DEFAULT 1 NOT NULL,
archive_for integer
);
ALTER TABLE arbiter.conversations_dlq OWNER TO "wire-server";
--
-- Name: conversations_dlq_id_seq; Type: SEQUENCE; Schema: arbiter; Owner: wire-server
--
CREATE SEQUENCE arbiter.conversations_dlq_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE arbiter.conversations_dlq_id_seq OWNER TO "wire-server";
--
-- Name: conversations_dlq_id_seq; Type: SEQUENCE OWNED BY; Schema: arbiter; Owner: wire-server
--
ALTER SEQUENCE arbiter.conversations_dlq_id_seq OWNED BY arbiter.conversations_dlq.id;
--
-- Name: conversations_groups; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.conversations_groups (
group_key text NOT NULL,
min_priority integer DEFAULT 0 NOT NULL,
min_id bigint DEFAULT 0 NOT NULL,
job_count integer DEFAULT 0 NOT NULL,
in_flight_until timestamp with time zone,
ready_count integer DEFAULT 0 NOT NULL,
next_due timestamp with time zone
);
ALTER TABLE arbiter.conversations_groups OWNER TO "wire-server";
--
-- Name: conversations_id_seq; Type: SEQUENCE; Schema: arbiter; Owner: wire-server
--
CREATE SEQUENCE arbiter.conversations_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE arbiter.conversations_id_seq OWNER TO "wire-server";
--
-- Name: conversations_id_seq; Type: SEQUENCE OWNED BY; Schema: arbiter; Owner: wire-server
--
ALTER SEQUENCE arbiter.conversations_id_seq OWNED BY arbiter.conversations.id;
--
-- Name: conversations_results; Type: TABLE; Schema: arbiter; Owner: wire-server
--
CREATE TABLE arbiter.conversations_results (
parent_id bigint NOT NULL,
child_id bigint NOT NULL,
result jsonb NOT NULL
);