wasi-pg-client 0.1.2

PostgreSQL client library for WASI Preview 2
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
//! Streaming API for query results.
//!
//! This module provides [`RowStream`] — an async stream of rows from a query
//! result that processes rows one at a time without buffering the entire
//! result set in memory.

use std::sync::Arc;

use crate::protocol::{BackendMessage, TransactionStatus};

use crate::connection::{Connection, ConnectionState};
use crate::error::{PgError, PgServerError, Result};
use crate::query::result::CommandTag;
use crate::query::row::{FieldDescription, Row};
use crate::query::{read_data_row, read_row_description};

#[cfg(feature = "tracing")]
use crate::tracing_ext::TARGET_QUERY;

/// Internal state of the row stream.
#[derive(Debug)]
enum RowStreamState {
    WaitingForDescription,
    ReceivingRows,
    Finishing { command_tag: CommandTag },
    Done { command_tag: CommandTag },
    Error,
}

/// An async stream of rows from a query result.
///
/// Rows are fetched from the server one at a time as the consumer calls `next()`.
/// This provides natural backpressure and O(1) memory per row.
///
/// `RowStream` borrows the connection mutably. You cannot use the connection
/// while iterating the stream. When the stream is dropped (or fully consumed),
/// the connection is available again.
///
/// If the stream is dropped before being fully consumed, the connection is
/// left in an inconsistent state. The [`Connection::needs_recovery`] flag is
/// set, and you must call [`Connection::recover`] before using the connection
/// again. To avoid this, either consume the stream fully or call
/// [`RowStream::consume`] before dropping.
#[non_exhaustive]
pub struct RowStream<'a> {
    conn: &'a mut Connection,
    columns: Option<Arc<Vec<FieldDescription>>>,
    state: RowStreamState,
    /// Whether this stream was created via the extended query protocol.
    /// Used to determine protocol-specific behavior (e.g., portal-based
    /// fetch with `max_rows` for cursor streaming in a future iteration).
    #[allow(dead_code)]
    extended_protocol: bool,
    /// Number of rows fetched so far (for tracing).
    rows_fetched: u64,
    /// Time when the stream was created (for tracing duration).
    #[cfg(feature = "tracing")]
    started_at: std::time::Instant,
}

impl<'a> RowStream<'a> {
    /// Create a new `RowStream` for a simple query.
    pub(crate) fn new_simple(conn: &'a mut Connection) -> Self {
        RowStream {
            conn,
            columns: None,
            state: RowStreamState::WaitingForDescription,
            extended_protocol: false,
            rows_fetched: 0,
            #[cfg(feature = "tracing")]
            started_at: std::time::Instant::now(),
        }
    }

    /// Create a new `RowStream` for an extended query.
    #[cfg_attr(not(test), allow(dead_code))]
    pub(crate) fn new_extended(conn: &'a mut Connection) -> Self {
        RowStream {
            conn,
            columns: None,
            state: RowStreamState::WaitingForDescription,
            extended_protocol: true,
            rows_fetched: 0,
            #[cfg(feature = "tracing")]
            started_at: std::time::Instant::now(),
        }
    }

    /// Create a new `RowStream` for an extended query with known columns.
    pub(crate) fn new_extended_with_columns(
        conn: &'a mut Connection,
        columns: Arc<Vec<FieldDescription>>,
    ) -> Self {
        RowStream {
            conn,
            columns: Some(columns),
            state: RowStreamState::WaitingForDescription,
            extended_protocol: true,
            rows_fetched: 0,
            #[cfg(feature = "tracing")]
            started_at: std::time::Instant::now(),
        }
    }

    /// Create a new `RowStream` for an extended query where the preamble
    /// (ParseComplete, BindComplete, RowDescription/NoData) has already
    /// been consumed. The stream starts directly in `ReceivingRows` state.
    pub(crate) fn new_extended_receiving(
        conn: &'a mut Connection,
        columns: Option<Arc<Vec<FieldDescription>>>,
    ) -> Self {
        RowStream {
            conn,
            columns,
            state: RowStreamState::ReceivingRows,
            extended_protocol: true,
            rows_fetched: 0,
            #[cfg(feature = "tracing")]
            started_at: std::time::Instant::now(),
        }
    }

    /// Fetch the next row from the stream.
    ///
    /// Returns `Ok(Some(row))` when a row is available, `Ok(None)` when the
    /// stream is exhausted, and `Err(...)` on a server or protocol error.
    /// After returning `None` or `Err`, subsequent calls return `None`.
    #[must_use = "stream errors should be checked"]
    pub async fn next(&mut self) -> Result<Option<Row>> {
        loop {
            match self.state {
                RowStreamState::Done { .. } | RowStreamState::Error => {
                    return Ok(None);
                }

                RowStreamState::WaitingForDescription => {
                    let msg = self
                        .conn
                        .codec
                        .read_message(&mut self.conn.transport)
                        .await?;
                    match msg {
                        BackendMessage::RowDescription(body) => {
                            self.columns = Some(Arc::new(read_row_description(body)?));
                            self.state = RowStreamState::ReceivingRows;
                        }
                        BackendMessage::CommandComplete(body) => {
                            let tag = CommandTag::new(body.tag().unwrap_or("").into());
                            self.state = RowStreamState::Finishing { command_tag: tag };
                        }
                        BackendMessage::EmptyQueryResponse => {
                            self.state = RowStreamState::Finishing {
                                command_tag: CommandTag::default(),
                            };
                        }
                        BackendMessage::ErrorResponse(body) => {
                            let server_err =
                                PgServerError::from_error_body(&body).map_err(PgError::Io)?;
                            self.conn.read_until_ready().await?;
                            self.conn.state = ConnectionState::Idle;
                            self.state = RowStreamState::Error;
                            return Err(PgError::Server(Box::new(server_err)));
                        }
                        BackendMessage::NotificationResponse(body) => {
                            self.conn.notification_queue.push_back(
                                crate::notification::Notification {
                                    process_id: body.process_id(),
                                    channel: body.channel().unwrap_or("").to_string(),
                                    payload: body.message().unwrap_or("").to_string(),
                                },
                            );
                            continue;
                        }
                        BackendMessage::NoticeResponse(body) => {
                            if let Ok(notice) = crate::query::Notice::from_fields(&body) {
                                self.conn.handle_notice(&notice);
                            }
                            continue;
                        }
                        BackendMessage::ParameterStatus(body) => {
                            if let (Ok(name), Ok(value)) = (body.name(), body.value()) {
                                self.conn
                                    .server_params
                                    .params
                                    .insert(name.to_string(), value.to_string());
                            }
                            continue;
                        }
                        BackendMessage::ParseComplete
                        | BackendMessage::BindComplete
                        | BackendMessage::NoData => {
                            continue;
                        }
                        _ => continue,
                    }
                }

                RowStreamState::ReceivingRows => {
                    let msg = self
                        .conn
                        .codec
                        .read_message(&mut self.conn.transport)
                        .await?;
                    match msg {
                        BackendMessage::DataRow(body) => {
                            let values = read_data_row(body)?;
                            let cols = self.columns.clone().unwrap_or_default();
                            self.rows_fetched += 1;
                            return Ok(Some(Row::new(cols, values)));
                        }
                        BackendMessage::CommandComplete(body) => {
                            let tag = CommandTag::new(body.tag().unwrap_or("").into());
                            self.state = RowStreamState::Finishing { command_tag: tag };
                        }
                        BackendMessage::ErrorResponse(body) => {
                            let server_err =
                                PgServerError::from_error_body(&body).map_err(PgError::Io)?;
                            self.conn.read_until_ready().await?;
                            self.conn.state = ConnectionState::Idle;
                            self.state = RowStreamState::Error;
                            return Err(PgError::Server(Box::new(server_err)));
                        }
                        BackendMessage::NotificationResponse(body) => {
                            self.conn.notification_queue.push_back(
                                crate::notification::Notification {
                                    process_id: body.process_id(),
                                    channel: body.channel().unwrap_or("").to_string(),
                                    payload: body.message().unwrap_or("").to_string(),
                                },
                            );
                            continue;
                        }
                        BackendMessage::NoticeResponse(body) => {
                            if let Ok(notice) = crate::query::Notice::from_fields(&body) {
                                self.conn.handle_notice(&notice);
                            }
                            continue;
                        }
                        BackendMessage::ParameterStatus(body) => {
                            if let (Ok(name), Ok(value)) = (body.name(), body.value()) {
                                self.conn
                                    .server_params
                                    .params
                                    .insert(name.to_string(), value.to_string());
                            }
                            continue;
                        }
                        BackendMessage::PortalSuspended => {
                            continue;
                        }
                        _ => continue,
                    }
                }

                RowStreamState::Finishing { .. } => {
                    let msg = self
                        .conn
                        .codec
                        .read_message(&mut self.conn.transport)
                        .await?;
                    match msg {
                        BackendMessage::ReadyForQuery(body) => {
                            let tag =
                                match std::mem::replace(&mut self.state, RowStreamState::Error) {
                                    RowStreamState::Finishing { command_tag } => command_tag,
                                    _ => CommandTag::default(),
                                };
                            self.conn.transaction_status =
                                TransactionStatus::from_u8(body.status())
                                    .unwrap_or(TransactionStatus::Idle);
                            self.conn.state = ConnectionState::Idle;
                            self.state = RowStreamState::Done { command_tag: tag };
                            #[cfg(feature = "tracing")]
                            tracing::info!(
                                target: TARGET_QUERY,
                                rows_fetched = self.rows_fetched,
                                command_tag = self.command_tag().map(|t| t.as_str()).unwrap_or(""),
                                elapsed_ms = self.started_at.elapsed().as_millis() as u64,
                                "Query stream completed"
                            );
                            return Ok(None);
                        }
                        BackendMessage::ErrorResponse(body) => {
                            // The server sent an error after CommandComplete but
                            // before ReadyForQuery. This can happen with certain
                            // PostgreSQL error conditions (e.g., cursor errors,
                            // constraint violations in multi-statement queries).
                            // We must surface this error rather than silently
                            // discarding it.
                            let server_err =
                                PgServerError::from_error_body(&body).map_err(PgError::Io)?;
                            self.conn.read_until_ready().await?;
                            self.conn.state = ConnectionState::Idle;
                            self.state = RowStreamState::Error;
                            return Err(PgError::Server(Box::new(server_err)));
                        }
                        BackendMessage::NotificationResponse(body) => {
                            self.conn.notification_queue.push_back(
                                crate::notification::Notification {
                                    process_id: body.process_id(),
                                    channel: body.channel().unwrap_or("").to_string(),
                                    payload: body.message().unwrap_or("").to_string(),
                                },
                            );
                            continue;
                        }
                        BackendMessage::NoticeResponse(body) => {
                            if let Ok(notice) = crate::query::Notice::from_fields(&body) {
                                self.conn.handle_notice(&notice);
                            }
                            continue;
                        }
                        BackendMessage::ParameterStatus(body) => {
                            if let (Ok(name), Ok(value)) = (body.name(), body.value()) {
                                self.conn
                                    .server_params
                                    .params
                                    .insert(name.to_string(), value.to_string());
                            }
                            continue;
                        }
                        _ => continue,
                    }
                }
            }
        }
    }

    /// Get the column metadata for the current result set.
    pub fn columns(&self) -> Option<&[FieldDescription]> {
        self.columns.as_ref().map(|c| c.as_slice())
    }

    /// Get the command tag after the stream ends.
    pub fn command_tag(&self) -> Option<&CommandTag> {
        match &self.state {
            RowStreamState::Done { command_tag } | RowStreamState::Finishing { command_tag } => {
                Some(command_tag)
            }
            _ => None,
        }
    }

    /// Returns true if the stream has been fully consumed or encountered an error.
    pub fn is_done(&self) -> bool {
        matches!(
            self.state,
            RowStreamState::Done { .. } | RowStreamState::Error
        )
    }

    /// Consume the remaining rows in the stream, discarding them.
    #[must_use = "consume errors should be checked"]
    pub async fn consume(mut self) -> Result<CommandTag> {
        while self.next().await?.is_some() {}
        match &self.state {
            RowStreamState::Done { command_tag } => Ok(command_tag.clone()),
            _ => Ok(CommandTag::default()),
        }
    }
}

impl<'a> Drop for RowStream<'a> {
    fn drop(&mut self) {
        if !self.is_done() {
            #[cfg(feature = "tracing")]
            if !std::thread::panicking() {
                tracing::warn!(target: TARGET_QUERY, "RowStream dropped without full consumption; connection may need recovery");
            }
            self.conn.needs_recovery = true;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::{Codec, ServerParams};
    use crate::config::Config;
    use crate::connection::ConnectionState;
    use crate::transport::{BufferedTransport, ClientTransport, MockTransport, PgTransport};
    use std::collections::VecDeque;

    fn make_connection(read_data: Vec<u8>) -> Connection {
        let transport = PgTransport::Plain(BufferedTransport::new(ClientTransport::Mock(
            MockTransport::new(read_data),
        )));
        Connection {
            transport,
            codec: Codec::new(),
            server_params: ServerParams::default(),
            state: ConnectionState::Idle,
            config: Config::new(),
            transaction_status: TransactionStatus::Idle,
            notification_queue: VecDeque::new(),
            notice_handler: None,
            statement_counter: 0,
            needs_recovery: false,
            health: crate::reconnect::session::ConnectionHealth::new(),
            session_state: crate::reconnect::session::SessionState::new(),
        }
    }

    fn build_row_description_msg(fields: &[(&str, u32)]) -> Vec<u8> {
        let mut buf = vec![b'T'];
        let mut body = Vec::new();
        body.extend_from_slice(&(fields.len() as i16).to_be_bytes());
        for (name, type_oid) in fields {
            body.extend_from_slice(name.as_bytes());
            body.push(0);
            body.extend_from_slice(&0u32.to_be_bytes());
            body.extend_from_slice(&0i16.to_be_bytes());
            body.extend_from_slice(&type_oid.to_be_bytes());
            body.extend_from_slice(&(-1i16).to_be_bytes());
            body.extend_from_slice(&(-1i32).to_be_bytes());
            body.extend_from_slice(&0i16.to_be_bytes());
        }
        let len = (body.len() + 4) as i32;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&body);
        buf
    }

    fn build_data_row_msg(values: &[Option<&str>]) -> Vec<u8> {
        let mut buf = vec![b'D'];
        let mut body = Vec::new();
        body.extend_from_slice(&(values.len() as i16).to_be_bytes());
        for val in values {
            match val {
                Some(v) => {
                    let bytes = v.as_bytes();
                    body.extend_from_slice(&(bytes.len() as i32).to_be_bytes());
                    body.extend_from_slice(bytes);
                }
                None => {
                    body.extend_from_slice(&(-1i32).to_be_bytes());
                }
            }
        }
        let len = (body.len() + 4) as i32;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&body);
        buf
    }

    fn build_command_complete_msg(tag: &str) -> Vec<u8> {
        let mut buf = vec![b'C'];
        let mut body = Vec::new();
        body.extend_from_slice(tag.as_bytes());
        body.push(0);
        let len = (body.len() + 4) as i32;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&body);
        buf
    }

    fn build_ready_for_query(status: u8) -> Vec<u8> {
        vec![b'Z', 0, 0, 0, 5, status]
    }

    fn build_error_response_msg(severity: &str, code: &str, message: &str) -> Vec<u8> {
        let mut buf = vec![b'E'];
        let mut body = Vec::new();
        // Severity
        body.push(b'S');
        body.extend_from_slice(severity.as_bytes());
        body.push(0);
        // Code (SQLSTATE)
        body.push(b'C');
        body.extend_from_slice(code.as_bytes());
        body.push(0);
        // Message
        body.push(b'M');
        body.extend_from_slice(message.as_bytes());
        body.push(0);
        // Terminator
        body.push(0);
        let len = (body.len() + 4) as i32;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&body);
        buf
    }

    #[tokio::test]
    async fn test_row_stream_basic() {
        let mut data = Vec::new();
        data.extend_from_slice(&build_row_description_msg(&[
            ("id", crate::types::INT4_OID),
            ("name", crate::types::TEXT_OID),
        ]));
        data.extend_from_slice(&build_data_row_msg(&[Some("1"), Some("alice")]));
        data.extend_from_slice(&build_data_row_msg(&[Some("2"), Some("bob")]));
        data.extend_from_slice(&build_data_row_msg(&[Some("3"), Some("charlie")]));
        data.extend_from_slice(&build_command_complete_msg("SELECT 3"));
        data.extend_from_slice(&build_ready_for_query(b'I'));
        let mut conn = make_connection(data);
        let mut stream = RowStream::new_simple(&mut conn);
        assert!(stream.columns().is_none());
        let row1 = stream.next().await.unwrap().unwrap();
        let id1: i32 = row1.get(0).unwrap();
        assert_eq!(id1, 1);
        assert!(stream.columns().is_some());
        let row2 = stream.next().await.unwrap().unwrap();
        let name2: String = row2.get(1).unwrap();
        assert_eq!(name2, "bob");
        let row3 = stream.next().await.unwrap().unwrap();
        let name3: String = row3.get(1).unwrap();
        assert_eq!(name3, "charlie");
        let done = stream.next().await.unwrap();
        assert!(done.is_none());
        assert!(stream.is_done());
        assert_eq!(stream.command_tag().unwrap().as_str(), "SELECT 3");
    }

    #[tokio::test]
    async fn test_row_stream_empty() {
        let mut data = Vec::new();
        data.extend_from_slice(&build_row_description_msg(&[(
            "id",
            crate::types::INT4_OID,
        )]));
        data.extend_from_slice(&build_command_complete_msg("SELECT 0"));
        data.extend_from_slice(&build_ready_for_query(b'I'));
        let mut conn = make_connection(data);
        let mut stream = RowStream::new_simple(&mut conn);
        let done = stream.next().await.unwrap();
        assert!(done.is_none());
        assert!(stream.is_done());
    }

    #[tokio::test]
    async fn test_row_stream_no_row_description() {
        let mut data = Vec::new();
        data.extend_from_slice(&build_command_complete_msg("INSERT 0 1"));
        data.extend_from_slice(&build_ready_for_query(b'I'));
        let mut conn = make_connection(data);
        let mut stream = RowStream::new_simple(&mut conn);
        let done = stream.next().await.unwrap();
        assert!(done.is_none());
        assert!(stream.is_done());
        assert_eq!(stream.command_tag().unwrap().as_str(), "INSERT 0 1");
    }

    #[tokio::test]
    async fn test_row_stream_consume() {
        let mut data = Vec::new();
        data.extend_from_slice(&build_row_description_msg(&[(
            "val",
            crate::types::INT4_OID,
        )]));
        data.extend_from_slice(&build_data_row_msg(&[Some("10")]));
        data.extend_from_slice(&build_data_row_msg(&[Some("20")]));
        data.extend_from_slice(&build_data_row_msg(&[Some("30")]));
        data.extend_from_slice(&build_command_complete_msg("SELECT 3"));
        data.extend_from_slice(&build_ready_for_query(b'I'));
        let mut conn = make_connection(data);
        let mut stream = RowStream::new_simple(&mut conn);
        let row1 = stream.next().await.unwrap().unwrap();
        let v: i32 = row1.get(0).unwrap();
        assert_eq!(v, 10);
        let tag = stream.consume().await.unwrap();
        assert_eq!(tag.as_str(), "SELECT 3");
        assert!(!conn.needs_recovery());
    }

    #[tokio::test]
    async fn test_row_stream_drop_sets_needs_recovery() {
        let mut data = Vec::new();
        data.extend_from_slice(&build_row_description_msg(&[(
            "val",
            crate::types::INT4_OID,
        )]));
        data.extend_from_slice(&build_data_row_msg(&[Some("10")]));
        data.extend_from_slice(&build_data_row_msg(&[Some("20")]));
        data.extend_from_slice(&build_command_complete_msg("SELECT 2"));
        data.extend_from_slice(&build_ready_for_query(b'I'));
        let mut conn = make_connection(data);
        {
            let mut stream = RowStream::new_simple(&mut conn);
            let _row1 = stream.next().await.unwrap().unwrap();
        }
        assert!(conn.needs_recovery());
    }

    #[tokio::test]
    async fn test_row_stream_extended_protocol() {
        let mut data = Vec::new();
        data.extend_from_slice(&[b'1', 0, 0, 0, 4]);
        data.extend_from_slice(&[b'2', 0, 0, 0, 4]);
        data.extend_from_slice(&build_row_description_msg(&[(
            "val",
            crate::types::INT4_OID,
        )]));
        data.extend_from_slice(&build_data_row_msg(&[Some("42")]));
        data.extend_from_slice(&build_command_complete_msg("SELECT 1"));
        data.extend_from_slice(&build_ready_for_query(b'I'));
        let mut conn = make_connection(data);
        let mut stream = RowStream::new_extended(&mut conn);
        let row = stream.next().await.unwrap().unwrap();
        let v: i32 = row.get(0).unwrap();
        assert_eq!(v, 42);
        let done = stream.next().await.unwrap();
        assert!(done.is_none());
    }

    #[tokio::test]
    async fn test_connection_recover_after_dropped_stream() {
        // Simulate: stream is dropped early, then recover() drains remaining messages
        let mut data = Vec::new();
        data.extend_from_slice(&build_row_description_msg(&[(
            "val",
            crate::types::INT4_OID,
        )]));
        data.extend_from_slice(&build_data_row_msg(&[Some("10")]));
        data.extend_from_slice(&build_data_row_msg(&[Some("20")]));
        data.extend_from_slice(&build_command_complete_msg("SELECT 2"));
        data.extend_from_slice(&build_ready_for_query(b'I'));
        let mut conn = make_connection(data);
        {
            let mut stream = RowStream::new_simple(&mut conn);
            let _row1 = stream.next().await.unwrap().unwrap();
            // Drop stream without consuming remaining rows
        }
        assert!(
            conn.needs_recovery(),
            "should need recovery after dropping stream"
        );

        // Now recover — this should read until ReadyForQuery
        conn.recover().await.unwrap();
        assert!(
            !conn.needs_recovery(),
            "should not need recovery after recover"
        );
        assert_eq!(
            conn.state(),
            ConnectionState::Idle,
            "should be idle after recover"
        );
    }

    #[tokio::test]
    async fn test_row_stream_error_in_finishing_state() {
        // Simulate: server sends ErrorResponse after CommandComplete but
        // before ReadyForQuery. This used to be silently discarded.
        let mut data = Vec::new();
        data.extend_from_slice(&build_row_description_msg(&[(
            "val",
            crate::types::INT4_OID,
        )]));
        data.extend_from_slice(&build_data_row_msg(&[Some("10")]));
        data.extend_from_slice(&build_command_complete_msg("SELECT 1"));
        // Server sends an error during the finishing phase
        data.extend_from_slice(&build_error_response_msg(
            "ERROR",
            "57014", // query_canceled
            "canceling statement due to user request",
        ));
        data.extend_from_slice(&build_ready_for_query(b'I'));
        let mut conn = make_connection(data);
        let mut stream = RowStream::new_simple(&mut conn);
        // First row is fine
        let row = stream.next().await.unwrap().unwrap();
        let v: i32 = row.get(0).unwrap();
        assert_eq!(v, 10);
        // After CommandComplete, the stream enters Finishing state.
        // The next call should encounter the ErrorResponse and return an error.
        let result = stream.next().await;
        assert!(
            result.is_err(),
            "ErrorResponse in Finishing state should be surfaced as an error"
        );
        let err = result.unwrap_err();
        match err {
            PgError::Server(server_err) => {
                assert_eq!(
                    server_err.code, "57014",
                    "error code should be query_canceled"
                );
            }
            other => panic!("expected PgError::Server, got {:?}", other),
        }
        // Drop the stream so we can access conn
        drop(stream);
        // Connection should be idle after the error is handled
        assert_eq!(
            conn.state(),
            ConnectionState::Idle,
            "connection should be idle after error in finishing state"
        );
    }
}