hyperdb-api-core 0.1.0

Internal implementation details for hyperdb-api. Not a stable API; use hyperdb-api instead.
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
// Copyright (c) 2026, Salesforce, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! High-level asynchronous client for Hyper database.
//!
//! This module provides [`AsyncClient`], the async version of [`Client`](crate::client::Client).
//! It uses tokio for async I/O operations.

use std::sync::Arc;

use tokio::net::TcpStream;
use tokio::sync::Mutex;
use tracing::{debug, info, warn};

#[cfg(unix)]
use tokio::net::UnixStream;

use super::async_connection::AsyncRawConnection;
use super::async_stream::AsyncStream;
use super::async_stream_query::AsyncQueryStream;
use super::cancel::Cancellable;
use super::config::Config;
use super::endpoint::ConnectionEndpoint;
use super::error::{Error, Result};
use super::notice::{Notice, NoticeReceiver};
use super::row::{Row, StreamRow};

use crate::protocol::message::Message;

/// An asynchronous client for Hyper database.
///
/// This is the async equivalent of [`Client`](crate::client::Client), designed for use
/// in tokio-based async applications. All I/O operations are non-blocking.
///
/// # Example
///
/// ```no_run
/// use hyperdb_api_core::client::{AsyncClient, Config};
///
/// #[tokio::main]
/// async fn main() -> hyperdb_api_core::client::Result<()> {
///     let config = Config::new()
///         .with_host("localhost")
///         .with_port(7483)
///         .with_database("test.hyper");
///
///     let client = AsyncClient::connect(&config).await?;
///     let rows = client.query("SELECT 1").await?;
///     client.close().await?;
///     Ok(())
/// }
/// ```
pub struct AsyncClient {
    /// The underlying async connection, protected by a mutex for concurrent access.
    connection: Arc<Mutex<AsyncRawConnection<AsyncStream>>>,
    /// Backend process ID (for cancel requests).
    process_id: i32,
    /// Secret key for authenticating cancel requests.
    secret_key: i32,
    /// Connection endpoint for cancel requests and reconnection.
    endpoint: ConnectionEndpoint,
    /// Optional notice receiver callback for server notices/warnings.
    notice_receiver: Option<Arc<NoticeReceiver>>,
}

impl std::fmt::Debug for AsyncClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AsyncClient")
            .field("process_id", &self.process_id)
            .field("secret_key", &self.secret_key)
            .field("endpoint", &self.endpoint)
            .field(
                "notice_receiver",
                &self.notice_receiver.as_ref().map(|_| "<callback>"),
            )
            .finish_non_exhaustive()
    }
}

impl AsyncClient {
    /// Connects to a Hyper server using the given configuration (async).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use hyperdb_api_core::client::{AsyncClient, Config};
    /// # async fn example() -> hyperdb_api_core::client::Result<()> {
    /// let config = Config::new()
    ///     .with_host("localhost")
    ///     .with_port(7483)
    ///     .with_database("test.hyper");
    ///
    /// let client = AsyncClient::connect(&config).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (connection) if the TCP connection cannot be
    ///   established to `config.host():config.port()`.
    /// - Propagates any [`Error`] from the startup handshake —
    ///   [`Error`] (auth) for missing/wrong credentials,
    ///   [`Error`] (server) for server-side startup errors, [`Error`] (protocol)
    ///   for out-of-sequence messages, or [`Error`] (I/O) for wire
    ///   failure.
    pub async fn connect(config: &Config) -> Result<Self> {
        info!(
            target: "hyperdb_api",
            host = %config.host(),
            port = config.port(),
            user = config.user().unwrap_or("(default)"),
            database = config.database().unwrap_or("(none)"),
            "connection-parameters"
        );

        let endpoint = ConnectionEndpoint::tcp(config.host(), config.port());
        let addr = format!("{}:{}", config.host(), config.port());
        let tcp_stream = TcpStream::connect(&addr).await.map_err(|e| {
            warn!(target: "hyperdb_api", %addr, error = %e, "connection-failed");
            Error::connection(format!("failed to connect to {addr}: {e}"))
        })?;

        // Set TCP options. See the sync mirror in
        // [`super::client::Client::connect`] for the full rationale and
        // empirical knee analysis. We bump `SO_RCVBUF` / `SO_SNDBUF` to
        // 4 MiB (Windows default ~64 KiB throttles loopback throughput;
        // Linux auto-tunes higher).
        tcp_stream.set_nodelay(true).ok();
        let sock = socket2::SockRef::from(&tcp_stream);
        sock.set_recv_buffer_size(4 * 1024 * 1024).ok();
        sock.set_send_buffer_size(4 * 1024 * 1024).ok();

        let stream = AsyncStream::tcp(tcp_stream);
        let mut connection = AsyncRawConnection::new(stream);

        // Perform startup with authentication
        let params = config.startup_params();
        let params_ref: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, *v)).collect();
        connection.startup(&params_ref, config.password()).await?;

        let process_id = connection.process_id();
        let secret_key = connection.secret_key();

        debug!(
            target: "hyperdb_api",
            process_id,
            "connection-established"
        );

        Ok(AsyncClient {
            connection: Arc::new(Mutex::new(connection)),
            process_id,
            secret_key,
            endpoint,
            notice_receiver: None,
        })
    }

    /// Connects to a Hyper server via Unix Domain Socket (async, Unix only).
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use hyperdb_api_core::client::{AsyncClient, Config};
    /// # use std::path::Path;
    /// # async fn example() -> hyperdb_api_core::client::Result<()> {
    /// let socket_path = Path::new("/tmp/hyper/.s.PGSQL.12345");
    /// let config = Config::new().with_database("test.hyper");
    /// let client = AsyncClient::connect_unix(socket_path, &config).await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (connection) if the Unix domain socket cannot
    ///   be connected.
    /// - Propagates any error from the startup handshake (see
    ///   [`Self::connect`]).
    #[cfg(unix)]
    pub async fn connect_unix(
        socket_path: impl AsRef<std::path::Path>,
        config: &Config,
    ) -> Result<Self> {
        use std::path::Path;

        let path = socket_path.as_ref();
        info!(
            target: "hyperdb_api",
            socket_path = %path.display(),
            user = config.user().unwrap_or("(default)"),
            database = config.database().unwrap_or("(none)"),
            "connection-parameters-unix"
        );

        let unix_stream = UnixStream::connect(path).await.map_err(|e| {
            warn!(target: "hyperdb_api", socket_path = %path.display(), error = %e, "connection-failed");
            Error::connection(format!("failed to connect to unix socket {}: {}", path.display(), e))
        })?;

        // Parse endpoint from socket path
        let directory = path.parent().unwrap_or(Path::new("/"));
        let name = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("socket");
        let endpoint = ConnectionEndpoint::domain_socket(directory, name);

        let stream = AsyncStream::unix(unix_stream);
        let mut connection = AsyncRawConnection::new(stream);

        // Perform startup with authentication
        let params = config.startup_params();
        let params_ref: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, *v)).collect();
        connection.startup(&params_ref, config.password()).await?;

        let process_id = connection.process_id();
        let secret_key = connection.secret_key();

        debug!(
            target: "hyperdb_api",
            process_id,
            "connection-established-unix"
        );

        Ok(AsyncClient {
            connection: Arc::new(Mutex::new(connection)),
            process_id,
            secret_key,
            endpoint,
            notice_receiver: None,
        })
    }

    /// Connects to a Hyper server via Windows Named Pipe (async, Windows only).
    ///
    /// # Arguments
    ///
    /// * `pipe_path` - The full pipe path (e.g., `\\.\pipe\hyper-12345`)
    /// * `config` - Connection configuration
    ///
    /// # Errors
    ///
    /// Returns an error if the Named Pipe cannot be opened (e.g., pipe does not
    /// exist, all instances are busy after the retry window, or permission is
    /// denied) or if the authentication handshake fails.
    #[cfg(windows)]
    pub async fn connect_named_pipe(pipe_path: &str, config: &Config) -> Result<Self> {
        use std::time::{Duration, Instant};
        use tokio::net::windows::named_pipe::ClientOptions;

        info!(
            target: "hyperdb_api",
            pipe_path = %pipe_path,
            user = config.user().unwrap_or("(default)"),
            database = config.database().unwrap_or("(none)"),
            "connection-parameters-named-pipe"
        );

        // Retry on `ERROR_PIPE_BUSY` (231) — Windows named pipes have a finite
        // number of server-side instances and concurrent clients can hit the
        // cap. See the sync mirror in [`super::client::Client::connect_named_pipe`]
        // for the full rationale.
        const RETRY_INTERVAL: Duration = Duration::from_millis(20);
        const MAX_WAIT: Duration = Duration::from_secs(10);
        const ERROR_PIPE_BUSY: i32 = 231;

        let deadline = Instant::now() + MAX_WAIT;
        let client = loop {
            match ClientOptions::new().open(pipe_path) {
                Ok(c) => break c,
                Err(e)
                    if e.raw_os_error() == Some(ERROR_PIPE_BUSY) && Instant::now() < deadline =>
                {
                    tokio::time::sleep(RETRY_INTERVAL).await;
                }
                Err(e) => {
                    warn!(target: "hyperdb_api", pipe_path = %pipe_path, error = %e, "connection-failed");
                    return Err(Error::connection(format!(
                        "failed to connect to named pipe {pipe_path}: {e}"
                    )));
                }
            }
        };

        // Parse endpoint from pipe path
        let endpoint = ConnectionEndpoint::parse(&format!(
            "tab.pipe://{}",
            pipe_path.trim_start_matches(r"\\").replace('\\', "/")
        ))
        .unwrap_or_else(|_| {
            let parts: Vec<&str> = pipe_path
                .trim_start_matches(r"\\")
                .splitn(3, '\\')
                .collect();
            if parts.len() >= 3 {
                ConnectionEndpoint::named_pipe(parts[0], parts[2])
            } else {
                ConnectionEndpoint::named_pipe(".", pipe_path)
            }
        });

        let stream = AsyncStream::named_pipe(client);
        let mut connection = AsyncRawConnection::new(stream);

        // Perform startup with authentication
        let params = config.startup_params();
        let params_ref: Vec<(&str, &str)> = params.iter().map(|(k, v)| (*k, *v)).collect();
        connection.startup(&params_ref, config.password()).await?;

        let process_id = connection.process_id();
        let secret_key = connection.secret_key();

        debug!(
            target: "hyperdb_api",
            process_id,
            "connection-established-named-pipe"
        );

        Ok(AsyncClient {
            connection: Arc::new(Mutex::new(connection)),
            process_id,
            secret_key,
            endpoint,
            notice_receiver: None,
        })
    }

    /// Connects to a Hyper server using a `ConnectionEndpoint` (async).
    ///
    /// This is a lower-level method that accepts a pre-parsed endpoint.
    ///
    /// # Errors
    ///
    /// Delegates to [`Self::connect`], [`Self::connect_unix`], or
    /// `Self::connect_named_pipe` depending on the endpoint variant,
    /// and propagates their errors unchanged.
    pub async fn connect_endpoint(endpoint: &ConnectionEndpoint, config: &Config) -> Result<Self> {
        match endpoint {
            ConnectionEndpoint::Tcp { host, port } => {
                let mut cfg = config.clone();
                cfg = cfg.with_host(host.clone()).with_port(*port);
                Self::connect(&cfg).await
            }
            #[cfg(unix)]
            ConnectionEndpoint::DomainSocket { directory, name } => {
                let socket_path = directory.join(name);
                Self::connect_unix(&socket_path, config).await
            }
            #[cfg(windows)]
            ConnectionEndpoint::NamedPipe { host, name } => {
                let pipe_path = format!(r"\\{host}\pipe\{name}");
                Self::connect_named_pipe(&pipe_path, config).await
            }
        }
    }

    /// Returns the connection endpoint.
    #[must_use]
    pub fn endpoint(&self) -> &ConnectionEndpoint {
        &self.endpoint
    }

    /// Returns the server process ID for this connection.
    #[must_use]
    pub fn process_id(&self) -> i32 {
        self.process_id
    }

    /// Returns the secret key for cancel requests.
    #[must_use]
    pub fn secret_key(&self) -> i32 {
        self.secret_key
    }

    /// Cancels the currently executing query on this connection (async).
    ///
    /// This method opens a separate connection to send a cancel request.
    /// For TCP endpoints, it opens a new TCP connection.
    /// For Unix domain sockets, it connects to the same socket path.
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (connection) if a fresh cancel-side socket
    ///   (TCP / UDS / named-pipe) cannot be opened to
    ///   [`Self::endpoint`].
    /// - Returns [`Error`] (I/O) if writing the cancel request fails.
    pub async fn cancel(&self) -> Result<()> {
        use crate::protocol::message::frontend;
        use bytes::BytesMut;
        use tokio::io::AsyncWriteExt;

        info!(
            target: "hyperdb_api",
            process_id = self.process_id,
            "query-cancel-request"
        );

        let endpoint_str = self.endpoint.to_string();

        match &self.endpoint {
            ConnectionEndpoint::Tcp { host, port } => {
                let addr = format!("{host}:{port}");
                let mut stream = TcpStream::connect(&addr).await.map_err(|e| {
                    warn!(
                        target: "hyperdb_api",
                        addr = %endpoint_str,
                        error = %e,
                        "query-cancel-connect-failed"
                    );
                    Error::connection(format!(
                        "failed to connect for cancel request to {endpoint_str}: {e}"
                    ))
                })?;
                // Cancel is a 16-byte fire-and-forget — disable Nagle so the
                // request hits the wire without waiting on a coalesce timer.
                stream.set_nodelay(true).ok();

                let mut buf = BytesMut::new();
                frontend::cancel_request(self.process_id, self.secret_key, &mut buf);

                stream.write_all(&buf).await.map_err(|e| {
                    warn!(
                        target: "hyperdb_api",
                        error = %e,
                        "query-cancel-send-failed"
                    );
                    Error::io(e)
                })?;
            }
            #[cfg(unix)]
            ConnectionEndpoint::DomainSocket { directory, name } => {
                let socket_path = directory.join(name);
                let mut stream = UnixStream::connect(&socket_path).await.map_err(|e| {
                    warn!(
                        target: "hyperdb_api",
                        addr = %endpoint_str,
                        error = %e,
                        "query-cancel-connect-failed"
                    );
                    Error::connection(format!(
                        "failed to connect for cancel request to {endpoint_str}: {e}"
                    ))
                })?;

                let mut buf = BytesMut::new();
                frontend::cancel_request(self.process_id, self.secret_key, &mut buf);

                stream.write_all(&buf).await.map_err(|e| {
                    warn!(
                        target: "hyperdb_api",
                        error = %e,
                        "query-cancel-send-failed"
                    );
                    Error::io(e)
                })?;
            }
            #[cfg(windows)]
            ConnectionEndpoint::NamedPipe { host, name } => {
                let pipe_path = format!(r"\\{host}\pipe\{name}");
                // Use sync file I/O for cancel (short-lived connection)
                let mut file = std::fs::OpenOptions::new()
                    .read(true)
                    .write(true)
                    .open(&pipe_path)
                    .map_err(|e| {
                        warn!(
                            target: "hyperdb_api",
                            addr = %endpoint_str,
                            error = %e,
                            "query-cancel-connect-failed"
                        );
                        Error::connection(format!(
                            "failed to connect for cancel request to {endpoint_str}: {e}"
                        ))
                    })?;

                let mut buf = BytesMut::new();
                frontend::cancel_request(self.process_id, self.secret_key, &mut buf);

                use std::io::Write;
                file.write_all(&buf).map_err(|e| {
                    warn!(
                        target: "hyperdb_api",
                        error = %e,
                        "query-cancel-send-failed"
                    );
                    Error::io(e)
                })?;

                file.flush().map_err(Error::io)?;
            }
        }

        debug!(target: "hyperdb_api", "query-cancel-sent");
        Ok(())
    }

    /// Executes a query and returns all result rows (async).
    ///
    /// # Errors
    ///
    /// Propagates any [`Error`] from the underlying connection's
    /// [`AsyncRawConnection::simple_query`] — [`Error`] (server) for
    /// server-side SQL errors, [`Error`] (I/O) / [`Error`] (closed) for
    /// transport failures, and [`Error`] (connection) if the connection
    /// is unhealthy. Row construction may also raise an [`Error`] when
    /// a `DataRow` cannot be decoded against its `RowDescription`.
    pub async fn query(&self, sql: &str) -> Result<Vec<Row>> {
        let mut conn = self.connection.lock().await;
        let messages = conn.simple_query(sql).await?;
        Self::process_query_messages(messages, self.notice_receiver.as_ref())
    }

    /// Executes a query with `HyperBinary` format for better performance (async).
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::query`].
    pub async fn query_fast(&self, sql: &str) -> Result<Vec<StreamRow>> {
        let mut conn = self.connection.lock().await;
        let messages = conn.query_binary(sql).await?;
        Ok(Self::process_binary_messages(
            messages,
            self.notice_receiver.as_ref(),
        ))
    }

    /// Executes a query with `HyperBinary` format and returns a streaming
    /// result reader (async).
    ///
    /// This is the async mirror of
    /// [`Client::query_streaming`](super::client::Client::query_streaming).
    /// The returned [`AsyncQueryStream`] yields rows in chunks so callers
    /// can process arbitrarily large result sets with constant memory. The
    /// connection mutex is held for the duration of iteration; dropping the
    /// stream before completion issues a best-effort cancel and marks the
    /// connection desynchronized.
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (connection) if the connection is unhealthy.
    /// - Returns [`Error`] (I/O) if writing the Parse/Bind/Execute/Sync
    ///   sequence fails on the transport.
    pub async fn query_streaming(
        &self,
        sql: &str,
        chunk_size: usize,
    ) -> Result<AsyncQueryStream<'_>> {
        let mut conn = self.connection.lock().await;
        conn.start_query_binary(sql).await?;
        Ok(AsyncQueryStream::new(conn, self, chunk_size))
    }

    /// Sends a best-effort `CancelRequest` using *synchronous* I/O so it
    /// is usable from [`Drop`] impls (notably
    /// [`AsyncQueryStream::drop`](super::async_stream_query::AsyncQueryStream)).
    ///
    /// Cancellation opens a short-lived TCP / UDS / Named-Pipe connection,
    /// writes the cancel packet, and drops it — the server recognizes the
    /// (`process_id`, `secret_key`) tuple and signals the long-running query
    /// to abort. No response is expected.
    fn cancel_sync(&self) -> Result<()> {
        use crate::protocol::message::frontend;
        use bytes::BytesMut;
        use std::io::Write;

        info!(
            target: "hyperdb_api",
            process_id = self.process_id,
            "query-cancel-request"
        );

        let endpoint_str = self.endpoint.to_string();

        match &self.endpoint {
            ConnectionEndpoint::Tcp { host, port } => {
                let addr = format!("{host}:{port}");
                let mut stream = std::net::TcpStream::connect(&addr).map_err(|e| {
                    warn!(
                        target: "hyperdb_api",
                        addr = %endpoint_str,
                        error = %e,
                        "query-cancel-connect-failed"
                    );
                    Error::connection(format!(
                        "failed to connect for cancel request to {endpoint_str}: {e}"
                    ))
                })?;
                // Cancel is a 16-byte fire-and-forget — disable Nagle so the
                // request hits the wire without waiting on a coalesce timer.
                stream.set_nodelay(true).ok();

                let mut buf = BytesMut::with_capacity(16);
                frontend::cancel_request(self.process_id, self.secret_key, &mut buf);

                stream.write_all(&buf).map_err(Error::io)?;
                stream.flush().map_err(Error::io)?;
            }
            #[cfg(unix)]
            ConnectionEndpoint::DomainSocket { directory, name } => {
                let socket_path = directory.join(name);
                let mut stream =
                    std::os::unix::net::UnixStream::connect(&socket_path).map_err(|e| {
                        warn!(
                            target: "hyperdb_api",
                            addr = %endpoint_str,
                            error = %e,
                            "query-cancel-connect-failed"
                        );
                        Error::connection(format!(
                            "failed to connect for cancel request to {endpoint_str}: {e}"
                        ))
                    })?;

                let mut buf = BytesMut::with_capacity(16);
                frontend::cancel_request(self.process_id, self.secret_key, &mut buf);

                stream.write_all(&buf).map_err(Error::io)?;
                stream.flush().map_err(Error::io)?;
            }
            #[cfg(windows)]
            ConnectionEndpoint::NamedPipe { host, name } => {
                let pipe_path = format!(r"\\{host}\pipe\{name}");
                let mut file = std::fs::OpenOptions::new()
                    .read(true)
                    .write(true)
                    .open(&pipe_path)
                    .map_err(|e| {
                        warn!(
                            target: "hyperdb_api",
                            addr = %endpoint_str,
                            error = %e,
                            "query-cancel-connect-failed"
                        );
                        Error::connection(format!(
                            "failed to connect for cancel request to {endpoint_str}: {e}"
                        ))
                    })?;

                let mut buf = BytesMut::with_capacity(16);
                frontend::cancel_request(self.process_id, self.secret_key, &mut buf);

                file.write_all(&buf).map_err(Error::io)?;
                file.flush().map_err(Error::io)?;
            }
        }

        debug!(target: "hyperdb_api", "query-cancel-sent");
        Ok(())
    }

    /// Executes a command (INSERT/UPDATE/DELETE/DDL) and returns affected row count (async).
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::query`] — server-side SQL errors,
    /// transport failures, and unhealthy-connection state all surface
    /// as [`Error`].
    pub async fn exec(&self, sql: &str) -> Result<u64> {
        let mut conn = self.connection.lock().await;
        let messages = conn.simple_query(sql).await?;
        Ok(Self::extract_row_count(&messages))
    }

    /// Returns a server parameter value by name.
    pub async fn parameter_status(&self, name: &str) -> Option<String> {
        let conn = self.connection.lock().await;
        conn.parameter_status(name)
            .map(std::string::ToString::to_string)
    }

    /// Sets the notice receiver callback.
    pub fn set_notice_receiver(&mut self, receiver: Option<Box<dyn Fn(Notice) + Send + Sync>>) {
        self.notice_receiver = receiver.map(Arc::from);
    }

    /// Closes the connection gracefully (async).
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (I/O) if writing the `Terminate` frame or
    /// flushing the async transport fails.
    pub async fn close(self) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.terminate().await
    }

    /// Executes a batch of statements separated by semicolons (async).
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::query`].
    pub async fn batch_execute(&self, sql: &str) -> Result<()> {
        let mut conn = self.connection.lock().await;
        let _messages = conn.simple_query(sql).await?;
        Ok(())
    }

    /// Starts a COPY IN operation for bulk data insertion (async).
    ///
    /// # Errors
    ///
    /// Delegates to [`Self::copy_in_with_format`]; see that method
    /// for concrete failure modes.
    pub async fn copy_in(
        &self,
        table_name: &str,
        columns: &[&str],
    ) -> Result<AsyncCopyInWriter<'_>> {
        self.copy_in_with_format(table_name, columns, "HYPERBINARY")
            .await
    }

    /// Starts a COPY IN operation and returns an owned-handle writer
    /// whose lifetime is independent of this client. The writer holds an
    /// `Arc`-cloned reference to the underlying connection mutex, so it
    /// can be stored in structs that need a `'static`-lifetime writer —
    /// e.g. N-API classes that can't carry borrowed references across
    /// JS callbacks.
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::copy_in_with_format`].
    pub async fn copy_in_arc_with_format(
        &self,
        table_name: &str,
        columns: &[&str],
        format: &str,
    ) -> Result<AsyncCopyInWriterOwned> {
        let mut conn = self.connection.lock().await;
        conn.start_copy_in_with_format(table_name, columns, format)
            .await?;
        drop(conn);
        Ok(AsyncCopyInWriterOwned::new(Arc::clone(&self.connection)))
    }

    /// Starts a COPY IN operation with a specified data format (async).
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (connection) if the connection is unhealthy.
    /// - Returns [`Error`] (server) if the server rejects the generated
    ///   `COPY ... FROM STDIN` statement.
    /// - Returns [`Error`] (I/O) on transport read/write failure.
    pub async fn copy_in_with_format(
        &self,
        table_name: &str,
        columns: &[&str],
        format: &str,
    ) -> Result<AsyncCopyInWriter<'_>> {
        let mut conn = self.connection.lock().await;
        conn.start_copy_in_with_format(table_name, columns, format)
            .await?;
        drop(conn);
        Ok(AsyncCopyInWriter::new(&self.connection))
    }

    /// Executes a COPY ... TO STDOUT query and returns all output data (async).
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (connection) if the connection is unhealthy.
    /// - Returns [`Error`] (server) when the server rejects the statement.
    /// - Returns [`Error`] (I/O) / [`Error`] (closed) on transport
    ///   read/write failure.
    pub async fn copy_out(&self, query: &str) -> Result<Vec<u8>> {
        let mut conn = self.connection.lock().await;
        conn.copy_out(query).await
    }

    /// Returns true if the connection is alive.
    #[must_use]
    pub fn is_alive(&self) -> bool {
        // Try to acquire the lock - if we can, connection is alive
        self.connection.try_lock().is_ok()
    }

    /// Prepares a statement for execution (async).
    ///
    /// # Errors
    ///
    /// Delegates to [`Self::prepare_typed`]; see that method for the
    /// failure modes.
    pub async fn prepare(&self, query: &str) -> Result<AsyncPreparedStatement> {
        self.prepare_typed(query, &[]).await
    }

    /// Prepares a statement with explicit parameter types (async).
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (connection) if the connection is unhealthy.
    /// - Returns [`Error`] (server) if the server rejects the `Parse`
    ///   request (SQL syntax, unknown parameter OIDs, etc.).
    /// - Returns [`Error`] (I/O) on transport read/write failure.
    pub async fn prepare_typed(
        &self,
        query: &str,
        param_types: &[crate::types::Oid],
    ) -> Result<AsyncPreparedStatement> {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);

        let name = format!(
            "__hyper_async_stmt_{}",
            COUNTER.fetch_add(1, Ordering::Relaxed)
        );
        let mut conn = self.connection.lock().await;
        let (params, columns) = conn.prepare(&name, query, param_types).await?;

        Ok(AsyncPreparedStatement {
            name,
            query: query.to_string(),
            param_types: params,
            columns,
            connection: Arc::downgrade(&self.connection),
            closed: false,
        })
    }

    /// Closes a prepared statement on the server (async).
    ///
    /// Prefer the RAII [`AsyncPreparedStatement::close`] method on the
    /// statement itself — it consumes the statement and prevents the
    /// auto-close Drop path from double-closing.
    ///
    /// # Errors
    ///
    /// Propagates any error from
    /// [`AsyncRawConnection::close_statement`] — unhealthy connection,
    /// server-side error during `Close`/`Sync`, or transport failure.
    pub async fn close_statement(&self, statement: &AsyncPreparedStatement) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.close_statement(&statement.name).await
    }

    /// Executes a prepared statement with parameters (async).
    ///
    /// # Errors
    ///
    /// Propagates any error from
    /// [`AsyncRawConnection::execute_prepared`] — unhealthy connection,
    /// parameter/type mismatch, server-side execution failure, or
    /// transport failure. Row construction may also raise an [`Error`]
    /// when a `DataRow` cannot be decoded.
    pub async fn execute_prepared<P: AsRef<[Option<Vec<u8>>]>>(
        &self,
        statement: &AsyncPreparedStatement,
        params: P,
    ) -> Result<Vec<Row>> {
        let params_ref: Vec<Option<&[u8]>> = params
            .as_ref()
            .iter()
            .map(|p| p.as_ref().map(std::vec::Vec::as_slice))
            .collect();

        let mut conn = self.connection.lock().await;
        conn.execute_prepared(&statement.name, &params_ref, statement.columns.len())
            .await
    }

    /// Executes a prepared statement that doesn't return rows (async).
    ///
    /// # Errors
    ///
    /// Same failure modes as [`Self::execute_prepared`] (excluding
    /// row-construction errors — this path never builds rows).
    pub async fn execute_prepared_no_result<P: AsRef<[Option<Vec<u8>>]>>(
        &self,
        statement: &AsyncPreparedStatement,
        params: P,
    ) -> Result<u64> {
        let params_ref: Vec<Option<&[u8]>> = params
            .as_ref()
            .iter()
            .map(|p| p.as_ref().map(std::vec::Vec::as_slice))
            .collect();

        let mut conn = self.connection.lock().await;
        conn.execute_prepared_no_result(&statement.name, &params_ref)
            .await
    }

    /// Executes a prepared statement with streaming results (async).
    ///
    /// Returns an [`AsyncPreparedQueryStream`](super::async_prepared_stream::AsyncPreparedQueryStream)
    /// that yields rows in chunks, keeping memory bounded regardless of
    /// result size. Async mirror of
    /// [`Client::execute_streaming`](crate::client::Client::execute_streaming).
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (connection) if the connection is unhealthy.
    /// - Returns [`Error`] (I/O) if writing the initial Bind/Execute/Sync
    ///   sequence fails on the transport.
    pub async fn execute_prepared_streaming<'a, P: AsRef<[Option<Vec<u8>>]>>(
        &'a self,
        statement: &AsyncPreparedStatement,
        params: P,
        chunk_size: usize,
    ) -> Result<super::async_prepared_stream::AsyncPreparedQueryStream<'a>> {
        let params_ref: Vec<Option<&[u8]>> = params
            .as_ref()
            .iter()
            .map(|p| p.as_ref().map(std::vec::Vec::as_slice))
            .collect();

        let mut conn = self.connection.lock().await;
        conn.start_execute_prepared(&statement.name, &params_ref, statement.columns.len())
            .await?;

        let columns = std::sync::Arc::new(statement.columns.clone());
        Ok(super::async_prepared_stream::AsyncPreparedQueryStream::new(
            conn, self, chunk_size, columns,
        ))
    }
}

impl Cancellable for AsyncClient {
    /// Fire-and-forget cancel via PG wire protocol `CancelRequest` on a
    /// fresh connection. Uses synchronous I/O so it is callable from
    /// `Drop` impls. Errors are logged and swallowed because cancellation
    /// is best-effort and callers cannot meaningfully recover.
    fn cancel(&self) {
        if let Err(e) = AsyncClient::cancel_sync(self) {
            warn!(
                target: "hyperdb_api_core::client",
                error = %e,
                process_id = self.process_id,
                "cancel request failed (best-effort, swallowed)",
            );
        }
    }
}

impl AsyncClient {
    fn process_query_messages(
        messages: Vec<Message>,
        notice_receiver: Option<&Arc<NoticeReceiver>>,
    ) -> Result<Vec<Row>> {
        use super::statement::{Column, ColumnFormat};

        let mut rows = Vec::new();
        let mut columns: Option<Arc<Vec<Column>>> = None;

        for msg in messages {
            match msg {
                Message::RowDescription(desc) => {
                    let mut cols = Vec::new();
                    for field in desc.fields().filter_map(std::result::Result::ok) {
                        cols.push(Column::new(
                            field.name().to_string(),
                            field.type_oid(),
                            field.type_modifier(),
                            ColumnFormat::from_code(field.format()),
                        ));
                    }
                    columns = Some(Arc::new(cols));
                }
                Message::DataRow(data) => {
                    if let Some(ref cols) = columns {
                        rows.push(Row::new(Arc::clone(cols), data)?);
                    }
                }
                Message::NoticeResponse(body) => {
                    if let Some(receiver) = notice_receiver {
                        let notice = Notice::from_response_body(&body);
                        receiver(notice);
                    }
                }
                _ => {}
            }
        }
        Ok(rows)
    }

    fn process_binary_messages(
        messages: Vec<Message>,
        notice_receiver: Option<&Arc<NoticeReceiver>>,
    ) -> Vec<StreamRow> {
        let mut rows = Vec::new();

        for msg in messages {
            match msg {
                Message::DataRow(data) => {
                    rows.push(StreamRow::new(data));
                }
                Message::NoticeResponse(body) => {
                    if let Some(receiver) = notice_receiver {
                        let notice = Notice::from_response_body(&body);
                        receiver(notice);
                    }
                }
                _ => {}
            }
        }
        rows
    }

    fn extract_row_count(messages: &[Message]) -> u64 {
        for msg in messages {
            if let Message::CommandComplete(body) = msg {
                if let Ok(tag) = body.tag() {
                    // Parse formats like "INSERT 0 5", "UPDATE 10", "DELETE 3"
                    let parts: Vec<&str> = tag.split_whitespace().collect();
                    if let Some(last) = parts.last() {
                        if let Ok(count) = last.parse() {
                            return count;
                        }
                    }
                }
            }
        }
        0
    }
}

/// An async prepared statement.
///
/// Represents a server-side prepared statement that can be executed
/// multiple times with different parameters. **Auto-closes on `Drop`**
/// via a best-effort `tokio::spawn` task — if no tokio runtime is
/// available at drop time we log a warning and flag the connection
/// desynchronized rather than silently leaking the server-side
/// statement slot.
///
/// For callers who need confirmed close with error propagation, use
/// [`AsyncPreparedStatement::close`] (explicit async close).
#[derive(Debug)]
pub struct AsyncPreparedStatement {
    /// Statement name on the server.
    pub(crate) name: String,
    /// Original SQL query string.
    query: String,
    /// Parameter type OIDs.
    param_types: Vec<crate::types::Oid>,
    /// Result column descriptions.
    pub(crate) columns: Vec<super::statement::Column>,
    /// Weak handle to the owning connection for the Drop path. `Weak`
    /// so that a lingering statement never keeps the connection alive
    /// past the `AsyncClient` it was prepared against.
    connection: std::sync::Weak<Mutex<AsyncRawConnection<AsyncStream>>>,
    /// Flipped by `close(self)` to suppress the Drop-path auto-close.
    closed: bool,
}

impl AsyncPreparedStatement {
    /// Returns the statement name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the original query.
    #[must_use]
    pub fn query(&self) -> &str {
        &self.query
    }

    /// Returns the parameter types.
    #[must_use]
    pub fn param_types(&self) -> &[crate::types::Oid] {
        &self.param_types
    }

    /// Returns the number of parameters.
    #[must_use]
    pub fn param_count(&self) -> usize {
        self.param_types.len()
    }

    /// Returns the result column descriptions.
    #[must_use]
    pub fn columns(&self) -> &[super::statement::Column] {
        &self.columns
    }

    /// Returns the number of result columns.
    #[must_use]
    pub fn column_count(&self) -> usize {
        self.columns.len()
    }

    /// Explicitly closes the prepared statement on the server (async).
    ///
    /// Consumes the statement — no further `execute_prepared` /
    /// `execute_prepared_streaming` calls are possible — and suppresses
    /// the Drop-path auto-close. Returns the `close_statement` result so
    /// callers can observe any transport errors.
    ///
    /// If you don't need error propagation, simply dropping the
    /// statement has the same effect (best-effort auto-close via
    /// `tokio::spawn`).
    ///
    /// # Errors
    ///
    /// Propagates any error from
    /// [`AsyncClient::close_statement`] — unhealthy connection,
    /// server-side error during `Close`/`Sync`, or transport failure.
    pub async fn close(mut self, client: &AsyncClient) -> Result<()> {
        self.closed = true;
        client.close_statement(&self).await
    }
}

impl Drop for AsyncPreparedStatement {
    fn drop(&mut self) {
        // Explicit close already ran — nothing to do.
        if self.closed {
            return;
        }

        // Best-effort close. Try to grab a tokio handle; if we're being
        // dropped outside a runtime (e.g. the runtime has already shut
        // down or the caller never had one), fall back to a warning and
        // flag the connection desynchronized so the next operation fails
        // loudly rather than racing with a lingering statement.
        let Some(conn) = self.connection.upgrade() else {
            // Connection has already been dropped — nothing to close.
            return;
        };

        let name = std::mem::take(&mut self.name);

        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            handle.spawn(async move {
                let mut c = conn.lock().await;
                if let Err(e) = c.close_statement(&name).await {
                    warn!(
                        target: "hyperdb_api_core::client",
                        statement = %name,
                        error = %e,
                        "AsyncPreparedStatement drop-close failed (best-effort, swallowed)"
                    );
                }
            });
        } else {
            // No runtime — can't do async I/O from here. Mark the
            // connection desynchronized so the next caller gets a
            // clear error instead of a latent leaked statement.
            if let Ok(mut c) = conn.try_lock() {
                c.mark_desynchronized();
            }
            warn!(
                target: "hyperdb_api_core::client",
                statement = %name,
                "AsyncPreparedStatement dropped outside of a tokio runtime; \
                 server-side statement slot leaked and connection marked \
                 desynchronized — call statement.close(&client) explicitly \
                 for deterministic cleanup"
            );
        }
    }
}

/// Async COPY IN writer for bulk data insertion.
///
/// # Drop Safety
///
/// If this writer is dropped without calling [`finish()`](Self::finish) or
/// [`cancel()`](Self::cancel), it will attempt a best-effort synchronous cancel
/// by queuing a `CopyFail` message in the connection's write buffer. The next
/// async operation on the connection will flush and drain the cancel response,
/// restoring the connection to a usable state.
#[derive(Debug)]
pub struct AsyncCopyInWriter<'a> {
    connection: &'a Mutex<AsyncRawConnection<AsyncStream>>,
    /// Set to `true` after `finish()` or `cancel()` consumes the writer.
    /// Checked in `Drop` to avoid queuing a spurious `CopyFail`.
    finished: bool,
}

/// Owned-handle variant of [`AsyncCopyInWriter`] that holds an
/// `Arc<Mutex<_>>` to the underlying connection instead of a borrow.
/// Used by callers that need a `'static`-lifetime writer — e.g. N-API
/// classes that can't carry borrowed references across JS callbacks.
///
/// Semantics are identical to [`AsyncCopyInWriter`]; the only
/// difference is lifetime.
#[derive(Debug)]
pub struct AsyncCopyInWriterOwned {
    connection: Arc<Mutex<AsyncRawConnection<AsyncStream>>>,
    finished: bool,
}

impl AsyncCopyInWriterOwned {
    /// Creates a new owned-handle COPY IN writer.
    pub(crate) fn new(connection: Arc<Mutex<AsyncRawConnection<AsyncStream>>>) -> Self {
        AsyncCopyInWriterOwned {
            connection,
            finished: false,
        }
    }

    /// Sends data to the server.
    ///
    /// # Errors
    ///
    /// Currently infallible — frame construction is pure. The `Result`
    /// return type is preserved for forward compatibility.
    pub async fn send(&mut self, data: &[u8]) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.send_copy_data(data)?;
        Ok(())
    }

    /// Flushes any buffered data to the server.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (I/O) if flushing the async transport fails.
    pub async fn flush(&mut self) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.flush().await
    }

    /// Sends COPY data directly to the stream without internal buffering.
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (protocol) if `data.len() + 4` exceeds
    ///   `u32::MAX`.
    /// - Returns [`Error`] (I/O) on transport write failure.
    pub async fn send_direct(&mut self, data: &[u8]) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.send_copy_data_direct(data).await
    }

    /// Flushes the TCP stream.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (I/O) if flushing the async transport fails.
    pub async fn flush_stream(&mut self) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.flush_stream().await
    }

    /// Finishes the COPY operation and returns the number of rows inserted.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (server) if the server reports an `ErrorResponse`
    /// (e.g. constraint violation), or [`Error`] (I/O) / [`Error`] (closed)
    /// on transport failure.
    pub async fn finish(mut self) -> Result<u64> {
        self.finished = true;
        let mut conn = self.connection.lock().await;
        conn.finish_copy().await
    }

    /// Cancels the COPY operation.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (I/O) on transport write failure, or
    /// [`Error`] (closed) if the server drops the connection before
    /// acknowledging the cancel.
    pub async fn cancel(mut self, reason: &str) -> Result<()> {
        self.finished = true;
        let mut conn = self.connection.lock().await;
        conn.cancel_copy(reason).await
    }
}

impl Drop for AsyncCopyInWriterOwned {
    fn drop(&mut self) {
        if self.finished {
            return;
        }
        // Mirror AsyncCopyInWriter's Drop — queue a CopyFail via
        // try_lock; the next async operation on the connection will
        // flush and drain.
        if let Ok(mut conn) = self.connection.try_lock() {
            conn.queue_copy_fail("AsyncCopyInWriterOwned dropped without finish/cancel");
        }
    }
}

impl<'a> AsyncCopyInWriter<'a> {
    /// Creates a new COPY IN writer.
    pub(crate) fn new(connection: &'a Mutex<AsyncRawConnection<AsyncStream>>) -> Self {
        AsyncCopyInWriter {
            connection,
            finished: false,
        }
    }

    /// Sends data to the server.
    ///
    /// # Errors
    ///
    /// Currently infallible — frame construction is pure. The `Result`
    /// return type is preserved for forward compatibility.
    pub async fn send(&mut self, data: &[u8]) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.send_copy_data(data)?;
        Ok(())
    }

    /// Flushes any buffered data to the server.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (I/O) if flushing the async transport fails.
    pub async fn flush(&mut self) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.flush().await
    }

    /// Sends COPY data directly to the stream without internal buffering.
    ///
    /// This writes data directly to the TCP stream, letting the kernel handle
    /// buffering. More efficient for streaming large amounts of data.
    /// Call `flush_stream()` periodically to ensure data is sent.
    ///
    /// # Errors
    ///
    /// - Returns [`Error`] (protocol) if `data.len() + 4` exceeds
    ///   `u32::MAX`.
    /// - Returns [`Error`] (I/O) on transport write failure.
    pub async fn send_direct(&mut self, data: &[u8]) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.send_copy_data_direct(data).await
    }

    /// Flushes the TCP stream.
    ///
    /// Use with `send_direct()` to periodically ensure data reaches the server.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (I/O) if flushing the async transport fails.
    pub async fn flush_stream(&mut self) -> Result<()> {
        let mut conn = self.connection.lock().await;
        conn.flush_stream().await
    }

    /// Finishes the COPY operation and returns the number of rows inserted.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (server) if the server reports an `ErrorResponse`,
    /// or [`Error`] (I/O) / [`Error`] (closed) on transport failure.
    pub async fn finish(mut self) -> Result<u64> {
        self.finished = true;
        let mut conn = self.connection.lock().await;
        conn.finish_copy().await
    }

    /// Cancels the COPY operation.
    ///
    /// # Errors
    ///
    /// Returns [`Error`] (I/O) on transport write failure, or
    /// [`Error`] (closed) if the server drops the connection before
    /// acknowledging the cancel.
    pub async fn cancel(mut self, reason: &str) -> Result<()> {
        self.finished = true;
        let mut conn = self.connection.lock().await;
        conn.cancel_copy(reason).await
    }
}

impl Drop for AsyncCopyInWriter<'_> {
    fn drop(&mut self) {
        if self.finished {
            return;
        }
        // Best-effort cancel: try to acquire the mutex synchronously and
        // queue a CopyFail message. We cannot do async I/O from Drop, so
        // the message is only written to the buffer — not flushed. The next
        // async operation will call drain_pending_copy_cancel() to flush it
        // and restore the connection to ReadyForQuery state.
        if let Ok(mut conn) = self.connection.try_lock() {
            conn.queue_copy_fail("COPY writer dropped without finish or cancel");
            warn!(
                target: "hyperdb_api_core::client",
                "AsyncCopyInWriter dropped without finish() or cancel(). \
                 Queued best-effort CopyFail — connection will self-heal on next operation."
            );
        } else {
            warn!(
                target: "hyperdb_api_core::client",
                "AsyncCopyInWriter dropped without finish() or cancel(), \
                 and the connection mutex was locked. The connection may be \
                 left in an unusable COPY-IN state."
            );
        }
    }
}