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
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
//! NativeConnection struct and implementation.
//!
//! Pure-Rust PostgreSQL connection for replication, providing the same
//! public API as the libpq `PgReplicationConnection`.

use bytes::{Bytes, BytesMut};
use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc as std_mpsc;
use std::sync::Arc;
use tokio::sync::{mpsc, oneshot};
use tokio_util::sync::CancellationToken;
use tracing::{debug, info};

use super::conninfo::ConnInfo;
use super::startup::{self, Transport};
use super::{copy, copy_out, query, wire};
use super::{NativePgResult, NativeResultStatus};

use crate::error::{ReplicationError, Result};
use crate::protocol::build_hot_standby_feedback_message;
use crate::types::{format_lsn, BaseBackupOptions, ReplicationSlotOptions, SlotType, XLogRecPtr};

// A `NativeConnection` is a handle. A dedicated worker thread owns the socket on
// its own current-thread runtime, so all I/O stays on one reactor and the
// connection works under any runtime flavor (or none), unlike the old
// `block_in_place` bridge that panicked on a current-thread runtime. Sync
// methods block on a `std::sync::mpsc` reply, async methods await a oneshot.

/// Commands sent from the `NativeConnection` handle to its worker thread.
///
/// Each command carries its own reply channel.
enum Command {
    /// Run a simple query and return the raw result. The handle interprets the
    /// status and `is_ok`, so this stays a thin I/O primitive.
    Query {
        sql: String,
        reply: std_mpsc::Sender<Result<NativePgResult>>,
    },
    /// Run a simple query that answers with `CopyInResponse`, streaming
    /// `payload` into it. Only `UPLOAD_MANIFEST` uses this.
    QueryCopyIn {
        sql: String,
        payload: Bytes,
        reply: std_mpsc::Sender<Result<NativePgResult>>,
    },
    /// Enter the streaming push loop: the worker continuously reads CopyData
    /// batches and pushes them down `batch_tx` until the token is cancelled, the
    /// receiver is dropped, or a read error occurs. Replaces the old per-event
    /// `GetCopyBatch` request/reply round-trip, which cost two cross-thread
    /// wakeups per WAL message.
    StreamCopy {
        token: CancellationToken,
        batch_tx: mpsc::Sender<Result<VecDeque<Bytes>>>,
    },
    /// Send one CopyData message (standby status update or hot standby feedback).
    PutCopyData {
        data: Bytes,
        reply: oneshot::Sender<Result<()>>,
    },
    /// Drain a `COPY ... TO STDOUT` stream, pushing each payload down `chunk_tx`.
    ///
    /// The terminal is explicit rather than a closed channel: `Ok(Some(bytes))`
    /// is a payload, `Ok(None)` is a clean end with the `CommandComplete` /
    /// `ReadyForQuery` epilogue already consumed, and `Err` is a failure. A
    /// channel that closes with no terminal means the worker died.
    CopyOutStream {
        chunk_tx: mpsc::Sender<Result<Option<Bytes>>>,
    },
    /// Best-effort graceful shutdown, then stop the worker loop.
    Close {
        /// Whether the worker still owes the server a client `CopyDone` — true
        /// only for CopyBoth, never for COPY OUT.
        send_copy_done: bool,
        reply: std_mpsc::Sender<()>,
    },
}

/// Bounded batch queue between the worker thread and the consumer. Bounds
/// memory and applies backpressure when the consumer falls behind; a handful of
/// batches is enough to let the worker's next read overlap the consumer's parse.
const BATCH_CHANNEL_CAP: usize = 16;

/// Fail a command that cannot legally run while a COPY OUT is in flight.
///
/// Mirrors what the `StreamCopy` / `CopyOutStream` arms of [`Worker::handle_stream_cmd`] already do for their own illegal cases: answer the caller rather than touching the pinned transport.
#[cold]
#[inline(never)]
fn reject_during_copy_out(cmd: Command) {
    let err = || {
        ReplicationError::protocol(
            "this connection is draining a COPY OUT; no other command may run on it until it ends"
                .to_string(),
        )
    };
    match cmd {
        Command::Query { reply, .. } | Command::QueryCopyIn { reply, .. } => {
            let _ = reply.send(Err(err()));
        }
        Command::PutCopyData { reply, .. } => {
            let _ = reply.send(Err(err()));
        }
        Command::StreamCopy { batch_tx, .. } => {
            let _ = batch_tx.try_send(Err(err()));
        }
        Command::CopyOutStream { chunk_tx } => {
            let _ = chunk_tx.try_send(Err(err()));
        }
        // Handled by the caller, which owns the shutdown sequence.
        Command::Close { reply, .. } => {
            let _ = reply.send(());
        }
    }
}

/// What to do after the streaming loop services an interleaved command.
enum StreamCmd {
    /// Keep streaming.
    Continue,
    /// `Close` was handled; the worker should stop.
    Close,
    /// The command channel is gone; the worker should stop.
    WorkerGone,
}

/// Transport-owning state that lives entirely on the worker thread.
struct Worker {
    transport: Transport,
    read_buf: BytesMut,
    server_ver: i32,
    alive: Arc<AtomicBool>,
}

impl Worker {
    async fn query(&mut self, sql: &str) -> Result<NativePgResult> {
        copy::clear_latched_terminal(&mut self.read_buf);
        query::simple_query(&mut self.transport, &mut self.read_buf, sql).await
    }

    async fn query_copy_in(&mut self, sql: &str, payload: &[u8]) -> Result<NativePgResult> {
        copy::clear_latched_terminal(&mut self.read_buf);
        query::simple_query_copy_in(&mut self.transport, &mut self.read_buf, sql, payload).await
    }

    /// Pump a `COPY ... TO STDOUT` stream into `chunk_tx` until it ends.
    ///
    /// Always sends exactly one terminal (`Ok(None)` or `Err`) unless the
    /// receiver has already gone away, so the consumer can distinguish a clean
    /// end from a dead worker.
    ///
    /// Services `cmd_rx` throughout, exactly as [`Worker::stream_copy`] does, and
    /// for the same reason: without it a `Close` waits for the whole relation to
    /// be read, and against a stalled server — or a consumer that stopped reading
    /// — it never arrives at all. Returns `true` if a `Close` was handled.
    async fn drain_copy_out(
        &mut self,
        chunk_tx: mpsc::Sender<Result<Option<Bytes>>>,
        cmd_rx: &mut mpsc::UnboundedReceiver<Command>,
    ) -> bool {
        // A message waiting for room in `chunk_tx`, with the flag saying it is the stream's terminal. Held across iterations instead of being passed to `send()`, so a command winning the race cannot drop the future that owns it — the same reason `stream_copy` reserves rather than sends.
        let mut held: Option<(Result<Option<Bytes>>, bool)> = None;
        loop {
            if let Some((message, stop)) = held.take() {
                tokio::select! {
                    biased;
                    cmd = cmd_rx.recv() => {
                        held = Some((message, stop));
                        match self.handle_copy_out_cmd(cmd).await {
                            StreamCmd::Continue => continue,
                            StreamCmd::Close => return true,
                            StreamCmd::WorkerGone => return false,
                        }
                    }
                    permit = chunk_tx.reserve() => {
                        match permit {
                            Ok(permit) => permit.send(message),
                            // The consumer abandoned the snapshot.
                            Err(_) => return false,
                        }
                        if stop {
                            return false;
                        }
                    }
                }
            } else {
                tokio::select! {
                    biased;
                    cmd = cmd_rx.recv() => {
                        match self.handle_copy_out_cmd(cmd).await {
                            // `Continue` here means a command was rejected — and
                            // its arrival has already dropped the in-flight
                            // `next_copy_out` future. That future splits the
                            // `'c'` off `read_buf` before awaiting
                            // `drain_epilogue`, so a drop in there has consumed
                            // the `CopyDone` and no restart can recover it: the
                            // retry would meet the epilogue's `CommandComplete`
                            // instead of a data frame. End the COPY with a real
                            // error rather than reading on blind.
                            StreamCmd::Continue => {
                                self.alive.store(false, Ordering::Relaxed);
                                held = Some((
                                    Err(ReplicationError::protocol(
                                        "COPY OUT aborted: another command was issued on this \
                                         connection while it was streaming".to_string(),
                                    )),
                                    true,
                                ));
                                continue;
                            }
                            StreamCmd::Close => return true,
                            StreamCmd::WorkerGone => return false,
                        }
                    }
                    read = copy_out::next_copy_out(&mut self.transport, &mut self.read_buf) => {
                        held = Some(match read {
                            Ok(copy_out::CopyOutItem::Data(payload)) => (Ok(Some(payload)), false),
                            Ok(copy_out::CopyOutItem::Done) => (Ok(None), true),
                            Err(e) => {
                                // A failed COPY leaves the transport in an unknown state.
                                self.alive.store(false, Ordering::Relaxed);
                                (Err(e), true)
                            }
                        });
                    }
                }
            }
        }
    }

    /// Streaming push loop. Continuously reads CopyData batches and pushes them to `batch_tx`, while still servicing interleaved commands (feedback `PutCopyData`, `Close`) on `cmd_rx`. Returns `true` if a `Close` was  handled (the worker should stop), `false` if streaming ended for any other reason (cancel, read error, or the consumer dropped the receiver).
    ///
    /// The two threads pipeline: while the consumer parses one batch, the workers already parked on the next socket read. A ready batch is held and sent via `reserve()` inside the same `select!` as command handling, so backpressure on a full channel never blocks an incoming feedback/Close.
    async fn stream_copy(
        &mut self,
        token: CancellationToken,
        batch_tx: mpsc::Sender<Result<VecDeque<Bytes>>>,
        cmd_rx: &mut mpsc::UnboundedReceiver<Command>,
    ) -> bool {
        let mut held: Option<VecDeque<Bytes>> = None;
        loop {
            if let Some(batch) = held.take() {
                tokio::select! {
                    biased;
                    cmd = cmd_rx.recv() => {
                        held = Some(batch);
                        match self.handle_stream_cmd(cmd).await {
                            StreamCmd::Continue => continue,
                            StreamCmd::Close => return true,
                            StreamCmd::WorkerGone => return false,
                        }
                    }
                    permit = batch_tx.reserve() => match permit {
                        Ok(permit) => permit.send(Ok(batch)),
                        Err(_) => return false, // consumer dropped the receiver
                    }
                }
            } else {
                let mut batch = VecDeque::new();
                tokio::select! {
                    biased;
                    cmd = cmd_rx.recv() => {
                        match self.handle_stream_cmd(cmd).await {
                            StreamCmd::Continue => continue,
                            StreamCmd::Close => return true,
                            StreamCmd::WorkerGone => return false,
                        }
                    }
                    read = copy::get_copy_data(
                        &mut self.transport, &mut self.read_buf, &mut batch, &token,
                    ) => match read {
                        Ok(first) => {
                            batch.push_front(first);
                            held = Some(batch);
                        }
                        Err(err) => {
                            if !err.is_cancelled() {
                                self.alive.store(false, Ordering::Relaxed);
                            }

                            // Deliver the error even when the consumer is behind.
                            //
                            // `try_send` dropped it silently whenever the channel
                            // was full (`BATCH_CHANNEL_CAP` = 16). The consumer
                            // then drained its backlog, found the channel closed,
                            // and got `worker_gone()` → `Backend`, which
                            // `is_permanent()` reports as **retryable**. So a
                            // permanent SQLSTATE — a dropped or invalidated slot,
                            // the cases `from_sqlstate` exists to classify — was
                            // laundered into a transient one and the stream
                            // reconnect-looped forever against a server that would
                            // never accept it. The variant is control flow here;
                            // losing it silently disables the decision.
                            //
                            // `reserve()` instead, exactly as the data path above
                            // does, and keep servicing commands while waiting so a
                            // `Close` can never deadlock behind a stalled consumer
                            // (the same property the struct doc claims for the
                            // batch path).
                            loop {
                                tokio::select! {
                                    biased;
                                    cmd = cmd_rx.recv() => {
                                        match self.handle_stream_cmd(cmd).await {
                                            StreamCmd::Continue => {}
                                            StreamCmd::Close => return true,
                                            StreamCmd::WorkerGone => return false,
                                        }
                                    }
                                    permit = batch_tx.reserve() => {
                                        if let Ok(permit) = permit {
                                            permit.send(Err(err));
                                        }
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /// Service a command that arrived while a COPY OUT is draining.
    ///
    /// Deliberately stricter than [`handle_stream_cmd`](Self::handle_stream_cmd):
    /// only `Close` may run here. The transport is pinned mid-COPY, so a `Query`
    /// would write a `'Q'` onto it and then read `CopyData` frames as if they
    /// were that query's reply — and [`query`](Self::query) first calls
    /// `copy::clear_latched_terminal`, which drops a leading `'c'` and so
    /// silently eats the `CopyDone`. A hard desync with no error.
    ///
    /// Rejecting does **not** by itself save the in-flight read: by the time this
    /// runs, the `select!` has already dropped the `next_copy_out` future. That
    /// future splits the `'c'` off `read_buf` before awaiting `drain_epilogue`,
    /// so a drop in there has already consumed the `CopyDone` and a restart would
    /// meet the epilogue instead of a data frame. [`drain_copy_out`] therefore
    /// ends the stream with an error rather than restarting the read.
    async fn handle_copy_out_cmd(&mut self, cmd: Option<Command>) -> StreamCmd {
        match cmd {
            Some(Command::Close {
                send_copy_done,
                reply,
            }) => {
                self.close(send_copy_done).await;
                let _ = reply.send(());
                StreamCmd::Close
            }
            None => StreamCmd::WorkerGone,
            Some(other) => {
                reject_during_copy_out(other);
                StreamCmd::Continue
            }
        }
    }

    /// Service a command that arrived mid-stream.
    async fn handle_stream_cmd(&mut self, cmd: Option<Command>) -> StreamCmd {
        match cmd {
            Some(Command::PutCopyData { data, reply }) => {
                let _ = reply.send(self.put_copy_data(&data).await);
                StreamCmd::Continue
            }
            Some(Command::Query { sql, reply }) => {
                let _ = reply.send(self.query(&sql).await);
                StreamCmd::Continue
            }
            Some(Command::QueryCopyIn {
                sql,
                payload,
                reply,
            }) => {
                let _ = reply.send(self.query_copy_in(&sql, &payload).await);
                StreamCmd::Continue
            }
            Some(Command::Close {
                send_copy_done,
                reply,
            }) => {
                self.close(send_copy_done).await;
                let _ = reply.send(());
                StreamCmd::Close
            }
            Some(Command::StreamCopy { batch_tx, .. }) => {
                // Already streaming; reject a duplicate request rather than nest.
                let _ = batch_tx.try_send(Err(ReplicationError::backend("already streaming")));
                StreamCmd::Continue
            }
            Some(Command::CopyOutStream { chunk_tx }) => {
                // A connection is either replication-streaming or running a COPY
                // OUT, never both: the snapshot helper uses a second connection
                // precisely because this one is pinned in COPY BOTH. Reaching
                // here means a caller mixed them up, so fail loudly instead of
                // interleaving two COPY sub-protocols on one transport.
                let _ = chunk_tx.try_send(Err(ReplicationError::protocol(
                    "cannot start a COPY OUT on a connection that is streaming replication"
                        .to_string(),
                )));
                StreamCmd::Continue
            }
            None => StreamCmd::WorkerGone,
        }
    }

    async fn put_copy_data(&mut self, data: &[u8]) -> Result<()> {
        copy::put_copy_data(&mut self.transport, data).await
    }

    /// Best-effort graceful shutdown: CopyDone if the client owes one, then
    /// Terminate.
    async fn close(&mut self, send_copy_done: bool) {
        if send_copy_done {
            let _ = copy::send_copy_done(&mut self.transport).await;
        }
        let terminate = wire::build_terminate();
        let _ = wire::write_all(&mut self.transport, &terminate).await;
        let _ = wire::flush(&mut self.transport).await;
    }
}

/// How the worker thread obtains its transport before serving commands.
enum WorkerInit {
    /// Establish a real connection on the worker's own reactor.
    Connect {
        conninfo: String,
        alive: Arc<AtomicBool>,
    },
    /// Test-only: adopt a pre-built loopback socket. The worker calls
    /// `from_std` on its own reactor, so `null_for_testing` needs no ambient
    /// runtime.
    #[cfg(test)]
    Null {
        std_tcp: std::net::TcpStream,
        server_ver: i32,
        alive: Arc<AtomicBool>,
    },
}

impl WorkerInit {
    async fn build(self) -> Result<Worker> {
        match self {
            WorkerInit::Connect { conninfo, alive } => {
                let info = ConnInfo::parse(&conninfo)?;
                debug!("worker connect: parsed conninfo, host={}", info.host);
                let (transport, server_ver, read_buf) = startup::connect(&info).await?;
                debug!("worker connect: startup complete, version={}", server_ver);
                Ok(Worker {
                    transport,
                    read_buf,
                    server_ver,
                    alive,
                })
            }
            #[cfg(test)]
            WorkerInit::Null {
                std_tcp,
                server_ver,
                alive,
            } => {
                let tcp = tokio::net::TcpStream::from_std(std_tcp).map_err(|e| {
                    ReplicationError::backend(format!("failed to adopt test socket: {e}"))
                })?;
                Ok(Worker {
                    transport: Transport::Plain(tcp),
                    read_buf: BytesMut::new(),
                    server_ver,
                    alive,
                })
            }
        }
    }
}

/// Build the transport and report the outcome back to the connecting thread.
///
/// Consumes `ready_tx`, which is dropped when this returns (on either path), so the worker command loop never has to thread it through or drop it by hand.
async fn build_and_report(
    init: WorkerInit,
    ready_tx: std_mpsc::Sender<Result<i32>>,
) -> Option<Worker> {
    match init.build().await {
        Ok(worker) => {
            let _ = ready_tx.send(Ok(worker.server_ver));
            Some(worker)
        }
        Err(e) => {
            let _ = ready_tx.send(Err(e));
            None
        }
    }
}

/// Entry point for the dedicated worker thread.
///
/// Builds a current-thread runtime, establishes the transport, reports the
/// outcome over `ready_tx`, then serves commands until `Close` or until the
/// command channel closes.
fn run_worker(
    init: WorkerInit,
    mut cmd_rx: mpsc::UnboundedReceiver<Command>,
    ready_tx: std_mpsc::Sender<Result<i32>>,
) {
    let rt = match tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
    {
        Ok(rt) => rt,
        Err(e) => {
            let _ = ready_tx.send(Err(ReplicationError::backend(format!(
                "failed to build worker runtime: {e}"
            ))));
            return;
        }
    };

    rt.block_on(async move {
        let Some(mut worker) = build_and_report(init, ready_tx).await else {
            return;
        };

        while let Some(cmd) = cmd_rx.recv().await {
            match cmd {
                Command::Query { sql, reply } => {
                    let _ = reply.send(worker.query(&sql).await);
                }
                Command::QueryCopyIn {
                    sql,
                    payload,
                    reply,
                } => {
                    let _ = reply.send(worker.query_copy_in(&sql, &payload).await);
                }
                Command::StreamCopy { token, batch_tx } => {
                    // Runs its own loop, servicing interleaved commands, until
                    // streaming ends. Returns true only if it handled a Close.
                    if worker.stream_copy(token, batch_tx, &mut cmd_rx).await {
                        break;
                    }
                }
                Command::PutCopyData { data, reply } => {
                    let _ = reply.send(worker.put_copy_data(&data).await);
                }
                Command::CopyOutStream { chunk_tx } => {
                    // Same contract as `StreamCopy` above: runs its own loop,
                    // services interleaved commands, and reports a handled Close.
                    if worker.drain_copy_out(chunk_tx, &mut cmd_rx).await {
                        break;
                    }
                }
                Command::Close {
                    send_copy_done,
                    reply,
                } => {
                    worker.close(send_copy_done).await;
                    let _ = reply.send(());
                    break;
                }
            }
        }
    });
}

/// Drive an async future to completion from a sync context, on a specific
/// runtime. Used only by the inline driver.
///
/// `handle` is the multi-thread runtime the connection's socket was created on (captured at `connect`). We always drive the future on *that* runtime so the socket stays registered on its original reactor — regardless of the caller's context. This matters most on `Drop`: a connection can be dropped after the ambient runtime context is gone (e.g. moved out of the `block_on` scope it was created in), and resolving the runtime via `Handle::try_current()` at that point would build a throwaway runtime whose reactor never owned the socket, orphaning it and risking a silent hang.
fn run_sync<F>(handle: &tokio::runtime::Handle, fut: F) -> F::Output
where
    F: std::future::Future + Send,
    F::Output: Send,
{
    match tokio::runtime::Handle::try_current() {
        // Nested inside a multi-thread runtime worker: we must not block it directly. `block_in_place` offloads this worker; the inner `block_on` then drives `fut` on the stored handle's reactor. In the common case the stored handle *is* the current runtime (the canonical pattern).
        Ok(cur) if cur.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread => {
            tokio::task::block_in_place(|| handle.block_on(fut))
        }
        // Inside a *current-thread* runtime, both of the other arms are illegal:
        // `block_in_place` requires a multi-thread runtime, and `block_on` panics
        // with "Cannot start a runtime from within a runtime". Drive it on a
        // scratch OS thread, which has no ambient runtime; `fut` still runs on the
        // stored handle's reactor, so the socket stays where it was registered.
        //
        // Reachable when an `Inline` connection (born on a multi-thread runtime)
        // is dropped under a current-thread one — including while unwinding, where
        // the old panic would have aborted the process.
        Ok(_) => std::thread::scope(|s| {
            s.spawn(|| handle.block_on(fut))
                .join()
                .unwrap_or_else(|e| std::panic::resume_unwind(e))
        }),
        // No ambient runtime (e.g. `Drop` on a plain thread): block on the stored handle directly. The runtime's own worker threads service the reactor.
        Err(_) => handle.block_on(fut),
    }
}

/// How a `NativeConnection` drives its socket I/O.
///
/// `Inline` runs the connection's `Worker` directly on the caller's runtime, chosen only on a multi-thread runtime, where this is safe and avoids the cross-thread channel/second-reactor cost. `Threaded` keeps the dedicated worker-thread bridge, used on a current-thread runtime (where the inline `run_sync` `block_in_place` would panic) and with no ambient runtime (where a per-call temporary runtime would orphan the socket). The Inline variant owns a `Worker` (large `Transport`) on the I/O hot path; boxing it would add a pointer indirection to every read, so the size gap with the channel-only Threaded variant is an intentional tradeoff (cf. `Transport`).
#[allow(clippy::large_enum_variant)]
enum Driver {
    /// Worker owned directly; sync methods use `run_sync`, async methods await.
    Inline {
        worker: Worker,
        pending: VecDeque<Bytes>,
        /// The multi-thread runtime the socket was created on; `run_sync` always drives sync I/O on it so the socket never ends up on a foreign or temporary reactor (see `run_sync`).
        handle: tokio::runtime::Handle,
    },
    /// Worker lives on its own thread; commands cross `cmd_tx`, batches `batch_rx`.
    Threaded {
        cmd_tx: mpsc::UnboundedSender<Command>,
        worker: Option<std::thread::JoinHandle<()>>,
        pending: VecDeque<Bytes>,
        batch_rx: Option<mpsc::Receiver<Result<VecDeque<Bytes>>>>,
    },
}

/// Pure-Rust PostgreSQL connection for replication.
///
/// Provides the same public API as the libpq `PgReplicationConnection` so that `stream.rs` works unchanged regardless of backend. Socket I/O runs either inline on the caller's runtime or on a dedicated worker thread, chosen at `connect` by the ambient runtime flavor (see `Driver`).
pub struct NativeConnection {
    /// How socket I/O is driven (inline vs. worker thread).
    driver: Driver,
    /// Server version number (e.g. 160001 for PG 16.1), cached at connect time.
    server_ver: i32,
    /// Which COPY sub-protocol the transport is currently inside.
    copy: CopyMode,
    /// Liveness flag shared with the worker, which clears it on a transient read error.
    alive: Arc<AtomicBool>,
}

/// Which COPY sub-protocol the transport is inside, and that mode's state.
///
/// A connection is in **at most one** of these at a time, which is the whole
/// reason this is an enum rather than the `in_copy_mode: bool` +
/// `copy_out: Option<_>` pair it replaces: those two fields could both be set,
/// a state no code rejected and the protocol cannot represent.
///
/// The modes are not interchangeable — they disagree on who ends the stream.
/// In CopyBoth the *client* sends `CopyDone`, so shutdown must send one; in
/// COPY OUT only the server may, and a client `CopyDone` is a protocol
/// violation. libpq documents the same asymmetry: `PQputCopyEnd` is illegal in
/// `PGRES_COPY_OUT`. The libpq backend models this the same way, so the two
/// backends stay legible side by side.
#[derive(Debug, Default)]
enum CopyMode {
    /// Not in a COPY; ordinary queries are allowed.
    #[default]
    Idle,
    /// A server-push stream whose batches are owned by [`Driver`]: `CopyBoth` from `START_REPLICATION`, or the COPY OUT that `BASE_BACKUP` answers with.
    ///
    /// `BASE_BACKUP` is the exception to the asymmetry described above — it is genuinely a COPY OUT, so the client `CopyDone` that shutdown writes is not legal for it. Harmless in practice, because `Terminate` follows immediately and the server is discarding the connection either way, andunchanged from the `in_copy_mode: bool` this enum replaced. Named here rather than fixed with a third variant, which would buy nothing beyond suppressing one doomed write.
    Replication,
    /// `COPY ... TO STDOUT`, from the snapshot helper. Read directly rather than through [`Driver`]'s batch channel.
    Out {
        /// Threaded driver only: the worker's push channel, started lazily on
        /// the first [`NativeConnection::copy_out_next`] call. `None` on the
        /// inline driver, which reads the transport directly.
        rx: Option<mpsc::Receiver<Result<Option<Bytes>>>>,
    },
}

impl CopyMode {
    /// True while a CopyBoth stream is open. Gates the streaming methods and
    /// decides whether shutdown owes the server a client `CopyDone`.
    #[inline]
    fn is_replication(&self) -> bool {
        matches!(self, Self::Replication)
    }

    /// True while a `COPY ... TO STDOUT` is open.
    #[inline]
    fn is_out(&self) -> bool {
        matches!(self, Self::Out { .. })
    }
}

impl NativeConnection {
    // ── Connection establishment ─────────────────────────────────────────

    /// Create a new PostgreSQL connection for logical replication.
    ///
    /// On a **multi-thread** runtime the connection runs inline on the caller's
    /// runtime (cheaper: no worker thread, no cross-thread channel). On a
    /// current-thread runtime *or with no ambient runtime* it spawns a dedicated
    /// worker thread. The choice is fixed here for the connection's lifetime.
    pub fn connect(conninfo: &str) -> Result<Self> {
        if Self::prefer_inline_driver() {
            Self::connect_inline(conninfo)
        } else {
            Self::connect_threaded(conninfo)
        }
    }

    /// The inline driver is chosen *only* under a persistent multi-thread ambient
    /// runtime, where `run_sync` (`block_in_place` + `Handle::block_on`) reuses
    /// that runtime's reactor across calls — so the connection's socket stays
    /// registered for the connection's whole lifetime.
    ///
    /// A current-thread ambient runtime can't run `block_in_place`, and with no
    /// ambient runtime each `run_sync` would spin up a *fresh* temporary runtime
    /// whose reactor dies when it returns, orphaning the long-lived socket. Both
    /// cases therefore use the worker thread, which owns one persistent runtime.
    fn prefer_inline_driver() -> bool {
        matches!(
            tokio::runtime::Handle::try_current(),
            Ok(h) if h.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread
        )
    }

    /// Inline driver: build the worker on the caller's runtime via `run_sync`.
    fn connect_inline(conninfo: &str) -> Result<Self> {
        let handle = tokio::runtime::Handle::current();
        let alive = Arc::new(AtomicBool::new(false));
        let worker = run_sync(
            &handle,
            WorkerInit::Connect {
                conninfo: conninfo.to_string(),
                alive: alive.clone(),
            }
            .build(),
        )?;
        alive.store(true, Ordering::Relaxed);
        let server_ver = worker.server_ver;
        debug!(
            "Connected to PostgreSQL {} via native rustls (inline)",
            server_ver
        );
        Ok(Self {
            driver: Driver::Inline {
                worker,
                pending: VecDeque::with_capacity(256),
                handle,
            },
            server_ver,
            copy: CopyMode::Idle,
            alive,
        })
    }

    /// Threaded driver: spawn the worker thread, which establishes the TCP connection (optionally upgraded to TLS via rustls) and performs the v3.0 startup handshake and authentication on its own runtime. Blocks until the worker reports success or failure.
    fn connect_threaded(conninfo: &str) -> Result<Self> {
        let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
        let (ready_tx, ready_rx) = std_mpsc::channel();
        let alive = Arc::new(AtomicBool::new(false));
        let worker_alive = alive.clone();
        let conninfo = conninfo.to_string();

        let worker = std::thread::Builder::new()
            .name("pg-walstream-native".to_string())
            .spawn(move || {
                run_worker(
                    WorkerInit::Connect {
                        conninfo,
                        alive: worker_alive,
                    },
                    cmd_rx,
                    ready_tx,
                )
            })
            .map_err(|e| {
                ReplicationError::backend(format!("failed to spawn native worker thread: {e}"))
            })?;

        match ready_rx.recv() {
            Ok(Ok(server_ver)) => {
                alive.store(true, Ordering::Relaxed);
                debug!("Connected to PostgreSQL {} via native rustls", server_ver);
                Ok(Self {
                    driver: Driver::Threaded {
                        cmd_tx,
                        worker: Some(worker),
                        pending: VecDeque::with_capacity(256),
                        batch_rx: None,
                    },
                    server_ver,
                    copy: CopyMode::Idle,
                    alive,
                })
            }
            Ok(Err(e)) => {
                let _ = worker.join();
                Err(e)
            }
            Err(_) => {
                let _ = worker.join();
                Err(ReplicationError::backend(
                    "native worker thread exited before connecting",
                ))
            }
        }
    }

    // ── Query execution ─────────────────────────────────────────────────

    /// Run a simple query: inline on the worker, or over the command channel.
    ///
    /// No read deadline, deliberately. A command timeout was prototyped here and
    /// removed: there is no safe default. `CREATE_REPLICATION_SLOT ... LOGICAL`
    /// defaults to snapshot export server-side (`snapshot_action =
    /// CRS_EXPORT_SNAPSHOT`, walsender.c) — this crate omits the keyword, so that
    /// is what it gets — and export waits for every concurrent write transaction
    /// to reach consistency. PostgreSQL's own comment there calls
    /// `DecodingContextFindStartpoint` something that "can take long time". A
    /// deadline firing on that would mark the connection dead, the stream layer
    /// would reconnect and reissue the command, and the wait would restart: an
    /// infinite retry loop that can never succeed. Worse than blocking, which at
    /// least is visible and does not churn connections.
    ///
    /// A future knob should therefore be per-command (fast commands only) or
    /// caller-supplied, not a blanket default. libpq takes the same position: it
    /// ships `connect_timeout` and no statement deadline.
    fn run_query(&mut self, sql: &str) -> Result<NativePgResult> {
        match &mut self.driver {
            Driver::Inline { worker, handle, .. } => run_sync(handle, worker.query(sql)),
            Driver::Threaded { cmd_tx, .. } => {
                let (reply_tx, reply_rx) = std_mpsc::channel();
                cmd_tx
                    .send(Command::Query {
                        sql: sql.to_string(),
                        reply: reply_tx,
                    })
                    .map_err(|_| Self::worker_gone())?;
                reply_rx.recv().map_err(|_| Self::worker_gone())?
            }
        }
    }

    /// Run a CopyIn query: inline on the worker, or over the command channel.
    fn run_query_copy_in(&mut self, sql: &str, payload: &[u8]) -> Result<NativePgResult> {
        match &mut self.driver {
            Driver::Inline { worker, handle, .. } => {
                run_sync(handle, worker.query_copy_in(sql, payload))
            }
            Driver::Threaded { cmd_tx, .. } => {
                let (reply_tx, reply_rx) = std_mpsc::channel();
                cmd_tx
                    .send(Command::QueryCopyIn {
                        sql: sql.to_string(),
                        payload: Bytes::copy_from_slice(payload),
                        reply: reply_tx,
                    })
                    .map_err(|_| Self::worker_gone())?;
                reply_rx.recv().map_err(|_| Self::worker_gone())?
            }
        }
    }

    #[cold]
    fn worker_gone() -> ReplicationError {
        ReplicationError::backend("native worker thread is gone")
    }

    #[cold]
    fn worker_reply_dropped() -> ReplicationError {
        ReplicationError::backend("native worker thread dropped the reply")
    }

    /// Execute a replication command (like IDENTIFY_SYSTEM).
    ///
    /// Rejected unless the transport is idle. Writing a `Query` onto a connection
    /// that is mid-COPY is a protocol violation, and on the inline driver it does
    /// not even fail loudly: `simple_query` skips the queued `CopyData` frames as
    /// unknown tags, reads the COPY's own `CommandComplete`/`ReadyForQuery`, and
    /// returns a bogus `CommandOk` — a silent wrong answer plus a permanent
    /// one-message offset for every later command. This is the hazard the
    /// internal `CopyMode` enum exists to make unrepresentable; the check belongs
    /// here too, not only in `copy_out_begin`.
    ///
    /// Every internal caller issues its command while idle and only then records
    /// the new mode, so none of them are affected.
    pub fn exec(&mut self, sql: &str) -> Result<NativePgResult> {
        match self.copy {
            CopyMode::Idle => {}
            CopyMode::Out { .. } => {
                return Err(ReplicationError::protocol(
                    "cannot run a query while a COPY OUT is in progress on this connection"
                        .to_string(),
                ))
            }
            CopyMode::Replication => {
                return Err(ReplicationError::protocol(
                    "cannot run a query while this connection is streaming replication".to_string(),
                ))
            }
        }

        let result = self.run_query(sql)?;

        let status_str = format!("{:?}", result.status());
        debug!("query : {} pg_result.status() : {}", sql, status_str);

        if !result.is_ok() {
            let error_msg = result
                .error_message()
                .unwrap_or_else(|| "Unknown error".to_string());
            return Err(ReplicationError::from_sqlstate(
                &result.error_sqlstate(),
                format!("Query execution failed: {error_msg}"),
            ));
        }

        Ok(result)
    }

    /// Send IDENTIFY_SYSTEM command.
    pub fn identify_system(&mut self) -> Result<NativePgResult> {
        debug!("Sending IDENTIFY_SYSTEM command");
        let result = self.exec("IDENTIFY_SYSTEM")?;

        if result.ntuples() > 0 {
            if let (Some(systemid), Some(timeline), Some(xlogpos)) = (
                result.get_value(0, 0),
                result.get_value(0, 1),
                result.get_value(0, 2),
            ) {
                debug!(
                    "System identification: systemid={}, timeline={}, xlogpos={}",
                    systemid, timeline, xlogpos
                );
            }
        }

        Ok(result)
    }

    // ── Replication ─────────────────────────────────────────────────────

    /// Start logical replication.
    pub fn start_replication(
        &mut self,
        slot_name: &str,
        start_lsn: XLogRecPtr,
        options: &[(&str, &str)],
    ) -> Result<()> {
        let sql = crate::sql_builder::build_start_replication_sql(slot_name, start_lsn, options)?;
        debug!("Starting replication: {}", sql);

        let result = self.run_query(&sql)?;
        if result.status() != &NativeResultStatus::CopyBoth {
            let error_msg = result
                .error_message()
                .unwrap_or_else(|| "Unknown error".to_string());
            return Err(ReplicationError::from_sqlstate(
                &result.error_sqlstate(),
                format!("START_REPLICATION did not enter COPY mode: {error_msg}"),
            ));
        }

        self.copy = CopyMode::Replication;
        debug!("Replication started successfully");
        Ok(())
    }

    /// Get copy data from the replication stream (truly async, non-blocking).
    ///
    /// Serves from the local batch buffer first. When empty, pulls the next batch the worker has already pushed down a buffered channel — no per-message request/reply round-trip. The worker streams continuously, so its next socket read overlaps the caller's parse of the current batch.
    ///
    /// Cancel via `cancellation_token` rather than by dropping this future.
    pub async fn get_copy_data_async(
        &mut self,
        cancellation_token: &CancellationToken,
    ) -> Result<Bytes> {
        self.ensure_replication_mode()?;
        let alive = self.alive.clone();

        match &mut self.driver {
            // Inline: read directly on the caller's runtime. `copy::get_copy_data`
            // serves from `pending` first, then reads+drains the socket.
            Driver::Inline {
                worker, pending, ..
            } => {
                let result = copy::get_copy_data(
                    &mut worker.transport,
                    &mut worker.read_buf,
                    pending,
                    cancellation_token,
                )
                .await;

                if result.as_ref().err().is_some_and(|e| !e.is_cancelled()) {
                    alive.store(false, Ordering::Relaxed);
                }
                result
            }
            // Threaded: serve from the local buffer, else pull the next batch the
            // worker has already pushed down the channel.
            Driver::Threaded {
                cmd_tx,
                pending,
                batch_rx,
                ..
            } => {
                if let Some(payload) = pending.pop_front() {
                    return Ok(payload);
                }

                // Lazily start the worker's streaming push loop on first use (and after a prior stream ended), binding it to this cancellation token.
                if batch_rx.is_none() {
                    let (batch_tx, rx) = mpsc::channel(BATCH_CHANNEL_CAP);
                    if cmd_tx
                        .send(Command::StreamCopy {
                            token: cancellation_token.clone(),
                            batch_tx,
                        })
                        .is_err()
                    {
                        alive.store(false, Ordering::Relaxed);
                        return Err(Self::worker_gone());
                    }
                    *batch_rx = Some(rx);
                }

                let batch = {
                    let rx = batch_rx.as_mut().unwrap();
                    tokio::select! {
                        biased;
                        _ = cancellation_token.cancelled() => {
                            // Stream is ending; drop the receiver so a later call restarts it.
                            *batch_rx = None;
                            return Err(ReplicationError::Cancelled("Operation cancelled".to_string()));
                        }
                        recv = rx.recv() => match recv {
                            Some(Ok(batch)) => batch,
                            Some(Err(e)) => {
                                *batch_rx = None;
                                return Err(e);
                            }
                            None => {
                                // Worker dropped the sender (stream ended); allow a restart.
                                *batch_rx = None;
                                alive.store(false, Ordering::Relaxed);
                                return Err(Self::worker_gone());
                            }
                        }
                    }
                };

                *pending = batch;
                Ok(pending
                    .pop_front()
                    .expect("stream_copy pushes only non-empty batches"))
            }
        }
    }

    /// Send feedback to the server (standby status update).
    pub async fn send_standby_status_update(
        &mut self,
        received_lsn: XLogRecPtr,
        flushed_lsn: XLogRecPtr,
        applied_lsn: XLogRecPtr,
        reply_requested: bool,
    ) -> Result<()> {
        self.ensure_replication_mode()?;

        let reply_data = crate::protocol::build_standby_status_update_message(
            received_lsn,
            flushed_lsn,
            applied_lsn,
            reply_requested,
        );
        self.put_copy_data(reply_data).await?;

        info!(
            "Sent standby status update: received={}, flushed={}, applied={}, reply_requested={}",
            format_lsn(received_lsn),
            format_lsn(flushed_lsn),
            format_lsn(applied_lsn),
            reply_requested
        );

        Ok(())
    }

    /// Send a client CopyDone to end the COPY stream cleanly.
    ///
    /// Idempotent: a no-op when not in COPY mode. On the Inline driver we write
    /// the CopyDone frame directly on the transport. On the Threaded driver we
    /// hand the existing graceful `Close` command to the worker (which sends
    /// CopyDone + Terminate) without blocking the async task on its reply —
    /// `Drop`'s `close_connection` still joins the worker, guaranteeing the
    /// frames flush before teardown completes.
    pub(crate) async fn end_copy(&mut self) -> Result<()> {
        if !self.copy.is_replication() {
            return Ok(());
        }
        match &mut self.driver {
            Driver::Inline { worker, .. } => {
                // Write the CopyDone, then leave COPY mode ONLY after it is fully flushed. If this errors or the future is cancelled mid-write, the mode stays `Replication` so Drop's `close(true)` resends a well-formed CopyDone rather than leaving a torn frame.
                if let Err(e) = copy::send_copy_done(&mut worker.transport).await {
                    self.alive.store(false, Ordering::Relaxed);
                    return Err(e);
                }
                self.copy = CopyMode::Idle;
            }
            Driver::Threaded {
                cmd_tx, batch_rx, ..
            } => {
                // The worker (not this task) writes the frame, so leaving COPY
                // mode up front is safe and keeps Drop from re-sending CopyDone.
                self.copy = CopyMode::Idle;
                // Dropping the batch receiver lets a back-pressured worker
                // (parked on `reserve()`) resolve and service the command.
                *batch_rx = None;
                let (reply_tx, reply_rx) = std_mpsc::channel();
                if cmd_tx
                    .send(Command::Close {
                        send_copy_done: true,
                        reply: reply_tx,
                    })
                    .is_ok()
                {
                    // The blocking `recv()` runs on a blocking thread so the async executor is not parked. `.is_ok()` above already handled a dead worker (channel closed), so this cannot hang on one.
                    let _ = tokio::task::spawn_blocking(move || reply_rx.recv()).await;
                }
                self.alive.store(false, Ordering::Relaxed);
            }
        }
        Ok(())
    }

    /// Send hot standby feedback message to the server.
    pub async fn send_hot_standby_feedback(
        &mut self,
        xmin: u32,
        xmin_epoch: u32,
        catalog_xmin: u32,
        catalog_xmin_epoch: u32,
    ) -> Result<()> {
        self.ensure_replication_mode()?;

        let feedback_data =
            build_hot_standby_feedback_message(xmin, xmin_epoch, catalog_xmin, catalog_xmin_epoch)?;

        self.put_copy_data(feedback_data).await?;

        debug!(
            "Sent hot standby feedback: xmin={}, catalog_xmin={}",
            xmin, catalog_xmin
        );
        Ok(())
    }

    /// Send one CopyData message: inline on the worker, or over the channel.
    async fn put_copy_data(&mut self, data: Bytes) -> Result<()> {
        match &mut self.driver {
            Driver::Inline { worker, .. } => worker.put_copy_data(data.as_ref()).await,
            Driver::Threaded { cmd_tx, .. } => {
                let (reply_tx, reply_rx) = oneshot::channel();
                cmd_tx
                    .send(Command::PutCopyData {
                        data,
                        reply: reply_tx,
                    })
                    .map_err(|_| Self::worker_gone())?;
                reply_rx.await.map_err(|_| Self::worker_reply_dropped())?
            }
        }
    }

    // ── Connection info ─────────────────────────────────────────────────

    /// Check if the connection is still alive.
    pub(crate) fn is_alive(&self) -> bool {
        self.alive.load(Ordering::Relaxed)
    }

    /// Force the connection to report dead.
    ///
    /// The COPY read paths clear liveness themselves, but a failing *command*
    /// does not: `start_replication` returns `Err` without touching `alive`, so a
    /// half-initialised recovery — a live connection that never entered COPY mode
    /// — still looked healthy. See the call site in
    /// `LogicalReplicationStream::recover_connection`.
    ///
    /// Takes `&mut self` to match the libpq backend, which sets a plain flag.
    pub(crate) fn mark_dead(&mut self) {
        self.alive.store(false, Ordering::Relaxed);
    }

    /// Get the server version.
    pub fn server_version(&self) -> i32 {
        self.server_ver
    }

    // ── Replication slot management ─────────────────────────────────────

    /// Create a replication slot with advanced options.
    pub fn create_replication_slot_with_options(
        &mut self,
        slot_name: &str,
        slot_type: SlotType,
        output_plugin: Option<&str>,
        options: &ReplicationSlotOptions,
    ) -> Result<NativePgResult> {
        let sql = crate::sql_builder::prepare_create_slot(
            self.server_version(),
            slot_name,
            slot_type,
            output_plugin,
            options,
        )?;
        debug!("Creating replication slot: {}", sql);
        self.exec(&sql)
    }

    /// Alter a replication slot (logical slots only).
    pub fn alter_replication_slot(
        &mut self,
        slot_name: &str,
        two_phase: Option<bool>,
        failover: Option<bool>,
    ) -> Result<NativePgResult> {
        let sql = crate::sql_builder::prepare_alter_slot(
            self.server_version(),
            slot_name,
            two_phase,
            failover,
        )?;

        debug!("Altering replication slot: {}", sql);
        let result = self.exec(&sql)?;
        debug!("Replication slot {} altered", slot_name);
        Ok(result)
    }

    /// Drop a replication slot.
    pub fn drop_replication_slot(&mut self, slot_name: &str, wait: bool) -> Result<()> {
        let sql = crate::sql_builder::build_drop_slot_sql(slot_name, wait)?;
        debug!("Dropping replication slot: {}", sql);
        let result = self.exec(&sql)?;
        if !result.is_ok() {
            return Err(ReplicationError::replication_slot(format!(
                "Failed to drop replication slot '{}': {}",
                slot_name,
                result
                    .error_message()
                    .unwrap_or_else(|| "unknown error".to_string())
            )));
        }
        debug!("Replication slot {} dropped", slot_name);
        Ok(())
    }

    /// Read information about a replication slot.
    pub fn read_replication_slot(
        &mut self,
        slot_name: &str,
    ) -> Result<crate::types::ReplicationSlotInfo> {
        let sql = crate::sql_builder::prepare_read_slot(self.server_version(), slot_name)?;
        debug!("Reading replication slot: {}", sql);
        let result = self.exec(&sql)?;
        if !result.is_ok() {
            return Err(ReplicationError::replication_slot(format!(
                "Failed to read replication slot '{}': {}",
                slot_name,
                result
                    .error_message()
                    .unwrap_or_else(|| "unknown error".to_string())
            )));
        }

        let slot_type = result.get_value(0, 0);
        let restart_lsn = result
            .get_value(0, 1)
            .and_then(|s| crate::types::parse_lsn(&s).ok())
            .map(crate::types::Lsn::new);
        let restart_tli = result.get_value(0, 2).and_then(|s| s.parse::<i32>().ok());

        Ok(crate::types::ReplicationSlotInfo {
            slot_type,
            restart_lsn,
            restart_tli,
        })
    }

    /// Start physical replication.
    pub fn start_physical_replication(
        &mut self,
        slot_name: Option<&str>,
        start_lsn: XLogRecPtr,
        timeline_id: Option<u32>,
    ) -> Result<()> {
        let sql = crate::sql_builder::build_start_physical_replication_sql(
            slot_name,
            start_lsn,
            timeline_id,
        )?;
        debug!("Starting physical replication: {}", sql);

        let result = self.run_query(&sql)?;
        match result.status() {
            NativeResultStatus::CopyBoth | NativeResultStatus::CopyOut => {}
            _ => {
                let error_msg = result
                    .error_message()
                    .unwrap_or_else(|| "Unknown error".to_string());
                return Err(ReplicationError::from_sqlstate(
                    &result.error_sqlstate(),
                    format!("START_REPLICATION did not enter COPY mode: {error_msg}"),
                ));
            }
        }

        self.copy = CopyMode::Replication;
        debug!("Physical replication started successfully");
        Ok(())
    }

    /// Upload a backup manifest in preparation for an incremental base backup.
    ///
    /// Sends `UPLOAD_MANIFEST` and streams `manifest` (the `backup_manifest` file from the prior full backup) into the resulting CopyIn. The server keeps it for the duration of the connection, so the following  [`base_backup`](Self::base_backup) with [`BaseBackupOptions::incremental`] must run on this same connection.
    ///
    /// Requires PostgreSQL 17+. The server also needs `summarize_wal = on`, or the subsequent incremental backup fails with a WAL-summary error.
    pub fn upload_manifest(&mut self, manifest: &[u8]) -> Result<()> {
        crate::sql_builder::check_upload_manifest_version(self.server_version())?;

        debug!("Uploading backup manifest ({} bytes)", manifest.len());
        let result = self.run_query_copy_in("UPLOAD_MANIFEST", manifest)?;
        if !result.is_ok() {
            let error_msg = result
                .error_message()
                .unwrap_or_else(|| "Unknown error".to_string());
            return Err(ReplicationError::from_sqlstate(
                &result.error_sqlstate(),
                format!("UPLOAD_MANIFEST failed: {error_msg}"),
            ));
        }
        debug!("Backup manifest uploaded");
        Ok(())
    }

    /// Start a base backup with options.
    pub fn base_backup(&mut self, options: &BaseBackupOptions) -> Result<NativePgResult> {
        let sql = crate::sql_builder::prepare_base_backup(self.server_version(), options)?;

        debug!("Starting base backup: {}", sql);
        let result = self.exec(&sql)?;

        self.copy = CopyMode::Replication;
        debug!("Base backup started successfully");
        Ok(result)
    }

    /// Blocking, like every other command on this type ([`exec`](Self::exec),
    /// `run_query`). Only the data loop ([`copy_out_next`](Self::copy_out_next))
    /// is async, and that is the part that can run for minutes.
    ///
    /// On success the transport is parked at the first `CopyData` frame:
    /// `simple_query` consumes the `CopyOutResponse` and stops *without* reading
    /// `ReadyForQuery`.
    pub(crate) fn copy_out_begin(&mut self, sql: &str) -> Result<()> {
        // Must be fully idle, not merely "not already in a COPY OUT": issuing a
        // Query while the transport sits in CopyBoth is a protocol violation,
        // and the old two-flag shape let that through because the replication
        // flag was a separate field this check never looked at.
        match self.copy {
            CopyMode::Idle => {}
            CopyMode::Out { .. } => {
                return Err(ReplicationError::protocol(
                    "a COPY OUT stream is already in progress on this connection".to_string(),
                ))
            }
            CopyMode::Replication => {
                return Err(ReplicationError::protocol(
                    "cannot start a COPY OUT on a connection that is streaming replication"
                        .to_string(),
                ))
            }
        }

        // Idle is not enough on its own. A failed `copy_out_next` returns the
        // mode to Idle *and* marks the connection dead, precisely because the
        // rest of the relation is still queued on the transport — so without this
        // check the Query below would be answered by the previous COPY's
        // epilogue, and the failure would surface as
        // `did not enter COPY OUT mode (status CommandOk)` against the wrong
        // statement.
        if !self.is_alive() {
            return Err(ReplicationError::protocol(
                "cannot start a COPY OUT on a connection that is no longer usable".to_string(),
            ));
        }

        let result = self.exec(sql)?;
        if result.status() != &NativeResultStatus::CopyOut {
            return Err(ReplicationError::protocol(format!(
                "{sql} did not enter COPY OUT mode (status {:?})",
                result.status()
            )));
        }

        self.copy = CopyMode::Out { rx: None };
        Ok(())
    }

    /// The next `CopyData` payload, or `Ok(None)` once the stream has ended
    /// cleanly and its epilogue has been consumed.
    ///
    /// After `Ok(None)` or any `Err` the stream is closed and a further call is
    /// an error; a failure additionally marks the connection dead, since a COPY
    /// interrupted mid-stream leaves the transport in an unknown state.
    ///
    /// Cancellation is checked ahead of the transport (a `biased` `select!`), so
    /// a cancelled read reports `Cancelled` on the very next poll and any rows
    /// already buffered go away with the connection. The libpq backend does the
    /// opposite: it keeps handing back rows it had already drained and reports
    /// `Cancelled` only once its queue is empty.
    pub(crate) async fn copy_out_next(
        &mut self,
        cancellation_token: &CancellationToken,
    ) -> Result<Option<Bytes>> {
        if !self.copy.is_out() {
            return Err(ReplicationError::protocol(
                "no COPY OUT stream is in progress on this connection".to_string(),
            ));
        }

        let result = self.copy_out_next_inner(cancellation_token).await;

        match &result {
            // Clean end: release the stream, keep the connection usable.
            Ok(None) => self.copy = CopyMode::Idle,
            Ok(Some(_)) => {}
            // Any early exit, cancellation included. COPY OUT has no
            // protocol-level early exit (see `copy_out_abort`), so the only way
            // to stop before the last row is to hang up — a cancelled read
            // leaves the rest of the relation queued on the transport. Returning
            // to Idle-and-alive would advertise that connection as reusable, and
            // the next `copy_out_begin` would read the *previous* COPY's
            // completion and fail with a misleading status.
            Err(_) => {
                self.copy = CopyMode::Idle;
                self.alive.store(false, Ordering::Relaxed);
            }
        }

        result
    }

    async fn copy_out_next_inner(
        &mut self,
        cancellation_token: &CancellationToken,
    ) -> Result<Option<Bytes>> {
        match &mut self.driver {
            // Inline: read directly on the caller's runtime, mirroring
            // `get_copy_data_async`'s Inline arm.
            Driver::Inline { worker, .. } => {
                tokio::select! {
                    biased;
                    _ = cancellation_token.cancelled() => {
                        Err(ReplicationError::Cancelled("COPY OUT cancelled".to_string()))
                    }
                    item = copy_out::next_copy_out(&mut worker.transport, &mut worker.read_buf) => {
                        match item? {
                            copy_out::CopyOutItem::Data(payload) => Ok(Some(payload)),
                            copy_out::CopyOutItem::Done => Ok(None),
                        }
                    }
                }
            }
            // Threaded: start the worker's drain loop lazily, then pull from it.
            Driver::Threaded { cmd_tx, .. } => {
                let CopyMode::Out { rx } = &mut self.copy else {
                    unreachable!("presence checked by copy_out_next");
                };

                if rx.is_none() {
                    let (chunk_tx, chan) = mpsc::channel(BATCH_CHANNEL_CAP);
                    if cmd_tx.send(Command::CopyOutStream { chunk_tx }).is_err() {
                        return Err(Self::worker_gone());
                    }
                    *rx = Some(chan);
                }

                let rx = rx.as_mut().expect("just populated");
                tokio::select! {
                    biased;
                    _ = cancellation_token.cancelled() => {
                        Err(ReplicationError::Cancelled("COPY OUT cancelled".to_string()))
                    }
                    received = rx.recv() => match received {
                        Some(item) => item,
                        // Closed with no terminal: the worker died.
                        None => Err(Self::worker_gone()),
                    },
                }
            }
        }
    }

    /// Abandon an in-flight `COPY ... TO STDOUT`.
    ///
    /// There is no protocol-level early exit from a COPY OUT: the client sends
    /// nothing, and `CopyDone` is the server's to send. So the only ways to stop
    /// early are to read the whole relation or to hang up. This marks the
    /// connection dead; the caller is expected to drop it, which sends
    /// `Terminate` and lets the server abort the COPY and its transaction.
    ///
    /// A no-op when no stream is in progress.
    pub(crate) fn copy_out_abort(&mut self) {
        if self.copy.is_out() {
            self.copy = CopyMode::Idle;
            self.mark_dead();
        }
    }

    // ── Helpers ──────────────────────────────────────────────────────────

    #[inline]
    fn ensure_replication_mode(&self) -> Result<()> {
        if !self.copy.is_replication() {
            return Err(ReplicationError::protocol(
                "Connection is not in replication mode".to_string(),
            ));
        }
        Ok(())
    }
    /// Gracefully close the replication connection.
    ///
    /// Sends a `Close` command so the worker does a best-effort shutdown
    /// (CopyDone if streaming, then Terminate), then joins the worker thread.
    fn close_connection(&mut self) {
        let send_copy_done = self.copy.is_replication();
        // Drop the COPY OUT receiver before anything below can block, for the
        // same reason `*batch_rx = None` precedes the wait on the replication
        // path: while the worker is parked on `chunk_tx.send()` under
        // backpressure, a live receiver keeps that send pending forever, so the
        // worker never reaches the `Close` and the `reply_rx.recv()` below waits
        // on a reply that can never come. Clearing it here closes the channel and
        // lets the send resolve.
        //
        // This used to run *after* the match, which is exactly the deadlock.
        self.copy = CopyMode::Idle;
        match &mut self.driver {
            Driver::Inline {
                worker,
                pending,
                handle,
            } => {
                // Best-effort graceful shutdown (CopyDone + Terminate) on the
                // connection's original runtime. `close` only borrows the worker, so we can block to completion in the cases where that is safe:
                //
                //   - nested in a multi-thread runtime → `block_in_place` + `block_on`
                //   - no ambient runtime (plain-thread Drop) → `block_on` directly
                //
                // We must NOT block when dropped *inside* a current-thread runtime: `block_in_place` requires a multi-thread runtime and `block_on`
                // panics ("cannot start a runtime from within a runtime"). There we skip the courtesy close; the socket still closes via TCP FIN when `worker` drops, and PostgreSQL reaps the walsender on disconnect.
                match tokio::runtime::Handle::try_current() {
                    Ok(cur)
                        if cur.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread =>
                    {
                        tokio::task::block_in_place(|| {
                            handle.block_on(worker.close(send_copy_done))
                        });
                    }
                    Err(_) => handle.block_on(worker.close(send_copy_done)),
                    Ok(_) => { /* current-thread runtime: cannot block safely; skip */ }
                }
                pending.clear();
            }
            Driver::Threaded {
                cmd_tx,
                worker,
                pending,
                batch_rx,
            } => {
                // Drop the streaming receiver first: when the worker is parked on
                // `batch_tx.reserve()` under backpressure, closing the channel lets that
                // branch resolve so the worker reaches the `Close` command promptly.
                *batch_rx = None;
                if let Some(handle) = worker.take() {
                    let (reply_tx, reply_rx) = std_mpsc::channel();
                    if cmd_tx
                        .send(Command::Close {
                            send_copy_done,
                            reply: reply_tx,
                        })
                        .is_ok()
                    {
                        // Wait for the worker to finish its shutdown I/O before joining.
                        //
                        // The streaming loop's `select!` is biased to handle commands
                        // first, so this `Close` interrupts an in-flight read or a parked
                        // `reserve()` immediately — no waiting for the next keepalive.
                        let _ = reply_rx.recv();
                    }
                    let _ = handle.join();
                }
                pending.clear();
            }
        }

        self.alive.store(false, Ordering::Relaxed);
    }
}

impl Drop for NativeConnection {
    fn drop(&mut self) {
        self.close_connection();
    }
}

#[cfg(test)]
impl NativeConnection {
    /// Create a null connection for testing (DO NOT call any methods that touch the DB)
    pub(crate) fn null_for_testing() -> Self {
        // Create a pair of connected TCP sockets. The peer end is closed when
        // this function returns, so any I/O the worker attempts on the socket
        // fails deterministically rather than blocking.
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let std_tcp = std::net::TcpStream::connect(addr).unwrap();
        std_tcp.set_nonblocking(true).unwrap();
        let _peer = listener.accept().unwrap();

        let (cmd_tx, cmd_rx) = mpsc::unbounded_channel();
        let (ready_tx, ready_rx) = std_mpsc::channel();
        let alive = Arc::new(AtomicBool::new(false));
        let worker_alive = alive.clone();

        let worker = std::thread::Builder::new()
            .name("pg-walstream-native-null".to_string())
            .spawn(move || {
                run_worker(
                    WorkerInit::Null {
                        std_tcp,
                        server_ver: 160000,
                        alive: worker_alive,
                    },
                    cmd_rx,
                    ready_tx,
                )
            })
            .unwrap();

        // The worker adopts the socket on its own reactor and reports back.
        let server_ver = ready_rx
            .recv()
            .expect("null worker exited before init")
            .expect("null worker failed to adopt the test socket");

        // A null test connection is intentionally not alive.
        Self {
            driver: Driver::Threaded {
                cmd_tx,
                worker: Some(worker),
                pending: VecDeque::new(),
                batch_rx: None,
            },
            server_ver,
            copy: CopyMode::Idle,
            alive,
        }
    }

    /// Create a null **inline-driver** connection for testing. Must be called on
    /// a multi-thread runtime so the connect-time `Handle` can be captured and
    /// `run_sync` is safe.
    fn null_for_testing_inline() -> Self {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let std_tcp = std::net::TcpStream::connect(addr).unwrap();
        std_tcp.set_nonblocking(true).unwrap();
        let _peer = listener.accept().unwrap();

        let handle = tokio::runtime::Handle::current();
        let alive = Arc::new(AtomicBool::new(false));
        let worker = run_sync(
            &handle,
            WorkerInit::Null {
                std_tcp,
                server_ver: 160000,
                alive: alive.clone(),
            }
            .build(),
        )
        .expect("null worker failed to adopt the test socket");
        let server_ver = worker.server_ver;

        Self {
            driver: Driver::Inline {
                worker,
                pending: VecDeque::new(),
                handle,
            },
            server_ver,
            copy: CopyMode::Idle,
            alive,
        }
    }

    /// Test-only: a null (Threaded-driver) connection pre-seeded with COPY-data
    /// frames that `get_copy_data_async` serves in order from `pending` before
    /// any socket I/O. The mode is set to `Replication` so the gate passes.
    /// Once the seeded frames are exhausted the next read hits the dead test
    /// socket and errors — a test consumes exactly what it seeds.
    pub(crate) fn null_for_testing_with_frames(frames: Vec<Bytes>) -> Self {
        let mut conn = Self::null_for_testing();
        conn.copy = CopyMode::Replication;
        if let Driver::Threaded { pending, .. } = &mut conn.driver {
            pending.extend(frames);
        }
        conn
    }

    /// Test-only: whether this connection uses the inline driver.
    fn driver_is_inline(&self) -> bool {
        matches!(self.driver, Driver::Inline { .. })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{ReplicationSlotOptions, SlotType};

    fn sanitize_sql_string_value(value: &str) -> String {
        let quoted = crate::sql_builder::quote_literal(value).unwrap();
        // Strip surrounding quotes to get just the sanitized interior
        quoted[1..quoted.len() - 1].to_string()
    }

    fn quote_sql_string_value(value: &str) -> String {
        crate::sql_builder::quote_literal(value).unwrap()
    }

    fn quote_sql_identifier(identifier: &str) -> String {
        crate::sql_builder::quote_ident(identifier).unwrap()
    }

    // === sanitize_sql_string_value ===

    #[test]
    fn test_sanitize_sql_string_value_no_quotes() {
        assert_eq!(sanitize_sql_string_value("test_value"), "test_value");
    }

    #[test]
    fn test_sanitize_sql_string_value_single_quote() {
        assert_eq!(sanitize_sql_string_value("test'value"), "test''value");
    }

    #[test]
    fn test_sanitize_sql_string_value_multiple_quotes() {
        assert_eq!(
            sanitize_sql_string_value("test'value'with'quotes"),
            "test''value''with''quotes"
        );
    }

    #[test]
    fn test_sanitize_sql_string_value_sql_injection_attempt() {
        assert_eq!(
            sanitize_sql_string_value("'; DROP TABLE users; --"),
            "''; DROP TABLE users; --"
        );
    }

    #[test]
    fn test_sanitize_sql_string_value_empty() {
        assert_eq!(sanitize_sql_string_value(""), "");
    }

    #[test]
    fn test_sanitize_sql_string_value_only_quote() {
        assert_eq!(sanitize_sql_string_value("'"), "''");
    }

    #[test]
    fn test_sanitize_sql_string_value_consecutive_quotes() {
        assert_eq!(sanitize_sql_string_value("''"), "''''");
    }

    // === quote_sql_string_value ===

    #[test]
    fn test_quote_sql_string_value_basic() {
        assert_eq!(quote_sql_string_value("test_value"), "'test_value'");
    }

    #[test]
    fn test_quote_sql_string_value_with_quotes() {
        assert_eq!(quote_sql_string_value("test'value"), "'test''value'");
    }

    #[test]
    fn test_quote_sql_string_value_sql_injection() {
        assert_eq!(
            quote_sql_string_value("'; DROP TABLE users; --"),
            "'''; DROP TABLE users; --'"
        );
    }

    #[test]
    fn test_quote_sql_string_value_empty() {
        assert_eq!(quote_sql_string_value(""), "''");
    }

    // === quote_sql_identifier ===

    #[test]
    fn test_quote_sql_identifier_simple() {
        assert_eq!(quote_sql_identifier("my_slot"), r#""my_slot""#);
    }

    #[test]
    fn test_quote_sql_identifier_with_double_quote() {
        assert_eq!(quote_sql_identifier(r#"a"b"#), r#""a""b""#);
    }

    #[test]
    fn test_quote_sql_identifier_multiple_quotes() {
        assert_eq!(quote_sql_identifier(r#"a""b"#), r#""a""""b""#);
    }

    #[test]
    fn test_quote_sql_identifier_empty() {
        assert_eq!(quote_sql_identifier(""), r#""""#);
    }

    #[test]
    fn test_quote_sql_identifier_special_chars() {
        assert_eq!(
            quote_sql_identifier("slot; DROP TABLE users; --"),
            r#""slot; DROP TABLE users; --""#
        );
    }

    // === Additional sanitization edge cases ===

    #[test]
    fn test_sanitize_complex_injection_attempt() {
        let input = "value' OR '1'='1";
        assert_eq!(sanitize_sql_string_value(input), "value'' OR ''1''=''1");
        assert_eq!(quote_sql_string_value(input), "'value'' OR ''1''=''1'");
    }

    #[test]
    fn test_sanitize_unicode_with_quotes() {
        assert_eq!(sanitize_sql_string_value("test'值'测试"), "test''值''测试");
    }

    #[test]
    fn test_sanitize_special_chars_without_quotes() {
        assert_eq!(
            sanitize_sql_string_value("test;value--comment/**/"),
            "test;value--comment/**/"
        );
    }

    #[test]
    fn test_quote_backslash_and_quote() {
        // A backslash switches quote_literal to the escape-string form ` E'…'`
        // (safe under standard_conforming_strings off or on); both the backslash
        // and the single quote are doubled.
        assert_eq!(
            quote_sql_string_value("test\\'value"),
            r#" E'test\\''value'"#
        );
    }

    #[test]
    fn test_sanitize_newlines_and_quotes() {
        assert_eq!(
            sanitize_sql_string_value("line1'quote\nline2'quote"),
            "line1''quote\nline2''quote"
        );
    }

    // === build_sql_options ===

    #[test]
    fn test_build_sql_options_empty() {
        let options: Vec<String> = vec![];
        assert_eq!(crate::sql_builder::build_sql_options(&options), "");
    }

    #[test]
    fn test_build_sql_options_single() {
        let options = vec!["proto_version '2'".to_string()];
        assert_eq!(
            crate::sql_builder::build_sql_options(&options),
            " (proto_version '2')"
        );
    }

    #[test]
    fn test_build_sql_options_multiple() {
        let options = vec![
            "proto_version '2'".to_string(),
            "publication_names '\"my_pub\"'".to_string(),
            "streaming 'on'".to_string(),
        ];
        assert_eq!(
            crate::sql_builder::build_sql_options(&options),
            " (proto_version '2', publication_names '\"my_pub\"', streaming 'on')"
        );
    }

    // === ensure_replication_mode, is_alive, server_version, close_connection, Drop ===

    #[tokio::test]
    async fn test_ensure_replication_mode_fails_when_not_replication() {
        let conn = NativeConnection::null_for_testing();
        let err = conn.ensure_replication_mode().unwrap_err();
        assert!(
            err.to_string().contains("not in replication mode"),
            "Expected replication mode error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_is_alive_returns_false_for_null_conn() {
        let conn = NativeConnection::null_for_testing();
        assert!(!conn.is_alive());
    }

    #[tokio::test]
    async fn test_server_version_returns_configured_value() {
        let conn = NativeConnection::null_for_testing();
        assert_eq!(conn.server_version(), 160000);
    }

    #[tokio::test]
    async fn test_create_slot_preflight_rejects_failover_below_pg17() {
        let mut conn = NativeConnection::null_for_testing();
        let opts = ReplicationSlotOptions {
            failover: true,
            ..Default::default()
        };
        let err = conn
            .create_replication_slot_with_options("s", SlotType::Logical, Some("pgoutput"), &opts)
            .unwrap_err();
        assert!(err.to_string().contains("FAILOVER"), "{err}");
        assert!(err.to_string().contains("17+"), "{err}");
    }

    #[tokio::test]
    async fn test_alter_slot_preflight_rejects_below_pg17() {
        let mut conn = NativeConnection::null_for_testing();
        let err = conn
            .alter_replication_slot("s", None, Some(true))
            .unwrap_err();
        assert!(err.to_string().contains("ALTER_REPLICATION_SLOT"), "{err}");
        assert!(err.to_string().contains("17+"), "{err}");
    }

    #[tokio::test]
    async fn test_read_slot_preflight_passes_on_pg16_then_hits_socket() {
        // READ_REPLICATION_SLOT is PG15+, so the PG16 preflight passes; the call
        // then fails at the null socket. Exercises the preflight line's Ok path.
        let mut conn = NativeConnection::null_for_testing();
        assert!(conn.read_replication_slot("s").is_err());
    }

    /// `null_for_testing` reports PG16, so the PG17 gate rejects UPLOAD_MANIFEST
    /// before any socket I/O — the preflight's Err path.
    #[tokio::test]
    async fn test_upload_manifest_preflight_rejects_below_pg17() {
        let mut conn = NativeConnection::null_for_testing();
        let err = conn.upload_manifest(b"{}").unwrap_err();
        assert!(err.to_string().contains("UPLOAD_MANIFEST"), "{err}");
        assert!(err.to_string().contains("17+"), "{err}");
    }

    #[tokio::test]
    async fn test_close_connection_null_conn() {
        let mut conn = NativeConnection::null_for_testing();
        conn.close_connection(); // should not panic
        assert!(!conn.is_alive());
    }

    #[tokio::test]
    async fn test_drop_null_conn_does_not_panic() {
        let conn = NativeConnection::null_for_testing();
        drop(conn); // should not panic
    }

    // Runtime-flavor coverage for the worker bridge: the sync methods used to
    // panic in block_in_place on a current-thread runtime. These pin that they
    // no longer do, across current-thread, multi-thread, no-runtime, and Drop.

    #[tokio::test]
    async fn test_sync_method_does_not_panic_on_current_thread_runtime() {
        // Default #[tokio::test] is current-thread; a sync call must error, not panic.
        let mut conn = NativeConnection::null_for_testing();
        let result = conn.exec("IDENTIFY_SYSTEM");
        assert!(
            result.is_err(),
            "exec on a null connection should error, not panic"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_sync_method_works_on_multi_thread_runtime() {
        let mut conn = NativeConnection::null_for_testing();
        assert!(conn.exec("IDENTIFY_SYSTEM").is_err());
    }

    #[test]
    fn test_sync_method_works_without_runtime() {
        let mut conn = NativeConnection::null_for_testing();
        assert!(conn.exec("IDENTIFY_SYSTEM").is_err());
    }

    #[tokio::test]
    async fn test_drop_does_not_panic_on_current_thread_runtime() {
        let conn = NativeConnection::null_for_testing();
        drop(conn); // must not panic on a current-thread runtime
    }

    // Inline-driver coverage. The default `#[tokio::test]` is current-thread, so
    // the tests above exercise the Threaded driver; these pin the Inline driver
    // (multi-thread / no-runtime), where sync methods go through `run_sync`.

    #[test]
    fn test_prefer_inline_driver_selection() {
        // No ambient runtime → threaded (a per-call temp runtime would orphan the socket).
        assert!(!NativeConnection::prefer_inline_driver());

        // Current-thread runtime → threaded.
        let ct = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        assert!(!ct.block_on(async { NativeConnection::prefer_inline_driver() }));

        // Multi-thread runtime → inline.
        let mt = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .unwrap();
        assert!(mt.block_on(async { NativeConnection::prefer_inline_driver() }));
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_inline_sync_method_does_not_panic_on_multi_thread_runtime() {
        let mut conn = NativeConnection::null_for_testing_inline();
        assert!(conn.driver_is_inline());
        // Sync exec drives async I/O via run_sync→block_in_place; dead socket → error, not panic.
        assert!(conn.exec("IDENTIFY_SYSTEM").is_err());
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_inline_drop_does_not_panic() {
        let conn = NativeConnection::null_for_testing_inline();
        drop(conn); // Drop → run_sync(worker.close) on a multi-thread runtime.
    }

    #[test]
    fn test_inline_drop_outside_ambient_runtime_does_not_panic() {
        // Build the inline connection inside a multi-thread runtime (so its socket
        // is created on that runtime's reactor and the connect-time `Handle` is
        // captured), then move it out and drop it with NO ambient runtime. The
        // stored handle must drive the shutdown on the original reactor — without
        // it, `run_sync` would build a throwaway runtime and orphan the socket, or
        // (per the reviewer's literal suggestion) call `block_in_place` off-runtime
        // and panic here.
        let rt = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .unwrap();
        let conn = rt.block_on(async { NativeConnection::null_for_testing_inline() });
        assert!(conn.driver_is_inline());
        drop(conn); // no ambient runtime here → must not panic
        drop(rt);
    }

    #[test]
    fn test_inline_drop_within_current_thread_runtime_does_not_panic() {
        // Build the inline connection on a multi-thread runtime (handle = mt), then
        // drop it from *inside* a current-thread runtime. `close_connection` must
        // not block_on the stored handle there — that panics with "cannot start a
        // runtime from within a runtime" (issue #76's failure mode). Best-effort
        // graceful close is skipped; the socket still closes via TCP FIN.
        let mt = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .enable_all()
            .build()
            .unwrap();
        let conn = mt.block_on(async { NativeConnection::null_for_testing_inline() });
        assert!(conn.driver_is_inline());

        let ct = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        ct.block_on(async move {
            drop(conn); // must not panic on a current-thread runtime
        });
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn test_inline_get_copy_data_drains_pending() {
        use tokio::io::AsyncWriteExt;
        let (worker, mut server) = worker_with_loopback().await;
        let mut conn = NativeConnection {
            driver: Driver::Inline {
                worker,
                pending: VecDeque::new(),
                handle: tokio::runtime::Handle::current(),
            },
            server_ver: 160000,
            copy: CopyMode::Replication, // skip the replication-mode gate
            alive: Arc::new(AtomicBool::new(true)),
        };

        // Server streams two WAL messages; the inline read path drains both.
        server.write_all(&copy_data_frame(b"one")).await.unwrap();
        server.write_all(&copy_data_frame(b"two")).await.unwrap();
        server.flush().await.unwrap();

        let token = CancellationToken::new();
        let first = conn.get_copy_data_async(&token).await.unwrap();
        let second = conn.get_copy_data_async(&token).await.unwrap();
        assert_eq!(&first[..], b"one");
        assert_eq!(&second[..], b"two");
    }

    // === Worker streaming push loop ===

    fn copy_data_frame(payload: &[u8]) -> Vec<u8> {
        let mut frame = Vec::with_capacity(5 + payload.len());
        frame.push(b'd');
        frame.extend_from_slice(&((4 + payload.len()) as i32).to_be_bytes());
        frame.extend_from_slice(payload);
        frame
    }

    async fn worker_with_loopback() -> (Worker, tokio::net::TcpStream) {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let client = tokio::net::TcpStream::connect(addr).await.unwrap();
        let (server, _) = listener.accept().await.unwrap();
        let worker = Worker {
            transport: Transport::Plain(client),
            read_buf: BytesMut::new(),
            server_ver: 160000,
            alive: Arc::new(AtomicBool::new(true)),
        };
        (worker, server)
    }

    // ── COPY OUT ─────────────────────────────────────────────────────────

    fn tagged_frame(tag: u8, payload: &[u8]) -> Vec<u8> {
        let mut frame = Vec::with_capacity(5 + payload.len());
        frame.push(tag);
        frame.extend_from_slice(&((4 + payload.len()) as i32).to_be_bytes());
        frame.extend_from_slice(payload);
        frame
    }

    /// A complete, successful COPY OUT wire script carrying `rows`.
    fn copy_out_script(rows: &[&[u8]]) -> Vec<u8> {
        let mut out = Vec::new();
        for row in rows {
            out.extend_from_slice(&copy_data_frame(row));
        }
        out.extend_from_slice(&tagged_frame(b'c', b""));
        out.extend_from_slice(&tagged_frame(b'C', b"COPY 1\0"));
        out.extend_from_slice(&tagged_frame(b'Z', b"I"));
        out
    }

    /// An inline-driver connection over a loopback socket, already marked as
    /// having a COPY OUT in flight (i.e. as if `copy_out_begin` had succeeded).
    async fn inline_conn_in_copy_out() -> (NativeConnection, tokio::net::TcpStream) {
        let (worker, server) = worker_with_loopback().await;
        let conn = NativeConnection {
            driver: Driver::Inline {
                worker,
                pending: VecDeque::new(),
                handle: tokio::runtime::Handle::current(),
            },
            server_ver: 160000,
            copy: CopyMode::Out { rx: None },
            alive: Arc::new(AtomicBool::new(true)),
        };
        (conn, server)
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn copy_out_next_yields_rows_then_none() {
        use tokio::io::AsyncWriteExt;
        let (mut conn, mut server) = inline_conn_in_copy_out().await;
        server
            .write_all(&copy_out_script(&[b"1\tAlice\n", b"2\tBob\n"]))
            .await
            .unwrap();

        let token = CancellationToken::new();
        let first = conn.copy_out_next(&token).await.unwrap().unwrap();
        assert_eq!(&first[..], b"1\tAlice\n");
        let second = conn.copy_out_next(&token).await.unwrap().unwrap();
        assert_eq!(&second[..], b"2\tBob\n");
        assert!(conn.copy_out_next(&token).await.unwrap().is_none());

        // A clean end releases the stream but leaves the connection usable.
        assert!(conn.is_alive());
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn copy_out_next_after_clean_end_is_an_error() {
        use tokio::io::AsyncWriteExt;
        let (mut conn, mut server) = inline_conn_in_copy_out().await;
        server.write_all(&copy_out_script(&[])).await.unwrap();

        let token = CancellationToken::new();
        assert!(conn.copy_out_next(&token).await.unwrap().is_none());

        let err = conn.copy_out_next(&token).await.unwrap_err();
        assert!(format!("{err}").contains("no COPY OUT stream"), "{err}");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn copy_out_next_without_begin_errors() {
        let mut conn = NativeConnection::null_for_testing_inline();
        let err = conn
            .copy_out_next(&CancellationToken::new())
            .await
            .unwrap_err();
        assert!(format!("{err}").contains("no COPY OUT stream"), "{err}");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn copy_out_error_marks_connection_dead() {
        use tokio::io::AsyncWriteExt;
        let (mut conn, mut server) = inline_conn_in_copy_out().await;

        let mut script = tagged_frame(b'E', b"C42501\0Mpermission denied\0\0");
        script.extend_from_slice(&tagged_frame(b'Z', b"I"));
        server.write_all(&script).await.unwrap();

        let err = conn
            .copy_out_next(&CancellationToken::new())
            .await
            .unwrap_err();
        assert!(format!("{err}").contains("42501"), "{err}");
        assert!(
            !conn.is_alive(),
            "a failed COPY must mark the connection dead"
        );
    }

    /// Cancelling is still a *cancellation* — the error variant stays
    /// `Cancelled`, so the stream layer treats it as a clean stop — but the
    /// connection does not survive it.
    ///
    /// COPY OUT has no protocol-level early exit, so a cancelled read leaves the
    /// rest of the relation queued on the transport. This used to reset the mode
    /// to `Idle` and leave `alive` true, which had two consequences: the
    /// connection advertised itself as reusable when reusing it would read the
    /// *previous* COPY's completion, and `copy_out_abort` — the one thing that
    /// would have hung up — became a no-op because the mode was already `Idle`.
    /// libpq's backend self-heals that leftover inside `PQexec`, so the two
    /// backends also disagreed.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn copy_out_cancellation_returns_cancelled_and_hangs_up() {
        let (mut conn, _server) = inline_conn_in_copy_out().await;
        let token = CancellationToken::new();
        token.cancel();

        let err = conn.copy_out_next(&token).await.unwrap_err();
        assert!(
            err.is_cancelled(),
            "the caller asked to stop; this is not a transport failure: {err:?}"
        );
        assert!(
            !conn.is_alive(),
            "a half-read COPY OUT cannot be reused, so the connection must hang up"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn copy_out_begin_rejects_a_second_stream() {
        let (mut conn, _server) = inline_conn_in_copy_out().await;
        let err = conn.copy_out_begin("COPY t TO STDOUT").unwrap_err();
        assert!(format!("{err}").contains("already in progress"), "{err}");
    }

    /// A replication connection is pinned in CopyBoth, so issuing the `COPY`
    /// *query* would already be a protocol violation. The old two-flag shape
    /// missed this: the guard only inspected the COPY OUT field, so a
    /// replication connection sailed past it into `exec`.
    ///
    /// The timeout is as much the point as the assertion: `copy_out_begin` is
    /// blocking, so a regressed guard does not return a wrong answer — it
    /// *hangs*, writing a Query the walsender will never read.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn copy_out_begin_rejects_a_replication_connection() {
        let (mut conn, _server) = inline_conn_in_copy_out().await;
        conn.copy = CopyMode::Replication;

        let (conn, result) = tokio::time::timeout(
            std::time::Duration::from_secs(5),
            tokio::task::spawn_blocking(move || {
                let result = conn.copy_out_begin("COPY t TO STDOUT");
                (conn, result)
            }),
        )
        .await
        .expect("the guard must reject before any I/O; a hang means it fell through to exec")
        .unwrap();

        let err = result.unwrap_err();
        assert!(
            format!("{err}").contains("streaming replication"),
            "expected the replication-mode rejection, got: {err}"
        );
        assert!(
            conn.copy.is_replication(),
            "a rejected COPY OUT must leave replication mode intact"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn copy_out_abort_marks_dead_and_clears_the_stream() {
        let (mut conn, _server) = inline_conn_in_copy_out().await;
        assert!(conn.is_alive());

        conn.copy_out_abort();
        assert!(
            !conn.is_alive(),
            "abort must hang up: COPY OUT has no early exit"
        );

        // Idempotent: a second abort on a connection with no stream is a no-op.
        conn.copy_out_abort();
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn worker_drain_copy_out_pushes_frames_then_terminal() {
        use tokio::io::AsyncWriteExt;
        let (mut worker, mut server) = worker_with_loopback().await;
        server
            .write_all(&copy_out_script(&[b"a\n", b"b\n"]))
            .await
            .unwrap();

        let (chunk_tx, mut rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        // The sender must outlive the call: a closed command channel reads as
        // "worker gone" and would exit the drain before it reads a frame.
        let (_cmd_tx, mut cmd_rx) = mpsc::unbounded_channel();
        worker.drain_copy_out(chunk_tx, &mut cmd_rx).await;

        assert_eq!(&rx.recv().await.unwrap().unwrap().unwrap()[..], b"a\n");
        assert_eq!(&rx.recv().await.unwrap().unwrap().unwrap()[..], b"b\n");
        assert!(
            rx.recv().await.unwrap().unwrap().is_none(),
            "clean end must be an explicit Ok(None) terminal"
        );
        assert!(
            rx.recv().await.is_none(),
            "channel closes after the terminal"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn worker_drain_copy_out_forwards_error_terminal() {
        use tokio::io::AsyncWriteExt;
        let (mut worker, mut server) = worker_with_loopback().await;

        let mut script = tagged_frame(b'E', b"C57P01\0Mshutting down\0\0");
        script.extend_from_slice(&tagged_frame(b'Z', b"I"));
        server.write_all(&script).await.unwrap();

        let (chunk_tx, mut rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        let (_cmd_tx, mut cmd_rx) = mpsc::unbounded_channel();
        worker.drain_copy_out(chunk_tx, &mut cmd_rx).await;

        let err = rx.recv().await.unwrap().unwrap_err();
        assert!(err.is_transient(), "{err:?}");
        assert!(!worker.alive.load(Ordering::Relaxed));
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn worker_drain_copy_out_stops_when_receiver_dropped() {
        use tokio::io::AsyncWriteExt;
        let (mut worker, mut server) = worker_with_loopback().await;
        server
            .write_all(&copy_out_script(&[b"row\n"]))
            .await
            .unwrap();

        let (chunk_tx, rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        drop(rx);
        let (_cmd_tx, mut cmd_rx) = mpsc::unbounded_channel();

        // Must return rather than spin forever against a dead receiver.
        tokio::time::timeout(
            std::time::Duration::from_secs(5),
            worker.drain_copy_out(chunk_tx, &mut cmd_rx),
        )
        .await
        .expect("drain_copy_out must exit when the consumer goes away");
    }

    /// The deadlock this closes: a `Close` arriving while the chunk channel is
    /// full must be serviced, not queued behind a consumer that stopped reading.
    ///
    /// Before `drain_copy_out` took `cmd_rx`, the worker parked in
    /// `chunk_tx.send()` forever while `close_connection` parked in
    /// `reply_rx.recv()` waiting for a reply that could never come — a hard hang
    /// with no timeout on either side, reachable from safe code by dropping a
    /// `SnapshotRows` mid-iteration.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn worker_drain_copy_out_handles_close_while_the_channel_is_full() {
        use tokio::io::AsyncWriteExt;
        let (worker, mut server) = worker_with_loopback().await;

        // More frames than the channel holds, and `_rx` is never read, so the
        // drain is guaranteed to end up parked on `reserve()`.
        let rows: Vec<&[u8]> = vec![b"row\n"; BATCH_CHANNEL_CAP + 8];
        server.write_all(&copy_out_script(&rows)).await.unwrap();

        let (chunk_tx, _rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        let probe = chunk_tx.clone();
        let (cmd_tx, mut cmd_rx) = mpsc::unbounded_channel();

        let mut worker = worker;
        let drain = tokio::spawn(async move { worker.drain_copy_out(chunk_tx, &mut cmd_rx).await });

        // Wait for the park rather than sleeping a fixed amount: a sleep that is
        // too short would silently downgrade this into "Close is handled from an
        // idle drain", which the pre-fix code also passed.
        let mut parked = false;
        for _ in 0..500 {
            if probe.capacity() == 0 {
                parked = true;
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }
        assert!(
            parked,
            "premise: the drain must be parked on a full channel before Close is sent"
        );

        let (reply_tx, reply_rx) = std_mpsc::channel();
        cmd_tx
            .send(Command::Close {
                send_copy_done: false,
                reply: reply_tx,
            })
            .expect("worker is still running");

        let handled = tokio::time::timeout(std::time::Duration::from_secs(5), drain)
            .await
            .expect("a Close must interrupt the drain, not wait for the consumer")
            .unwrap();

        assert!(handled, "a handled Close must stop the worker loop");
        assert!(
            reply_rx.recv().is_ok(),
            "close_connection blocks on this reply; without it, Drop hangs forever"
        );
    }

    /// A command arriving mid-drain has already dropped the in-flight
    /// `next_copy_out` future. If that drop landed inside `drain_epilogue` the
    /// `CopyDone` is gone, so restarting the read cannot recover the stream —
    /// the consumer must be told, not left waiting on a socket with nothing
    /// left to send.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn worker_drain_copy_out_aborts_when_another_command_arrives() {
        use tokio::io::AsyncWriteExt;
        let (worker, mut server) = worker_with_loopback().await;

        // One data frame and *no* terminator, so the drain delivers the row and
        // then parks on the next read — which is where an interleaved command
        // races it, and the only place the dropped-future hazard exists.
        server.write_all(&copy_data_frame(b"row\n")).await.unwrap();

        let (chunk_tx, mut rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        let (cmd_tx, mut cmd_rx) = mpsc::unbounded_channel();

        let mut worker = worker;
        let drain = tokio::spawn(async move { worker.drain_copy_out(chunk_tx, &mut cmd_rx).await });

        // Receiving the row proves the drain got past it and is now parked on the
        // read, rather than the command racing a loop that never started.
        let first = rx.recv().await.expect("the row must arrive");
        assert_eq!(first.unwrap(), Some(Bytes::from_static(b"row\n")));

        let (reply_tx, _reply_rx) = std_mpsc::channel();
        cmd_tx
            .send(Command::Query {
                sql: "SELECT 1".to_string(),
                reply: reply_tx,
            })
            .expect("worker is still running");

        let terminal = tokio::time::timeout(std::time::Duration::from_secs(5), rx.recv())
            .await
            .expect("the consumer must be told, not left waiting on a dead stream")
            .expect("a terminal must be delivered, not a closed channel");

        let err = terminal.expect_err("an interleaved command must end the COPY with an error");
        assert!(
            format!("{err}").contains("another command was issued"),
            "{err}"
        );

        let handled = tokio::time::timeout(std::time::Duration::from_secs(5), drain)
            .await
            .expect("the drain must stop rather than read on blind")
            .unwrap();
        assert!(!handled, "a rejected command is not a Close");
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn threaded_copy_out_next_reports_dead_worker() {
        // `null_for_testing` has a live worker thread but a dropped peer, so the
        // lazily-started drain loop terminates and closes the channel.
        let mut conn = NativeConnection::null_for_testing();
        conn.copy = CopyMode::Out { rx: None };

        let result = conn.copy_out_next(&CancellationToken::new()).await;
        assert!(result.is_err(), "a dead peer must surface as an error");
    }

    /// F4: a full batch channel must not swallow the terminal error.
    ///
    /// `try_send` dropped it whenever the consumer was `BATCH_CHANNEL_CAP`
    /// batches behind. The consumer then drained the backlog, saw the channel
    /// closed, and got `worker_gone()` → `Backend` → classified **transient**.
    /// A permanent SQLSTATE therefore became retryable and the stream
    /// reconnect-looped forever against a server that would never accept it.
    ///
    /// The channel has to be *full at the moment the error is produced*, which
    /// needs choreography: frames are written individually so each becomes its
    /// own batch, the consumer stays asleep until the channel is full and the
    /// worker is parked on `reserve()`, and only then is one slot freed — the
    /// worker spends it on the batch it was holding, so it reads the
    /// ErrorResponse with the channel full again. Verified to FAIL against the
    /// pre-fix `try_send` in a scratch worktree.
    #[tokio::test]
    async fn stream_copy_delivers_error_even_when_channel_is_full() {
        use tokio::io::AsyncWriteExt;
        let (worker, mut server) = worker_with_loopback().await;
        let (_cmd_tx, mut cmd_rx) = mpsc::unbounded_channel::<Command>();
        let (batch_tx, mut batch_rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        let token = CancellationToken::new();

        let handle = tokio::spawn(async move {
            let mut worker = worker;
            worker.stream_copy(token, batch_tx, &mut cmd_rx).await
        });

        // One frame per write, spaced out, so each lands as its own batch and the
        // channel genuinely fills. Exactly `CAP + 1`: the first `CAP` fill the
        // channel, the extra one is what the worker is *holding* when it parks on
        // `reserve()`. One more than that and the worker would read the spare
        // frame after the slot frees instead of the ErrorResponse, which is
        // precisely the window this test has to hit. Consumer reads nothing yet.
        for i in 0..(BATCH_CHANNEL_CAP + 1) {
            server
                .write_all(&copy_data_frame(format!("f{i}").as_bytes()))
                .await
                .unwrap();
            server.flush().await.unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(5)).await;
        }
        // Channel is full; the worker is parked on `reserve()` holding a batch.
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        // Terminal with a *permanent* SQLSTATE (28000). The worker cannot read it
        // yet — it is still blocked on the permit.
        let payload = b"C28000\0Mslot gone\0\0";
        let mut err_frame = vec![b'E'];
        err_frame.extend_from_slice(&((4 + payload.len()) as i32).to_be_bytes());
        err_frame.extend_from_slice(payload);
        server.write_all(&err_frame).await.unwrap();
        server.flush().await.unwrap();

        // Free exactly one slot. The worker spends it on the held batch, then
        // reads the ErrorResponse with the channel full again — the precise
        // moment the old `try_send` threw the error away.
        let _ = batch_rx.recv().await.expect("first batch");
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        // Now drain. The error must still arrive, with its SQLSTATE intact.
        let mut saw_error = None;
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        while std::time::Instant::now() < deadline {
            match batch_rx.recv().await {
                Some(Ok(_)) => continue,
                Some(Err(e)) => {
                    saw_error = Some(e);
                    break;
                }
                None => break,
            }
        }

        let err = saw_error.expect("the terminal error must survive a full channel");
        assert!(err.to_string().contains("28000"), "{err}");
        assert!(
            err.is_permanent(),
            "a permanent SQLSTATE must not be laundered into a transient error: {err:?}"
        );
        let _ = handle.await;
    }

    /// `mark_dead` is what stops a failed `start_replication` from leaving a
    /// live, non-COPY connection looking healthy — the state that made
    /// `next_event_with_retry` skip recovery and busy-spin.
    #[test]
    fn mark_dead_flips_is_alive() {
        let mut conn = NativeConnection::null_for_testing();
        conn.alive.store(true, Ordering::Relaxed);
        assert!(conn.is_alive());
        conn.mark_dead();
        assert!(!conn.is_alive());
        // Idempotent — recovery may call it on an already-dead connection.
        conn.mark_dead();
        assert!(!conn.is_alive());
    }

    /// While parked delivering a terminal error on a full channel, the worker
    /// must still service commands. A `Close` there ends the worker (`true`), the
    /// contract `run_worker` reads; returning `false` left it serving commands on
    /// a transport it had just Terminated.
    #[tokio::test]
    async fn stream_copy_services_close_while_blocked_on_error_delivery() {
        use tokio::io::AsyncWriteExt;
        let (worker, mut server) = worker_with_loopback().await;
        let (cmd_tx, mut cmd_rx) = mpsc::unbounded_channel::<Command>();
        let (batch_tx, mut batch_rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        let token = CancellationToken::new();

        let handle = tokio::spawn(async move {
            let mut worker = worker;
            worker.stream_copy(token, batch_tx, &mut cmd_rx).await
        });

        // Same choreography as `stream_copy_delivers_error_even_when_channel_is_full`:
        // `CAP + 1` frames so the worker ends up holding one with the channel
        // full, then the terminal.
        for i in 0..(BATCH_CHANNEL_CAP + 1) {
            server
                .write_all(&copy_data_frame(format!("f{i}").as_bytes()))
                .await
                .unwrap();
            server.flush().await.unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(5)).await;
        }
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        let payload = b"C28000\0Mgone\0\0";
        let mut err_frame = vec![b'E'];
        err_frame.extend_from_slice(&((4 + payload.len()) as i32).to_be_bytes());
        err_frame.extend_from_slice(payload);
        server.write_all(&err_frame).await.unwrap();
        server.flush().await.unwrap();

        // Free one slot: the worker spends it on the held batch, reads the
        // terminal, and parks in the *error-delivery* loop with the channel full
        // again. That is the select! this test exists to reach.
        let _ = batch_rx.recv().await.expect("first batch");
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;

        // Close must be honoured from inside that loop.
        let (reply_tx, reply_rx) = std_mpsc::channel();
        cmd_tx
            .send(Command::Close {
                send_copy_done: true,
                reply: reply_tx,
            })
            .unwrap();

        let stopped = tokio::time::timeout(std::time::Duration::from_secs(5), handle)
            .await
            .expect("Close must not deadlock behind a stalled consumer")
            .unwrap();
        assert!(stopped, "Close must report `true` so run_worker stops");
        let _ = reply_rx.recv();
    }

    #[tokio::test]
    async fn test_stream_copy_pushes_batches_then_close() {
        use tokio::io::AsyncWriteExt;
        let (worker, mut server) = worker_with_loopback().await;
        let (cmd_tx, mut cmd_rx) = mpsc::unbounded_channel::<Command>();
        let (batch_tx, mut batch_rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        let token = CancellationToken::new();

        let handle = tokio::spawn(async move {
            let mut worker = worker;
            worker.stream_copy(token, batch_tx, &mut cmd_rx).await
        });

        // Server streams two WAL messages; the worker pushes them down the channel.
        server.write_all(&copy_data_frame(b"one")).await.unwrap();
        server.write_all(&copy_data_frame(b"two")).await.unwrap();
        server.flush().await.unwrap();

        let mut got = Vec::new();
        while got.len() < 2 {
            let batch = batch_rx.recv().await.unwrap().unwrap();
            got.extend(batch);
        }
        assert_eq!(&got[0][..], b"one");
        assert_eq!(&got[1][..], b"two");

        // A Close command interrupts the loop and is reported as `true`.
        let (reply_tx, reply_rx) = std_mpsc::channel();
        cmd_tx
            .send(Command::Close {
                send_copy_done: true,
                reply: reply_tx,
            })
            .unwrap();
        assert!(handle.await.unwrap(), "Close should stop the worker");
        let _ = reply_rx.recv();
    }

    #[tokio::test]
    async fn test_stream_copy_cancel_pushes_error_and_stops() {
        let (mut worker, _server) = worker_with_loopback().await;
        let (_cmd_tx, mut cmd_rx) = mpsc::unbounded_channel::<Command>();
        let (batch_tx, mut batch_rx) = mpsc::channel(BATCH_CHANNEL_CAP);
        let token = CancellationToken::new();
        token.cancel();

        // A pre-cancelled token makes the first read return Cancelled, which the
        // loop forwards down the channel before stopping (not a Close → false).
        let stopped_via_close = worker.stream_copy(token, batch_tx, &mut cmd_rx).await;
        assert!(!stopped_via_close);
        match batch_rx.try_recv() {
            Ok(Err(ReplicationError::Cancelled(_))) => {}
            other => panic!("expected a Cancelled error, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn end_copy_noop_when_not_in_copy_mode() {
        // A null connection is not in COPY mode; end_copy must be a safe no-op.
        let mut conn = NativeConnection::null_for_testing();
        assert!(conn.end_copy().await.is_ok());
    }

    #[tokio::test]
    async fn end_copy_in_copy_mode_fires_close() {
        // Force COPY mode so end_copy takes the live Threaded branch (drop
        // batch_rx + fire Command::Close), not just the no-op guard. The null
        // worker's socket is closed, so the CopyDone/Terminate best-effort I/O
        // fails silently and the worker exits — end_copy still returns Ok.
        let mut conn = NativeConnection::null_for_testing();
        conn.copy = CopyMode::Replication;
        assert!(conn.end_copy().await.is_ok());
        // Flag cleared → a second call takes the no-op guard path.
        assert!(conn.end_copy().await.is_ok());
    }
}