pg-wired 0.4.0

Minimal async PostgreSQL wire protocol v3 client: connection, extended query, pipelining, COPY, LISTEN/NOTIFY, TLS, SCRAM.
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
//! Async split sender/receiver connection.
//! Inspired by hsqlx's PgWire.Async architecture.
//!
//! A single TCP connection is shared by many concurrent handler tasks.
//! The writer task coalesces messages from multiple requests into one write().
//! The reader task parses responses and dispatches them to waiting handlers via FIFO.

use std::collections::VecDeque;
use std::sync::Arc;

use bytes::BytesMut;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::{mpsc, oneshot, Mutex};

use crate::connection::WireConn;
use crate::error::PgWireError;
use crate::protocol::backend;
use crate::protocol::frontend;
use crate::protocol::types::{BackendMsg, FormatCode, FrontendMsg, RawRow};

// ---------------------------------------------------------------------------
// Request types
// ---------------------------------------------------------------------------

/// A request to execute on the connection. Internal plumbing between the
/// public `submit` / `submit_batch` API and the writer task.
pub(crate) struct PipelineRequest {
    pub(crate) messages: BytesMut,
    pub(crate) collector: ResponseCollector,
    pub(crate) response_tx: oneshot::Sender<Result<PipelineResponse, PgWireError>>,
}

/// How to collect response messages for a request.
#[allow(dead_code)]
#[non_exhaustive]
pub enum ResponseCollector {
    /// Collect DataRows until ReadyForQuery (for SELECT queries).
    Rows,
    /// Just drain until ReadyForQuery (for setup commands like BEGIN, SET ROLE).
    Drain,
    /// Stream rows one at a time via channels. Sends header first, then individual rows.
    Stream {
        /// One-shot channel for the row description (sent once before any rows).
        header_tx: oneshot::Sender<Result<StreamHeader, PgWireError>>,
        /// Bounded channel for individual rows; closed on completion or error.
        row_tx: mpsc::Sender<Result<StreamedRow, PgWireError>>,
    },
    /// COPY IN: after receiving CopyInResponse, send the provided data then CopyDone.
    CopyIn {
        /// The data to send after CopyInResponse.
        data: Vec<u8>,
    },
    /// COPY OUT: collect CopyData messages until CopyDone.
    CopyOut,
}

/// Response from a pipeline request.
#[non_exhaustive]
pub enum PipelineResponse {
    /// A query that produced a row set (`SELECT`, `RETURNING`, etc.).
    Rows {
        /// Column metadata from RowDescription (empty if no RowDescription received).
        fields: Vec<crate::protocol::types::FieldDescription>,
        /// Row data.
        rows: Vec<RawRow>,
        /// CommandComplete tag (e.g. "SELECT 3", "INSERT 0 1").
        command_tag: String,
    },
    /// A statement that produced no row set (e.g., `BEGIN`, `SET ROLE`,
    /// non-RETURNING DML).
    Done,
}

/// Metadata sent at the start of a streaming response.
#[derive(Debug, Clone)]
pub struct StreamHeader {
    /// Column descriptions (name, OID, format) for the streamed result set.
    pub fields: Vec<crate::protocol::types::FieldDescription>,
}

/// A single streamed row.
pub type StreamedRow = RawRow;

// ---------------------------------------------------------------------------
// Async connection
// ---------------------------------------------------------------------------

/// A shared async connection that multiplexes requests from many tasks.
pub struct AsyncConn {
    request_tx: mpsc::Sender<PipelineRequest>,
    stmt_cache: std::sync::Mutex<std::collections::HashMap<String, (String, u64)>>,
    stmt_counter: std::sync::atomic::AtomicU64,
    alive: Arc<std::sync::atomic::AtomicBool>,
    backend_pid: i32,
    backend_secret: i32,
    addr: String,
    /// Channel for async notifications received during query execution.
    /// Notifications are NOT silently dropped, they're forwarded here.
    #[allow(dead_code)]
    notification_tx: mpsc::Sender<crate::protocol::types::BackendMsg>,
    notification_rx: std::sync::Mutex<Option<mpsc::Receiver<crate::protocol::types::BackendMsg>>>,
    /// True if any operation since the last `take_state_mutated()` may have
    /// left the session in a non-default state (open transaction, SET
    /// without LOCAL, advisory lock, temp table, prepared cursor, etc.).
    ///
    /// Set explicitly by callers issuing such operations
    /// (`mark_state_mutated`), and automatically by the reader task whenever
    /// ReadyForQuery reports a non-idle transaction status. Callers that
    /// only run self-contained Bind/Execute/Sync queries leave this `false`,
    /// allowing pools to skip an expensive DISCARD ALL on return.
    state_mutated: Arc<std::sync::atomic::AtomicBool>,
    /// True if a caller has declared the connection unusable (e.g., a
    /// transaction was dropped without commit/rollback, leaving the session
    /// in an unknown state). The reader/writer tasks may still be running, so
    /// `is_alive()` is true, but pools should treat the connection as broken
    /// and destroy it on return rather than reusing it.
    broken: Arc<std::sync::atomic::AtomicBool>,
    /// Cumulative count of asynchronous notifications dropped because the
    /// notification channel was full or no application code was draining it.
    /// Surfaced via [`AsyncConn::dropped_notifications`] so callers can detect
    /// missed `LISTEN` events.
    dropped_notifications: Arc<std::sync::atomic::AtomicU64>,
}

impl std::fmt::Debug for AsyncConn {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AsyncConn")
            .field("addr", &self.addr)
            .field("backend_pid", &self.backend_pid)
            .field("alive", &self.is_alive())
            .finish()
    }
}

impl AsyncConn {
    /// Check if the connection is still alive (writer/reader tasks running).
    pub fn is_alive(&self) -> bool {
        self.alive.load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Backend process ID assigned by the server.
    pub fn backend_pid(&self) -> i32 {
        self.backend_pid
    }

    /// Server address this connection is talking to.
    pub fn addr(&self) -> &str {
        &self.addr
    }

    /// Produce a cancel token for the running session on this connection.
    pub fn cancel_token(&self) -> crate::cancel::CancelToken {
        crate::cancel::CancelToken::new(self.addr.clone(), self.backend_pid, self.backend_secret)
    }

    /// Mark the connection as having mutated session state since the last
    /// reset. Pools call `take_state_mutated()` on return to decide whether
    /// to issue `DISCARD ALL`. Callers issuing `BEGIN`, `SET` (without
    /// `LOCAL`), advisory locks, temp tables, etc., should call this before
    /// submitting.
    pub fn mark_state_mutated(&self) {
        self.state_mutated
            .store(true, std::sync::atomic::Ordering::Release);
    }

    /// Atomically read and clear the state-mutated flag. Returns the
    /// previous value: `true` means the caller should issue a reset.
    pub fn take_state_mutated(&self) -> bool {
        self.state_mutated
            .swap(false, std::sync::atomic::Ordering::AcqRel)
    }

    /// Read the state-mutated flag without clearing it.
    pub fn is_state_mutated(&self) -> bool {
        self.state_mutated
            .load(std::sync::atomic::Ordering::Acquire)
    }

    /// Mark the connection as broken. The reader/writer tasks may still be
    /// running, but the session is in an indeterminate state (for example,
    /// a transaction was dropped without commit or rollback) and the
    /// connection must not be reused. Pool integrations check
    /// [`AsyncConn::is_broken`] on return and destroy the connection
    /// instead of returning it to the idle set.
    pub fn mark_broken(&self) {
        self.broken
            .store(true, std::sync::atomic::Ordering::Release);
    }

    /// True if the connection has been declared broken by a caller via
    /// [`AsyncConn::mark_broken`]. Independent of [`AsyncConn::is_alive`],
    /// which only reflects whether the reader/writer tasks are still running.
    pub fn is_broken(&self) -> bool {
        self.broken.load(std::sync::atomic::Ordering::Acquire)
    }

    /// Test-only helper that flips the `alive` flag to `false` without
    /// actually exiting the writer task. Used by pg-wired's own tests and
    /// by downstream crates' integration tests (e.g. resolute) to exercise
    /// the dead-conn branch of [`AsyncConn::enqueue_rollback`] (and any
    /// other code that gates on `is_alive`) without racing against the
    /// real task-exit timing. Not part of the stable API: the `__` prefix
    /// and `#[doc(hidden)]` mark this as off-limits for production use.
    #[doc(hidden)]
    pub fn __force_mark_dead_for_test(&self) {
        self.alive
            .store(false, std::sync::atomic::Ordering::Release);
    }

    /// Fire-and-forget enqueue of a `ROLLBACK` simple-query, intended to be
    /// callable from a synchronous `Drop`. Returns `true` if the request was
    /// queued on the writer task, `false` if the connection is not alive or
    /// the channel was full/closed (in which case the caller should fall
    /// back to [`AsyncConn::mark_broken`] so the connection is discarded
    /// by the pool).
    ///
    /// PostgreSQL accepts `ROLLBACK` from any in-transaction state — including
    /// the aborted state (`25P02`) that a failed query leaves behind — so this
    /// reliably restores the session to idle. The response is drained and
    /// discarded; ordering on the writer queue is preserved, so any
    /// subsequent request (e.g., the pool's `DISCARD ALL` reset) sees a clean
    /// connection.
    pub fn enqueue_rollback(&self) -> bool {
        if !self.is_alive() {
            return false;
        }
        try_enqueue_rollback(&self.request_tx)
    }
}

/// Inner helper for [`AsyncConn::enqueue_rollback`]: encodes a `ROLLBACK`
/// simple-query and tries to push it onto the writer's request channel.
/// Extracted so the channel-full and channel-closed branches can be unit
/// tested without instantiating a real `AsyncConn`.
fn try_enqueue_rollback(request_tx: &mpsc::Sender<PipelineRequest>) -> bool {
    let mut buf = BytesMut::with_capacity(16);
    frontend::encode_message(&FrontendMsg::Query(b"ROLLBACK"), &mut buf);
    let (tx, _rx) = oneshot::channel();
    request_tx
        .try_send(PipelineRequest {
            messages: buf,
            collector: ResponseCollector::Drain,
            response_tx: tx,
        })
        .is_ok()
}

struct PendingResponse {
    collector: ResponseCollector,
    response_tx: oneshot::Sender<Result<PipelineResponse, PgWireError>>,
}

impl AsyncConn {
    /// Create a new async connection from a raw WireConn.
    /// Spawns writer and reader tasks.
    pub fn new(conn: WireConn) -> Self {
        let backend_pid = conn.pid;
        let backend_secret = conn.secret;
        // Extract peer address before consuming the stream.
        let addr = conn
            .stream
            .peer_addr()
            .map(|a| a.to_string())
            .unwrap_or_default();

        let (notification_tx, notification_rx) = mpsc::channel(4096);
        let (request_tx, request_rx) = mpsc::channel::<PipelineRequest>(256);
        let pending: Arc<Mutex<VecDeque<PendingResponse>>> = Arc::new(Mutex::new(VecDeque::new()));
        let pending_notify = Arc::new(tokio::sync::Notify::new());
        let alive = Arc::new(std::sync::atomic::AtomicBool::new(true));
        let state_mutated = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let broken = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let dropped_notifications = Arc::new(std::sync::atomic::AtomicU64::new(0));

        let (stream_read, stream_write) = tokio::io::split(conn.into_stream());

        // Spawn writer task — sets alive=false on exit.
        {
            let pending = Arc::clone(&pending);
            let pending_notify = Arc::clone(&pending_notify);
            let alive = Arc::clone(&alive);
            tokio::spawn(async move {
                writer_task(request_rx, stream_write, pending, pending_notify).await;
                alive.store(false, std::sync::atomic::Ordering::Relaxed);
                tracing::warn!("pg-wired writer task exited");
            });
        }

        // Spawn reader task — sets alive=false on exit.
        {
            let pending = Arc::clone(&pending);
            let pending_notify = Arc::clone(&pending_notify);
            let alive_clone = Arc::clone(&alive);
            let state_mutated = Arc::clone(&state_mutated);
            let ntf_tx = notification_tx.clone();
            let dropped = Arc::clone(&dropped_notifications);
            tokio::spawn(async move {
                reader_task(
                    stream_read,
                    pending,
                    pending_notify,
                    ntf_tx,
                    state_mutated,
                    dropped,
                )
                .await;
                alive_clone.store(false, std::sync::atomic::Ordering::Relaxed);
                tracing::warn!("pg-wired reader task exited");
            });
        }

        Self {
            request_tx,
            stmt_cache: std::sync::Mutex::new(std::collections::HashMap::new()),
            stmt_counter: std::sync::atomic::AtomicU64::new(0),
            alive,
            backend_pid,
            backend_secret,
            addr,
            notification_tx,
            notification_rx: std::sync::Mutex::new(Some(notification_rx)),
            state_mutated,
            broken,
            dropped_notifications,
        }
    }

    /// Cumulative number of `NotificationResponse` messages this connection
    /// has discarded since it was created.
    ///
    /// Notifications are dropped when (a) the application has not called
    /// [`AsyncConn::take_notification_receiver`] yet, or (b) the receiver is
    /// not draining fast enough and the bounded channel fills up. Compare
    /// successive readings to detect missed `LISTEN` events.
    pub fn dropped_notifications(&self) -> u64 {
        self.dropped_notifications
            .load(std::sync::atomic::Ordering::Relaxed)
    }

    /// Take the notification receiver. Call once to get a channel that
    /// receives `NotificationResponse` messages that arrive during queries.
    pub fn take_notification_receiver(
        &self,
    ) -> Option<mpsc::Receiver<crate::protocol::types::BackendMsg>> {
        self.notification_rx
            .lock()
            .ok()
            .and_then(|mut guard| guard.take())
    }

    /// Look up or allocate a statement name.
    ///
    /// Cache hit: returns the cached name with `needs_parse=false`. The
    /// caller submits only `Bind/Execute/Sync`.
    ///
    /// Cache miss: allocates a fresh, unique name from the connection's
    /// statement counter and returns `(name, needs_parse=true)`. The name
    /// is NOT yet published in the cache: the caller MUST include a
    /// `Parse` for the new name in the same atomic submit as
    /// `Bind/Execute/Sync` (so the Parse runs inside whatever
    /// role-switched transaction the caller has framed, e.g. `BEGIN; SET
    /// LOCAL ROLE …; …`), and then call [`Self::cache_statement`] to
    /// publish the name only after the Parse has succeeded on the wire.
    ///
    /// Why publish-after-success: an earlier version pre-queued the
    /// Parse as its own writer request and published the cache entry
    /// up-front to avoid a race where a concurrent caller saw the
    /// cached name and submitted a Bind-only request that races ahead
    /// of the Parse. That eliminated the race, but ran the Parse
    /// outside any transaction, under the connection's persistent role
    /// (e.g. PostgREST's `authenticator`). SQL that references objects
    /// only reachable after `SET LOCAL ROLE` to a user role failed
    /// with `42501 permission denied` during Parse, while every
    /// subsequent Bind for the same name failed with `26000: prepared
    /// statement "sN" does not exist`. Publishing only after a
    /// successful Parse keeps caching role-correct: each first-time
    /// concurrent caller pays for its own Parse (rather than sharing a
    /// pre-queued one), and `cache_statement` uses first-publisher-wins
    /// semantics so the losing names become session-bounded orphans
    /// (bounded by the 256-entry LRU on this connection).
    pub fn lookup_or_alloc(&self, sql: &str, _param_oids: &[u32]) -> (Vec<u8>, bool) {
        let cache = match self.stmt_cache.lock() {
            Ok(c) => c,
            Err(poisoned) => poisoned.into_inner(),
        };
        if let Some((name, _)) = cache.get(sql) {
            return (name.as_bytes().to_vec(), false);
        }
        // Allocate a unique name. Counters never collide, so concurrent
        // misses get distinct names. The cache stays empty for `sql`
        // until the caller calls `cache_statement` after a successful
        // Parse.
        let n = self
            .stmt_counter
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let name = format!("s{n}");
        (name.into_bytes(), true)
    }

    /// Publish a freshly Parsed statement in the cache so subsequent
    /// lookups for the same SQL skip the Parse step.
    ///
    /// Called by the high-level `exec_*` helpers (and any external
    /// caller of [`Self::lookup_or_alloc`]) after the writer submit that
    /// included `Parse` for `name` returned successfully. Skipping this
    /// step doesn't cause correctness problems; the next lookup just
    /// misses and re-Parses.
    ///
    /// First-publisher-wins: if another concurrent miss already
    /// published a different name for the same SQL, that name stays in
    /// the cache and the caller's name becomes a server-side orphan
    /// (cleaned up at session end; bounded by LRU eviction during the
    /// session).
    ///
    /// LRU eviction: when the cache reaches its 256-entry cap, the
    /// oldest entry by counter is removed and a `Close + Sync` is
    /// fire-and-forget queued to free the server-side prepared
    /// statement.
    pub fn cache_statement(&self, sql: &str, name: &[u8]) {
        let Ok(name_str) = std::str::from_utf8(name) else {
            return;
        };
        let counter = name_str
            .strip_prefix('s')
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or_else(|| self.stmt_counter.load(std::sync::atomic::Ordering::Relaxed));
        let mut cache = match self.stmt_cache.lock() {
            Ok(c) => c,
            Err(poisoned) => poisoned.into_inner(),
        };
        if cache.contains_key(sql) {
            return;
        }
        if cache.len() >= 256 {
            if let Some((oldest_key, oldest_name)) = cache
                .iter()
                .min_by_key(|(_, (_, counter))| *counter)
                .map(|(k, (name, _))| (k.clone(), name.clone()))
            {
                cache.remove(&oldest_key);
                let mut close_buf = BytesMut::with_capacity(32);
                frontend::encode_message(
                    &FrontendMsg::Close {
                        kind: b'S',
                        name: oldest_name.as_bytes(),
                    },
                    &mut close_buf,
                );
                frontend::encode_message(&FrontendMsg::Sync, &mut close_buf);
                let (tx, _rx) = oneshot::channel();
                let _ = self.request_tx.try_send(PipelineRequest {
                    messages: close_buf,
                    collector: ResponseCollector::Drain,
                    response_tx: tx,
                });
            }
        }
        cache.insert(sql.to_string(), (name_str.to_string(), counter));
    }

    /// Execute COPY FROM STDIN: sends the COPY command, then data in chunks, then CopyDone.
    /// Returns the number of rows copied (from CommandComplete tag).
    ///
    /// Data is sent in chunks of up to 1MB to avoid buffering the entire payload
    /// in a single BytesMut. For small payloads (< 1MB), this is a single write.
    pub async fn copy_in(&self, copy_sql: &str, data: &[u8]) -> Result<u64, PgWireError> {
        use crate::protocol::types::FrontendMsg;
        const CHUNK_SIZE: usize = 1024 * 1024; // 1MB chunks

        // Build the message buffer: Query + chunked CopyData + CopyDone.
        let mut buf = BytesMut::with_capacity(copy_sql.len() + data.len().min(CHUNK_SIZE) + 64);
        frontend::encode_message(&FrontendMsg::Query(copy_sql.as_bytes()), &mut buf);

        // Send data in chunks to avoid a single huge allocation.
        for chunk in data.chunks(CHUNK_SIZE) {
            frontend::encode_message(&FrontendMsg::CopyData(chunk), &mut buf);
        }
        // Empty data is valid (0 rows copied).
        frontend::encode_message(&FrontendMsg::CopyDone, &mut buf);

        let resp = self
            .submit(buf, ResponseCollector::CopyIn { data: Vec::new() })
            .await?;
        match resp {
            PipelineResponse::Rows { command_tag, .. } => Ok(parse_copy_count(&command_tag)),
            PipelineResponse::Done => Ok(0),
        }
    }

    /// Execute COPY FROM STDIN with streaming: sends the COPY command, then
    /// reads data from an async reader in chunks, avoiding buffering the entire
    /// payload in memory.
    ///
    /// ```no_run
    /// # async fn _doctest() -> Result<(), Box<dyn std::error::Error>> {
    /// # let conn: pg_wired::AsyncConn = unimplemented!();
    /// use tokio::fs::File;
    /// let file = File::open("data.csv").await?;
    /// let _count = conn.copy_in_stream("COPY users FROM STDIN WITH (FORMAT csv)", file).await?;
    /// # Ok(()) }
    /// ```
    pub async fn copy_in_stream<R: tokio::io::AsyncRead + Unpin>(
        &self,
        copy_sql: &str,
        mut reader: R,
    ) -> Result<u64, PgWireError> {
        use tokio::io::AsyncReadExt;
        const CHUNK_SIZE: usize = 1024 * 1024; // 1MB chunks

        // Send the COPY command.
        let mut buf = BytesMut::with_capacity(copy_sql.len() + 16);
        frontend::encode_message(&FrontendMsg::Query(copy_sql.as_bytes()), &mut buf);

        // Read and send data in chunks.
        let mut chunk = vec![0u8; CHUNK_SIZE];
        loop {
            let n = reader.read(&mut chunk).await?;
            if n == 0 {
                break;
            }
            frontend::encode_message(&FrontendMsg::CopyData(&chunk[..n]), &mut buf);
        }
        frontend::encode_message(&FrontendMsg::CopyDone, &mut buf);

        let resp = self
            .submit(buf, ResponseCollector::CopyIn { data: Vec::new() })
            .await?;
        match resp {
            PipelineResponse::Rows { command_tag, .. } => Ok(parse_copy_count(&command_tag)),
            PipelineResponse::Done => Ok(0),
        }
    }

    /// Execute COPY TO STDOUT: sends the COPY command, collects all CopyData.
    pub async fn copy_out(&self, copy_sql: &str) -> Result<Vec<u8>, PgWireError> {
        use crate::protocol::types::FrontendMsg;
        let mut buf = BytesMut::new();
        frontend::encode_message(&FrontendMsg::Query(copy_sql.as_bytes()), &mut buf);

        let resp = self.submit(buf, ResponseCollector::CopyOut).await?;
        match resp {
            PipelineResponse::Rows { rows, .. } => {
                // For CopyOut, we reuse the Rows variant but each `RawRow` carries
                // one cell which is the raw COPY data chunk (see `collect_copy_out`).
                let mut result = Vec::new();
                for row in rows {
                    for data in row.iter().flatten() {
                        result.extend_from_slice(data);
                    }
                }
                Ok(result)
            }
            PipelineResponse::Done => Ok(Vec::new()),
        }
    }

    /// Evict a SQL statement from the cache, forcing re-parse on next use.
    /// Used for prepared statement invalidation after schema changes.
    pub fn invalidate_statement(&self, sql: &str) {
        let mut cache = match self.stmt_cache.lock() {
            Ok(c) => c,
            Err(poisoned) => poisoned.into_inner(),
        };
        cache.remove(sql);
    }

    /// Clear the entire statement cache. Must be called after `DISCARD ALL`
    /// which destroys server-side prepared statements.
    pub fn clear_statement_cache(&self) {
        let mut cache = match self.stmt_cache.lock() {
            Ok(c) => c,
            Err(poisoned) => poisoned.into_inner(),
        };
        cache.clear();
    }

    /// Execute a pipelined transaction with automatic statement caching.
    ///
    /// On a successful Parse the new statement name is published in the
    /// cache via [`Self::cache_statement`]. If a cached statement turns
    /// out to be invalid (PG error 26000 or 0A000), the cache entry is
    /// evicted and the transaction is retried once with a fresh Parse.
    /// This handles schema changes invalidating cached plans after their
    /// initial Parse.
    pub async fn exec_transaction(
        &self,
        setup_sql: &str,
        query_sql: &str,
        params: &[Option<&[u8]>],
        param_oids: &[u32],
    ) -> Result<Vec<RawRow>, PgWireError> {
        let (stmt_name, needs_parse) = self.lookup_or_alloc(query_sql, param_oids);
        match self
            .pipeline_transaction(
                setup_sql,
                query_sql,
                params,
                param_oids,
                &stmt_name,
                needs_parse,
            )
            .await
        {
            Ok(rows) => {
                if needs_parse {
                    self.cache_statement(query_sql, &stmt_name);
                }
                Ok(rows)
            }
            Err(PgWireError::Pg(ref pg_err))
                if !needs_parse && is_stale_statement_error(pg_err) =>
            {
                tracing::debug!(
                    sql = query_sql,
                    "prepared statement invalidated — re-parsing in transaction"
                );
                self.invalidate_statement(query_sql);
                let (stmt_name, _) = self.lookup_or_alloc(query_sql, param_oids);
                let result = self
                    .pipeline_transaction(
                        setup_sql, query_sql, params, param_oids, &stmt_name, true,
                    )
                    .await;
                if result.is_ok() {
                    self.cache_statement(query_sql, &stmt_name);
                }
                result
            }
            Err(e) => Err(e),
        }
    }

    /// Execute a parameterized query with automatic statement caching.
    /// If a cached statement is invalidated by a schema change (PG error 26000
    /// or 0A000), automatically evicts the cache entry, re-parses, and retries once.
    pub async fn exec_query(
        &self,
        sql: &str,
        params: &[Option<&[u8]>],
        param_oids: &[u32],
    ) -> Result<Vec<RawRow>, PgWireError> {
        let (stmt_name, needs_parse) = self.lookup_or_alloc(sql, param_oids);
        match self
            .query(sql, params, param_oids, &stmt_name, needs_parse)
            .await
        {
            Ok(rows) => {
                if needs_parse {
                    self.cache_statement(sql, &stmt_name);
                }
                Ok(rows)
            }
            Err(PgWireError::Pg(ref pg_err))
                if !needs_parse && is_stale_statement_error(pg_err) =>
            {
                tracing::debug!(sql = sql, "prepared statement invalidated — re-parsing");
                self.invalidate_statement(sql);
                let (stmt_name, _) = self.lookup_or_alloc(sql, param_oids);
                let result = self.query(sql, params, param_oids, &stmt_name, true).await;
                if result.is_ok() {
                    self.cache_statement(sql, &stmt_name);
                }
                result
            }
            Err(e) => Err(e),
        }
    }

    /// Maximum time to wait for a response from the reader task.
    /// Prevents hanging forever if the reader/writer task dies mid-request.
    const REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(300);

    /// Submit a request to the connection. Returns a future that resolves
    /// when the response is available. Times out after 5 minutes to prevent
    /// hanging forever if the reader/writer task dies.
    pub async fn submit(
        &self,
        messages: BytesMut,
        collector: ResponseCollector,
    ) -> Result<PipelineResponse, PgWireError> {
        let (response_tx, response_rx) = oneshot::channel();
        let req = PipelineRequest {
            messages,
            collector,
            response_tx,
        };
        self.request_tx
            .send(req)
            .await
            .map_err(|_| PgWireError::ConnectionClosed)?;
        match tokio::time::timeout(Self::REQUEST_TIMEOUT, response_rx).await {
            Ok(Ok(result)) => result,
            Ok(Err(_)) => Err(PgWireError::ConnectionClosed),
            Err(_elapsed) => {
                tracing::error!(
                    "request timed out after {:?} — reader/writer task may be dead",
                    Self::REQUEST_TIMEOUT
                );
                Err(PgWireError::ConnectionClosed)
            }
        }
    }

    /// Submit a batch of requests in FIFO order. All requests are queued
    /// before any response is awaited, so the writer task sees them together
    /// and coalesces them into a single write() syscall. The server then
    /// pipelines the N responses back-to-back, giving one network round-trip
    /// for all N queries.
    ///
    /// Returns one `Result<PipelineResponse, PgWireError>` per input item,
    /// in the same order. The outer `Result` fails only if queueing fails
    /// (channel closed). Each inner `Result` reflects the per-query outcome.
    pub async fn submit_batch(
        &self,
        items: Vec<(BytesMut, ResponseCollector)>,
    ) -> Result<Vec<Result<PipelineResponse, PgWireError>>, PgWireError> {
        let mut receivers = Vec::with_capacity(items.len());
        for (messages, collector) in items {
            let (response_tx, response_rx) = oneshot::channel();
            self.request_tx
                .send(PipelineRequest {
                    messages,
                    collector,
                    response_tx,
                })
                .await
                .map_err(|_| PgWireError::ConnectionClosed)?;
            receivers.push(response_rx);
        }
        let mut results = Vec::with_capacity(receivers.len());
        for rx in receivers {
            match tokio::time::timeout(Self::REQUEST_TIMEOUT, rx).await {
                Ok(Ok(r)) => results.push(r),
                Ok(Err(_)) => results.push(Err(PgWireError::ConnectionClosed)),
                Err(_) => {
                    tracing::error!(
                        "submit_batch request timed out after {:?}",
                        Self::REQUEST_TIMEOUT
                    );
                    results.push(Err(PgWireError::ConnectionClosed));
                }
            }
        }
        Ok(results)
    }

    /// Send a Terminate message to the server and wait for the writer/reader
    /// tasks to exit. After this returns, the connection is unusable; further
    /// calls fail with `ConnectionClosed`. Idempotent: calling `close` on an
    /// already-closed connection is a no-op and returns `Ok`.
    pub async fn close(&self) -> Result<(), PgWireError> {
        if !self.is_alive() {
            return Ok(());
        }
        let mut buf = BytesMut::with_capacity(5);
        frontend::encode_message(&FrontendMsg::Terminate, &mut buf);
        // Submit Terminate through the writer so ordering is preserved wrt
        // any in-flight requests ahead of us. The server replies with nothing
        // and closes the socket, so we expect `ConnectionClosed` back from
        // the drain collector — treat that as a successful close.
        match self.submit(buf, ResponseCollector::Drain).await {
            Ok(_) | Err(PgWireError::ConnectionClosed) => Ok(()),
            Err(PgWireError::Io(e)) if e.kind() == std::io::ErrorKind::BrokenPipe => Ok(()),
            Err(e) => Err(e),
        }
    }

    /// Submit a streaming request. Returns the column header and an mpsc receiver
    /// that yields rows one at a time.
    pub async fn submit_stream(
        &self,
        messages: BytesMut,
        row_buffer: usize,
    ) -> Result<
        (
            StreamHeader,
            mpsc::Receiver<Result<StreamedRow, PgWireError>>,
        ),
        PgWireError,
    > {
        let (header_tx, header_rx) = oneshot::channel();
        let (row_tx, row_rx) = mpsc::channel(row_buffer);
        let (response_tx, _response_rx) = oneshot::channel();
        let req = PipelineRequest {
            messages,
            collector: ResponseCollector::Stream { header_tx, row_tx },
            response_tx,
        };
        self.request_tx
            .send(req)
            .await
            .map_err(|_| PgWireError::ConnectionClosed)?;
        let header = header_rx
            .await
            .map_err(|_| PgWireError::ConnectionClosed)??;
        Ok((header, row_rx))
    }

    /// Execute a pipelined transaction:
    /// setup (simple query) + data query (extended protocol) + COMMIT (simple query)
    /// All coalesced into one TCP write. Binary-safe parameterized data query.
    pub async fn pipeline_transaction(
        &self,
        setup_sql: &str,
        query_sql: &str,
        params: &[Option<&[u8]>],
        param_oids: &[u32],
        stmt_name: &[u8],
        needs_parse: bool,
    ) -> Result<Vec<RawRow>, PgWireError> {
        let mut buf = BytesMut::with_capacity(1024);

        // 1. Simple query for setup (BEGIN + SET ROLE + set_config).
        frontend::encode_message(&FrontendMsg::Query(setup_sql.as_bytes()), &mut buf);

        // Submit setup as Drain — we don't care about its response data.
        let setup_msgs = buf.split();

        // 2. Extended query for data.
        let text_fmts: Vec<FormatCode> = vec![FormatCode::Text; params.len().max(1)];
        let result_fmts = [FormatCode::Text];

        if needs_parse {
            frontend::encode_message(
                &FrontendMsg::Parse {
                    name: stmt_name,
                    sql: query_sql.as_bytes(),
                    param_oids,
                },
                &mut buf,
            );
        }

        frontend::encode_message(
            &FrontendMsg::Bind {
                portal: b"",
                statement: stmt_name,
                param_formats: &text_fmts[..params.len()],
                params,
                result_formats: &result_fmts,
            },
            &mut buf,
        );

        frontend::encode_message(
            &FrontendMsg::Execute {
                portal: b"",
                max_rows: 0,
            },
            &mut buf,
        );

        frontend::encode_message(&FrontendMsg::Sync, &mut buf);

        let data_msgs = buf.split();

        // 3. Simple query for COMMIT — in its own buffer so each request
        // carries exactly the bytes that produce its ReadyForQuery response.
        let mut commit_buf = BytesMut::with_capacity(32);
        frontend::encode_message(&FrontendMsg::Query(b"COMMIT"), &mut commit_buf);

        // Submit all three as separate requests with different collectors.
        // They'll be coalesced by the writer into one write() syscall.
        let (setup_tx, setup_rx) = oneshot::channel();
        let (data_tx, data_rx) = oneshot::channel();
        let (commit_tx, commit_rx) = oneshot::channel();

        // Send all three requests to the writer channel.
        // The writer drains the channel and writes them all at once.
        self.request_tx
            .send(PipelineRequest {
                messages: setup_msgs,
                collector: ResponseCollector::Drain,
                response_tx: setup_tx,
            })
            .await
            .map_err(|_| PgWireError::ConnectionClosed)?;

        self.request_tx
            .send(PipelineRequest {
                messages: data_msgs,
                collector: ResponseCollector::Rows,
                response_tx: data_tx,
            })
            .await
            .map_err(|_| PgWireError::ConnectionClosed)?;

        self.request_tx
            .send(PipelineRequest {
                messages: commit_buf,
                collector: ResponseCollector::Drain,
                response_tx: commit_tx,
            })
            .await
            .map_err(|_| PgWireError::ConnectionClosed)?;

        // Wait for all responses.
        setup_rx
            .await
            .map_err(|_| PgWireError::ConnectionClosed)??;

        let data_resp = data_rx.await.map_err(|_| PgWireError::ConnectionClosed)??;

        commit_rx
            .await
            .map_err(|_| PgWireError::ConnectionClosed)??;

        match data_resp {
            PipelineResponse::Rows { rows, .. } => Ok(rows),
            PipelineResponse::Done => Ok(Vec::new()),
        }
    }

    /// Execute a simple parameterized query (no transaction).
    pub async fn query(
        &self,
        sql: &str,
        params: &[Option<&[u8]>],
        param_oids: &[u32],
        stmt_name: &[u8],
        needs_parse: bool,
    ) -> Result<Vec<RawRow>, PgWireError> {
        self.query_with_formats(sql, params, param_oids, &[], &[], stmt_name, needs_parse)
            .await
    }

    /// Execute a parameterized query with explicit per-param and per-result
    /// format codes (text = 0, binary = 1).
    ///
    /// `param_formats` is interpreted per PostgreSQL wire protocol rules:
    /// - empty: all params are text
    /// - length 1: the single code applies to every param
    /// - length N (== params.len()): one code per param
    ///
    /// Same rules apply to `result_formats` for output columns (empty → all
    /// text; single code → applies to all columns; per-column list otherwise).
    #[allow(clippy::too_many_arguments)]
    pub async fn query_with_formats(
        &self,
        sql: &str,
        params: &[Option<&[u8]>],
        param_oids: &[u32],
        param_formats: &[FormatCode],
        result_formats: &[FormatCode],
        stmt_name: &[u8],
        needs_parse: bool,
    ) -> Result<Vec<RawRow>, PgWireError> {
        let mut buf = BytesMut::with_capacity(512);

        // Default to all-text if caller passes empty slices.
        let text_param_fmts: Vec<FormatCode>;
        let param_fmts_slice: &[FormatCode] = if param_formats.is_empty() {
            text_param_fmts = vec![FormatCode::Text; params.len().max(1)];
            &text_param_fmts[..params.len()]
        } else {
            param_formats
        };
        let default_result_fmts = [FormatCode::Text];
        let result_fmts_slice: &[FormatCode] = if result_formats.is_empty() {
            &default_result_fmts
        } else {
            result_formats
        };

        if needs_parse {
            frontend::encode_message(
                &FrontendMsg::Parse {
                    name: stmt_name,
                    sql: sql.as_bytes(),
                    param_oids,
                },
                &mut buf,
            );
        }

        frontend::encode_message(
            &FrontendMsg::Bind {
                portal: b"",
                statement: stmt_name,
                param_formats: param_fmts_slice,
                params,
                result_formats: result_fmts_slice,
            },
            &mut buf,
        );

        frontend::encode_message(
            &FrontendMsg::Execute {
                portal: b"",
                max_rows: 0,
            },
            &mut buf,
        );

        frontend::encode_message(&FrontendMsg::Sync, &mut buf);

        let resp = self.submit(buf, ResponseCollector::Rows).await?;
        match resp {
            PipelineResponse::Rows { rows, .. } => Ok(rows),
            PipelineResponse::Done => Ok(Vec::new()),
        }
    }

    /// Variant of `exec_query` with per-param and per-result format codes.
    /// See `query_with_formats` for format code semantics.
    pub async fn exec_query_with_formats(
        &self,
        sql: &str,
        params: &[Option<&[u8]>],
        param_oids: &[u32],
        param_formats: &[FormatCode],
        result_formats: &[FormatCode],
    ) -> Result<Vec<RawRow>, PgWireError> {
        let (stmt_name, needs_parse) = self.lookup_or_alloc(sql, param_oids);
        match self
            .query_with_formats(
                sql,
                params,
                param_oids,
                param_formats,
                result_formats,
                &stmt_name,
                needs_parse,
            )
            .await
        {
            Ok(rows) => {
                if needs_parse {
                    self.cache_statement(sql, &stmt_name);
                }
                Ok(rows)
            }
            Err(PgWireError::Pg(ref pg_err))
                if !needs_parse && is_stale_statement_error(pg_err) =>
            {
                tracing::debug!(sql = sql, "prepared statement invalidated — re-parsing");
                self.invalidate_statement(sql);
                let (stmt_name, _) = self.lookup_or_alloc(sql, param_oids);
                let result = self
                    .query_with_formats(
                        sql,
                        params,
                        param_oids,
                        param_formats,
                        result_formats,
                        &stmt_name,
                        true,
                    )
                    .await;
                if result.is_ok() {
                    self.cache_statement(sql, &stmt_name);
                }
                result
            }
            Err(e) => Err(e),
        }
    }
}

// ---------------------------------------------------------------------------
// Writer task
// ---------------------------------------------------------------------------

async fn writer_task(
    mut rx: mpsc::Receiver<PipelineRequest>,
    mut stream: tokio::io::WriteHalf<crate::tls::MaybeTlsStream>,
    pending: Arc<Mutex<VecDeque<PendingResponse>>>,
    pending_notify: Arc<tokio::sync::Notify>,
) {
    let mut write_buf = BytesMut::with_capacity(8192);

    loop {
        // Wait for the first request.
        let first = match rx.recv().await {
            Some(req) => req,
            None => {
                // Channel closed — drain any pending responses with ConnectionClosed.
                drain_pending_on_exit(&pending).await;
                return;
            }
        };

        // Drain any additional queued requests (batch coalescing).
        write_buf.clear();
        write_buf.extend_from_slice(&first.messages);

        let mut batch: Vec<PendingResponse> = vec![PendingResponse {
            collector: first.collector,
            response_tx: first.response_tx,
        }];

        // Non-blocking drain of all queued requests.
        while let Ok(req) = rx.try_recv() {
            write_buf.extend_from_slice(&req.messages);
            batch.push(PendingResponse {
                collector: req.collector,
                response_tx: req.response_tx,
            });
        }

        // ONE write() syscall for all coalesced messages.
        // Write BEFORE enqueuing pending responses — if the write fails,
        // we send errors to callers instead of leaving them hanging.
        let write_result = stream.write_all(&write_buf).await;
        let write_err = match write_result {
            Ok(_) => stream.flush().await.err(),
            Err(e) => Some(e),
        };

        if let Some(e) = write_err {
            tracing::error!("Writer error: {e}");
            let msg = e.to_string();
            for p in batch {
                let _ = p.response_tx.send(Err(PgWireError::Io(std::io::Error::new(
                    std::io::ErrorKind::BrokenPipe,
                    msg.clone(),
                ))));
            }
            // Drain any already-pending responses so the reader doesn't hang.
            drain_pending_on_exit(&pending).await;
            return;
        }

        // Write succeeded — enqueue pending responses for the reader.
        {
            let mut pq = pending.lock().await;
            for p in batch {
                pq.push_back(p);
            }
        }
        // Wake the reader task to process the newly enqueued responses.
        pending_notify.notify_one();
    }
}

/// On writer exit, drain all pending responses with ConnectionClosed errors
/// so callers don't wait for the 5-minute timeout.
async fn drain_pending_on_exit(pending: &Arc<Mutex<VecDeque<PendingResponse>>>) {
    let mut pq = pending.lock().await;
    while let Some(pr) = pq.pop_front() {
        let _ = pr.response_tx.send(Err(PgWireError::ConnectionClosed));
    }
}

// ---------------------------------------------------------------------------
// Reader task
// ---------------------------------------------------------------------------

async fn reader_task(
    mut stream: tokio::io::ReadHalf<crate::tls::MaybeTlsStream>,
    pending: Arc<Mutex<VecDeque<PendingResponse>>>,
    pending_notify: Arc<tokio::sync::Notify>,
    notification_tx: mpsc::Sender<BackendMsg>,
    state_mutated: Arc<std::sync::atomic::AtomicBool>,
    dropped_notifications: Arc<std::sync::atomic::AtomicU64>,
) {
    let mut recv_buf = BytesMut::with_capacity(32 * 1024);

    loop {
        // Wait for a pending response to become available.
        let pr = loop {
            {
                let mut pq = pending.lock().await;
                if let Some(pr) = pq.pop_front() {
                    break pr;
                }
            }
            // No pending — wait for the writer to signal.
            pending_notify.notified().await;
        };

        // Collect the response based on the collector type.
        let result = match pr.collector {
            ResponseCollector::Rows => {
                collect_rows(
                    &mut stream,
                    &mut recv_buf,
                    &notification_tx,
                    &state_mutated,
                    &dropped_notifications,
                )
                .await
            }
            ResponseCollector::Drain => {
                drain_until_ready(&mut stream, &mut recv_buf, Some(&state_mutated))
                    .await
                    .map(|_| PipelineResponse::Done)
            }
            ResponseCollector::Stream { header_tx, row_tx } => {
                stream_rows(
                    &mut stream,
                    &mut recv_buf,
                    header_tx,
                    row_tx,
                    &notification_tx,
                    &state_mutated,
                    &dropped_notifications,
                )
                .await;
                Ok(PipelineResponse::Done)
            }
            ResponseCollector::CopyIn { .. } => {
                collect_copy_in_response(&mut stream, &mut recv_buf, &state_mutated).await
            }
            ResponseCollector::CopyOut => {
                collect_copy_out(&mut stream, &mut recv_buf, &state_mutated).await
            }
        };

        // Send the response back to the caller.
        let _ = pr.response_tx.send(result);
    }
}

async fn read_msg(
    stream: &mut tokio::io::ReadHalf<crate::tls::MaybeTlsStream>,
    buf: &mut BytesMut,
) -> Result<BackendMsg, PgWireError> {
    loop {
        if let Some(msg) = backend::parse_message(buf).map_err(PgWireError::Protocol)? {
            return Ok(msg);
        }
        let n = stream.read_buf(buf).await?;
        if n == 0 {
            // EOF — try to parse any remaining data in the buffer before giving up.
            // This handles the case where the last message arrived just before the
            // connection closed and is already fully buffered.
            if let Some(msg) = backend::parse_message(buf).map_err(PgWireError::Protocol)? {
                return Ok(msg);
            }
            return Err(PgWireError::ConnectionClosed);
        }
    }
}

/// If the ReadyForQuery status byte is anything other than `I` (idle),
/// flag the connection as state-mutated. `T` (in transaction) and `E`
/// (failed transaction) both leave session state that needs DISCARD ALL.
fn note_rfq_status(status: u8, state_mutated: &std::sync::atomic::AtomicBool) {
    if status != b'I' {
        state_mutated.store(true, std::sync::atomic::Ordering::Release);
    }
}

async fn collect_rows(
    stream: &mut tokio::io::ReadHalf<crate::tls::MaybeTlsStream>,
    buf: &mut BytesMut,
    notification_tx: &mpsc::Sender<BackendMsg>,
    state_mutated: &std::sync::atomic::AtomicBool,
    dropped_notifications: &std::sync::atomic::AtomicU64,
) -> Result<PipelineResponse, PgWireError> {
    let mut rows = Vec::new();
    let mut fields = Vec::new();
    let mut command_tag = String::new();
    loop {
        let msg = read_msg(stream, buf).await?;
        match msg {
            BackendMsg::DataRow(row) => rows.push(row),
            BackendMsg::RowDescription { fields: f } => fields = f,
            BackendMsg::CommandComplete { tag } => command_tag = tag,
            BackendMsg::ReadyForQuery { status } => {
                note_rfq_status(status, state_mutated);
                return Ok(PipelineResponse::Rows {
                    fields,
                    rows,
                    command_tag,
                });
            }
            BackendMsg::ErrorResponse { fields } => {
                drain_until_ready(stream, buf, Some(state_mutated)).await?;
                return Err(PgWireError::Pg(fields));
            }
            msg @ BackendMsg::NotificationResponse { .. } => {
                // Forward notification instead of dropping.
                #[allow(clippy::collapsible_match)]
                if notification_tx.try_send(msg).is_err() {
                    dropped_notifications.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                    tracing::warn!("notification channel full, dropping notification");
                }
            }
            BackendMsg::ParseComplete
            | BackendMsg::BindComplete
            | BackendMsg::NoData
            | BackendMsg::NoticeResponse { .. }
            | BackendMsg::EmptyQueryResponse => {}
            _ => {}
        }
    }
}

async fn drain_until_ready(
    stream: &mut tokio::io::ReadHalf<crate::tls::MaybeTlsStream>,
    buf: &mut BytesMut,
    state_mutated: Option<&std::sync::atomic::AtomicBool>,
) -> Result<(), PgWireError> {
    loop {
        let msg = read_msg(stream, buf).await?;
        if let BackendMsg::ReadyForQuery { status } = msg {
            if let Some(sm) = state_mutated {
                note_rfq_status(status, sm);
            }
            return Ok(());
        }
        if let BackendMsg::ErrorResponse { ref fields } = msg {
            tracing::warn!("Error in drain: {}: {}", fields.code, fields.message);
        }
    }
}

/// Stream rows one at a time, sending header first, then individual rows.
async fn stream_rows(
    stream: &mut tokio::io::ReadHalf<crate::tls::MaybeTlsStream>,
    buf: &mut BytesMut,
    header_tx: oneshot::Sender<Result<StreamHeader, PgWireError>>,
    row_tx: mpsc::Sender<Result<StreamedRow, PgWireError>>,
    notification_tx: &mpsc::Sender<BackendMsg>,
    state_mutated: &std::sync::atomic::AtomicBool,
    dropped_notifications: &std::sync::atomic::AtomicU64,
) {
    let mut header_tx = Some(header_tx);
    let mut fields = Vec::new();
    loop {
        let msg = match read_msg(stream, buf).await {
            Ok(msg) => msg,
            Err(e) => {
                if let Some(htx) = header_tx.take() {
                    let _ = htx.send(Err(e));
                } else {
                    let _ = row_tx.send(Err(e)).await;
                }
                return;
            }
        };
        match msg {
            BackendMsg::RowDescription { fields: f } => {
                fields = f;
            }
            BackendMsg::DataRow(row) => {
                if let Some(htx) = header_tx.take() {
                    let _ = htx.send(Ok(StreamHeader {
                        fields: fields.clone(),
                    }));
                }
                if row_tx.send(Ok(row)).await.is_err() {
                    let _ = drain_until_ready(stream, buf, Some(state_mutated)).await;
                    return;
                }
            }
            BackendMsg::CommandComplete { .. } => {
                if let Some(htx) = header_tx.take() {
                    let _ = htx.send(Ok(StreamHeader {
                        fields: std::mem::take(&mut fields),
                    }));
                }
            }
            BackendMsg::ReadyForQuery { status } => {
                note_rfq_status(status, state_mutated);
                if let Some(htx) = header_tx.take() {
                    let _ = htx.send(Ok(StreamHeader {
                        fields: std::mem::take(&mut fields),
                    }));
                }
                return;
            }
            BackendMsg::ErrorResponse { fields: err } => {
                if let Some(htx) = header_tx.take() {
                    let _ = htx.send(Err(PgWireError::Pg(err)));
                } else {
                    let _ = row_tx.send(Err(PgWireError::Pg(err))).await;
                }
                let _ = drain_until_ready(stream, buf, Some(state_mutated)).await;
                return;
            }
            msg @ BackendMsg::NotificationResponse { .. } => {
                #[allow(clippy::collapsible_match)]
                if notification_tx.try_send(msg).is_err() {
                    dropped_notifications.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                    tracing::warn!("notification channel full, dropping notification");
                }
            }
            BackendMsg::ParseComplete
            | BackendMsg::BindComplete
            | BackendMsg::NoData
            | BackendMsg::PortalSuspended
            | BackendMsg::NoticeResponse { .. }
            | BackendMsg::EmptyQueryResponse => {}
            _ => {}
        }
    }
}

/// Handle COPY IN response: skip CopyInResponse, wait for CommandComplete + ReadyForQuery.
/// The actual CopyData + CopyDone were pre-buffered in the write, so PG processes them.
async fn collect_copy_in_response(
    stream: &mut tokio::io::ReadHalf<crate::tls::MaybeTlsStream>,
    buf: &mut BytesMut,
    state_mutated: &std::sync::atomic::AtomicBool,
) -> Result<PipelineResponse, PgWireError> {
    let mut command_tag = String::new();
    loop {
        let msg = read_msg(stream, buf).await?;
        match msg {
            BackendMsg::CopyInResponse { .. } => {}
            BackendMsg::CommandComplete { tag } => command_tag = tag,
            BackendMsg::ReadyForQuery { status } => {
                note_rfq_status(status, state_mutated);
                return Ok(PipelineResponse::Rows {
                    fields: Vec::new(),
                    rows: Vec::new(),
                    command_tag,
                });
            }
            BackendMsg::ErrorResponse { fields } => {
                drain_until_ready(stream, buf, Some(state_mutated)).await?;
                return Err(PgWireError::Pg(fields));
            }
            _ => {}
        }
    }
}

/// Collect COPY OUT data: CopyOutResponse → CopyData* → CopyDone → CommandComplete → ReadyForQuery.
async fn collect_copy_out(
    stream: &mut tokio::io::ReadHalf<crate::tls::MaybeTlsStream>,
    buf: &mut BytesMut,
    state_mutated: &std::sync::atomic::AtomicBool,
) -> Result<PipelineResponse, PgWireError> {
    let mut data_chunks: Vec<RawRow> = Vec::new();
    let mut command_tag = String::new();
    loop {
        let msg = read_msg(stream, buf).await?;
        match msg {
            BackendMsg::CopyOutResponse { .. } => {}
            BackendMsg::CopyData { data } => {
                let body = bytes::Bytes::from(data);
                data_chunks.push(RawRow::from_full_body(body));
            }
            BackendMsg::CopyDone => {}
            BackendMsg::CommandComplete { tag } => command_tag = tag,
            BackendMsg::ReadyForQuery { status } => {
                note_rfq_status(status, state_mutated);
                return Ok(PipelineResponse::Rows {
                    fields: Vec::new(),
                    rows: data_chunks,
                    command_tag,
                });
            }
            BackendMsg::ErrorResponse { fields } => {
                drain_until_ready(stream, buf, Some(state_mutated)).await?;
                return Err(PgWireError::Pg(fields));
            }
            _ => {}
        }
    }
}

/// Check if a PostgreSQL error indicates a stale/invalidated prepared statement.
/// Error codes: 26000 (invalid_sql_statement_name), 0A000 (feature_not_supported
/// — used when cached plan changes type).
fn is_stale_statement_error(err: &crate::protocol::types::PgError) -> bool {
    matches!(err.code.as_str(), "26000" | "0A000")
}

fn parse_copy_count(tag: &str) -> u64 {
    // COPY tag format: "COPY 123"
    tag.strip_prefix("COPY ")
        .and_then(|s| s.parse::<u64>().ok())
        .unwrap_or(0)
}

// Extension to WireConn to extract the underlying stream.
impl WireConn {
    pub(crate) fn into_stream(self) -> crate::tls::MaybeTlsStream {
        self.stream
    }
}

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

    /// Channel-full branch: when the request channel has no spare capacity,
    /// `try_enqueue_rollback` returns `false` instead of blocking.
    #[tokio::test]
    async fn try_enqueue_rollback_returns_false_when_channel_full() {
        let (tx, _rx) = mpsc::channel::<PipelineRequest>(2);
        // Fill the channel by reusing the same helper. capacity=2 plus the
        // single buffered slot tokio reserves means we may need to push
        // until try_send fails; loop until we observe the false return.
        let mut filled = false;
        for _ in 0..16 {
            if !try_enqueue_rollback(&tx) {
                filled = true;
                break;
            }
        }
        assert!(
            filled,
            "expected try_enqueue_rollback to eventually return false on a full channel"
        );
        assert!(
            !try_enqueue_rollback(&tx),
            "subsequent calls on a full channel must keep returning false"
        );
    }

    /// Channel-closed branch: dropping the receiver makes `try_send` fail
    /// with `Closed`, which `try_enqueue_rollback` reports as `false`.
    #[tokio::test]
    async fn try_enqueue_rollback_returns_false_when_channel_closed() {
        let (tx, rx) = mpsc::channel::<PipelineRequest>(8);
        drop(rx);
        assert!(
            !try_enqueue_rollback(&tx),
            "try_enqueue_rollback must return false when the receiver has been dropped"
        );
    }

    /// Happy path: with a live receiver and free capacity, the helper
    /// reports success and the receiver observes a queued request whose
    /// payload starts with the simple-query opcode `'Q'`.
    #[tokio::test]
    async fn try_enqueue_rollback_returns_true_and_enqueues_query() {
        let (tx, mut rx) = mpsc::channel::<PipelineRequest>(2);
        assert!(try_enqueue_rollback(&tx));
        let req = rx.recv().await.expect("request should be received");
        assert_eq!(
            req.messages.first().copied(),
            Some(b'Q'),
            "queued request should be a simple Query message"
        );
        // Body should mention ROLLBACK (text follows length prefix and is
        // null-terminated; just substring-search to keep the test simple).
        assert!(
            req.messages.windows(8).any(|w| w == b"ROLLBACK"),
            "queued request should contain the ROLLBACK statement text"
        );
    }
}