pg_walstream 0.9.0

PostgreSQL logical replication protocol library - parse and handle PostgreSQL WAL streaming messages
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
#![cfg(any(feature = "libpq", feature = "rustls-tls"))]

//! Integration tests for the managed initial snapshot.
//!
//! These prove the claims the unit tests cannot: that `COPY` TEXT values are
//! byte-identical to what pgoutput produces, that the snapshot-to-stream handoff
//! has no gap and no duplicate window, and that publication column lists, row
//! filters and partitioned publications resolve to the right tables.
//!
//! ## Prerequisites
//!
//! - PostgreSQL 15+ with `wal_level = logical`
//! - `DATABASE_URL` — a replication connection string
//! - `DATABASE_URL_REGULAR` — a regular connection to the same database
//!
//! ```bash
//! cargo test --test initial_snapshot -- --ignored --nocapture --test-threads=1
//! ```

use pg_walstream::snapshot::SnapshotOutcome;
use pg_walstream::{
    CancellationToken, ChangeEvent, EventType, LogicalReplicationStream, PgReplicationConnection,
    ReplicationSlotOptions, ReplicationStreamConfig, RetryConfig, StreamingMode,
};
use std::time::Duration;

/// Bound for the tests that wrap their body in `tokio::time::timeout`.
///
/// Not every test does — the ones that loop on `next_row()` mostly rely on the
/// job-level `timeout-minutes` in `ci-checks.yml` instead. That is the real
/// backstop; this constant only sharpens the diagnosis where it is applied, by
/// failing the single test rather than the whole job.
const TEST_TIMEOUT: Duration = Duration::from_secs(60);

fn replication_conn_string() -> String {
    std::env::var("DATABASE_URL").unwrap_or_else(|_| {
        "postgresql://postgres:postgres@localhost:5432/test_walstream?replication=database"
            .to_string()
    })
}

fn regular_conn_string() -> String {
    std::env::var("DATABASE_URL_REGULAR").unwrap_or_else(|_| {
        let repl = replication_conn_string();
        repl.replace("?replication=database", "")
            .replace("&replication=database", "")
    })
}

fn regular_conn() -> PgReplicationConnection {
    PgReplicationConnection::connect(&regular_conn_string()).expect("regular connection")
}

fn server_version() -> i32 {
    regular_conn().server_version()
}

/// A config with a unique slot name.
///
/// Deliberately **not** a temporary slot: `snapshot()` rejects those, because a
/// reconnect would re-create the slot at a later consistent point and silently
/// lose everything in between. No snapshot flag is set here — `snapshot()`
/// applies `SNAPSHOT 'export'` itself.
fn snapshot_config(slot: &str, publication: &str) -> ReplicationStreamConfig {
    ReplicationStreamConfig::new(
        slot.to_string(),
        publication.to_string(),
        2,
        StreamingMode::On,
        Duration::from_secs(10),
        Duration::from_secs(30),
        Duration::from_secs(60),
        RetryConfig::default(),
    )
}

/// Drop `slot` if it exists, including when a stream still holds it.
///
/// `pg_drop_replication_slot` fails with "replication slot is active for PID"
/// while a walsender holds it, and the old version swallowed that error — so any
/// test that called `start()` left its slot behind for the whole run. A leaked
/// slot pins WAL indefinitely, which is the one test-hygiene failure that can
/// fill the disk of whoever runs the suite. Terminate the holder first, then
/// retry briefly: the server clears `active_pid` asynchronously after the
/// backend goes away.
fn drop_slot(slot: &str) {
    // Not a silent `return`: once leaked walsenders exhaust `max_wal_senders`
    // this is where it first shows, and skipping cleanup turns that into a
    // misleading failure three tests later.
    let mut conn = PgReplicationConnection::connect(&replication_conn_string())
        .expect("drop_slot: replication connection");
    for attempt in 0..50 {
        let _ = conn.exec(&format!(
            "SELECT pg_terminate_backend(active_pid) FROM pg_replication_slots \
             WHERE slot_name = '{slot}' AND active_pid IS NOT NULL"
        ));
        let _ = conn.exec(&format!(
            "SELECT pg_drop_replication_slot('{slot}') WHERE EXISTS \
             (SELECT 1 FROM pg_replication_slots WHERE slot_name = '{slot}')"
        ));
        match conn.exec(&format!(
            "SELECT count(*) FROM pg_replication_slots WHERE slot_name = '{slot}'"
        )) {
            Ok(r) if probe_count(&r) == 0 => return,
            _ => {}
        }
        if attempt == 49 {
            panic!("could not drop replication slot {slot}; it would pin WAL for the whole run");
        }
        std::thread::sleep(Duration::from_millis(100));
    }
}

/// Drops its slot when it goes out of scope, however the test leaves.
///
/// Without this every test leaked its slot on a failed assertion: the trailing
/// `drop_slot` is unreachable once a panic unwinds. There are ~42 slot names in
/// this file against `max_replication_slots = 16`, so a handful of leaks turns
/// into a run of `53400 all replication slots are in use` failures that bury
/// whichever assertion actually broke.
///
/// Declare it *before* the stream it must outlive, so the stream is dropped —
/// and the slot therefore inactive — by the time `drop_slot` runs.
struct SlotGuard(&'static str);

impl Drop for SlotGuard {
    fn drop(&mut self) {
        // `drop_slot` panics on some paths; a panic inside Drop during unwind
        // aborts the process, so swallow it when we are already unwinding.
        if std::thread::panicking() {
            let _ = std::panic::catch_unwind(|| drop_slot(self.0));
        } else {
            drop_slot(self.0);
        }
    }
}

/// Drop `slot` now, and again when the returned guard goes out of scope.
fn slot_guard(slot: &'static str) -> SlotGuard {
    drop_slot(slot);
    SlotGuard(slot)
}

/// Fresh table + publication, seeded with `rows` names.
fn setup(table: &str, publication: &str, rows: &[&str]) {
    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!(
        "CREATE TABLE {table} (id SERIAL PRIMARY KEY, name TEXT)"
    ))
    .expect("create table");
    for name in rows {
        conn.exec(&format!(
            "INSERT INTO {table} (name) VALUES ('{}')",
            name.replace('\'', "''")
        ))
        .expect("seed");
    }
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("create publication");
}

/// Collect every snapshot event, then return the stream ready for `start()`.
async fn collect_snapshot(
    stream: LogicalReplicationStream,
) -> (Vec<ChangeEvent>, LogicalReplicationStream) {
    match stream.snapshot().await.expect("snapshot") {
        SnapshotOutcome::Unavailable(s) => (Vec::new(), s),
        SnapshotOutcome::Available(snap) => {
            let mut events = snap.events();
            let mut collected = Vec::new();
            while let Some(event) = events.next_event().await.expect("next_event") {
                collected.push(event);
            }
            (collected, events.finish().await.expect("finish"))
        }
    }
}

fn probe_count(result: &pg_walstream::PgResult) -> i64 {
    result
        .get_value(0, 0)
        .and_then(|v| v.parse().ok())
        .unwrap_or(-1)
}

/// `collect_snapshot`, but from an already-resolved outcome.
async fn collect_snapshot_from(
    outcome: SnapshotOutcome,
) -> (Vec<ChangeEvent>, LogicalReplicationStream) {
    match outcome {
        SnapshotOutcome::Unavailable(s) => (Vec::new(), s),
        SnapshotOutcome::Available(snap) => {
            let mut events = snap.events();
            let mut collected = Vec::new();
            while let Some(event) = events.next_event().await.expect("next_event") {
                collected.push(event);
            }
            (collected, events.finish().await.expect("finish"))
        }
    }
}

fn insert_name(event: &ChangeEvent) -> String {
    match &event.event_type {
        EventType::Insert { data, .. } => data
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or_default()
            .to_string(),
        other => panic!("expected an Insert, got {other:?}"),
    }
}

/// Drive the live stream until `want` inserts have arrived, or time out.
async fn drain_inserts(stream: &mut LogicalReplicationStream, want: usize) -> Vec<ChangeEvent> {
    let token = CancellationToken::new();
    let guard = token.clone();
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_secs(10)).await;
        guard.cancel();
    });

    let mut out = Vec::new();
    while out.len() < want {
        match stream.next_event(&token).await {
            Ok(event) if matches!(event.event_type, EventType::Insert { .. }) => out.push(event),
            Ok(_) => {}
            // Only the 10s budget above is an expected exit. Returning a short
            // vector for a dead connection or a protocol error surfaces at the
            // caller as `index out of bounds`, which names neither.
            Err(e) if e.is_cancelled() => break,
            Err(e) => panic!("stream failed after {} inserts: {e}", out.len()),
        }
    }
    out
}

// ── Core behaviour ──────────────────────────────────────────────────────────

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn snapshot_copies_all_published_rows() {
    let (slot, table, publication) = ("it_snap_all", "snap_all", "snap_all_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["alice", "bob", "charlie"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let (events, _stream) = tokio::time::timeout(TEST_TIMEOUT, collect_snapshot(stream))
        .await
        .expect("timed out");

    let mut names: Vec<String> = events.iter().map(insert_name).collect();
    names.sort();
    assert_eq!(names, vec!["alice", "bob", "charlie"]);
}

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn snapshot_events_carry_the_consistent_point_lsn() {
    let (slot, table, publication) = ("it_snap_lsn", "snap_lsn", "snap_lsn_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a", "b"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let outcome = stream.snapshot().await.expect("snapshot");
    let SnapshotOutcome::Available(snap) = outcome else {
        panic!("expected a snapshot for a fresh slot");
    };
    let point = snap.consistent_point();
    assert_ne!(point.value(), 0, "consistent_point must be captured");

    let mut events = snap.events();
    let mut seen = 0;
    while let Some(event) = events.next_event().await.expect("next_event") {
        assert_eq!(event.lsn, point, "every snapshot row carries the same LSN");
        seen += 1;
    }
    events.finish().await.expect("finish");
    // The loop body is the whole test, so a zero-row snapshot would assert
    // nothing at all and still report success.
    assert_eq!(seen, 2, "premise: both seeded rows must have been copied");
}

/// The decisive end-to-end claim: a row inserted *after* the snapshot was
/// exported but *before* `start()` must arrive on the stream exactly once, and
/// must not appear in the snapshot.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn snapshot_then_stream_has_no_gap_and_no_duplicate() {
    let (slot, table, publication) = ("it_snap_gap", "snap_gap", "snap_gap_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["before_1", "before_2"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let outcome = stream.snapshot().await.expect("snapshot");
    let SnapshotOutcome::Available(snap) = outcome else {
        panic!("expected a snapshot");
    };

    // Written after the snapshot was exported, before streaming begins.
    regular_conn()
        .exec(&format!("INSERT INTO {table} (name) VALUES ('after')"))
        .expect("insert after export");

    let mut events = snap.events();
    let mut snapshot_names = Vec::new();
    while let Some(event) = events.next_event().await.expect("next_event") {
        snapshot_names.push(insert_name(&event));
    }
    let mut stream = events.finish().await.expect("finish");

    snapshot_names.sort();
    assert_eq!(
        snapshot_names,
        vec!["before_1", "before_2"],
        "the post-export row must NOT be in the snapshot"
    );

    stream.start(None).await.expect("start");
    let live = tokio::time::timeout(TEST_TIMEOUT, drain_inserts(&mut stream, 1))
        .await
        .expect("timed out");

    let live_names: Vec<String> = live.iter().map(insert_name).collect();
    assert_eq!(
        live_names.iter().filter(|n| *n == "after").count(),
        1,
        "the post-export row must arrive on the stream exactly once, got {live_names:?}"
    );
    assert!(
        !live_names.iter().any(|n| n.starts_with("before_")),
        "snapshot rows must not be replayed on the stream: {live_names:?}"
    );
}

/// Values must be byte-identical between the snapshot and the live stream. This
/// is the premise of decoding `COPY` in TEXT rather than BINARY format: anything
/// else and the serde layer would behave differently across the handoff.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn copy_text_values_are_byte_identical_to_pgoutput() {
    let (slot, table, publication) = ("it_snap_bytes", "snap_bytes", "snap_bytes_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!(
        "CREATE TABLE {table} (
            id SERIAL PRIMARY KEY,
            t_tab TEXT, t_nl TEXT, t_cr TEXT, t_bs TEXT, t_lit_null TEXT,
            t_empty TEXT, t_null TEXT,
            b_bytea BYTEA, j_jsonb JSONB, a_arr TEXT[], n_num NUMERIC, ts TIMESTAMPTZ
        )"
    ))
    .expect("create table");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("create publication");

    // Every character COPY TEXT escapes, plus the types whose text output
    // contains a backslash.
    let insert = format!(
        "INSERT INTO {table}
         (t_tab, t_nl, t_cr, t_bs, t_lit_null, t_empty, t_null,
          b_bytea, j_jsonb, a_arr, n_num, ts)
         VALUES (E'a\\tb', E'a\\nb', E'a\\rb', E'a\\\\b', E'\\\\N', '', NULL,
                 '\\x48656c6c6f'::bytea, '{{\"k\": [1, 2]}}'::jsonb,
                 ARRAY['x','y'], 1234.5678, '2026-01-02 03:04:05+00')"
    );

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let outcome = stream.snapshot().await.expect("snapshot");
    let SnapshotOutcome::Available(snap) = outcome else {
        panic!("expected a snapshot");
    };

    // Insert after the export so the SAME logical row is seen by both paths:
    // absent from the snapshot, present on the stream.
    conn.exec(&insert).expect("insert");

    let mut stream = snap.abandon().await.expect("abandon");
    stream.start(None).await.expect("start");
    let live = tokio::time::timeout(TEST_TIMEOUT, drain_inserts(&mut stream, 1))
        .await
        .expect("timed out");
    assert!(!live.is_empty(), "no insert arrived within 10s");
    let streamed = match &live[0].event_type {
        EventType::Insert { data, .. } => data.clone(),
        other => panic!("expected an Insert, got {other:?}"),
    };
    // Released early: the second half of the test opens its own slot, and this
    // one has nothing left to prove.
    drop_slot(slot);

    // Now snapshot the same committed row through COPY.
    let slot2 = "it_snap_bytes_2";
    let _guard2 = slot_guard(slot2);
    let stream2 = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot2, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = tokio::time::timeout(TEST_TIMEOUT, collect_snapshot(stream2))
        .await
        .expect("timed out");
    let snapshotted = match &events[0].event_type {
        EventType::Insert { data, .. } => data.clone(),
        other => panic!("expected an Insert, got {other:?}"),
    };

    for column in [
        "t_tab",
        "t_nl",
        "t_cr",
        "t_bs",
        "t_lit_null",
        "t_empty",
        "b_bytea",
        "j_jsonb",
        "a_arr",
        "n_num",
        "ts",
    ] {
        // Without this, two `None`s (the column absent from BOTH decodes) would
        // satisfy the comparison while proving nothing — and this is the suite's
        // central claim that COPY TEXT matches pgoutput byte for byte.
        assert!(
            snapshotted.get(column).is_some(),
            "column {column} missing from the snapshot decode"
        );
        assert_eq!(
            snapshotted.get(column),
            streamed.get(column),
            "column {column} differs between COPY TEXT and pgoutput"
        );
    }

    // `t_null` is genuinely NULL, so it is checked for NULL-ness rather than
    // presence — the one column the `is_some()` guard above cannot cover.
    assert_eq!(
        snapshotted.get("t_null").map(|v| v.is_null()),
        streamed.get("t_null").map(|v| v.is_null()),
        "NULL must decode as NULL on both paths"
    );
    assert_eq!(snapshotted.get("t_null").map(|v| v.is_null()), Some(true));
}

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn snapshot_rows_api_matches_events_api() {
    let (table, publication) = ("snap_parity", "snap_parity_pub");
    setup(table, publication, &["one", "two"]);

    let mut from_rows = Vec::new();
    let slot_rows = "it_snap_parity_rows";
    let _guard_rows = slot_guard(slot_rows);
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot_rows, publication),
    )
    .await
    .expect("connect");
    // Not `if let`: `Unavailable` means the slot leaked from an earlier run, and
    // falling through would leave `from_rows` empty. Paired with an equally empty
    // `from_events` the final `assert_eq!` compares two empty vectors and passes
    // having proved nothing about the two APIs agreeing.
    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("a fresh slot must export a snapshot; {slot_rows} leaked from a previous run");
    };
    let mut rows = snap.rows();
    while let Some(row) = rows.next_row().await.expect("next_row") {
        from_rows.push(
            row.data
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string(),
        );
    }
    rows.finish().await.expect("finish");

    let slot_events = "it_snap_parity_events";
    let _guard_events = slot_guard(slot_events);
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot_events, publication),
    )
    .await
    .expect("connect");
    let outcome = stream.snapshot().await.expect("snapshot");
    assert!(
        outcome.is_available(),
        "a fresh slot must export a snapshot; {slot_events} leaked from a previous run"
    );
    let (events, _s) = collect_snapshot_from(outcome).await;
    let from_events: Vec<String> = events.iter().map(insert_name).collect();

    // Without this the parity assertion below is satisfiable by two empty
    // vectors, which is the failure it exists to catch.
    let mut seeded = from_rows.clone();
    seeded.sort();
    assert_eq!(
        seeded,
        vec!["one", "two"],
        "premise: the row API must actually have copied the seeded rows"
    );
    assert_eq!(from_rows, from_events);
}

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn typed_deserialization_works_on_snapshot_rows() {
    #[derive(serde::Deserialize)]
    struct Row {
        id: i32,
        name: String,
    }

    let (slot, table, publication) = ("it_snap_typed", "snap_typed", "snap_typed_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["typed"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = collect_snapshot(stream).await;

    let row: Row = events[0].deserialize_insert().expect("deserialize");
    assert!(row.id > 0);
    assert_eq!(row.name, "typed");
}

/// One router, one set of handlers, both phases.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn wal_router_runs_across_snapshot_and_stream() {
    use pg_walstream::router::WalRouter;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    /// Deserializing at all is the point: it proves a snapshot row goes through
    /// the same serde path a streamed row does.
    #[derive(serde::Deserialize)]
    #[allow(dead_code)]
    struct Row {
        name: String,
    }

    let (slot, table, publication) = ("it_snap_router", "snap_router", "snap_router_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["r1", "r2"]);

    let hits = Arc::new(AtomicUsize::new(0));
    let counter = Arc::clone(&hits);
    let mut router = WalRouter::new();
    router.on_insert(table, move |_: Row| {
        let counter = Arc::clone(&counter);
        async move {
            counter.fetch_add(1, Ordering::Relaxed);
            Ok(())
        }
    });

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let stream = match stream.snapshot().await.expect("snapshot") {
        SnapshotOutcome::Unavailable(s) => s,
        SnapshotOutcome::Available(snap) => {
            let mut events = snap.events();
            router
                .run_snapshot(&mut events)
                .await
                .expect("run_snapshot");
            events.finish().await.expect("finish")
        }
    };

    assert_eq!(
        hits.load(Ordering::Relaxed),
        2,
        "the snapshot phase must reach the same handler as the live phase"
    );

    drop(stream);
}

// ── Lifecycle and escape hatches ────────────────────────────────────────────

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn slot_already_exists_yields_unavailable() {
    let (slot, table, publication) = ("it_snap_exists", "snap_exists", "snap_exists_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["x"]);

    // First pass creates the slot and exports a snapshot.
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let outcome = stream.snapshot().await.expect("snapshot");
    assert!(
        outcome.is_available(),
        "a fresh slot must export a snapshot"
    );
    drop(outcome.skip().await.expect("skip"));

    // Second pass finds the slot already there: not an error, just no snapshot.
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let outcome = stream.snapshot().await.expect("snapshot");
    assert!(
        !outcome.is_available(),
        "a pre-existing slot exports no snapshot"
    );

    let mut stream = outcome.skip().await.expect("skip");
    stream.start(None).await.expect("start must still work");
}

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn abandon_returns_the_stream_and_start_succeeds() {
    let (slot, table, publication) = ("it_snap_abandon", "snap_abandon", "snap_abandon_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let mut stream = snap.abandon().await.expect("abandon");
    stream.start(None).await.expect("start after abandon");
}

/// Abandoning part-way must hang up rather than drain the rest of the table.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn abandon_midway_drops_reader_without_hanging() {
    let (slot, table, publication) = ("it_snap_mid", "snap_mid", "snap_mid_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!("CREATE TABLE {table} (id INT, name TEXT)"))
        .expect("create");
    conn.exec(&format!(
        "INSERT INTO {table} SELECT g, repeat('x', 200) FROM generate_series(1, 20000) g"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let mut rows = snap.rows();
    rows.next_row().await.expect("first row").expect("some");

    tokio::time::timeout(Duration::from_secs(20), rows.abandon())
        .await
        .expect("abandon must not wait for the rest of the table")
        .expect("abandon");
}

/// A half-consumed snapshot must not hand back the stream: that is exactly the
/// silent gap this API exists to prevent.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn finish_before_completion_is_rejected() {
    let (slot, table, publication) = ("it_snap_partial", "snap_partial", "snap_partial_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a", "b", "c"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let mut rows = snap.rows();
    rows.next_row().await.expect("first").expect("some");

    assert!(
        rows.finish().await.is_err(),
        "finish() on a half-consumed snapshot must fail"
    );
}

/// The hazard this guards: if a failed snapshot left its slot behind, the retry
/// would find it present, `ensure_replication_slot` would swallow "already
/// exists", no snapshot would be exported, and the caller would get
/// `Unavailable` — indistinguishable from the legitimate "resuming an existing
/// subscription" path. A transient setup failure would silently become an
/// unbounded data gap.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn a_failed_snapshot_leaves_no_slot_so_a_retry_still_gets_one() {
    let (slot, table, publication) = ("it_snap_cleanup", "snap_cleanup", "snap_cleanup_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a", "b"]);

    // First attempt: fail inside the handler, part-way through the copy.
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("a fresh slot must export a snapshot");
    };
    let failed = snap
        .events()
        .run(|_| async {
            Err(pg_walstream::ReplicationError::protocol(
                "simulated sink failure".to_string(),
            ))
        })
        .await;
    assert!(failed.is_err(), "the handler error must propagate");

    // The slot must be gone.
    let mut probe = regular_conn();
    let slots = probe
        .exec(&format!(
            "SELECT count(*) FROM pg_replication_slots WHERE slot_name = '{slot}'"
        ))
        .expect("probe");
    assert_eq!(
        probe_count(&slots),
        0,
        "a failed snapshot must not leave its slot behind"
    );

    // Second attempt must therefore still get a real snapshot, not Unavailable.
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let outcome = stream.snapshot().await.expect("snapshot");
    assert!(
        outcome.is_available(),
        "the retry after a failed snapshot must still export a baseline"
    );

    let (events, _s) = collect_snapshot_from(outcome).await;
    let mut names: Vec<String> = events.iter().map(insert_name).collect();
    names.sort();
    assert_eq!(names, vec!["a", "b"]);
}

/// The `?`-shaped leak, which the by-value cleanup on `run`/`finish` never saw.
///
/// The module's own doc example consumes the snapshot through a *borrow*
/// (`router.run_snapshot(&mut events).await?`), so on error the `?` **drops**
/// the handle instead of consuming it. Before `SnapshotRows` had a `Drop` impl
/// the slot survived that, and the retry above it got `Unavailable` — which
/// reads as "resuming an existing subscription" — and streamed with no
/// baseline. A transient sink failure became a silent, unbounded data gap.
///
/// Distinct from `a_failed_snapshot_leaves_no_slot_so_a_retry_still_gets_one`,
/// which uses the by-value `run()` and passed even while this path leaked.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn a_snapshot_dropped_mid_consumption_leaves_no_slot() {
    let (slot, table, publication) = ("it_snap_dropped", "snap_dropped", "snap_dropped_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a", "b"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("a fresh slot must export a snapshot");
    };

    {
        // `for_each_event` borrows, so this block reproduces exactly what a `?`
        // at the caller's site does: the handle is dropped, never consumed.
        let mut events = snap.events();
        let failed = events
            .for_each_event(|_| async {
                Err(pg_walstream::ReplicationError::protocol(
                    "simulated sink failure".to_string(),
                ))
            })
            .await;
        assert!(failed.is_err(), "the handler error must propagate");
    }

    let mut probe = regular_conn();
    let slots = probe
        .exec(&format!(
            "SELECT count(*) FROM pg_replication_slots WHERE slot_name = '{slot}'"
        ))
        .expect("probe");
    assert_eq!(
        probe_count(&slots),
        0,
        "dropping a snapshot handle must discard its slot, or the retry silently \
         streams with no baseline"
    );

    // The retry must therefore still get a real baseline, not `Unavailable`.
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let outcome = stream.snapshot().await.expect("snapshot");
    assert!(
        outcome.is_available(),
        "the retry after a dropped snapshot must still export a baseline"
    );

    let (events, _s) = collect_snapshot_from(outcome).await;
    assert_eq!(events.len(), 2, "the retry must copy every row");
}

/// The same rule one level up: a `Snapshot` dropped before `rows()`/`events()`
/// is ever called must not leave its slot either. The window is narrow — a `?`
/// between `snapshot()` and the first consumption call — but the consequence is
/// identical, so the two types enforce it the same way.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn a_snapshot_dropped_before_consumption_leaves_no_slot() {
    let (slot, table, publication) = ("it_snap_predrop", "snap_predrop", "snap_predrop_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    {
        let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
            panic!("a fresh slot must export a snapshot");
        };
        assert_eq!(snap.tables().len(), 1, "premise: the plan resolved a table");
    } // dropped without rows()/events()/abandon()

    let mut probe = regular_conn();
    let slots = probe
        .exec(&format!(
            "SELECT count(*) FROM pg_replication_slots WHERE slot_name = '{slot}'"
        ))
        .expect("probe");
    assert_eq!(
        probe_count(&slots),
        0,
        "a Snapshot dropped before consumption must discard its slot"
    );
}

/// `START_REPLICATION` destroys the exported snapshot server-side, so the name
/// must not survive `start()`.
///
/// Clearing it at the top of `start()` is not enough on its own: `initialize()`
/// creates the slot in that same call when it does not exist yet, and a slot
/// created with `SNAPSHOT 'export'` re-exports a name *after* the clear ran. So
/// A direct `start()` on a slot created with `SNAPSHOT 'export'` — no `snapshot()` call
/// in between — handed back a name the server had just destroyed, which
/// `SET TRANSACTION SNAPSHOT` would reject.
///
/// The unit test cannot reach this: its null connection fails inside
/// `initialize()`, before the slot is ever created.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn a_direct_start_clears_the_exported_snapshot_name() {
    let (slot, table, publication) = ("it_snap_startclear", "snap_startclear", "snap_sc_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a"]);

    let mut stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    // No `snapshot()` call: `start()` itself creates the slot, and creating it
    // with SNAPSHOT 'export' is what re-populates the name mid-call.
    stream.start(None).await.expect("start");

    assert!(
        stream.exported_snapshot_name().is_none(),
        "START_REPLICATION destroyed the exported snapshot; the name must not outlive it"
    );
}

/// A real transport failure mid-COPY, not a simulated handler error.
///
/// Every other failure test reaches the poison latch through a handler that
/// returns `Err`, so the reader's own I/O error path — the one that decides
/// which `ReplicationError` comes out, and therefore whether a caller retries —
/// was never executed. Kill the reader backend from outside and assert the
/// handle refuses to yield the stream and drops its slot.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn killing_the_reader_mid_copy_poisons_the_handle_and_drops_the_slot() {
    let (slot, table, publication) = ("it_snap_killrdr", "snap_killrdr", "snap_killrdr_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!("CREATE TABLE {table} (id INT, payload TEXT)"))
        .expect("create");
    // Big enough that the COPY is still in flight after the first row.
    conn.exec(&format!(
        "INSERT INTO {table} SELECT g, repeat('x', 200) FROM generate_series(1, 200000) g"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let mut rows = snap.rows();
    rows.next_row().await.expect("first row").expect("some");

    // Terminate the reader backend out from under the in-flight COPY.
    // `state = 'active'` and this test's own table name are both load-bearing:
    // `pg_stat_activity.query` keeps the LAST statement, so a bare
    // `query LIKE 'COPY %'` also matches idle bystanders from other suites —
    // killing them, and counting them towards the premise below.
    let mut probe = regular_conn();
    let killed = probe
        .exec(
            "SELECT count(*) FROM (
               SELECT pg_terminate_backend(pid) FROM pg_stat_activity
                WHERE datname = current_database()
                  AND pid <> pg_backend_pid()
                  AND state = 'active'
                  AND query LIKE 'COPY %'
                  AND query LIKE '%snap_killrdr%'
             ) t",
        )
        .expect("terminate");
    assert!(
        probe_count(&killed) >= 1,
        "premise: the reader's COPY backend must have been found and killed"
    );

    // Draining must fail rather than silently ending the snapshot early — an
    // `Ok(None)` here would look like a complete copy and let `finish()` hand
    // back a stream with a half-copied baseline.
    let err = loop {
        match rows.next_row().await {
            Ok(Some(_)) => continue,
            Ok(None) => panic!("a killed reader must not look like a completed snapshot"),
            Err(e) => break e,
        }
    };
    assert!(
        !err.is_cancelled(),
        "a killed backend is a failure, not a cancellation: {err}"
    );

    // The handle is poisoned: the stream must not be recoverable from it.
    let salvage = rows.finish().await;
    assert!(
        salvage.is_err(),
        "a failed snapshot must not yield the stream"
    );

    let mut probe = regular_conn();
    let slots = probe
        .exec(&format!(
            "SELECT count(*) FROM pg_replication_slots WHERE slot_name = '{slot}'"
        ))
        .expect("probe");
    assert_eq!(
        probe_count(&slots),
        0,
        "a reader failure must discard the slot, exactly like a handler failure"
    );

    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
}

/// Dropping a half-consumed snapshot must terminate and still discard the slot.
///
/// **What this does NOT prove:** the COPY-OUT drop deadlock. Verified by
/// mutation — this test passes against the pre-fix code too, because
/// `SnapshotRows::drop` calls `close_reader()` → `copy_out_abort()`, which
/// clears `CopyMode::Out { rx }` and so drops the worker's receiver *before* the
/// connection is dropped. The worker's send then fails and it exits. The
/// deadlock needs a `NativeConnection` dropped mid-COPY-OUT with no abort first,
/// which this path cannot produce; that case is covered at the worker level by
/// `worker_drain_copy_out_handles_close_while_the_channel_is_full`.
///
/// What it does pin: the mid-COPY drop terminates rather than hanging, and the
/// slot is gone afterwards. A plain `#[test]` with its own runtime on a spawned
/// thread, not `#[tokio::test]`: a hang here would be a *blocking* drop, so a
/// `tokio::time::timeout` on the same thread would never get to run.
#[test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
fn dropping_a_half_consumed_snapshot_terminates_and_discards_the_slot() {
    let (slot, table, publication) = ("it_snap_drophang", "snap_drophang", "snap_drophang_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!("CREATE TABLE {table} (id INT, payload TEXT)"))
        .expect("create");
    // Far more frames than the worker's channel holds, so it is certainly
    // parked on a full channel when the handle is dropped.
    conn.exec(&format!(
        "INSERT INTO {table} SELECT g, repeat('y', 500) FROM generate_series(1, 200000) g"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("publication");

    let (done_tx, done_rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("runtime");
        rt.block_on(async {
            let stream = LogicalReplicationStream::new(
                &replication_conn_string(),
                snapshot_config(slot, publication),
            )
            .await
            .expect("connect");

            let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot")
            else {
                panic!("expected a snapshot");
            };
            let mut rows = snap.rows();
            rows.next_row().await.expect("first row").expect("some");

            // Let the worker run ahead and fill its channel.
            tokio::time::sleep(Duration::from_millis(500)).await;

            // The drop under test: no finish(), no abandon().
            drop(rows);
        });
        let _ = done_tx.send(());
    });

    done_rx
        .recv_timeout(Duration::from_secs(60))
        .expect("dropping a half-consumed snapshot must not hang");

    // And the drop must still have discarded the slot.
    let mut probe = regular_conn();
    let slots = probe
        .exec(&format!(
            "SELECT count(*) FROM pg_replication_slots WHERE slot_name = '{slot}'"
        ))
        .expect("probe");
    assert_eq!(
        probe_count(&slots),
        0,
        "the drop must discard the slot even when it happens mid-COPY"
    );

    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
}

/// Abandoning is deliberate, so the slot is kept — the caller chose to stream
/// from the consistent point without an initial copy.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn abandon_keeps_the_slot() {
    let (slot, table, publication) = ("it_snap_keep", "snap_keep", "snap_keep_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    drop(snap.abandon().await.expect("abandon"));

    let mut probe = regular_conn();
    let slots = probe
        .exec(&format!(
            "SELECT count(*) FROM pg_replication_slots WHERE slot_name = '{slot}'"
        ))
        .expect("probe");
    assert_eq!(
        probe_count(&slots),
        1,
        "abandon is a deliberate choice; the slot must survive it"
    );
}

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn handler_error_consumes_the_handle() {
    let (slot, table, publication) = ("it_snap_handler", "snap_handler", "snap_handler_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a", "b"]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };

    let failed = snap
        .events()
        .run(|_| async {
            Err(pg_walstream::ReplicationError::protocol(
                "handler exploded".to_string(),
            ))
        })
        .await;
    assert!(failed.is_err(), "a handler error must propagate");
    // The handle was moved into `run`, so there is nothing to resume from — the
    // borrow checker enforces that; this test pins the runtime half.
}

// ── Cancellation ────────────────────────────────────────────────────────────

/// Seed a table big enough that its `COPY` cannot finish between two polls, so
/// cancellation lands mid-stream rather than after the fact.
fn seed_large(table: &str, publication: &str, rows: u32) {
    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!("CREATE TABLE {table} (id INT, payload TEXT)"))
        .expect("create");
    conn.exec(&format!(
        "INSERT INTO {table} SELECT g, repeat('x', 400) FROM generate_series(1, {rows}) g"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("publication");
}

/// Without a working cancellation path there is no way to stop a snapshot of a
/// 50-million-row table: the only exits would be running it to completion or
/// killing the process. This drives the real path
/// (`SnapshotRows` -> `copy_out_next` -> the `tokio::select!` on the token) and
/// requires it to unblock promptly.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn cancelling_mid_snapshot_stops_promptly() {
    let (slot, table, publication) = ("it_snap_cancel", "snap_cancel", "snap_cancel_pub");
    let _guard = slot_guard(slot);
    seed_large(table, publication, 200_000);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let token = CancellationToken::new();
    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let mut rows = snap.with_cancellation(token.clone()).rows();

    // Prove the copy is genuinely in flight before cancelling.
    rows.next_row().await.expect("first row").expect("some");

    token.cancel();

    // The next read must unblock on the token, not run the table to completion.
    let outcome = tokio::time::timeout(Duration::from_secs(20), async {
        loop {
            match rows.next_row().await {
                Ok(Some(_)) => continue,
                other => return other,
            }
        }
    })
    .await
    .expect("cancellation must unblock the copy loop, not wait for the whole table");

    let err = outcome.expect_err("a cancelled snapshot must not report completion");
    assert!(
        err.is_cancelled() || format!("{err}").contains("poisoned"),
        "expected a cancellation, got {err:?}"
    );
}

/// The same property on the **inline** driver.
///
/// `NativeConnection::connect` picks the driver from the ambient runtime flavour:
/// a current-thread runtime (what `#[tokio::test]` gives you by default) takes
/// the threaded worker path, a multi-thread runtime takes the inline path. They
/// are two separate `tokio::select!` sites, so a test on one proves nothing about
/// the other — a mutation check confirmed that breaking the inline arm left the
/// default-flavour tests entirely green.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn cancelling_mid_snapshot_stops_promptly_on_the_inline_driver() {
    let (slot, table, publication) = (
        "it_snap_cancel_inline",
        "snap_cancel_inline",
        "snap_cancel_inline_pub",
    );
    let _guard = slot_guard(slot);
    seed_large(table, publication, 200_000);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let token = CancellationToken::new();
    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let mut rows = snap.with_cancellation(token.clone()).rows();

    rows.next_row().await.expect("first row").expect("some");
    token.cancel();

    let outcome = tokio::time::timeout(Duration::from_secs(20), async {
        loop {
            match rows.next_row().await {
                Ok(Some(_)) => continue,
                other => return other,
            }
        }
    })
    .await
    .expect("cancellation must unblock the inline copy loop too");

    let err = outcome.expect_err("a cancelled snapshot must not report completion");
    assert!(
        err.is_cancelled() || format!("{err}").contains("poisoned"),
        "expected a cancellation, got {err:?}"
    );
}

/// A cancelled snapshot is incomplete, so it must never hand back the stream as
/// if it had succeeded — that would splice a half-copied baseline onto the live
/// stream, which is exactly the silent gap this API exists to prevent.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn a_cancelled_snapshot_is_not_reported_as_success() {
    let (slot, table, publication) = (
        "it_snap_cancel_run",
        "snap_cancel_run",
        "snap_cancel_run_pub",
    );
    let _guard = slot_guard(slot);
    seed_large(table, publication, 200_000);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let token = CancellationToken::new();
    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let events = snap.with_cancellation(token.clone()).events();

    // Cancel once a few rows have been delivered.
    let seen = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let counter = std::sync::Arc::clone(&seen);
    let guard = token.clone();

    let result = tokio::time::timeout(
        Duration::from_secs(30),
        events.run(move |_| {
            let counter = std::sync::Arc::clone(&counter);
            let guard = guard.clone();
            async move {
                if counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 50 {
                    guard.cancel();
                }
                Ok(())
            }
        }),
    )
    .await
    .expect("run must return once cancelled");

    assert!(
        result.is_err(),
        "a cancelled, half-copied snapshot must NOT return the stream as success"
    );
    assert!(
        seen.load(std::sync::atomic::Ordering::Relaxed) < 200_000,
        "the test did not actually cancel mid-copy"
    );

    // The slot is discarded on failure, so a retry gets a fresh export rather
    // than silently finding the slot present and proceeding with no baseline.
    let mut probe = regular_conn();
    let slots = probe
        .exec(&format!(
            "SELECT count(*) FROM pg_replication_slots WHERE slot_name = '{slot}'"
        ))
        .expect("probe");
    assert_eq!(
        probe_count(&slots),
        0,
        "a cancelled snapshot must not leave its slot behind"
    );
}

// ── Table resolution ────────────────────────────────────────────────────────

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn empty_table_produces_no_events() {
    let (slot, table, publication) = ("it_snap_empty", "snap_empty", "snap_empty_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &[]);

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let outcome = stream.snapshot().await.expect("snapshot");
    // `collect_snapshot` yields an empty Vec for `Unavailable` too, so without
    // this the emptiness assertion below would also pass for a slot that leaked
    // from a previous run and exported no snapshot at all.
    assert!(
        outcome.is_available(),
        "a fresh slot must export a snapshot; {slot} leaked from a previous run"
    );
    let (events, _s) = collect_snapshot_from(outcome).await;
    assert!(events.is_empty());
}

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn multi_table_publication_snapshots_every_table() {
    let (slot, publication) = ("it_snap_multi", "snap_multi_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    for t in ["snap_multi_a", "snap_multi_b"] {
        let _ = conn.exec(&format!("DROP TABLE IF EXISTS {t} CASCADE"));
        conn.exec(&format!(
            "CREATE TABLE {t} (id SERIAL PRIMARY KEY, name TEXT)"
        ))
        .expect("create");
        conn.exec(&format!("INSERT INTO {t} (name) VALUES ('{t}')"))
            .expect("seed");
    }
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE snap_multi_a, snap_multi_b"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = collect_snapshot(stream).await;

    let mut names: Vec<String> = events.iter().map(insert_name).collect();
    names.sort();
    assert_eq!(names, vec!["snap_multi_a", "snap_multi_b"]);
}

/// A wide table large enough that rows certainly span `CopyData` frames.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn large_table_spans_many_copydata_frames() {
    let (slot, table, publication) = ("it_snap_large", "snap_large", "snap_large_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!("CREATE TABLE {table} (id INT, payload TEXT)"))
        .expect("create");
    conn.exec(&format!(
        "INSERT INTO {table} SELECT g, repeat('abc', 400) FROM generate_series(1, 50000) g"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let (events, _s) = tokio::time::timeout(Duration::from_secs(180), collect_snapshot(stream))
        .await
        .expect("timed out");
    assert_eq!(events.len(), 50_000);
}

/// Writes committed during the copy must not appear in the snapshot — and must
/// still arrive on the live stream, exactly once.
///
/// The second half is the part `snapshot_then_stream_has_no_gap_and_no_duplicate`
/// does not reach: its write lands after the export but *before* the COPY begins.
/// A bug in where `start(None)` resumes could lose a write committed while the
/// copy is in flight and still pass both tests if this one stopped at `finish()`.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn snapshot_is_isolated_from_concurrent_writes() {
    let (slot, table, publication) = ("it_snap_iso", "snap_iso", "snap_iso_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!("CREATE TABLE {table} (id INT, name TEXT)"))
        .expect("create");
    conn.exec(&format!(
        "INSERT INTO {table} SELECT g, 'seed' FROM generate_series(1, 5000) g"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let mut rows = snap.rows();
    rows.next_row().await.expect("first").expect("some");

    // Committed on a third connection, mid-copy.
    regular_conn()
        .exec(&format!("INSERT INTO {table} VALUES (99999, 'intruder')"))
        .expect("concurrent insert");

    let mut count = 1;
    let mut saw_intruder = false;
    while let Some(row) = rows.next_row().await.expect("next_row") {
        count += 1;
        if row.data.get("name").and_then(|v| v.as_str()) == Some("intruder") {
            saw_intruder = true;
        }
    }
    let mut stream = rows.finish().await.expect("finish");

    assert_eq!(count, 5000, "the snapshot must see exactly the seeded rows");
    assert!(!saw_intruder, "a concurrent write leaked into the snapshot");

    // Excluded from the snapshot, so the live stream now owes us exactly one
    // copy of it. Ask for *two*: `drain_inserts` returns as soon as it has
    // `want`, so asking for one would return at the first arrival and a
    // duplicate could never be observed. Asking for two burns the 10s token in
    // the correct case and returns whatever actually arrived.
    stream.start(None).await.expect("start");
    let live = drain_inserts(&mut stream, 2).await;

    let intruders = live
        .iter()
        .filter(|e| match &e.event_type {
            EventType::Insert { data, .. } => {
                data.get("name").and_then(|v| v.as_str()) == Some("intruder")
            }
            _ => false,
        })
        .count();
    assert_eq!(
        intruders, 1,
        "a write committed mid-copy must arrive on the stream exactly once"
    );
}

/// `publication_name` may be a comma-separated, optionally quoted list, because
/// that is what `START_REPLICATION ... (publication_names '...')` takes.
/// `split_publication_names` is unit-tested, but nothing proved the catalog query
/// actually resolves a multi-publication list end to end.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn multiple_publications_are_all_snapshotted() {
    let slot = "it_snap_multipub";
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    for (t, p) in [
        ("snap_mp_a", "snap_mp_pub_a"),
        ("snap_mp_b", "snap_mp_pub_b"),
    ] {
        let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {p}"));
        let _ = conn.exec(&format!("DROP TABLE IF EXISTS {t} CASCADE"));
        conn.exec(&format!(
            "CREATE TABLE {t} (id SERIAL PRIMARY KEY, name TEXT)"
        ))
        .expect("create");
        conn.exec(&format!("INSERT INTO {t} (name) VALUES ('{t}')"))
            .expect("seed");
        conn.exec(&format!("CREATE PUBLICATION {p} FOR TABLE {t}"))
            .expect("publication");
    }

    // The quoted-CSV form the replication protocol uses.
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, "\"snap_mp_pub_a\",\"snap_mp_pub_b\""),
    )
    .await
    .expect("connect");

    let (events, _s) = collect_snapshot(stream).await;
    let mut names: Vec<String> = events.iter().map(insert_name).collect();
    names.sort();
    assert_eq!(names, vec!["snap_mp_a", "snap_mp_b"]);
}

/// Every identifier goes through `quote_ident`, but only `public` was ever
/// exercised. A non-default schema is the case where a missing qualification
/// would resolve to the wrong table rather than error.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn non_public_schema_is_qualified_correctly() {
    let (slot, publication) = ("it_snap_schema", "snap_schema_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec("DROP SCHEMA IF EXISTS snap_sch CASCADE");
    conn.exec("CREATE SCHEMA snap_sch").expect("schema");
    // A same-named table in public, to catch an unqualified COPY resolving here.
    let _ = conn.exec("DROP TABLE IF EXISTS public.snap_dup CASCADE");
    conn.exec("CREATE TABLE public.snap_dup (id INT, name TEXT)")
        .expect("decoy");
    conn.exec("INSERT INTO public.snap_dup VALUES (1, 'WRONG_public')")
        .expect("decoy seed");

    conn.exec("CREATE TABLE snap_sch.snap_dup (id INT, name TEXT)")
        .expect("create");
    conn.exec("INSERT INTO snap_sch.snap_dup VALUES (1, 'right_schema')")
        .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE snap_sch.snap_dup"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = collect_snapshot(stream).await;

    let names: Vec<String> = events.iter().map(insert_name).collect();
    assert_eq!(
        names,
        vec!["right_schema"],
        "an unqualified COPY would have read public.snap_dup instead"
    );

    let EventType::Insert { schema, .. } = &events[0].event_type else {
        panic!("expected an Insert");
    };
    assert_eq!(&**schema, "snap_sch");

    let _ = conn.exec("DROP SCHEMA IF EXISTS snap_sch CASCADE");
}

/// `decode_line` returns a `SmallVec<[ColumnData; 16]>`. A table wider than that
/// spills to the heap — a path no other test reaches.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn wide_table_beyond_smallvec_inline_capacity() {
    let (slot, table, publication) = ("it_snap_wide", "snap_wide", "snap_wide_pub");
    let _guard = slot_guard(slot);

    let columns: Vec<String> = (0..40).map(|i| format!("c{i} TEXT")).collect();
    let values: Vec<String> = (0..40).map(|i| format!("'v{i}'")).collect();

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!("CREATE TABLE {table} ({})", columns.join(", ")))
        .expect("create");
    conn.exec(&format!(
        "INSERT INTO {table} VALUES ({})",
        values.join(", ")
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = collect_snapshot(stream).await;

    assert_eq!(events.len(), 1);
    let EventType::Insert { data, .. } = &events[0].event_type else {
        panic!("expected an Insert");
    };
    for i in 0..40 {
        assert_eq!(
            data.get(&format!("c{i}")).and_then(|v| v.as_str()),
            Some(format!("v{i}").as_str()),
            "column c{i} must survive the SmallVec spill"
        );
    }
}

/// `COPY t TO STDOUT` is `SELECT ... FROM ONLY t` — it does **not** read
/// inheritance children. The subselect form used for row filters has to say
/// `ONLY` explicitly or it silently means something different.
///
/// `CREATE PUBLICATION ... FOR TABLE parent` publishes the child too (verified
/// against the catalog: both rows appear in `pg_publication_tables`, both
/// carrying the parent's row filter). So the plan copies both tables, and
/// without `ONLY` the parent's COPY re-reads every child row that the child's
/// own COPY then reads again — duplicated snapshot rows, no error.
///
/// Partitioned tables are the deliberate exception and must keep reading every
/// partition, which `partitioned_publication_via_root_uses_the_root_name`
/// covers.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn inheritance_children_are_not_copied_twice() {
    let ver = server_version();
    if ver < 150000 {
        println!(
            "SKIP: inheritance_children_are_not_copied_twice requires PG >= 15 (server {ver})"
        );
        return;
    }
    let (slot, publication) = ("it_snap_inherit", "snap_inherit_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec("DROP TABLE IF EXISTS snap_inherit_child, snap_inherit_parent CASCADE");
    conn.exec("CREATE TABLE snap_inherit_parent (id INT, name TEXT)")
        .expect("parent");
    conn.exec("CREATE TABLE snap_inherit_child () INHERITS (snap_inherit_parent)")
        .expect("child");
    conn.exec("INSERT INTO snap_inherit_parent VALUES (1, 'parent-row')")
        .expect("parent row");
    conn.exec("INSERT INTO snap_inherit_child VALUES (2, 'child-row')")
        .expect("child row");
    // The row filter is what selects the subselect form of the COPY.
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE snap_inherit_parent WHERE (id > 0)"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = collect_snapshot(stream).await;

    let mut names: Vec<String> = events.iter().map(insert_name).collect();
    names.sort();
    assert_eq!(
        names,
        vec!["child-row", "parent-row"],
        "each row must be copied exactly once; a duplicated child row means the \
         parent's COPY read through to the child"
    );

    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec("DROP TABLE IF EXISTS snap_inherit_child, snap_inherit_parent CASCADE");
}

// ── PG15+ publication features ──────────────────────────────────────────────

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn publication_column_list_is_respected() {
    let ver = server_version();
    if ver < 150000 {
        println!("SKIP: publication_column_list_is_respected requires PG >= 15 (server {ver})");
        return;
    }

    let (slot, table, publication) = ("it_snap_collist", "snap_collist", "snap_collist_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!(
        "CREATE TABLE {table} (a INT PRIMARY KEY, b TEXT, c TEXT)"
    ))
    .expect("create");
    conn.exec(&format!("INSERT INTO {table} VALUES (1, 'bee', 'cee')"))
        .expect("seed");
    // b is deliberately omitted: a naive zip of COPY output against the
    // publication's columns would shift every value by one.
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table} (a, c)"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = collect_snapshot(stream).await;

    let EventType::Insert { data, .. } = &events[0].event_type else {
        panic!("expected an Insert");
    };
    assert_eq!(data.get("a").and_then(|v| v.as_str()), Some("1"));
    assert_eq!(
        data.get("c").and_then(|v| v.as_str()),
        Some("cee"),
        "values must align with the published column list"
    );
    assert!(data.get("b").is_none(), "unpublished column must be absent");
}

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn publication_row_filter_is_respected() {
    let ver = server_version();
    if ver < 150000 {
        println!("SKIP: publication_row_filter_is_respected requires PG >= 15 (server {ver})");
        return;
    }

    let (slot, table, publication) = ("it_snap_filter", "snap_filter", "snap_filter_pub");
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!(
        "CREATE TABLE {table} (id INT PRIMARY KEY, name TEXT)"
    ))
    .expect("create");
    conn.exec(&format!(
        "INSERT INTO {table} SELECT g, 'row' || g FROM generate_series(1, 10) g"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table} WHERE (id > 5)"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = collect_snapshot(stream).await;

    assert_eq!(
        events.len(),
        5,
        "the row filter must be applied to the copy"
    );
}

/// `publish_via_partition_root` makes `pg_publication_tables` report the ROOT
/// table, so the snapshot copies the root (reading every partition) and the
/// relation name matches what pgoutput will send. Without that, a router keyed on
/// the root name would silently drop every snapshot row.
///
/// This test is what makes that behaviour a verified fact rather than an
/// assumption about `pg_get_publication_tables()`.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn partitioned_publication_via_root_uses_the_root_name() {
    let (slot, publication) = ("it_snap_part", "snap_part_pub");
    let root = "snap_part_root";
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {root} CASCADE"));
    conn.exec(&format!(
        "CREATE TABLE {root} (id INT, name TEXT) PARTITION BY RANGE (id)"
    ))
    .expect("create root");
    conn.exec(&format!(
        "CREATE TABLE {root}_p1 PARTITION OF {root} FOR VALUES FROM (1) TO (100)"
    ))
    .expect("create p1");
    conn.exec(&format!(
        "CREATE TABLE {root}_p2 PARTITION OF {root} FOR VALUES FROM (100) TO (200)"
    ))
    .expect("create p2");
    conn.exec(&format!(
        "INSERT INTO {root} VALUES (1, 'in_p1'), (150, 'in_p2')"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {root} \
         WITH (publish_via_partition_root = true)"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let SnapshotOutcome::Available(snap) = stream.snapshot().await.expect("snapshot") else {
        panic!("expected a snapshot");
    };
    let table_names: Vec<String> = snap.tables().iter().map(|t| t.qualified_name()).collect();
    assert_eq!(
        table_names,
        vec![format!("public.{root}")],
        "with publish_via_partition_root the snapshot must target the ROOT, not the leaves"
    );

    let mut events = snap.events();
    let mut seen = Vec::new();
    while let Some(event) = events.next_event().await.expect("next_event") {
        match &event.event_type {
            EventType::Insert { table, data, .. } => {
                assert_eq!(&**table, root, "events must carry the root relation name");
                seen.push(
                    data.get("name")
                        .and_then(|v| v.as_str())
                        .unwrap_or_default()
                        .to_string(),
                );
            }
            other => panic!("expected an Insert, got {other:?}"),
        }
    }
    events.finish().await.expect("finish");

    seen.sort();
    assert_eq!(
        seen,
        vec!["in_p1", "in_p2"],
        "copying the root must read every partition"
    );
}

/// The mirror image of `inheritance_children_are_not_copied_twice`, and the more
/// dangerous direction.
///
/// A row filter forces the `COPY (SELECT ...)` form, and `ONLY` must be omitted
/// for a partitioned root — the root holds no rows of its own, so
/// `SELECT ... FROM ONLY <root>` returns **nothing**. If the carve-out ever
/// inverted, this table's snapshot would be silently empty: no error, no short
/// read, just a missing baseline. The unit test
/// `copy_sql_omits_only_for_a_partitioned_table_with_a_row_filter` only compares
/// a string, so it would happily be "fixed" alongside the bug.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn partitioned_root_with_a_row_filter_still_reads_every_partition() {
    let ver = server_version();
    if ver < 150000 {
        println!(
            "SKIP: partitioned_root_with_a_row_filter_still_reads_every_partition \
             requires PG >= 15 (server {ver})"
        );
        return;
    }
    let (slot, publication) = ("it_snap_partfilter", "snap_partfilter_pub");
    let root = "snap_partfilter_root";
    let _guard = slot_guard(slot);

    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {root} CASCADE"));
    conn.exec(&format!(
        "CREATE TABLE {root} (id INT, name TEXT) PARTITION BY RANGE (id)"
    ))
    .expect("create root");
    conn.exec(&format!(
        "CREATE TABLE {root}_p1 PARTITION OF {root} FOR VALUES FROM (1) TO (100)"
    ))
    .expect("create p1");
    conn.exec(&format!(
        "CREATE TABLE {root}_p2 PARTITION OF {root} FOR VALUES FROM (100) TO (200)"
    ))
    .expect("create p2");
    // `filtered_out` is below the filter, so a passing test also proves the
    // WHERE clause survived the `ONLY` change rather than being dropped.
    conn.exec(&format!(
        "INSERT INTO {root} VALUES (1, 'filtered_out'), (50, 'in_p1'), (150, 'in_p2')"
    ))
    .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {root} WHERE (id > 10) \
         WITH (publish_via_partition_root = true)"
    ))
    .expect("publication");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let (events, _s) = collect_snapshot(stream).await;

    let mut names: Vec<String> = events.iter().map(insert_name).collect();
    names.sort();
    assert_eq!(
        names,
        vec!["in_p1", "in_p2"],
        "a filtered partitioned root must still read every partition; an empty result \
         means `ONLY` was applied to a root that holds no rows itself"
    );

    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {root} CASCADE"));
}

/// A pre-existing slot lands in `Unavailable`, and *that arm* is where a
/// bootstrapping caller must fail.
///
/// `Unavailable` normally means "resume an existing subscription", but it cannot
/// be told apart from a slot left behind by an attempt whose cleanup never
/// reached the server. There is no config flag for this: both variants own the
/// stream and `SnapshotOutcome` is `#[must_use]`, so the caller is already
/// forced to write this arm — the decision belongs in it, with the caller's own
/// error, rather than in a builder call three screens away.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn a_pre_existing_slot_reports_unavailable_so_the_caller_can_refuse() {
    let (slot, table, publication) = ("it_snap_required", "snap_required", "snap_required_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a"]);

    // First pass creates the slot and takes a real baseline.
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");
    let outcome = stream.snapshot().await.expect("first snapshot");
    assert!(
        outcome.is_available(),
        "premise: a fresh slot must export a snapshot"
    );
    drop(outcome.skip().await.expect("skip keeps the slot"));

    // Second pass finds the slot, so no snapshot is exported.
    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    // The bootstrapping caller's own check, in the arm they must write anyway.
    // This is what `with_required_snapshot(true)` used to do, minus the field,
    // the setter and the unreachable match arm.
    let refused: Result<(), String> = match stream.snapshot().await.expect("snapshot") {
        SnapshotOutcome::Unavailable(_) => {
            Err("bootstrap requires a fresh slot; drop the existing one".to_string())
        }
        SnapshotOutcome::Available(snap) => {
            snap.abandon().await.expect("abandon");
            Ok(())
        }
    };
    let err = refused.expect_err("a pre-existing slot must reach the Unavailable arm");
    assert!(err.contains("fresh slot"), "{err}");
}

// ── Configuration guards ────────────────────────────────────────────────────

#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn temporary_slot_is_rejected() {
    use pg_walstream::ReplicationSlotOptions;

    let (slot, table, publication) = ("it_snap_temp", "snap_temp", "snap_temp_pub");
    let _guard = slot_guard(slot);
    setup(table, publication, &["a"]);

    let config = snapshot_config(slot, publication).with_slot_options(ReplicationSlotOptions {
        temporary: true,
        snapshot: Some("export".to_string()),
        ..Default::default()
    });

    let stream = LogicalReplicationStream::new(&replication_conn_string(), config)
        .await
        .expect("connect");

    match stream.snapshot().await {
        Ok(_) => panic!("a temporary slot must be rejected: a reconnect loses data silently"),
        Err(e) => assert!(format!("{e}").contains("temporary slot"), "{e}"),
    }
}

/// `snapshot()` sets `SNAPSHOT 'export'` itself, so no builder call can suppress
/// it — not even one that writes `snapshot: "nothing"` directly through
/// `with_slot_options`.
///
/// This replaces a test that asserted the opposite. The old API required
/// `with_initial_snapshot(true)`, which meant the primary entry point was inert
/// unless an unrelated builder call had been made first, and `with_slot_options`
/// could silently undo it depending on call order. Both failure modes were
/// runtime errors on a path whose whole purpose is not to fail silently.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn snapshot_forces_the_export_regardless_of_slot_options() {
    let (slot, table, publication) = (
        "it_snap_forces_export",
        "snap_forces_export",
        "snap_forces_export_pub",
    );
    let _guard = slot_guard(slot);
    setup(table, publication, &["a"]);

    let config = snapshot_config(slot, publication).with_slot_options(ReplicationSlotOptions {
        snapshot: Some("nothing".to_string()),
        ..Default::default()
    });
    let stream = LogicalReplicationStream::new(&replication_conn_string(), config)
        .await
        .expect("connect");

    let (events, _stream) = tokio::time::timeout(TEST_TIMEOUT, collect_snapshot(stream))
        .await
        .expect("timed out");

    let names: Vec<String> = events.iter().map(insert_name).collect();
    assert_eq!(
        names,
        vec!["a"],
        "snapshot() must export regardless of what the caller set"
    );
}

// ── Generated columns ───────────────────────────────────────────────────────
//
// `COPY t (cols) TO STDOUT` refuses a stored generated column outright:
//
//     ERROR:  column "g" is a generated column
//     DETAIL: Generated columns cannot be used in COPY.
//
// so the planner has to route those tables through `COPY (SELECT ...)`. Whether
// the column should be in the snapshot at all differs by version, and getting it
// backwards is silent: the snapshot's row shape stops matching pgoutput's.
//
// Neither case below is reachable on PG 15/16/17 — the versions most likely to
// be a CI default — and neither is visible to `cargo test --lib`, because the
// failure happens server-side.

/// Create a table with a stored generated column, publish it, and seed one row.
fn setup_generated(table: &str, publication: &str, publication_opts: &str) {
    let mut conn = regular_conn();
    let _ = conn.exec(&format!("DROP PUBLICATION IF EXISTS {publication}"));
    let _ = conn.exec(&format!("DROP TABLE IF EXISTS {table} CASCADE"));
    conn.exec(&format!(
        "CREATE TABLE {table} (
             id SERIAL PRIMARY KEY,
             a  INT NOT NULL,
             g  INT GENERATED ALWAYS AS (a * 2) STORED
         )"
    ))
    .expect("create table");
    conn.exec(&format!("INSERT INTO {table} (a) VALUES (5)"))
        .expect("seed");
    conn.exec(&format!(
        "CREATE PUBLICATION {publication} FOR TABLE {table}{publication_opts}"
    ))
    .expect("create publication");
}

/// The column names an `Insert` event carries, in order.
fn insert_columns(event: &ChangeEvent) -> Vec<String> {
    match &event.event_type {
        EventType::Insert { data, .. } => data.iter().map(|(name, _)| name.to_string()).collect(),
        other => panic!("expected an Insert, got {other:?}"),
    }
}

/// A published table with a stored generated column must snapshot at all.
///
/// On PG14 this is the regression test for the pre-PG15 catalog path, which
/// enumerates `pg_attribute` directly (there is no `attnames` to filter on) and
/// therefore used to sweep the generated column into the `COPY` column list,
/// failing the whole snapshot. On PG15+ `attnames` already excludes it, so this
/// documents the agreed behavior rather than guarding a bug.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn snapshot_handles_a_generated_column() {
    let (slot, table, publication) = ("it_snap_gen", "snap_gen", "snap_gen_pub");
    let _guard = slot_guard(slot);
    setup_generated(table, publication, "");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let (events, _stream) = tokio::time::timeout(TEST_TIMEOUT, collect_snapshot(stream))
        .await
        .expect("timed out");

    assert_eq!(events.len(), 1, "the seeded row must be copied");
    let columns = insert_columns(&events[0]);
    assert!(
        columns.contains(&"id".to_string()) && columns.contains(&"a".to_string()),
        "the ordinary columns must be present: {columns:?}"
    );
    assert!(
        !columns.contains(&"g".to_string()),
        "without publish_generated_columns, pgoutput never sends the generated \
         column, so the snapshot must not invent it: {columns:?}"
    );
}

/// The snapshot's row shape must equal the live stream's, generated column or
/// not.
///
/// This is the assertion that actually matters: a snapshot that is one column
/// wider or narrower than pgoutput's `Relation` deserializes into a different
/// struct shape, and nothing downstream would report it.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn a_generated_column_has_the_same_shape_in_both_phases() {
    let (slot, table, publication) = ("it_snap_gen_shape", "snap_gen_shape", "snap_gen_shape_pub");
    let _guard = slot_guard(slot);
    setup_generated(table, publication, "");

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let outcome = stream.snapshot().await.expect("snapshot");
    let SnapshotOutcome::Available(snap) = outcome else {
        panic!("expected a snapshot");
    };

    let mut events = snap.events();
    let snapshot_event = events
        .next_event()
        .await
        .expect("next_event")
        .expect("the seeded row");
    let snapshot_columns = insert_columns(&snapshot_event);
    while events.next_event().await.expect("next_event").is_some() {}
    let mut stream = events.finish().await.expect("finish");

    // A row committed after the export: it arrives on the stream, not the
    // snapshot, so the two column lists come from independent code paths.
    regular_conn()
        .exec(&format!("INSERT INTO {table} (a) VALUES (7)"))
        .expect("insert");

    stream.start(None).await.expect("start");
    let live = tokio::time::timeout(TEST_TIMEOUT, drain_inserts(&mut stream, 1))
        .await
        .expect("timed out");
    assert!(!live.is_empty(), "no insert arrived within 10s");
    let live_columns = insert_columns(&live[0]);

    assert_eq!(
        snapshot_columns, live_columns,
        "COPY TEXT and pgoutput must agree on the column set, or the two phases \
         deserialize into different shapes"
    );
}

/// PG18's `publish_generated_columns = stored` replicates the generated column,
/// so the snapshot must **include** it — dropping it would make the snapshot
/// narrower than every streamed row of the same table.
///
/// Verified by decoding slot output directly: the `Relation` message carries
/// three columns (`id`, `a`, `g`). This is the case where "just exclude
/// generated columns" is the wrong fix.
#[tokio::test]
#[ignore = "requires live PostgreSQL with wal_level=logical"]
async fn snapshot_includes_a_published_generated_column_on_pg18() {
    let ver = server_version();
    if ver < 180000 {
        println!(
            "SKIP: snapshot_includes_a_published_generated_column_on_pg18 \
             requires PG >= 18 (server {ver})"
        );
        return;
    }

    let (slot, table, publication) = ("it_snap_gen_pub", "snap_gen_pub_t", "snap_gen_pub_p");
    let _guard = slot_guard(slot);
    setup_generated(
        table,
        publication,
        " WITH (publish_generated_columns = stored)",
    );

    let stream = LogicalReplicationStream::new(
        &replication_conn_string(),
        snapshot_config(slot, publication),
    )
    .await
    .expect("connect");

    let outcome = stream.snapshot().await.expect("snapshot");
    let SnapshotOutcome::Available(snap) = outcome else {
        panic!("expected a snapshot");
    };

    let mut events = snap.events();
    let snapshot_event = events
        .next_event()
        .await
        .expect("next_event")
        .expect("the seeded row");
    let snapshot_columns = insert_columns(&snapshot_event);
    let snapshot_data = match &snapshot_event.event_type {
        EventType::Insert { data, .. } => data.clone(),
        other => panic!("expected an Insert, got {other:?}"),
    };
    while events.next_event().await.expect("next_event").is_some() {}
    let mut stream = events.finish().await.expect("finish");

    assert!(
        snapshot_columns.contains(&"g".to_string()),
        "publish_generated_columns=stored replicates g, so the snapshot must \
         carry it too: {snapshot_columns:?}"
    );
    assert_eq!(
        snapshot_data.get("g").map(|v| v.as_str()),
        Some(Some("10")),
        "the generated value must be the server's, not a placeholder"
    );

    regular_conn()
        .exec(&format!("INSERT INTO {table} (a) VALUES (7)"))
        .expect("insert");

    stream.start(None).await.expect("start");
    let live = tokio::time::timeout(TEST_TIMEOUT, drain_inserts(&mut stream, 1))
        .await
        .expect("timed out");
    assert!(!live.is_empty(), "no insert arrived within 10s");

    assert_eq!(
        snapshot_columns,
        insert_columns(&live[0]),
        "the whole point: including g in one phase and not the other is the bug"
    );
}