openadr 0.6.0

OpenADR 3.1 in Rust: VTN server, VEN runtime, typed client and an open conformance suite
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
//! The Postgres backend.
//!
//! The multi-tenant, utility-scale VTN. SQLite covers the site controller and the single-tenant
//! pilot; this covers the deployment with tens of thousands of VENs, several server instances and
//! a database that is somebody else's job to run.
//!
//! ## Why this is not "the SQLite queries with different placeholders"
//!
//! Two Postgres primitives change the design rather than the syntax:
//!
//! * **`SELECT … FOR UPDATE SKIP LOCKED`** is the right way to claim outbox entries. SQLite has no
//!   equivalent, so there the lease columns *are* the mechanism and two dispatchers contend on one
//!   write lock. Here the lease is only a recovery net for a dispatcher that dies mid-delivery, and
//!   claiming scales with the number of dispatchers.
//! * **`LISTEN`/`NOTIFY`** wakes a dispatcher when a write commits, so notification latency is the
//!   commit rather than the poll interval. That matters when the payload is a curtailment
//!   instruction. See [`Storage::await_outbox`].
//!
//! Target filtering also stops being a placeholder list: `= ANY($1)` binds the whole set as one
//! array parameter, so a VEN granted two hundred targets is one bind rather than two hundred.
//!
//! ## Shape
//!
//! Identical to SQLite's, deliberately: one table per object holding the request body as JSON plus
//! the columns the API queries, cascades and uniqueness declared in the schema, one `object_target`
//! table so the privacy predicate is one join whatever it filters. The conformance suite is
//! what holds the two to the same behaviour.

use async_trait::async_trait;
use sqlx::{PgPool, Row, postgres::PgPoolOptions};
use std::sync::Arc;

use crate::core::{Grant, GrantIndex, OwnerFilter, TargetFilter, active_window};
use crate::model::notification::AnyObject;
use crate::model::{
    ClientId, Event, EventRequest, ObjectId, ObjectType, Operation, Program, ProgramRequest,
    Report, ReportRequest, Resource, Subscription, SubscriptionRequest, Target, Timestamp, Ven,
};
use crate::vtn::notify::{Delivery, Fanout};

use super::outbox::support;
use super::sql::{
    cooldown_end, decode, decode_subscription, encode, encode_subscription, id, not_found,
    route_columns, route_from_columns, stamp, time,
};
use super::{
    BreakerPolicy, DeadLetter, EventQuery, OutboxId, OutboxStats, OwnerKind, ProgramQuery, Queued,
    ReportQuery, ReportStats, ResourceQuery, RetryPolicy, Sealer, SharedStorage, Storage,
    StorageError, Subscriber, SubscriberHealth, SubscriptionQuery, VenQuery, sequenced_id,
};

/// The channel a write announces itself on, and a dispatcher listens to.
const OUTBOX_CHANNEL: &str = "openadr_outbox";

/// The schema.
///
/// Applied on connect. The project is pre-release and the schema is authoritative rather than
/// historical: there are no migrations to run.
///
/// Timestamps are `TEXT COLLATE "C"`, not `TIMESTAMPTZ`. `TIMESTAMPTZ` would mean converting
/// through `chrono` or `time` on every read and write, for a value this crate already has as a
/// `jiff::Timestamp`; the text form is written fixed-width by [`stamp`] so it orders correctly, and
/// the `C` collation makes that byte-wise regardless of what the database's default collation does
/// with punctuation.
const SCHEMA: &str = r#"
-- What shape these tables are. One row, checked at start-up: `CREATE TABLE IF NOT EXISTS` adds a
-- missing table and never alters a present one, so without this a schema change half-applies in
-- silence. See `SCHEMA_VERSION`.
CREATE TABLE IF NOT EXISTS schema_version (
    only_row INTEGER PRIMARY KEY CHECK (only_row = 1),
    version  BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS program (
    id            TEXT PRIMARY KEY,
    created_at    TEXT COLLATE "C" NOT NULL,
    modified_at   TEXT COLLATE "C" NOT NULL,
    program_name  TEXT NOT NULL UNIQUE,
    doc           TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS event (
    id           TEXT PRIMARY KEY,
    created_at   TEXT COLLATE "C" NOT NULL,
    modified_at  TEXT COLLATE "C" NOT NULL,
    program_id   TEXT NOT NULL REFERENCES program(id) ON DELETE CASCADE,
    -- The window the event is live over, resolved once at write time, so `?active=` is a
    -- comparison of two columns rather than an interval expansion per event per request.
    -- NULL active_from means "nothing to elapse"; NULL active_to means "never ends".
    active_from  TEXT COLLATE "C",
    active_to    TEXT COLLATE "C",
    doc          TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS event_program ON event(program_id);
CREATE INDEX IF NOT EXISTS event_active ON event(active_to);

CREATE TABLE IF NOT EXISTS report (
    id           TEXT PRIMARY KEY,
    created_at   TEXT COLLATE "C" NOT NULL,
    modified_at  TEXT COLLATE "C" NOT NULL,
    event_id     TEXT NOT NULL REFERENCES event(id) ON DELETE CASCADE,
    client_id    TEXT,
    client_name  TEXT NOT NULL,
    doc          TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS report_event ON report(event_id);
CREATE INDEX IF NOT EXISTS report_client ON report(client_id);
-- Retention sweeps oldest-first over the whole table, which without this is a full scan on every
-- pass of a task that runs for ever.
CREATE INDEX IF NOT EXISTS report_created ON report(created_at);

CREATE TABLE IF NOT EXISTS subscription (
    id           TEXT PRIMARY KEY,
    created_at   TEXT COLLATE "C" NOT NULL,
    modified_at  TEXT COLLATE "C" NOT NULL,
    client_id    TEXT NOT NULL,
    -- 'BL' or 'VEN'. The fan-out runs long after the request that created the row and has no
    -- credential to ask, and a business-logic subscriber evaluated as a VEN with an empty grant
    -- is told about no targeted object at all.
    owner_kind   TEXT NOT NULL DEFAULT 'VEN',
    client_name  TEXT NOT NULL,
    program_id   TEXT REFERENCES program(id) ON DELETE CASCADE,
    doc          TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS subscription_client ON subscription(client_id);

-- Which object types a subscription watches, so ?objects= is an indexed join rather than a
-- JSON scan.
CREATE TABLE IF NOT EXISTS subscription_object (
    subscription_id TEXT NOT NULL REFERENCES subscription(id) ON DELETE CASCADE,
    object_type     TEXT NOT NULL,
    PRIMARY KEY (subscription_id, object_type)
);
-- The fan-out asks "which subscriptions watch this object type", and the primary key above leads
-- with `subscription_id` — so answering it meant walking every subscription in the VTN and probing.
-- On the highest-rate write in the system, `POST /reports`, that is O(all subscriptions) for an
-- answer that is almost always empty: a thousand event subscriptions cost every report write a
-- thousand probes. Leading with `object_type` makes it a range scan over the matching rows only
-- `[D-116]`.
CREATE INDEX IF NOT EXISTS subscription_object_type
    ON subscription_object(object_type, subscription_id);


CREATE TABLE IF NOT EXISTS ven (
    id           TEXT PRIMARY KEY,
    created_at   TEXT COLLATE "C" NOT NULL,
    modified_at  TEXT COLLATE "C" NOT NULL,
    client_id    TEXT NOT NULL UNIQUE,
    ven_name     TEXT NOT NULL UNIQUE,
    doc          TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS resource (
    id             TEXT PRIMARY KEY,
    created_at     TEXT COLLATE "C" NOT NULL,
    modified_at    TEXT COLLATE "C" NOT NULL,
    ven_id         TEXT NOT NULL REFERENCES ven(id) ON DELETE CASCADE,
    resource_name  TEXT NOT NULL,
    doc            TEXT NOT NULL,
    UNIQUE (ven_id, resource_name)
);

-- Targets for every kind of object that carries them, in one table so the privacy predicate is one
-- join whatever it is filtering.
CREATE TABLE IF NOT EXISTS object_target (
    object_id    TEXT NOT NULL,
    object_type  TEXT NOT NULL,
    target       TEXT NOT NULL,
    PRIMARY KEY (object_id, object_type, target)
);
CREATE INDEX IF NOT EXISTS object_target_lookup ON object_target(target, object_type);

-- Monotonic identifiers, so ordering by (created_at, id) is stable and pages never reshuffle.
CREATE TABLE IF NOT EXISTS id_sequence (
    name  TEXT PRIMARY KEY,
    next  BIGINT NOT NULL
);

-- The transactional outbox. One row per (change, recipient), written in the same transaction as the
-- change itself, so a notification cannot be lost to a crash between the two.
-- The subscriber circuit breaker. Only the unhealthy have a row, so the table's size is the size
-- of the problem and a working VTN reads nothing here.
CREATE TABLE IF NOT EXISTS subscriber_health (
    subscription_id      TEXT PRIMARY KEY REFERENCES subscription(id) ON DELETE CASCADE,
    consecutive_failures INTEGER NOT NULL DEFAULT 0,
    -- When the breaker last opened, and when one probe will be let through. NULL means closed.
    cut_off_since        TEXT COLLATE "C",
    retry_at             TEXT COLLATE "C",
    last_error           TEXT
);

CREATE TABLE IF NOT EXISTS outbox (
    id               BIGSERIAL PRIMARY KEY,
    enqueued_at      TEXT COLLATE "C" NOT NULL,
    subscription_id  TEXT,
    -- What the notification is about. Not needed to deliver it, but an operator looking at a stuck
    -- or abandoned row needs to know which event or report it concerns.
    object_type      TEXT NOT NULL,
    object_id        TEXT NOT NULL,
    callback_url     TEXT,
    bearer_token     TEXT,
    topic            TEXT,
    notification     TEXT NOT NULL,
    attempts         INTEGER NOT NULL DEFAULT 0,
    -- NULL means abandoned: attempts are exhausted and it will not be tried again.
    next_attempt_at  TEXT COLLATE "C",
    -- A lease held by a dispatcher. With SKIP LOCKED this is not how entries are claimed; it is how
    -- an entry held by a dispatcher that died is eventually reclaimed.
    lease_until      TEXT COLLATE "C",
    lease_owner      TEXT,
    last_error       TEXT
);
CREATE INDEX IF NOT EXISTS outbox_due ON outbox(next_attempt_at, lease_until);
"#;

/// A durable store backed by Postgres.
pub struct PostgresStorage {
    pool: PgPool,
    url: String,
    /// Seals the credentials this VTN holds on a subscriber's behalf. [`Sealer::Plain`] by
    /// default — durable storage without a key stores them as given, and `openadr vtn` warns.
    sealer: Sealer,
    /// The `LISTEN` connection, opened on first use.
    ///
    /// Separate from the pool because a listening connection is not returnable to it: it is blocked
    /// in `recv`. Behind a mutex because `recv` needs `&mut`, and behind an `Option` because a VTN
    /// that never runs a dispatcher should not hold a connection open for nothing.
    listener: tokio::sync::Mutex<Option<sqlx::postgres::PgListener>>,
}

impl std::fmt::Debug for PostgresStorage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PostgresStorage").finish_non_exhaustive()
    }
}

impl PostgresStorage {
    /// Connect and apply the schema.
    pub async fn open(url: &str) -> Result<Self, StorageError> {
        let pool = PgPoolOptions::new()
            .max_connections(16)
            .connect(url)
            .await
            .map_err(|e| StorageError::Unavailable(e.to_string()))?;

        let store = Self {
            pool,
            url: url.to_string(),
            sealer: Sealer::plain(),
            listener: tokio::sync::Mutex::new(None),
        };
        store.apply_schema().await?;
        Ok(store)
    }

    /// Seal subscriber credentials at rest with a 32-byte key.
    ///
    /// See [`Sealer`]. Without one, a subscription's `bearerToken` is stored as given.
    pub fn with_encryption_key(mut self, key: &[u8]) -> Result<Self, StorageError> {
        self.sealer = Sealer::with_key(key)?;
        Ok(self)
    }

    /// Connect and wrap in an `Arc`, ready for [`crate::vtn::VtnBuilder::storage`].
    pub async fn shared(url: &str) -> Result<SharedStorage, StorageError> {
        Ok(Arc::new(Self::open(url).await?))
    }

    /// Apply the schema, under an advisory lock so that two instances may start at once.
    ///
    /// `CREATE TABLE IF NOT EXISTS` is not safe to run concurrently on PostgreSQL: the existence
    /// check and the creation are not one step, so two sessions racing on a table both decide to
    /// create it and the loser fails with an error naming an internal catalogue index. This is the
    /// shared-VTN backend, so two instances starting at once is a rolling deploy rather than an edge
    /// case (D-125).
    ///
    /// The lock is transaction-scoped, so it is released on commit or on the connection dying and a
    /// process killed mid-schema blocks nobody.
    async fn apply_schema(&self) -> Result<(), StorageError> {
        /// `openadr` schema lock. Arbitrary, and only ever compared with itself.
        const SCHEMA_LOCK: i64 = 0x0AD3_5CE3_0000_0001_u64 as i64;

        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query("SELECT pg_advisory_xact_lock($1)")
            .bind(SCHEMA_LOCK)
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        sqlx::raw_sql(SCHEMA).execute(&mut *tx).await.map_err(db)?;

        // Inside the advisory lock and inside the transaction, so two VTNs starting at once
        // cannot both read "no stamp" and both decide they are the first (D-125).
        let found: Option<i64> = sqlx::query_scalar("SELECT version FROM schema_version")
            .fetch_optional(&mut *tx)
            .await
            .map_err(db)?;
        super::check_schema_version(found)?;
        sqlx::query(
            "INSERT INTO schema_version (only_row, version) VALUES (1, $1)
             ON CONFLICT (only_row) DO UPDATE SET version = EXCLUDED.version",
        )
        .bind(super::SCHEMA_VERSION)
        .execute(&mut *tx)
        .await
        .map_err(db)?;

        tx.commit().await.map_err(db)?;
        Ok(())
    }

    /// Empty every table. For tests, which want a clean database per behaviour.
    #[doc(hidden)]
    pub async fn truncate_all(&self) -> Result<(), StorageError> {
        sqlx::raw_sql(
            "TRUNCATE program, event, report, subscription, subscription_object, subscriber_health, \
             ven, resource, object_target, id_sequence, outbox RESTART IDENTITY CASCADE",
        )
        .execute(&self.pool)
        .await
        .map_err(db)?;
        Ok(())
    }

    /// Mint an identifier that sorts in creation order.
    async fn next_id(
        tx: &mut sqlx::PgConnection,
        kind: ObjectType,
    ) -> Result<ObjectId, StorageError> {
        let name = kind.collection();
        // One statement: the upsert returns the value it wrote, so there is no read-back to race.
        let n: i64 = sqlx::query(
            "INSERT INTO id_sequence (name, next) VALUES ($1, 1) \
             ON CONFLICT (name) DO UPDATE SET next = id_sequence.next + 1 \
             RETURNING next",
        )
        .bind(name)
        .fetch_one(&mut *tx)
        .await
        .map_err(db)?
        .get(0);

        Ok(sequenced_id(kind, n))
    }

    /// Queue the notifications a change produces, inside the transaction that made it.
    ///
    /// The `NOTIFY` is transactional too: Postgres holds it until commit, so a dispatcher is never
    /// woken for a change that then rolled back.
    async fn queue(
        tx: &mut sqlx::PgConnection,
        fanout: &Fanout,
        object: AnyObject,
        operation: Operation,
        sealer: &Sealer,
    ) -> Result<(), StorageError> {
        let deliveries = fanout.deliveries(&object, operation);
        if deliveries.is_empty() {
            return Ok(());
        }
        insert_deliveries(tx, &deliveries, fanout.now(), sealer).await?;
        announce(tx).await
    }

    /// The objects a cascade is about to take with it.
    ///
    /// A cascade deletes objects a subscriber asked to hear about, and the database will not
    /// announce them. Reading them first costs two queries on a `DELETE` — and only when something
    /// is actually subscribed — and it is the difference between a VEN learning that its dispatch
    /// was cancelled and holding an event that no longer exists.
    async fn cascaded_from_program(
        tx: &mut sqlx::PgConnection,
        program_id: &ObjectId,
    ) -> Result<Vec<AnyObject>, StorageError> {
        let mut out = Vec::new();
        let events = sqlx::query(
            "SELECT id, created_at, modified_at, doc FROM event WHERE program_id = $1 \
             ORDER BY created_at, id",
        )
        .bind(program_id.as_str())
        .fetch_all(&mut *tx)
        .await
        .map_err(db)?;
        for row in &events {
            out.push(AnyObject::Event(Event {
                id: id(row.get(0))?,
                created_date_time: time(row.get(1))?,
                modification_date_time: time(row.get(2))?,
                object_type: ObjectType::Event,
                content: decode(row.get(3))?,
            }));
        }
        let reports = sqlx::query(
            "SELECT r.id, r.created_at, r.modified_at, r.client_id, r.doc FROM report r \
             JOIN event e ON e.id = r.event_id WHERE e.program_id = $1 ORDER BY r.created_at, r.id",
        )
        .bind(program_id.as_str())
        .fetch_all(&mut *tx)
        .await
        .map_err(db)?;
        for row in &reports {
            let client: Option<String> = row.get(3);
            out.push(AnyObject::Report(Report {
                id: id(row.get(0))?,
                created_date_time: time(row.get(1))?,
                modification_date_time: time(row.get(2))?,
                object_type: ObjectType::Report,
                client_id: client
                    .map(|c| ClientId::new(c).map_err(|e| StorageError::Unavailable(e.to_string())))
                    .transpose()?,
                content: decode(row.get(4))?,
            }));
        }
        let subscriptions = sqlx::query(
            "SELECT id, created_at, modified_at, client_id, doc FROM subscription \
             WHERE program_id = $1 ORDER BY created_at, id",
        )
        .bind(program_id.as_str())
        .fetch_all(&mut *tx)
        .await
        .map_err(db)?;
        for row in &subscriptions {
            out.push(AnyObject::Subscription(Subscription {
                id: id(row.get(0))?,
                created_date_time: time(row.get(1))?,
                modification_date_time: time(row.get(2))?,
                object_type: ObjectType::Subscription,
                client_id: ClientId::new(row.get::<String, _>(3))
                    .map_err(|e| StorageError::Unavailable(e.to_string()))?,
                content: decode(row.get(4))?,
            }));
        }
        Ok(out)
    }

    /// The reports an event's deletion is about to take with it.
    ///
    /// `report.eventID` is a cascading foreign key, so the rows go whether or not anybody says so.
    /// A VEN that filed a compliance report is subscribed to its deletion, and OpenADR has no way
    /// to tell it afterwards that one happened.
    async fn cascaded_from_event(
        tx: &mut sqlx::PgConnection,
        event_id: &ObjectId,
    ) -> Result<Vec<AnyObject>, StorageError> {
        let rows = sqlx::query(
            "SELECT id, created_at, modified_at, client_id, doc FROM report WHERE event_id = $1 \
             ORDER BY created_at, id",
        )
        .bind(event_id.as_str())
        .fetch_all(&mut *tx)
        .await
        .map_err(db)?;
        rows.iter()
            .map(|row| {
                let client: Option<String> = row.get(3);
                Ok(AnyObject::Report(Report {
                    id: id(row.get(0))?,
                    created_date_time: time(row.get(1))?,
                    modification_date_time: time(row.get(2))?,
                    object_type: ObjectType::Report,
                    client_id: client
                        .map(|c| {
                            ClientId::new(c).map_err(|e| StorageError::Unavailable(e.to_string()))
                        })
                        .transpose()?,
                    content: decode(row.get(4))?,
                }))
            })
            .collect()
    }

    /// The resources a VEN's deletion is about to take with it.
    async fn cascaded_from_ven(
        tx: &mut sqlx::PgConnection,
        ven_id: &ObjectId,
    ) -> Result<Vec<AnyObject>, StorageError> {
        let rows = sqlx::query(
            "SELECT id, created_at, modified_at, doc FROM resource WHERE ven_id = $1 \
             ORDER BY created_at, id",
        )
        .bind(ven_id.as_str())
        .fetch_all(&mut *tx)
        .await
        .map_err(db)?;
        rows.iter()
            .map(|row| {
                let mut resource: Resource = decode(row.get(3))?;
                resource.id = id(row.get(0))?;
                resource.created_date_time = time(row.get(1))?;
                resource.modification_date_time = time(row.get(2))?;
                Ok(AnyObject::Resource(resource))
            })
            .collect()
    }

    /// Replace the target rows for an object.
    async fn write_targets(
        tx: &mut sqlx::PgConnection,
        object_id: &ObjectId,
        object_type: ObjectType,
        targets: &[Target],
    ) -> Result<(), StorageError> {
        sqlx::query("DELETE FROM object_target WHERE object_id = $1 AND object_type = $2")
            .bind(object_id.as_str())
            .bind(object_type.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        for target in targets {
            sqlx::query(
                "INSERT INTO object_target (object_id, object_type, target) VALUES ($1, $2, $3) \
                 ON CONFLICT DO NOTHING",
            )
            .bind(object_id.as_str())
            .bind(object_type.as_str())
            .bind(target.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        }
        Ok(())
    }
}

/// Tell any listening dispatcher that the queue has grown.
async fn announce(tx: &mut sqlx::PgConnection) -> Result<(), StorageError> {
    sqlx::query("SELECT pg_notify($1, '')")
        .bind(OUTBOX_CHANNEL)
        .execute(tx)
        .await
        .map_err(db)?;
    Ok(())
}

/// Map a database failure onto a storage error, recognising the constraint violations that the API
/// reports as `409` or `400` rather than `500`.
fn db(e: sqlx::Error) -> StorageError {
    if let sqlx::Error::Database(err) = &e {
        // SQLSTATE, not message text: Postgres localises messages, and a VTN whose conflict
        // detection depends on the server's `lc_messages` is a VTN that returns 500s abroad.
        match err.code().as_deref() {
            Some("23505") => {
                let (object_type, field) = classify_unique(err.constraint().unwrap_or_default());
                return StorageError::Conflict {
                    object_type,
                    field,
                    value: err.message().to_string(),
                };
            }
            Some("23503") => {
                return StorageError::DanglingReference {
                    field: "reference",
                    value: err.message().to_string(),
                };
            }
            _ => {}
        }
    }
    StorageError::Unavailable(e.to_string())
}

/// Map a database error, supplying the value a uniqueness conflict is *about*.
///
/// The database names the column; only the caller knows the value the client sent. Without this the
/// `problem` body quotes the server's own message back — `VEN with clientID "unique constraint
/// failed: ven.client_id" already exists` — which tells the client nothing it can act on and
/// discloses the schema, on a path that is by definition reachable by anyone who can write.
fn conflict_value(e: sqlx::Error, value: impl Fn(&'static str) -> String) -> StorageError {
    match db(e) {
        StorageError::Conflict {
            object_type, field, ..
        } => StorageError::Conflict {
            object_type,
            field,
            value: value(field),
        },
        other => other,
    }
}

/// Recover which constraint a uniqueness violation refers to.
///
/// Postgres names the *constraint*, and the names are the ones the schema declares implicitly:
/// `<table>_<column>_key` for a column `UNIQUE`, `<table>_<cols>_key` for a table one.
fn classify_unique(constraint: &str) -> (ObjectType, &'static str) {
    match constraint {
        "program_program_name_key" => (ObjectType::Program, "programName"),
        "ven_client_id_key" => (ObjectType::Ven, "clientID"),
        "ven_ven_name_key" => (ObjectType::Ven, "venName"),
        "resource_ven_id_resource_name_key" => (ObjectType::Resource, "resourceName"),
        _ => (ObjectType::Program, "unknown"),
    }
}

/// A `WHERE` fragment and the parameter number the caller should use next.
struct Sql {
    clause: String,
    next: usize,
}

/// Render [`TargetFilter`] as a predicate.
///
/// One `= ANY($n)` rather than SQLite's placeholder list, so a VEN granted two hundred targets
/// costs one bind and one plan rather than two hundred of each.
fn target_sql(filter: &TargetFilter, object_type: ObjectType, alias: &str, next: usize) -> Sql {
    let untargeted = format!(
        "NOT EXISTS (SELECT 1 FROM object_target t \
         WHERE t.object_id = {alias}.id AND t.object_type = '{}')",
        object_type.as_str()
    );
    match filter {
        TargetFilter::Any => Sql {
            clause: "TRUE".into(),
            next,
        },
        TargetFilter::Untargeted => Sql {
            clause: untargeted,
            next,
        },
        TargetFilter::UntargetedOrAnyOf(allowed) if allowed.is_empty() => Sql {
            clause: untargeted,
            next,
        },
        TargetFilter::AnyOf(allowed) if allowed.is_empty() => Sql {
            clause: "FALSE".into(),
            next,
        },
        TargetFilter::UntargetedOrAnyOf(_) => Sql {
            clause: format!("({untargeted} OR {})", carries(alias, object_type, next)),
            next: next + 1,
        },
        TargetFilter::AnyOf(_) => Sql {
            clause: carries(alias, object_type, next),
            next: next + 1,
        },
    }
}

/// `EXISTS` over the target table, bound with `= ANY($n)` so a VEN granted two hundred targets is
/// one bind and one query plan rather than two hundred of each.
fn carries(alias: &str, object_type: ObjectType, next: usize) -> String {
    format!(
        "EXISTS (SELECT 1 FROM object_target t \
         WHERE t.object_id = {alias}.id AND t.object_type = '{}' \
         AND t.target = ANY(${next}))",
        object_type.as_str()
    )
}

/// The targets a [`TargetFilter`] binds, if any.
fn target_binds(filter: &TargetFilter) -> Option<Vec<String>> {
    match filter {
        TargetFilter::UntargetedOrAnyOf(allowed) | TargetFilter::AnyOf(allowed)
            if !allowed.is_empty() =>
        {
            Some(allowed.iter().map(|t| t.to_string()).collect())
        }
        _ => None,
    }
}

/// Render [`OwnerFilter`] as a predicate.
fn owner_sql(filter: &OwnerFilter, column: &str, next: usize) -> Sql {
    match filter {
        OwnerFilter::Any => Sql {
            clause: "TRUE".into(),
            next,
        },
        OwnerFilter::Only(_) => Sql {
            clause: format!("{column} = ${next}"),
            next: next + 1,
        },
        OwnerFilter::Nothing => Sql {
            clause: "FALSE".into(),
            next,
        },
    }
}

fn owner_bind(filter: &OwnerFilter) -> Option<ClientId> {
    match filter {
        OwnerFilter::Only(client) => Some(client.clone()),
        _ => None,
    }
}

/// Record which object types a subscription watches.
async fn write_subscription_objects(
    tx: &mut sqlx::PgConnection,
    subscription_id: &ObjectId,
    request: &SubscriptionRequest,
) -> Result<(), StorageError> {
    sqlx::query("DELETE FROM subscription_object WHERE subscription_id = $1")
        .bind(subscription_id.as_str())
        .execute(&mut *tx)
        .await
        .map_err(db)?;
    for operation in &request.object_operations {
        for object_type in &operation.objects {
            sqlx::query(
                "INSERT INTO subscription_object (subscription_id, object_type) VALUES ($1, $2) \
                 ON CONFLICT DO NOTHING",
            )
            .bind(subscription_id.as_str())
            .bind(object_type.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        }
    }
    Ok(())
}

/// Queue every delivery a write produced, in **one** statement.
///
/// One `INSERT` per delivery costs a round trip per recipient inside the transaction, which is most
/// of what the fan-out was measured to cost (`tests/load.rs`, D-116). `UNNEST` makes it one
/// statement; the rows, the transaction and the order are identical, since `outbox.id` is a sequence
/// a multi-row insert assigns in argument order.
async fn insert_deliveries(
    tx: &mut sqlx::PgConnection,
    deliveries: &[Delivery],
    now: Timestamp,
    sealer: &Sealer,
) -> Result<(), StorageError> {
    let mut subscription_ids = Vec::with_capacity(deliveries.len());
    let mut object_types = Vec::with_capacity(deliveries.len());
    let mut object_ids = Vec::with_capacity(deliveries.len());
    let mut callback_urls = Vec::with_capacity(deliveries.len());
    let mut bearer_tokens = Vec::with_capacity(deliveries.len());
    let mut topics = Vec::with_capacity(deliveries.len());
    let mut notifications = Vec::with_capacity(deliveries.len());

    for delivery in deliveries {
        let (object_type, object_id) = support::subject(delivery);
        let (callback_url, bearer_token, topic) = route_columns(&delivery.route, sealer)?;
        subscription_ids.push(delivery.subscription_id.as_ref().map(|s| s.to_string()));
        object_types.push(object_type.to_string());
        object_ids.push(object_id.to_string());
        callback_urls.push(callback_url);
        bearer_tokens.push(bearer_token);
        topics.push(topic);
        notifications.push(encode(&delivery.notification)?);
    }

    sqlx::query(
        "INSERT INTO outbox (enqueued_at, subscription_id, object_type, object_id, \
         callback_url, bearer_token, topic, notification, next_attempt_at) \
         SELECT $1, t.subscription_id, t.object_type, t.object_id, t.callback_url, \
                t.bearer_token, t.topic, t.notification, $1 \
         FROM UNNEST($2::text[], $3::text[], $4::text[], $5::text[], $6::text[], $7::text[], \
                     $8::text[]) \
              AS t(subscription_id, object_type, object_id, callback_url, bearer_token, topic, \
                   notification)",
    )
    .bind(stamp(now))
    .bind(&subscription_ids)
    .bind(&object_types)
    .bind(&object_ids)
    .bind(&callback_urls)
    .bind(&bearer_tokens)
    .bind(&topics)
    .bind(&notifications)
    .execute(tx)
    .await
    .map_err(db)?;
    Ok(())
}

#[async_trait]
impl Storage for PostgresStorage {
    async fn healthy(&self) -> bool {
        sqlx::query("SELECT 1").fetch_one(&self.pool).await.is_ok()
    }

    // -- programs ----------------------------------------------------------

    async fn create_program(
        &self,
        request: ProgramRequest,
        now: Timestamp,
        fanout: &Fanout,
    ) -> Result<Program, StorageError> {
        let mut tx = self.pool.begin().await.map_err(db)?;
        let object_id = Self::next_id(&mut tx, ObjectType::Program).await?;
        sqlx::query(
            "INSERT INTO program (id, created_at, modified_at, program_name, doc) \
             VALUES ($1, $2, $3, $4, $5)",
        )
        .bind(object_id.as_str())
        .bind(stamp(now))
        .bind(stamp(now))
        .bind(request.program_name.as_str())
        .bind(encode(&request)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| conflict_value(e, |_| request.program_name.to_string()))?;
        Self::write_targets(&mut tx, &object_id, ObjectType::Program, &request.targets).await?;

        let program = Program {
            id: object_id,
            created_date_time: now,
            modification_date_time: now,
            object_type: ObjectType::Program,
            content: request,
        };
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Program(program.clone()),
            Operation::Create,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(program)
    }

    async fn get_program(&self, object_id: &ObjectId) -> Result<Program, StorageError> {
        let row = sqlx::query("SELECT id, created_at, modified_at, doc FROM program WHERE id = $1")
            .bind(object_id.as_str())
            .fetch_optional(&self.pool)
            .await
            .map_err(db)?
            .ok_or_else(|| not_found(ObjectType::Program, object_id))?;
        Ok(Program {
            id: id(row.get(0))?,
            created_date_time: time(row.get(1))?,
            modification_date_time: time(row.get(2))?,
            object_type: ObjectType::Program,
            content: decode(row.get(3))?,
        })
    }

    async fn list_programs(&self, query: &ProgramQuery) -> Result<Vec<Program>, StorageError> {
        let filter = query.access.target_filter();
        let targets = target_sql(&filter, ObjectType::Program, "o", 1);
        let mut n = targets.next;
        let mut sql = format!(
            "SELECT o.id, o.created_at, o.modified_at, o.doc FROM program o WHERE {}",
            targets.clause
        );
        if query.program_name.is_some() {
            sql.push_str(&format!(" AND o.program_name = ${n}"));
            n += 1;
        }
        sql.push_str(&format!(
            " ORDER BY o.created_at, o.id LIMIT ${n} OFFSET ${}",
            n + 1
        ));

        let mut q = sqlx::query(sqlx::AssertSqlSafe(sql));
        if let Some(binds) = target_binds(&filter) {
            q = q.bind(binds);
        }
        if let Some(name) = &query.program_name {
            q = q.bind(name.to_string());
        }
        q = q.bind(query.page.limit as i64).bind(query.page.skip as i64);

        let rows = q.fetch_all(&self.pool).await.map_err(db)?;
        rows.iter()
            .map(|row| {
                Ok(Program {
                    id: id(row.get(0))?,
                    created_date_time: time(row.get(1))?,
                    modification_date_time: time(row.get(2))?,
                    object_type: ObjectType::Program,
                    content: decode(row.get(3))?,
                })
            })
            .collect()
    }

    async fn update_program(
        &self,
        object_id: &ObjectId,
        request: ProgramRequest,
        now: Timestamp,
        fanout: &Fanout,
    ) -> Result<Program, StorageError> {
        let existing = self.get_program(object_id).await?;
        // A `PUT` that changes nothing must not bump the modification time: a client that polls
        // on it would see a change that did not happen.
        let modified = if existing.content == request {
            existing.modification_date_time
        } else {
            now
        };

        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query(
            "UPDATE program SET modified_at = $2, program_name = $3, doc = $4 WHERE id = $1",
        )
        .bind(object_id.as_str())
        .bind(stamp(modified))
        .bind(request.program_name.as_str())
        .bind(encode(&request)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| conflict_value(e, |_| request.program_name.to_string()))?;
        Self::write_targets(&mut tx, object_id, ObjectType::Program, &request.targets).await?;

        let program = Program {
            id: object_id.clone(),
            created_date_time: existing.created_date_time,
            modification_date_time: modified,
            object_type: ObjectType::Program,
            content: request,
        };
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Program(program.clone()),
            Operation::Update,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(program)
    }

    async fn delete_program(
        &self,
        object_id: &ObjectId,
        fanout: &Fanout,
    ) -> Result<Program, StorageError> {
        let program = self.get_program(object_id).await?;
        let mut tx = self.pool.begin().await.map_err(db)?;
        let cascaded = if fanout.is_empty() {
            Vec::new()
        } else {
            Self::cascaded_from_program(&mut tx, object_id).await?
        };
        // Events, reports and programme-scoped subscriptions go by cascade. `object_target` is
        // polymorphic and so has no foreign key to be cascaded *through*: every kind of row a
        // cascade removes has to be cleared here by hand. Reports carry no targets; events and
        // subscriptions both do, and the subscriptions were missed (D-123).
        sqlx::query(
            "DELETE FROM object_target WHERE object_id IN \
             (SELECT id FROM event WHERE program_id = $1 \
              UNION ALL SELECT id FROM subscription WHERE program_id = $1)",
        )
        .bind(object_id.as_str())
        .execute(&mut *tx)
        .await
        .map_err(db)?;
        sqlx::query("DELETE FROM object_target WHERE object_id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        sqlx::query("DELETE FROM program WHERE id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        for object in cascaded {
            Self::queue(&mut tx, fanout, object, Operation::Delete, &self.sealer).await?;
        }
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Program(program.clone()),
            Operation::Delete,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(program)
    }

    // -- events ------------------------------------------------------------

    async fn create_event(
        &self,
        request: EventRequest,
        now: Timestamp,
        fanout: &Fanout,
    ) -> Result<Event, StorageError> {
        let window = active_window(&request, now).ok().flatten();
        let mut tx = self.pool.begin().await.map_err(db)?;
        let object_id = Self::next_id(&mut tx, ObjectType::Event).await?;
        sqlx::query(
            "INSERT INTO event (id, created_at, modified_at, program_id, active_from, active_to, doc) \
             VALUES ($1, $2, $3, $4, $5, $6, $7)",
        )
        .bind(object_id.as_str())
        .bind(stamp(now))
        .bind(stamp(now))
        .bind(request.program_id.as_str())
        .bind(window.map(|(from, _)| stamp(from)))
        .bind(window.and_then(|(_, to)| to).map(stamp))
        .bind(encode(&request)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| match db(e) {
            // The only foreign key on an event is its programme.
            StorageError::DanglingReference { .. } => StorageError::DanglingReference {
                field: "programID",
                value: request.program_id.to_string(),
            },
            other => other,
        })?;
        Self::write_targets(&mut tx, &object_id, ObjectType::Event, &request.targets).await?;

        let event = Event {
            id: object_id,
            created_date_time: now,
            modification_date_time: now,
            object_type: ObjectType::Event,
            content: request,
        };
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Event(event.clone()),
            Operation::Create,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(event)
    }

    async fn get_event(&self, object_id: &ObjectId) -> Result<Event, StorageError> {
        let row = sqlx::query("SELECT id, created_at, modified_at, doc FROM event WHERE id = $1")
            .bind(object_id.as_str())
            .fetch_optional(&self.pool)
            .await
            .map_err(db)?
            .ok_or_else(|| not_found(ObjectType::Event, object_id))?;
        Ok(Event {
            id: id(row.get(0))?,
            created_date_time: time(row.get(1))?,
            modification_date_time: time(row.get(2))?,
            object_type: ObjectType::Event,
            content: decode(row.get(3))?,
        })
    }

    async fn list_events(&self, query: &EventQuery) -> Result<Vec<Event>, StorageError> {
        let filter = query.access.target_filter();
        let targets = target_sql(&filter, ObjectType::Event, "o", 1);
        let mut n = targets.next;
        let mut sql = format!(
            "SELECT o.id, o.created_at, o.modified_at, o.doc FROM event o WHERE {}",
            targets.clause
        );
        if query.program_id.is_some() {
            sql.push_str(&format!(" AND o.program_id = ${n}"));
            n += 1;
        }
        if query.active_at.is_some() {
            // `active_from IS NULL` is a report-only event: nothing to elapse, so always live.
            sql.push_str(&format!(
                " AND (o.active_from IS NULL OR o.active_to IS NULL OR o.active_to > ${n})"
            ));
            n += 1;
        }
        sql.push_str(&format!(
            " ORDER BY o.created_at, o.id LIMIT ${n} OFFSET ${}",
            n + 1
        ));

        let mut q = sqlx::query(sqlx::AssertSqlSafe(sql));
        if let Some(binds) = target_binds(&filter) {
            q = q.bind(binds);
        }
        if let Some(program_id) = &query.program_id {
            q = q.bind(program_id.to_string());
        }
        if let Some(at) = query.active_at {
            q = q.bind(stamp(at));
        }
        q = q.bind(query.page.limit as i64).bind(query.page.skip as i64);

        let rows = q.fetch_all(&self.pool).await.map_err(db)?;
        rows.iter()
            .map(|row| {
                Ok(Event {
                    id: id(row.get(0))?,
                    created_date_time: time(row.get(1))?,
                    modification_date_time: time(row.get(2))?,
                    object_type: ObjectType::Event,
                    content: decode(row.get(3))?,
                })
            })
            .collect()
    }

    async fn update_event(
        &self,
        object_id: &ObjectId,
        request: EventRequest,
        now: Timestamp,
        fanout: &Fanout,
    ) -> Result<Event, StorageError> {
        let existing = self.get_event(object_id).await?;
        let modified = if existing.content == request {
            existing.modification_date_time
        } else {
            now
        };
        // The window is derived from the intervals, so it is recomputed with them.
        let window = active_window(&request, now).ok().flatten();

        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query(
            "UPDATE event SET modified_at = $2, program_id = $3, active_from = $4, \
             active_to = $5, doc = $6 WHERE id = $1",
        )
        .bind(object_id.as_str())
        .bind(stamp(modified))
        .bind(request.program_id.as_str())
        .bind(window.map(|(from, _)| stamp(from)))
        .bind(window.and_then(|(_, to)| to).map(stamp))
        .bind(encode(&request)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| match db(e) {
            StorageError::DanglingReference { .. } => StorageError::DanglingReference {
                field: "programID",
                value: request.program_id.to_string(),
            },
            other => other,
        })?;
        Self::write_targets(&mut tx, object_id, ObjectType::Event, &request.targets).await?;

        let event = Event {
            id: object_id.clone(),
            created_date_time: existing.created_date_time,
            modification_date_time: modified,
            object_type: ObjectType::Event,
            content: request,
        };
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Event(event.clone()),
            Operation::Update,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(event)
    }

    async fn delete_event(
        &self,
        object_id: &ObjectId,
        fanout: &Fanout,
    ) -> Result<Event, StorageError> {
        let event = self.get_event(object_id).await?;
        let mut tx = self.pool.begin().await.map_err(db)?;
        let cascaded = if fanout.is_empty() {
            Vec::new()
        } else {
            Self::cascaded_from_event(&mut tx, object_id).await?
        };
        sqlx::query("DELETE FROM object_target WHERE object_id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        sqlx::query("DELETE FROM event WHERE id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        for object in cascaded {
            Self::queue(&mut tx, fanout, object, Operation::Delete, &self.sealer).await?;
        }
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Event(event.clone()),
            Operation::Delete,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(event)
    }

    // -- reports -----------------------------------------------------------

    async fn create_report(
        &self,
        request: ReportRequest,
        owner: Option<ClientId>,
        now: Timestamp,
        fanout: &Fanout,
    ) -> Result<Report, StorageError> {
        let mut tx = self.pool.begin().await.map_err(db)?;
        let object_id = Self::next_id(&mut tx, ObjectType::Report).await?;
        sqlx::query(
            "INSERT INTO report (id, created_at, modified_at, event_id, client_id, \
             client_name, doc) VALUES ($1, $2, $3, $4, $5, $6, $7)",
        )
        .bind(object_id.as_str())
        .bind(stamp(now))
        .bind(stamp(now))
        .bind(request.event_id.as_str())
        .bind(owner.as_ref().map(|c| c.as_str()))
        .bind(request.client_name.as_str())
        .bind(encode(&request)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| match db(e) {
            StorageError::DanglingReference { .. } => StorageError::DanglingReference {
                field: "eventID",
                value: request.event_id.to_string(),
            },
            other => other,
        })?;

        let report = Report {
            id: object_id,
            created_date_time: now,
            modification_date_time: now,
            object_type: ObjectType::Report,
            client_id: owner,
            content: request,
        };
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Report(report.clone()),
            Operation::Create,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(report)
    }

    async fn get_report(&self, object_id: &ObjectId) -> Result<Report, StorageError> {
        let row = sqlx::query(
            "SELECT id, created_at, modified_at, client_id, doc FROM report WHERE id = $1",
        )
        .bind(object_id.as_str())
        .fetch_optional(&self.pool)
        .await
        .map_err(db)?
        .ok_or_else(|| not_found(ObjectType::Report, object_id))?;
        let client: Option<String> = row.get(3);
        Ok(Report {
            id: id(row.get(0))?,
            created_date_time: time(row.get(1))?,
            modification_date_time: time(row.get(2))?,
            object_type: ObjectType::Report,
            client_id: client
                .map(|c| ClientId::new(c).map_err(|e| StorageError::Unavailable(e.to_string())))
                .transpose()?,
            content: decode(row.get(4))?,
        })
    }

    async fn list_reports(&self, query: &ReportQuery) -> Result<Vec<Report>, StorageError> {
        let filter = query.access.owner_filter();
        let owner = owner_sql(&filter, "o.client_id", 1);
        let mut n = owner.next;
        let mut sql = format!(
            "SELECT o.id, o.created_at, o.modified_at, o.client_id, o.doc FROM report o \
             JOIN event e ON e.id = o.event_id WHERE {}",
            owner.clause
        );
        if query.program_id.is_some() {
            sql.push_str(&format!(" AND e.program_id = ${n}"));
            n += 1;
        }
        if query.event_id.is_some() {
            sql.push_str(&format!(" AND o.event_id = ${n}"));
            n += 1;
        }
        if query.client_name.is_some() {
            sql.push_str(&format!(" AND o.client_name = ${n}"));
            n += 1;
        }
        sql.push_str(&format!(
            " ORDER BY o.created_at, o.id LIMIT ${n} OFFSET ${}",
            n + 1
        ));

        let mut q = sqlx::query(sqlx::AssertSqlSafe(sql));
        if let Some(client) = owner_bind(&filter) {
            q = q.bind(client.to_string());
        }
        if let Some(program_id) = &query.program_id {
            q = q.bind(program_id.to_string());
        }
        if let Some(event_id) = &query.event_id {
            q = q.bind(event_id.to_string());
        }
        if let Some(name) = &query.client_name {
            q = q.bind(name.to_string());
        }
        q = q.bind(query.page.limit as i64).bind(query.page.skip as i64);

        let rows = q.fetch_all(&self.pool).await.map_err(db)?;
        rows.iter()
            .map(|row| {
                let client: Option<String> = row.get(3);
                Ok(Report {
                    id: id(row.get(0))?,
                    created_date_time: time(row.get(1))?,
                    modification_date_time: time(row.get(2))?,
                    object_type: ObjectType::Report,
                    client_id: client
                        .map(|c| {
                            ClientId::new(c).map_err(|e| StorageError::Unavailable(e.to_string()))
                        })
                        .transpose()?,
                    content: decode(row.get(4))?,
                })
            })
            .collect()
    }

    async fn update_report(
        &self,
        object_id: &ObjectId,
        request: ReportRequest,
        now: Timestamp,
        fanout: &Fanout,
    ) -> Result<Report, StorageError> {
        let existing = self.get_report(object_id).await?;
        let modified = if existing.content == request {
            existing.modification_date_time
        } else {
            now
        };

        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query(
            "UPDATE report SET modified_at = $2, event_id = $3, client_name = $4, doc = $5 \
             WHERE id = $1",
        )
        .bind(object_id.as_str())
        .bind(stamp(modified))
        .bind(request.event_id.as_str())
        .bind(request.client_name.as_str())
        .bind(encode(&request)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| match db(e) {
            StorageError::DanglingReference { .. } => StorageError::DanglingReference {
                field: "eventID",
                value: request.event_id.to_string(),
            },
            other => other,
        })?;

        let report = Report {
            id: object_id.clone(),
            created_date_time: existing.created_date_time,
            modification_date_time: modified,
            object_type: ObjectType::Report,
            client_id: existing.client_id,
            content: request,
        };
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Report(report.clone()),
            Operation::Update,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(report)
    }

    async fn delete_report(
        &self,
        object_id: &ObjectId,
        fanout: &Fanout,
    ) -> Result<Report, StorageError> {
        let report = self.get_report(object_id).await?;
        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query("DELETE FROM report WHERE id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Report(report.clone()),
            Operation::Delete,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(report)
    }

    // -- subscriptions -----------------------------------------------------

    async fn create_subscription(
        &self,
        request: SubscriptionRequest,
        owner: ClientId,
        owner_kind: OwnerKind,
        now: Timestamp,
        fanout: &Fanout,
    ) -> Result<Subscription, StorageError> {
        let mut tx = self.pool.begin().await.map_err(db)?;
        let object_id = Self::next_id(&mut tx, ObjectType::Subscription).await?;
        sqlx::query(
            "INSERT INTO subscription (id, created_at, modified_at, client_id, owner_kind, \
             client_name, program_id, doc) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
        )
        .bind(object_id.as_str())
        .bind(stamp(now))
        .bind(stamp(now))
        .bind(owner.as_str())
        .bind(owner_kind.as_str())
        .bind(request.client_name.as_str())
        .bind(request.program_id.as_ref().map(|p| p.as_str()))
        .bind(encode_subscription(&request, &self.sealer)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| match db(e) {
            StorageError::DanglingReference { .. } => StorageError::DanglingReference {
                field: "programID",
                value: request
                    .program_id
                    .as_ref()
                    .map(|p| p.to_string())
                    .unwrap_or_default(),
            },
            other => other,
        })?;
        write_subscription_objects(&mut tx, &object_id, &request).await?;
        Self::write_targets(
            &mut tx,
            &object_id,
            ObjectType::Subscription,
            &request.targets,
        )
        .await?;

        let subscription = Subscription {
            id: object_id,
            created_date_time: now,
            modification_date_time: now,
            object_type: ObjectType::Subscription,
            client_id: owner,
            content: request,
        };
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Subscription(subscription.clone()),
            Operation::Create,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(subscription)
    }

    async fn get_subscription(&self, object_id: &ObjectId) -> Result<Subscription, StorageError> {
        let row = sqlx::query(
            "SELECT id, created_at, modified_at, client_id, doc FROM subscription WHERE id = $1",
        )
        .bind(object_id.as_str())
        .fetch_optional(&self.pool)
        .await
        .map_err(db)?
        .ok_or_else(|| not_found(ObjectType::Subscription, object_id))?;
        let client: String = row.get(3);
        Ok(Subscription {
            id: id(row.get(0))?,
            created_date_time: time(row.get(1))?,
            modification_date_time: time(row.get(2))?,
            object_type: ObjectType::Subscription,
            client_id: ClientId::new(client)
                .map_err(|e| StorageError::Unavailable(e.to_string()))?,
            content: decode_subscription(row.get(4), &self.sealer)?,
        })
    }

    async fn list_subscriptions(
        &self,
        query: &SubscriptionQuery,
    ) -> Result<Vec<Subscription>, StorageError> {
        let filter = query.access.owner_filter();
        let owner = owner_sql(&filter, "o.client_id", 1);
        let mut n = owner.next;
        let mut sql = format!(
            "SELECT o.id, o.created_at, o.modified_at, o.client_id, o.doc FROM subscription o \
             WHERE {}",
            owner.clause
        );
        if query.program_id.is_some() {
            sql.push_str(&format!(" AND o.program_id = ${n}"));
            n += 1;
        }
        if query.client_name.is_some() {
            sql.push_str(&format!(" AND o.client_name = ${n}"));
            n += 1;
        }
        if !query.objects.is_empty() {
            sql.push_str(&format!(
                " AND EXISTS (SELECT 1 FROM subscription_object s \
                 WHERE s.subscription_id = o.id AND s.object_type = ANY(${n}))"
            ));
            n += 1;
        }
        sql.push_str(&format!(
            " ORDER BY o.created_at, o.id LIMIT ${n} OFFSET ${}",
            n + 1
        ));

        let mut q = sqlx::query(sqlx::AssertSqlSafe(sql));
        if let Some(client) = owner_bind(&filter) {
            q = q.bind(client.to_string());
        }
        if let Some(program_id) = &query.program_id {
            q = q.bind(program_id.to_string());
        }
        if let Some(name) = &query.client_name {
            q = q.bind(name.to_string());
        }
        if !query.objects.is_empty() {
            let objects: Vec<String> = query.objects.iter().map(|o| o.to_string()).collect();
            q = q.bind(objects);
        }
        q = q.bind(query.page.limit as i64).bind(query.page.skip as i64);

        let rows = q.fetch_all(&self.pool).await.map_err(db)?;
        rows.iter()
            .map(|row| {
                let client: String = row.get(3);
                Ok(Subscription {
                    id: id(row.get(0))?,
                    created_date_time: time(row.get(1))?,
                    modification_date_time: time(row.get(2))?,
                    object_type: ObjectType::Subscription,
                    client_id: ClientId::new(client)
                        .map_err(|e| StorageError::Unavailable(e.to_string()))?,
                    content: decode_subscription(row.get(4), &self.sealer)?,
                })
            })
            .collect()
    }

    async fn subscribers(
        &self,
        object_types: &[ObjectType],
    ) -> Result<Vec<Subscriber>, StorageError> {
        if object_types.is_empty() {
            return Ok(Vec::new());
        }
        let objects: Vec<String> = object_types.iter().map(|o| o.to_string()).collect();
        let rows = sqlx::query(
            "SELECT o.id, o.created_at, o.modified_at, o.client_id, o.owner_kind, o.doc \
             FROM subscription o WHERE EXISTS (SELECT 1 FROM subscription_object s \
             WHERE s.subscription_id = o.id AND s.object_type = ANY($1)) \
             ORDER BY o.created_at, o.id",
        )
        .bind(objects)
        .fetch_all(&self.pool)
        .await
        .map_err(db)?;
        rows.iter()
            .map(|row| {
                let client: String = row.get(3);
                let kind: String = row.get(4);
                Ok(Subscriber {
                    subscription: Subscription {
                        id: id(row.get(0))?,
                        created_date_time: time(row.get(1))?,
                        modification_date_time: time(row.get(2))?,
                        object_type: ObjectType::Subscription,
                        client_id: ClientId::new(client)
                            .map_err(|e| StorageError::Unavailable(e.to_string()))?,
                        content: decode_subscription(row.get(5), &self.sealer)?,
                    },
                    owner: OwnerKind::from_str_or_ven(&kind),
                })
            })
            .collect()
    }

    async fn update_subscription(
        &self,
        object_id: &ObjectId,
        request: SubscriptionRequest,
        now: Timestamp,
        fanout: &Fanout,
    ) -> Result<Subscription, StorageError> {
        let existing = self.get_subscription(object_id).await?;
        let modified = if existing.content == request {
            existing.modification_date_time
        } else {
            now
        };

        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query(
            "UPDATE subscription SET modified_at = $2, client_name = $3, program_id = $4, \
             doc = $5 WHERE id = $1",
        )
        .bind(object_id.as_str())
        .bind(stamp(modified))
        .bind(request.client_name.as_str())
        .bind(request.program_id.as_ref().map(|p| p.as_str()))
        .bind(encode_subscription(&request, &self.sealer)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| match db(e) {
            StorageError::DanglingReference { .. } => StorageError::DanglingReference {
                field: "programID",
                value: request
                    .program_id
                    .as_ref()
                    .map(|p| p.to_string())
                    .unwrap_or_default(),
            },
            other => other,
        })?;
        write_subscription_objects(&mut tx, object_id, &request).await?;
        Self::write_targets(
            &mut tx,
            object_id,
            ObjectType::Subscription,
            &request.targets,
        )
        .await?;

        let subscription = Subscription {
            id: object_id.clone(),
            created_date_time: existing.created_date_time,
            modification_date_time: modified,
            object_type: ObjectType::Subscription,
            client_id: existing.client_id,
            content: request,
        };
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Subscription(subscription.clone()),
            Operation::Update,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(subscription)
    }

    async fn delete_subscription(
        &self,
        object_id: &ObjectId,
        fanout: &Fanout,
    ) -> Result<Subscription, StorageError> {
        let subscription = self.get_subscription(object_id).await?;
        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query("DELETE FROM object_target WHERE object_id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        sqlx::query("DELETE FROM subscription WHERE id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Subscription(subscription.clone()),
            Operation::Delete,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(subscription)
    }

    // -- vens --------------------------------------------------------------

    async fn create_ven(&self, mut ven: Ven, fanout: &Fanout) -> Result<Ven, StorageError> {
        let mut tx = self.pool.begin().await.map_err(db)?;
        let object_id = Self::next_id(&mut tx, ObjectType::Ven).await?;
        sqlx::query(
            "INSERT INTO ven (id, created_at, modified_at, client_id, ven_name, doc) \
             VALUES ($1, $2, $3, $4, $5, $6)",
        )
        .bind(object_id.as_str())
        .bind(stamp(ven.created_date_time))
        .bind(stamp(ven.modification_date_time))
        .bind(ven.client_id.as_str())
        .bind(ven.ven_name.as_str())
        .bind(encode(&ven)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| {
            conflict_value(e, |field| match field {
                "clientID" => ven.client_id.to_string(),
                _ => ven.ven_name.to_string(),
            })
        })?;
        Self::write_targets(&mut tx, &object_id, ObjectType::Ven, &ven.targets).await?;

        ven.id = object_id;
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Ven(ven.clone()),
            Operation::Create,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(ven)
    }

    async fn get_ven(&self, object_id: &ObjectId) -> Result<Ven, StorageError> {
        let row = sqlx::query("SELECT id, created_at, modified_at, doc FROM ven WHERE id = $1")
            .bind(object_id.as_str())
            .fetch_optional(&self.pool)
            .await
            .map_err(db)?
            .ok_or_else(|| not_found(ObjectType::Ven, object_id))?;
        let mut ven: Ven = decode(row.get(3))?;
        ven.id = id(row.get(0))?;
        ven.created_date_time = time(row.get(1))?;
        ven.modification_date_time = time(row.get(2))?;
        Ok(ven)
    }

    async fn get_ven_by_client(&self, client_id: &ClientId) -> Result<Option<Ven>, StorageError> {
        let row =
            sqlx::query("SELECT id, created_at, modified_at, doc FROM ven WHERE client_id = $1")
                .bind(client_id.as_str())
                .fetch_optional(&self.pool)
                .await
                .map_err(db)?;
        row.map(|row| {
            let mut ven: Ven = decode(row.get(3))?;
            ven.id = id(row.get(0))?;
            ven.created_date_time = time(row.get(1))?;
            ven.modification_date_time = time(row.get(2))?;
            Ok(ven)
        })
        .transpose()
    }

    async fn list_vens(&self, query: &VenQuery) -> Result<Vec<Ven>, StorageError> {
        let filter = query.access.owner_filter();
        let owner = owner_sql(&filter, "o.client_id", 1);
        // `?targets=` on an owned collection is an ordinary additive filter, not the privacy rule:
        // `[Def §Object Privacy]` says target hiding is not performed on ven and resource objects.
        let requested = query.access.requested_filter();
        let targets = target_sql(&requested, ObjectType::Ven, "o", owner.next);
        let mut n = targets.next;
        let mut sql = format!(
            "SELECT o.id, o.created_at, o.modified_at, o.doc FROM ven o WHERE {} AND {}",
            owner.clause, targets.clause
        );
        if query.ven_name.is_some() {
            sql.push_str(&format!(" AND o.ven_name = ${n}"));
            n += 1;
        }
        sql.push_str(&format!(
            " ORDER BY o.created_at, o.id LIMIT ${n} OFFSET ${}",
            n + 1
        ));

        let mut q = sqlx::query(sqlx::AssertSqlSafe(sql));
        if let Some(client) = owner_bind(&filter) {
            q = q.bind(client.to_string());
        }
        if let Some(binds) = target_binds(&requested) {
            q = q.bind(binds);
        }
        if let Some(name) = &query.ven_name {
            q = q.bind(name.to_string());
        }
        q = q.bind(query.page.limit as i64).bind(query.page.skip as i64);

        let rows = q.fetch_all(&self.pool).await.map_err(db)?;
        rows.iter()
            .map(|row| {
                let mut ven: Ven = decode(row.get(3))?;
                ven.id = id(row.get(0))?;
                ven.created_date_time = time(row.get(1))?;
                ven.modification_date_time = time(row.get(2))?;
                Ok(ven)
            })
            .collect()
    }

    async fn update_ven(
        &self,
        object_id: &ObjectId,
        ven: Ven,
        fanout: &Fanout,
    ) -> Result<Ven, StorageError> {
        let existing = self.get_ven(object_id).await?;
        let unchanged = existing.client_id == ven.client_id
            && existing.ven_name == ven.ven_name
            && existing.targets == ven.targets
            && existing.attributes == ven.attributes;
        let modified = if unchanged {
            existing.modification_date_time
        } else {
            ven.modification_date_time
        };

        let stored = Ven {
            id: object_id.clone(),
            created_date_time: existing.created_date_time,
            modification_date_time: modified,
            ..ven
        };

        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query(
            "UPDATE ven SET modified_at = $2, client_id = $3, ven_name = $4, doc = $5 \
             WHERE id = $1",
        )
        .bind(object_id.as_str())
        .bind(stamp(modified))
        .bind(stored.client_id.as_str())
        .bind(stored.ven_name.as_str())
        .bind(encode(&stored)?)
        .execute(&mut *tx)
        .await
        .map_err(|e| {
            conflict_value(e, |field| match field {
                "clientID" => stored.client_id.to_string(),
                _ => stored.ven_name.to_string(),
            })
        })?;
        Self::write_targets(&mut tx, object_id, ObjectType::Ven, &stored.targets).await?;
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Ven(stored.clone()),
            Operation::Update,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(stored)
    }

    async fn delete_ven(&self, object_id: &ObjectId, fanout: &Fanout) -> Result<Ven, StorageError> {
        let ven = self.get_ven(object_id).await?;
        let mut tx = self.pool.begin().await.map_err(db)?;
        let cascaded = if fanout.is_empty() {
            Vec::new()
        } else {
            Self::cascaded_from_ven(&mut tx, object_id).await?
        };
        sqlx::query(
            "DELETE FROM object_target WHERE object_id IN \
             (SELECT id FROM resource WHERE ven_id = $1)",
        )
        .bind(object_id.as_str())
        .execute(&mut *tx)
        .await
        .map_err(db)?;
        sqlx::query("DELETE FROM object_target WHERE object_id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        sqlx::query("DELETE FROM ven WHERE id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        for object in cascaded {
            Self::queue(&mut tx, fanout, object, Operation::Delete, &self.sealer).await?;
        }
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Ven(ven.clone()),
            Operation::Delete,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(ven)
    }

    // -- resources ---------------------------------------------------------

    async fn create_resource(
        &self,
        mut resource: Resource,
        fanout: &Fanout,
    ) -> Result<Resource, StorageError> {
        let mut tx = self.pool.begin().await.map_err(db)?;
        let object_id = Self::next_id(&mut tx, ObjectType::Resource).await?;
        sqlx::query(
            "INSERT INTO resource (id, created_at, modified_at, ven_id, resource_name, doc) \
             VALUES ($1, $2, $3, $4, $5, $6)",
        )
        .bind(object_id.as_str())
        .bind(stamp(resource.created_date_time))
        .bind(stamp(resource.modification_date_time))
        .bind(resource.ven_id.as_str())
        .bind(resource.resource_name.as_str())
        .bind(encode(&resource)?)
        .execute(&mut *tx)
        .await
        .map_err(
            |e| match conflict_value(e, |_| resource.resource_name.to_string()) {
                StorageError::DanglingReference { .. } => StorageError::DanglingReference {
                    field: "venID",
                    value: resource.ven_id.to_string(),
                },
                other => other,
            },
        )?;
        Self::write_targets(&mut tx, &object_id, ObjectType::Resource, &resource.targets).await?;

        resource.id = object_id;
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Resource(resource.clone()),
            Operation::Create,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(resource)
    }

    async fn get_resource(&self, object_id: &ObjectId) -> Result<Resource, StorageError> {
        let row =
            sqlx::query("SELECT id, created_at, modified_at, doc FROM resource WHERE id = $1")
                .bind(object_id.as_str())
                .fetch_optional(&self.pool)
                .await
                .map_err(db)?
                .ok_or_else(|| not_found(ObjectType::Resource, object_id))?;
        let mut resource: Resource = decode(row.get(3))?;
        resource.id = id(row.get(0))?;
        resource.created_date_time = time(row.get(1))?;
        resource.modification_date_time = time(row.get(2))?;
        Ok(resource)
    }

    async fn list_resources(&self, query: &ResourceQuery) -> Result<Vec<Resource>, StorageError> {
        // A resource is owned through its VEN, so ownership is a join rather than a column.
        let filter = query.access.owner_filter();
        let owner = owner_sql(&filter, "v.client_id", 1);
        let requested = query.access.requested_filter();
        let targets = target_sql(&requested, ObjectType::Resource, "o", owner.next);
        let mut n = targets.next;
        let mut sql = format!(
            "SELECT o.id, o.created_at, o.modified_at, o.doc FROM resource o \
             JOIN ven v ON v.id = o.ven_id WHERE {} AND {}",
            owner.clause, targets.clause
        );
        if query.ven_id.is_some() {
            sql.push_str(&format!(" AND o.ven_id = ${n}"));
            n += 1;
        }
        if query.resource_name.is_some() {
            sql.push_str(&format!(" AND o.resource_name = ${n}"));
            n += 1;
        }
        sql.push_str(&format!(
            " ORDER BY o.created_at, o.id LIMIT ${n} OFFSET ${}",
            n + 1
        ));

        let mut q = sqlx::query(sqlx::AssertSqlSafe(sql));
        if let Some(client) = owner_bind(&filter) {
            q = q.bind(client.to_string());
        }
        if let Some(binds) = target_binds(&requested) {
            q = q.bind(binds);
        }
        if let Some(ven_id) = &query.ven_id {
            q = q.bind(ven_id.to_string());
        }
        if let Some(name) = &query.resource_name {
            q = q.bind(name.to_string());
        }
        q = q.bind(query.page.limit as i64).bind(query.page.skip as i64);

        let rows = q.fetch_all(&self.pool).await.map_err(db)?;
        rows.iter()
            .map(|row| {
                let mut resource: Resource = decode(row.get(3))?;
                resource.id = id(row.get(0))?;
                resource.created_date_time = time(row.get(1))?;
                resource.modification_date_time = time(row.get(2))?;
                Ok(resource)
            })
            .collect()
    }

    async fn update_resource(
        &self,
        object_id: &ObjectId,
        resource: Resource,
        fanout: &Fanout,
    ) -> Result<Resource, StorageError> {
        let existing = self.get_resource(object_id).await?;
        let unchanged = existing.resource_name == resource.resource_name
            && existing.ven_id == resource.ven_id
            && existing.targets == resource.targets
            && existing.attributes == resource.attributes;
        let stored = Resource {
            id: object_id.clone(),
            created_date_time: existing.created_date_time,
            modification_date_time: if unchanged {
                existing.modification_date_time
            } else {
                resource.modification_date_time
            },
            ..resource
        };

        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query(
            "UPDATE resource SET modified_at = $2, ven_id = $3, resource_name = $4, doc = $5 \
             WHERE id = $1",
        )
        .bind(object_id.as_str())
        .bind(stamp(stored.modification_date_time))
        .bind(stored.ven_id.as_str())
        .bind(stored.resource_name.as_str())
        .bind(encode(&stored)?)
        .execute(&mut *tx)
        .await
        .map_err(
            |e| match conflict_value(e, |_| stored.resource_name.to_string()) {
                StorageError::DanglingReference { .. } => StorageError::DanglingReference {
                    field: "venID",
                    value: stored.ven_id.to_string(),
                },
                other => other,
            },
        )?;
        Self::write_targets(&mut tx, object_id, ObjectType::Resource, &stored.targets).await?;
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Resource(stored.clone()),
            Operation::Update,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(stored)
    }

    async fn delete_resource(
        &self,
        object_id: &ObjectId,
        fanout: &Fanout,
    ) -> Result<Resource, StorageError> {
        let resource = self.get_resource(object_id).await?;
        let mut tx = self.pool.begin().await.map_err(db)?;
        sqlx::query("DELETE FROM object_target WHERE object_id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        sqlx::query("DELETE FROM resource WHERE id = $1")
            .bind(object_id.as_str())
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        Self::queue(
            &mut tx,
            fanout,
            AnyObject::Resource(resource.clone()),
            Operation::Delete,
            &self.sealer,
        )
        .await?;
        tx.commit().await.map_err(db)?;
        Ok(resource)
    }

    // -- privacy support ---------------------------------------------------

    async fn grant_for(&self, client_id: &ClientId) -> Result<Grant, StorageError> {
        // A client's grant is its VEN's targets plus every one of its resources' targets.
        let rows = sqlx::query(
            "SELECT t.target FROM ven v \
             JOIN object_target t ON t.object_id = v.id AND t.object_type = 'VEN' \
             WHERE v.client_id = $1 \
             UNION \
             SELECT t.target FROM ven v \
             JOIN resource r ON r.ven_id = v.id \
             JOIN object_target t ON t.object_id = r.id AND t.object_type = 'RESOURCE' \
             WHERE v.client_id = $1",
        )
        .bind(client_id.as_str())
        .fetch_all(&self.pool)
        .await
        .map_err(db)?;

        Ok(Grant::from_targets(rows.iter().filter_map(|row| {
            Target::new(row.get::<String, _>(0)).ok()
        })))
    }

    async fn dead_letters(&self, limit: usize) -> Result<Vec<DeadLetter>, StorageError> {
        let rows = sqlx::query(
            "SELECT id, enqueued_at, attempts, last_error, subscription_id, object_type, \
                    object_id, callback_url, topic \
             FROM outbox WHERE next_attempt_at IS NULL ORDER BY id DESC LIMIT $1",
        )
        .bind(limit as i64)
        .fetch_all(&self.pool)
        .await
        .map_err(db)?;

        rows.iter()
            .map(|row| {
                let subscription: Option<String> = row.get(4);
                let object_type: String = row.get(5);
                let callback_url: Option<String> = row.get(7);
                let topic: Option<String> = row.get(8);
                Ok(DeadLetter {
                    id: row.get(0),
                    enqueued_at: time(row.get(1))?,
                    // `attempts` is INTEGER here and INTEGER means 32 bits in Postgres.
                    attempts: row.get::<i32, _>(2) as u32,
                    last_error: row.get(3),
                    subscription_id: subscription.as_deref().map(id).transpose()?,
                    object_type: object_type.parse().map_err(|_| {
                        StorageError::Unavailable(format!("unknown object type {object_type:?}"))
                    })?,
                    object_id: id(row.get(6))?,
                    destination: callback_url.or(topic).unwrap_or_default(),
                })
            })
            .collect()
    }

    async fn revive_dead(&self, now: Timestamp) -> Result<u64, StorageError> {
        // The attempt counter is reset with the entry: those attempts were spent against a receiver
        // that was broken, and charging them to a fixed one abandons the entry on the first try.
        let mut tx = self.pool.begin().await.map_err(db)?;
        let result = sqlx::query(
            "UPDATE outbox SET next_attempt_at = $1, attempts = 0, lease_until = NULL, \
                    lease_owner = NULL \
             WHERE next_attempt_at IS NULL",
        )
        .bind(stamp(now))
        .execute(&mut *tx)
        .await
        .map_err(db)?;
        if result.rows_affected() > 0 {
            // The same announcement a write makes. An operator who has just fixed a receiver and
            // pressed retry is watching; waiting out the idle interval for something that is
            // already due is latency with no reason behind it.
            announce(&mut tx).await?;
        }
        tx.commit().await.map_err(db)?;
        Ok(result.rows_affected())
    }

    async fn note_delivery(
        &self,
        subscription: &ObjectId,
        abandoned: bool,
        now: Timestamp,
        policy: &BreakerPolicy,
        error: Option<&str>,
    ) -> Result<bool, StorageError> {
        if !abandoned {
            // Health is the absence of a row, so a delivery that worked is a delete.
            sqlx::query("DELETE FROM subscriber_health WHERE subscription_id = $1")
                .bind(subscription.as_str())
                .execute(&self.pool)
                .await
                .map_err(db)?;
            return Ok(false);
        }

        let mut tx = self.pool.begin().await.map_err(db)?;
        // Count and read the current state in one statement, so two dispatchers abandoning
        // notifications for the same dead subscriber cannot both believe they opened the breaker.
        let row = sqlx::query(
            "INSERT INTO subscriber_health (subscription_id, consecutive_failures, last_error) \
             VALUES ($1, 1, $2) \
             ON CONFLICT (subscription_id) DO UPDATE SET \
                consecutive_failures = subscriber_health.consecutive_failures + 1, \
                last_error = excluded.last_error \
             RETURNING consecutive_failures, (cut_off_since IS NULL)::int",
        )
        .bind(subscription.as_str())
        .bind(error)
        .fetch_one(&mut *tx)
        .await
        .map_err(db)?;
        // `INTEGER` is 32 bits here, as it is for the outbox's `attempts`.
        let failures = u32::try_from(row.get::<i32, _>(0)).unwrap_or(u32::MAX);
        let was_closed: i32 = row.get(1);

        let mut opened = false;
        if policy.trips(failures) {
            opened = was_closed != 0;
            // `COALESCE` so `cut_off_since` says when this outage started, not when it was last
            // extended — which is the number an operator reads.
            sqlx::query(
                "UPDATE subscriber_health \
                 SET retry_at = $2, cut_off_since = COALESCE(cut_off_since, $3) \
                 WHERE subscription_id = $1",
            )
            .bind(subscription.as_str())
            .bind(cooldown_end(now, policy))
            .bind(stamp(now))
            .execute(&mut *tx)
            .await
            .map_err(db)?;
        }
        tx.commit().await.map_err(db)?;
        Ok(opened)
    }

    async fn subscriber_health(&self) -> Result<Vec<SubscriberHealth>, StorageError> {
        let rows = sqlx::query(
            "SELECT subscription_id, consecutive_failures, cut_off_since, retry_at, last_error \
             FROM subscriber_health ORDER BY consecutive_failures DESC, subscription_id",
        )
        .fetch_all(&self.pool)
        .await
        .map_err(db)?;
        rows.iter()
            .map(|row| {
                Ok(SubscriberHealth {
                    subscription_id: id(row.get(0))?,
                    consecutive_failures: u32::try_from(row.get::<i32, _>(1)).unwrap_or(u32::MAX),
                    cut_off_since: row
                        .get::<Option<String>, _>(2)
                        .map(|s| time(&s))
                        .transpose()?,
                    retry_at: row
                        .get::<Option<String>, _>(3)
                        .map(|s| time(&s))
                        .transpose()?,
                    last_error: row.get(4),
                })
            })
            .collect()
    }

    async fn clear_subscriber_health(&self) -> Result<u64, StorageError> {
        let result = sqlx::query("DELETE FROM subscriber_health")
            .execute(&self.pool)
            .await
            .map_err(db)?;
        Ok(result.rows_affected())
    }

    async fn purge_reports(&self, before: Timestamp, limit: usize) -> Result<u64, StorageError> {
        // `FOR UPDATE SKIP LOCKED`, like the outbox claim: several VTN instances may run a
        // retention task, and without it they select the same oldest rows and one of them does the
        // work twice. `created_at` is `stamp`ed under `COLLATE "C"`, so text ordering is
        // chronological ordering.
        let result = sqlx::query(
            "DELETE FROM report WHERE id IN \
             (SELECT id FROM report WHERE created_at < $1 ORDER BY created_at, id \
              LIMIT $2 FOR UPDATE SKIP LOCKED)",
        )
        .bind(stamp(before))
        .bind(limit as i64)
        .execute(&self.pool)
        .await
        .map_err(db)?;
        Ok(result.rows_affected())
    }

    async fn report_stats(&self, now: Timestamp) -> Result<ReportStats, StorageError> {
        let row = sqlx::query("SELECT COUNT(*), MIN(created_at) FROM report")
            .fetch_one(&self.pool)
            .await
            .map_err(db)?;
        let count: i64 = row.get(0);
        let oldest: Option<String> = row.get(1);
        let oldest = oldest.as_deref().map(time).transpose()?;
        Ok(ReportStats {
            count: count.max(0) as u64,
            oldest_seconds: support::age_seconds(now, oldest),
        })
    }

    async fn all_grants(&self) -> Result<Vec<(ObjectId, ClientId, Grant)>, StorageError> {
        let rows = sqlx::query(
            "SELECT v.id, v.client_id, t.target FROM ven v \
             LEFT JOIN object_target t ON t.object_id = v.id AND t.object_type = 'VEN' \
             UNION ALL \
             SELECT v.id, v.client_id, t.target FROM ven v \
             JOIN resource r ON r.ven_id = v.id \
             JOIN object_target t ON t.object_id = r.id AND t.object_type = 'RESOURCE'",
        )
        .fetch_all(&self.pool)
        .await
        .map_err(db)?;

        let mut index = GrantIndex::new();
        let mut vens: Vec<(ObjectId, ClientId)> = Vec::new();
        for row in &rows {
            let ven_id = id(row.get(0))?;
            let client = ClientId::new(row.get::<String, _>(1))
                .map_err(|e| StorageError::Unavailable(e.to_string()))?;
            if !vens.iter().any(|(v, _)| v == &ven_id) {
                vens.push((ven_id, client.clone()));
            }
            if let Some(raw) = row.get::<Option<String>, _>(2)
                && let Ok(target) = Target::new(raw)
            {
                index.add(&client, [target]);
            }
        }
        vens.sort_by(|a, b| a.0.as_str().cmp(b.0.as_str()));
        Ok(vens
            .into_iter()
            .map(|(ven_id, client)| {
                let grant = index.get(&client);
                (ven_id, client, grant)
            })
            .collect())
    }

    // -- outbox ------------------------------------------------------------

    async fn enqueue(&self, deliveries: Vec<Delivery>, now: Timestamp) -> Result<(), StorageError> {
        if deliveries.is_empty() {
            return Ok(());
        }
        let mut tx = self.pool.begin().await.map_err(db)?;
        insert_deliveries(&mut tx, &deliveries, now, &self.sealer).await?;
        announce(&mut tx).await?;
        tx.commit().await.map_err(db)?;
        Ok(())
    }

    async fn claim_due(
        &self,
        now: Timestamp,
        limit: usize,
        lease: core::time::Duration,
        owner: &str,
    ) -> Result<Vec<Queued>, StorageError> {
        let lease_until = now
            .checked_add(jiff::Span::new().seconds(lease.as_secs() as i64))
            .unwrap_or(now);

        // `FOR UPDATE SKIP LOCKED` is the whole reason this backend exists for a busy VTN: N
        // dispatchers claim N disjoint batches without any of them waiting on the others. The lease
        // columns are still written, but only so that an entry held by a dispatcher that died is
        // reclaimed rather than stuck — they are no longer how a claim is made.
        let rows = sqlx::query(
            "UPDATE outbox SET lease_until = $2, lease_owner = $3 \
             WHERE id IN ( \
                 SELECT id FROM outbox \
                 WHERE next_attempt_at IS NOT NULL AND next_attempt_at <= $1 \
                   AND (lease_until IS NULL OR lease_until <= $1) \
                 ORDER BY id LIMIT $4 \
                 FOR UPDATE SKIP LOCKED \
             ) \
             RETURNING id, attempts, subscription_id, callback_url, bearer_token, topic, \
                       notification",
        )
        .bind(stamp(now))
        .bind(stamp(lease_until))
        .bind(owner)
        .bind(limit as i64)
        .fetch_all(&self.pool)
        .await
        .map_err(db)?;

        let mut claimed: Vec<Queued> = rows
            .iter()
            .map(|row| {
                let subscription_id: Option<String> = row.get(2);
                Ok(Queued {
                    id: OutboxId(row.get::<i64, _>(0)),
                    attempts: row.get::<i32, _>(1) as u32,
                    delivery: Delivery {
                        subscription_id: subscription_id
                            .map(|s| {
                                ObjectId::new(s)
                                    .map_err(|e| StorageError::Unavailable(e.to_string()))
                            })
                            .transpose()?,
                        route: route_from_columns(
                            row.get(3),
                            row.get(4),
                            row.get(5),
                            &self.sealer,
                        )?,
                        notification: decode(row.get(6))?,
                    },
                })
            })
            .collect::<Result<_, StorageError>>()?;
        // `RETURNING` has no defined order; the queue is meant to drain in enqueue order.
        claimed.sort_by_key(|q| q.id);
        Ok(claimed)
    }

    async fn complete(&self, id: OutboxId) -> Result<(), StorageError> {
        sqlx::query("DELETE FROM outbox WHERE id = $1")
            .bind(id.0)
            .execute(&self.pool)
            .await
            .map_err(db)?;
        Ok(())
    }

    async fn record_failure(
        &self,
        id: OutboxId,
        failure: &crate::vtn::notify::DeliveryFailure,
        now: Timestamp,
        policy: &RetryPolicy,
    ) -> Result<bool, StorageError> {
        let mut tx = self.pool.begin().await.map_err(db)?;
        let Some(row) = sqlx::query("SELECT attempts FROM outbox WHERE id = $1 FOR UPDATE")
            .bind(id.0)
            .fetch_optional(&mut *tx)
            .await
            .map_err(db)?
        else {
            return Ok(false);
        };
        let attempts = row.get::<i32, _>(0) as u32 + 1;
        let next = failure
            .retriable
            .then(|| support::next_attempt(now, attempts, policy))
            .flatten();

        sqlx::query(
            "UPDATE outbox SET attempts = $2, last_error = $3, next_attempt_at = $4, \
             lease_until = NULL, lease_owner = NULL WHERE id = $1",
        )
        .bind(id.0)
        .bind(attempts as i32)
        .bind(failure.message.as_str())
        .bind(next.map(stamp))
        .execute(&mut *tx)
        .await
        .map_err(db)?;
        tx.commit().await.map_err(db)?;
        Ok(next.is_none())
    }

    async fn outbox_stats(&self, now: Timestamp) -> Result<OutboxStats, StorageError> {
        let row = sqlx::query(
            "SELECT \
                COUNT(*) FILTER (WHERE next_attempt_at IS NOT NULL), \
                COUNT(*) FILTER (WHERE next_attempt_at IS NULL), \
                MIN(enqueued_at) FILTER (WHERE next_attempt_at IS NOT NULL) \
             FROM outbox",
        )
        .fetch_one(&self.pool)
        .await
        .map_err(db)?;

        let oldest: Option<String> = row.get(2);
        let oldest = oldest.as_deref().map(time).transpose()?;
        Ok(OutboxStats {
            pending: row.get::<i64, _>(0) as u64,
            dead: row.get::<i64, _>(1) as u64,
            oldest_pending_seconds: support::age_seconds(now, oldest),
        })
    }

    /// Wait for a write to announce itself, rather than polling.
    ///
    /// A curtailment instruction should not sit in the queue for a poll interval because nothing
    /// was happening a moment ago. `LISTEN` costs one connection and removes that delay entirely.
    ///
    /// The timeout is still honoured, and is what makes this safe: if the listening connection is
    /// lost, or a notification is missed, the dispatcher still wakes up and looks. A missed wake-up
    /// costs latency, never a delivery.
    async fn await_outbox(&self, timeout: core::time::Duration) {
        let mut guard = self.listener.lock().await;
        if guard.is_none() {
            match sqlx::postgres::PgListener::connect(&self.url).await {
                Ok(mut listener) => {
                    if let Err(e) = listener.listen(OUTBOX_CHANNEL).await {
                        tracing::warn!(error = %e, "could not LISTEN; falling back to polling");
                    } else {
                        *guard = Some(listener);
                    }
                }
                Err(e) => {
                    tracing::warn!(error = %e, "could not open a listener; falling back to polling")
                }
            }
        }

        match guard.as_mut() {
            Some(listener) => match tokio::time::timeout(timeout, listener.recv()).await {
                // A notification, or the timeout: either way, go and look.
                Ok(Ok(_)) | Err(_) => {}
                Ok(Err(e)) => {
                    // The listening connection failed. Drop it so the next pass reconnects, and
                    // fall back to the timeout in the meantime — a lost listener costs latency,
                    // never a delivery.
                    tracing::warn!(error = %e, "outbox listener failed; will reconnect");
                    *guard = None;
                }
            },
            None => tokio::time::sleep(timeout).await,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A clean store in a database named after the test that asked for it.
    async fn fresh(test: &str) -> PostgresStorage {
        let url = test_database_for(test).await.expect("a Postgres server");
        let store = PostgresStorage::open(&url).await.unwrap();
        store.truncate_all().await.unwrap();
        store
    }

    fn now() -> Timestamp {
        "2026-01-01T00:00:00Z".parse().unwrap()
    }

    fn delivery(n: usize) -> Delivery {
        use crate::model::notification::Notification;
        Delivery {
            subscription_id: None,
            route: crate::vtn::notify::Route::Webhook {
                callback_url: format!("https://example.com/{n}"),
                bearer_token: None,
            },
            notification: Notification::new(
                Operation::Create,
                AnyObject::Program(Program {
                    id: "prg-1".parse().unwrap(),
                    created_date_time: now(),
                    modification_date_time: now(),
                    object_type: ObjectType::Program,
                    content: ProgramRequest::new("p".parse().unwrap()),
                }),
            ),
        }
    }

    /// The reason this backend exists for a busy VTN.
    #[tokio::test]
    async fn two_dispatchers_claim_disjoint_batches() {
        if test_server().await.is_none() {
            return;
        }
        let store = fresh("dispatchers").await;
        store
            .enqueue((0..8).map(delivery).collect(), now())
            .await
            .unwrap();

        let lease = core::time::Duration::from_secs(60);
        // Concurrently, so the two claims genuinely overlap rather than running in sequence.
        let (a, b) = tokio::join!(
            store.claim_due(now(), 4, lease, "d1"),
            store.claim_due(now(), 4, lease, "d2"),
        );
        let (a, b) = (a.unwrap(), b.unwrap());

        assert_eq!(a.len() + b.len(), 8, "every entry should have been claimed");
        for entry in &a {
            assert!(
                !b.iter().any(|other| other.id == entry.id),
                "two dispatchers claimed the same entry"
            );
        }
        // And nothing is left for a third.
        assert!(
            store
                .claim_due(now(), 8, lease, "d3")
                .await
                .unwrap()
                .is_empty()
        );
    }

    /// A write should wake a dispatcher, not make it wait for the poll interval.
    #[tokio::test]
    async fn a_write_wakes_a_waiting_dispatcher() {
        if test_server().await.is_none() {
            return;
        }
        let store = Arc::new(fresh("listen_notify").await);

        // Prime the listener, so the wait under test is not measuring a connection setup.
        store
            .await_outbox(core::time::Duration::from_millis(50))
            .await;

        let writer = {
            let store = Arc::clone(&store);
            tokio::spawn(async move {
                tokio::time::sleep(core::time::Duration::from_millis(100)).await;
                store.enqueue(vec![delivery(0)], now()).await.unwrap();
            })
        };

        let started = std::time::Instant::now();
        // A generous bound: if `LISTEN` did nothing, this would take the full ten seconds.
        store
            .await_outbox(core::time::Duration::from_secs(10))
            .await;
        let waited = started.elapsed();
        writer.await.unwrap();

        assert!(
            waited < core::time::Duration::from_secs(2),
            "the dispatcher waited {waited:?}, so it polled rather than being told"
        );
        assert_eq!(store.outbox_stats(now()).await.unwrap().pending, 1);
    }

    /// A Postgres to run the conformance suite against, or `None` when there is none to be had.
    ///
    /// `OPENADR_TEST_POSTGRES` first — CI provides a service container, and starting a second one
    /// inside it is slower and no more truthful — then a container started here, so that an
    /// ordinary `cargo test` *runs* this backend rather than skipping it. The suite is the only
    /// thing holding three backends to one set of behaviours (D-032); skipping one quietly reduces
    /// that to two. Started once and reused, with `truncate_all` between behaviours.
    /// The PostgreSQL the storage suite runs against, matching `.github/workflows/ci.yml`.
    const POSTGRES_TAG: &str = "17-alpine";

    /// A database of this test's own, on whatever server `test_server` found.
    ///
    /// Every Postgres test here starts from an empty database, and emptying one means
    /// `TRUNCATE … CASCADE`, which takes an `ACCESS EXCLUSIVE` lock on every table it names. Two
    /// tests sharing a database therefore do not merely interfere — they deadlock, and PostgreSQL
    /// picks one to kill. Cargo runs tests in parallel, so sharing is the default rather than the
    /// accident, and the failure arrives as `deadlock detected` in whichever test lost.
    ///
    /// Created once per name and reused, so a suite that builds a backend per behaviour pays for it
    /// once. `CREATE DATABASE` on one that exists is the reuse, not an error.
    async fn test_database_for(name: &str) -> Option<String> {
        use sqlx::Connection as _;

        let server = test_server().await?;
        let database = format!("openadr_test_{name}");
        let mut conn = sqlx::PgConnection::connect(&server).await.ok()?;
        // Racing another test on the same name is not possible — each names itself — but racing the
        // *server's* catalogue with another `CREATE DATABASE` is, so a duplicate is reuse.
        let _ = sqlx::query(sqlx::AssertSqlSafe(format!("CREATE DATABASE {database}")))
            .execute(&mut conn)
            .await;
        let _ = conn.close().await;

        let (base, _) = server.rsplit_once('/')?;
        Some(format!("{base}/{database}"))
    }

    /// The PostgreSQL server, from the environment or from a container started here.
    async fn test_server() -> Option<String> {
        use tokio::sync::OnceCell;

        if let Ok(url) = std::env::var("OPENADR_TEST_POSTGRES") {
            return Some(url);
        }

        // The container is kept in a `static` so it outlives every behaviour; testcontainers' own
        // reaper removes it when the test process ends.
        static CONTAINER: OnceCell<
            Option<(
                testcontainers::ContainerAsync<testcontainers_modules::postgres::Postgres>,
                String,
            )>,
        > = OnceCell::const_new();

        CONTAINER
            .get_or_init(|| async {
                use testcontainers::ImageExt as _;
                use testcontainers::runners::AsyncRunner as _;
                // Pinned to the version CI runs. The module defaults to Postgres 11, so leaving it
                // alone would test one major version locally and another in CI — a difference that
                // shows up only when a query uses something the older one does not have.
                let container = testcontainers_modules::postgres::Postgres::default()
                    .with_tag(POSTGRES_TAG)
                    .start()
                    .await
                    .inspect_err(|e| {
                        eprintln!(
                            "no Docker for a Postgres container ({e}); set \
                             OPENADR_TEST_POSTGRES to run this backend"
                        )
                    })
                    .ok()?;
                let port = container.get_host_port_ipv4(5432).await.ok()?;
                let url = format!("postgres://postgres:postgres@127.0.0.1:{port}/postgres");
                Some((container, url))
            })
            .await
            .as_ref()
            .map(|(_, url)| url.clone())
    }

    /// A cascade leaves no target rows behind.
    ///
    /// The same assertion the SQLite backend makes, for the same reason: `object_target` is
    /// polymorphic, so it carries no foreign key and no cascade can reach it — every kind of row a
    /// cascade removes has to be cleared by hand, and the programme-scoped subscriptions were not
    /// (D-123). The storage suite structurally cannot state this: the rows are unreachable through
    /// the trait, and the in-memory backend has no such table.
    #[tokio::test]
    async fn deleting_a_programme_leaves_no_orphaned_target_rows() {
        if test_server().await.is_none() {
            return;
        }
        let store = fresh("orphans").await;

        let program_id = super::super::suite::seed_a_cascade_of_targeted_children(&store).await;
        store
            .delete_program(&program_id, &Fanout::none())
            .await
            .unwrap();

        let orphans: i64 = sqlx::query_scalar(super::super::suite::ORPHANED_TARGETS)
            .fetch_one(&store.pool)
            .await
            .unwrap();
        assert_eq!(
            orphans, 0,
            "a programme's cascade left {orphans} target rows pointing at objects that are gone"
        );
    }

    // The conformance suite, against a real Postgres — the one CI provides, or one started here.
    super::super::suite::run_suite!(@optional async {
        let url = test_database_for("suite").await?;
        let store = PostgresStorage::open(&url).await.unwrap();
        // Each behaviour starts from an empty database, exactly as the other backends do.
        store.truncate_all().await.unwrap();
        Some(Arc::new(store) as SharedStorage)
    });
}