wasi-pg-client 0.1.1

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
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
//! Cursor support for fetching large result sets in batches.
//!
//! A [`Cursor`] executes a portal with a limited `max_rows` count and
//! provides `fetch_next()` to retrieve subsequent batches until the result
//! set is exhausted.
//!
//! A [`CursorStream`] wraps a cursor and yields rows one at a time,
//! automatically fetching the next batch when the current batch is exhausted.

use std::sync::Arc;

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

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

// ---------------------------------------------------------------------------
// Cursor
// ---------------------------------------------------------------------------

/// A cursor for fetching a large result set in batches.
///
/// Created via [`Connection::query_cursor`]. Each call to [`Cursor::fetch_next`]
/// returns the next batch of rows. The cursor is automatically closed when
/// dropped, but explicit [`Cursor::close`] is recommended for clean shutdown.
#[non_exhaustive]
pub struct Cursor<'a> {
    conn: &'a mut Connection,
    portal_name: String,
    columns: Arc<Vec<FieldDescription>>,
    fetch_size: i32,
    done: bool,
    /// Whether this cursor started the transaction and should commit on close.
    owns_transaction: bool,
}

impl<'a> Cursor<'a> {
    /// Fetch the next batch of rows.
    ///
    /// Returns an empty vector when all rows have been consumed.
    #[must_use = "cursor errors should be checked"]
    pub async fn fetch_next(&mut self) -> Result<Vec<Row>> {
        if self.done {
            return Ok(Vec::new());
        }

        self.conn.transition(ConnectionState::ActiveExtendedQuery)?;

        // Execute portal with limited max_rows
        self.conn
            .codec
            .encode_and_write(
                &mut self.conn.transport,
                &FrontendMessage::Execute {
                    portal: self.portal_name.clone(),
                    max_rows: self.fetch_size,
                },
            )
            .await?;

        self.conn
            .codec
            .encode_and_write(&mut self.conn.transport, &FrontendMessage::Sync)
            .await?;

        // Flush the batch
        self.conn
            .transport
            .flush()
            .await
            .map_err(PgError::Transport)?;

        let mut rows = Vec::new();

        loop {
            let msg = self
                .conn
                .codec
                .read_message(&mut self.conn.transport)
                .await?;
            if self.conn.handle_async_message(&msg) {
                continue;
            }
            match msg {
                BackendMessage::RowDescription(body) => {
                    self.columns = Arc::new(read_row_description(body)?);
                }
                BackendMessage::DataRow(body) => {
                    let values = read_data_row(body)?;
                    rows.push(Row::new(self.columns.clone(), values));
                }
                BackendMessage::CommandComplete(_body) => {
                    self.done = true;
                }
                BackendMessage::PortalSuspended => {
                    // More rows available; portal remains open
                }
                BackendMessage::ReadyForQuery(body) => {
                    self.conn.transaction_status = TransactionStatus::from_u8(body.status())
                        .unwrap_or(TransactionStatus::Idle);
                    self.conn.state = ConnectionState::Idle;
                    break;
                }
                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;
                    return Err(PgError::Server(Box::new(server_err)));
                }
                _ => {}
            }
        }

        Ok(rows)
    }

    /// Close the cursor, releasing the portal on the server.
    ///
    /// If the cursor automatically started a transaction (because no
    /// transaction was active when the cursor was created), the transaction
    /// is committed.
    #[must_use = "cursor close errors should be checked"]
    pub async fn close(mut self) -> Result<()> {
        self.conn.transition(ConnectionState::ActiveExtendedQuery)?;

        self.conn
            .codec
            .encode_and_write(
                &mut self.conn.transport,
                &FrontendMessage::Close {
                    variant: b'P',
                    name: self.portal_name.clone(),
                },
            )
            .await?;

        self.conn
            .codec
            .encode_and_write(&mut self.conn.transport, &FrontendMessage::Sync)
            .await?;

        // Flush the batch
        self.conn
            .transport
            .flush()
            .await
            .map_err(PgError::Transport)?;

        loop {
            let msg = self
                .conn
                .codec
                .read_message(&mut self.conn.transport)
                .await?;
            if self.conn.handle_async_message(&msg) {
                continue;
            }
            match msg {
                BackendMessage::CloseComplete => {}
                BackendMessage::ReadyForQuery(body) => {
                    self.conn.transaction_status = TransactionStatus::from_u8(body.status())
                        .unwrap_or(TransactionStatus::Idle);
                    self.conn.state = ConnectionState::Idle;
                    break;
                }
                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;
                    return Err(PgError::Server(Box::new(server_err)));
                }
                _ => {}
            }
        }

        // Commit the transaction if we started it
        if self.owns_transaction {
            self.conn.execute("COMMIT").await?;
        }

        self.done = true;
        Ok(())
    }

    /// Returns true if all rows have been fetched.
    pub fn is_done(&self) -> bool {
        self.done
    }
}

// ---------------------------------------------------------------------------
// CursorStream
// ---------------------------------------------------------------------------

/// Internal state of the cursor stream.
#[derive(Debug)]
enum CursorStreamState {
    /// Rows are available in the buffer or more can be fetched.
    Active,
    /// All rows have been consumed and the cursor is closed.
    Done { command_tag: CommandTag },
    /// An error occurred.
    Error,
}

/// A streaming cursor that yields rows one at a time from a portal.
///
/// Unlike [`Cursor`] which returns batches of rows, `CursorStream` provides
/// a row-by-row iterator interface. When the current batch is exhausted,
/// it automatically fetches the next batch from the server.
///
/// `CursorStream` borrows the connection mutably. You cannot use the
/// connection while iterating. 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.
#[non_exhaustive]
pub struct CursorStream<'a> {
    conn: &'a mut Connection,
    portal_name: String,
    columns: Arc<Vec<FieldDescription>>,
    fetch_size: i32,
    state: CursorStreamState,
    /// Rows buffered from the current batch, waiting to be yielded.
    buffered_rows: Vec<Row>,
    /// Whether this cursor started the transaction and should commit on close.
    owns_transaction: bool,
}

impl<'a> CursorStream<'a> {
    /// Create a new `CursorStream` from an already-set-up portal.
    pub(crate) fn new(
        conn: &'a mut Connection,
        portal_name: String,
        columns: Arc<Vec<FieldDescription>>,
        fetch_size: i32,
        owns_transaction: bool,
    ) -> Self {
        CursorStream {
            conn,
            portal_name,
            columns,
            fetch_size,
            state: CursorStreamState::Active,
            buffered_rows: Vec::new(),
            owns_transaction,
        }
    }

    /// 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 = "cursor stream errors should be checked"]
    pub async fn next(&mut self) -> Result<Option<Row>> {
        loop {
            match self.state {
                CursorStreamState::Done { .. } | CursorStreamState::Error => {
                    // Yield any remaining buffered rows before reporting done
                    if let Some(row) = self.buffered_rows.pop() {
                        return Ok(Some(row));
                    }
                    return Ok(None);
                }

                CursorStreamState::Active => {
                    // First, yield from the buffer if we have rows from a previous fetch
                    if let Some(row) = self.buffered_rows.pop() {
                        return Ok(Some(row));
                    }

                    // Buffer is empty — fetch the next batch from the server
                    self.conn.transition(ConnectionState::ActiveExtendedQuery)?;

                    self.conn
                        .codec
                        .encode_and_write(
                            &mut self.conn.transport,
                            &FrontendMessage::Execute {
                                portal: self.portal_name.clone(),
                                max_rows: self.fetch_size,
                            },
                        )
                        .await?;

                    self.conn
                        .codec
                        .encode_and_write(&mut self.conn.transport, &FrontendMessage::Sync)
                        .await?;

                    self.conn
                        .transport
                        .flush()
                        .await
                        .map_err(PgError::Transport)?;

                    // Read the batch response
                    let mut command_tag: Option<CommandTag> = None;
                    let mut rows: Vec<Row> = Vec::new();

                    loop {
                        let msg = self
                            .conn
                            .codec
                            .read_message(&mut self.conn.transport)
                            .await?;
                        if self.conn.handle_async_message(&msg) {
                            continue;
                        }
                        match msg {
                            BackendMessage::RowDescription(body) => {
                                self.columns = Arc::new(read_row_description(body)?);
                            }
                            BackendMessage::DataRow(body) => {
                                let values = read_data_row(body)?;
                                rows.push(Row::new(self.columns.clone(), values));
                            }
                            BackendMessage::CommandComplete(body) => {
                                command_tag =
                                    Some(CommandTag::new(body.tag().unwrap_or("").into()));
                            }
                            BackendMessage::PortalSuspended => {
                                // More rows available; portal remains open
                            }
                            BackendMessage::ReadyForQuery(body) => {
                                self.conn.transaction_status =
                                    TransactionStatus::from_u8(body.status())
                                        .unwrap_or(TransactionStatus::Idle);
                                self.conn.state = ConnectionState::Idle;
                                break;
                            }
                            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 = CursorStreamState::Error;
                                return Err(PgError::Server(Box::new(server_err)));
                            }
                            _ => {}
                        }
                    }

                    // If CommandComplete was received, all rows are done
                    if let Some(tag) = command_tag {
                        self.state = CursorStreamState::Done { command_tag: tag };
                    }

                    // If no rows were returned and we're not done, loop to fetch again
                    // (this can happen with PortalSuspended when fetch_size rows were
                    // already consumed in a previous batch)
                    if rows.is_empty() && !self.is_done() {
                        continue;
                    }

                    // Reverse the buffer so we can pop() from the front efficiently
                    rows.reverse();
                    self.buffered_rows = rows;

                    // Yield the first row from the buffer
                    if let Some(row) = self.buffered_rows.pop() {
                        return Ok(Some(row));
                    }

                    // No rows and done
                    return Ok(None);
                }
            }
        }
    }

    /// Get the column metadata for the current result set.
    pub fn columns(&self) -> &[FieldDescription] {
        &self.columns
    }

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

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

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

    /// Close the portal on the server and commit the transaction if we own it.
    async fn close_portal(&mut self) -> Result<()> {
        if matches!(self.state, CursorStreamState::Done { .. }) {
            // Already done — just commit if needed
            if self.owns_transaction {
                self.conn.execute("COMMIT").await?;
            }
            return Ok(());
        }

        self.conn.transition(ConnectionState::ActiveExtendedQuery)?;

        self.conn
            .codec
            .encode_and_write(
                &mut self.conn.transport,
                &FrontendMessage::Close {
                    variant: b'P',
                    name: self.portal_name.clone(),
                },
            )
            .await?;

        self.conn
            .codec
            .encode_and_write(&mut self.conn.transport, &FrontendMessage::Sync)
            .await?;

        self.conn
            .transport
            .flush()
            .await
            .map_err(PgError::Transport)?;

        loop {
            let msg = self
                .conn
                .codec
                .read_message(&mut self.conn.transport)
                .await?;
            if self.conn.handle_async_message(&msg) {
                continue;
            }
            match msg {
                BackendMessage::CloseComplete => {}
                BackendMessage::ReadyForQuery(body) => {
                    self.conn.transaction_status = TransactionStatus::from_u8(body.status())
                        .unwrap_or(TransactionStatus::Idle);
                    self.conn.state = ConnectionState::Idle;
                    break;
                }
                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;
                    return Err(PgError::Server(Box::new(server_err)));
                }
                _ => {}
            }
        }

        // Commit the transaction if we started it
        if self.owns_transaction {
            self.conn.execute("COMMIT").await?;
        }

        self.state = CursorStreamState::Done {
            command_tag: CommandTag::default(),
        };
        Ok(())
    }
}

impl<'a> Drop for CursorStream<'a> {
    fn drop(&mut self) {
        if !self.is_done() {
            self.conn.needs_recovery = true;
        }
    }
}

// ---------------------------------------------------------------------------
// Connection method
// ---------------------------------------------------------------------------

impl Connection {
    /// Open a cursor for a parameterized query.
    ///
    /// The query is parsed and bound to a named portal. The first batch of
    /// rows is fetched via [`Cursor::fetch_next`].
    ///
    /// **Important:** Named portals only survive within a transaction
    /// block. If no transaction is active, this method automatically
    /// begins one so the portal remains valid across `fetch_next` calls.
    /// The transaction is committed when the cursor is closed.
    #[must_use = "cursor errors should be checked"]
    pub async fn query_cursor(
        &mut self,
        sql: &str,
        params: &[&dyn crate::types::ToSql],
        fetch_size: i32,
    ) -> Result<Cursor<'_>> {
        // Named portals only survive within a transaction. Start one if
        // we're not already in a transaction.
        let need_transaction = self.transaction_status == crate::protocol::TransactionStatus::Idle;
        if need_transaction {
            // Use simple query for BEGIN — it's a single statement with no params
            self.query("BEGIN").await?;
        }

        self.transition(ConnectionState::ActiveExtendedQuery)?;

        let param_values = encode_params_text(params)?;
        let portal_name = format!("__pg_portal_{}", self.statement_counter);
        self.statement_counter += 1;

        // Parse (unnamed statement)
        self.codec
            .encode_and_write(
                &mut self.transport,
                &FrontendMessage::Parse {
                    name: String::new(),
                    sql: sql.to_string(),
                    param_types: vec![],
                },
            )
            .await?;

        // Bind (named portal)
        self.codec
            .encode_and_write(
                &mut self.transport,
                &FrontendMessage::Bind {
                    portal: portal_name.clone(),
                    statement: String::new(),
                    param_formats: vec![crate::protocol::FormatCode::Text],
                    params: param_values,
                    result_formats: vec![crate::protocol::FormatCode::Binary],
                },
            )
            .await?;

        // Describe the named portal so the server sends RowDescription
        // (or NoData for non-SELECT statements).
        self.codec
            .encode_and_write(
                &mut self.transport,
                &FrontendMessage::Describe {
                    variant: b'P',
                    name: portal_name.clone(),
                },
            )
            .await?;

        // Sync to complete the sub-protocol
        self.codec
            .encode_and_write(&mut self.transport, &FrontendMessage::Sync)
            .await?;

        // Flush the batch
        self.transport.flush().await.map_err(PgError::Transport)?;

        let mut columns: Option<Arc<Vec<FieldDescription>>> = None;

        loop {
            let msg = self.codec.read_message(&mut self.transport).await?;
            if self.handle_async_message(&msg) {
                continue;
            }
            match msg {
                BackendMessage::ParseComplete => {}
                BackendMessage::BindComplete => {}
                BackendMessage::NoData => {
                    // Non-SELECT query opened as cursor
                }
                BackendMessage::RowDescription(body) => {
                    columns = Some(Arc::new(read_row_description(body)?));
                }
                BackendMessage::ReadyForQuery(body) => {
                    self.transaction_status = TransactionStatus::from_u8(body.status())
                        .unwrap_or(TransactionStatus::Idle);
                    self.state = ConnectionState::Idle;
                    break;
                }
                BackendMessage::ErrorResponse(body) => {
                    let server_err = PgServerError::from_error_body(&body).map_err(PgError::Io)?;
                    self.read_until_ready().await?;
                    self.state = ConnectionState::Idle;
                    return Err(PgError::Server(Box::new(server_err)));
                }
                _ => {}
            }
        }

        Ok(Cursor {
            conn: self,
            portal_name,
            columns: columns.unwrap_or_default(),
            fetch_size,
            done: false,
            owns_transaction: need_transaction,
        })
    }

    /// Open a streaming cursor for a parameterized query.
    ///
    /// Like [`Connection::query_cursor`], but returns a [`CursorStream`] that
    /// yields rows one at a time instead of in batches. When the current batch
    /// is exhausted, the next batch is automatically fetched from the server.
    ///
    /// **Important:** Named portals only survive within a transaction
    /// block. If no transaction is active, this method automatically
    /// begins one so the portal remains valid. The transaction is committed
    /// when the stream is fully consumed or closed.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let mut stream = conn.query_cursor_stream(
    ///     "SELECT id, name FROM users WHERE active = $1",
    ///     &[&true],
    ///     100, // fetch 100 rows at a time
    /// ).await?;
    /// while let Some(row) = stream.next().await? {
    ///     let id: i32 = row.get(0)?;
    /// }
    /// ```
    #[must_use = "cursor stream errors should be checked"]
    pub async fn query_cursor_stream(
        &mut self,
        sql: &str,
        params: &[&dyn crate::types::ToSql],
        fetch_size: i32,
    ) -> Result<CursorStream<'_>> {
        // Named portals only survive within a transaction. Start one if
        // we're not already in a transaction.
        let need_transaction = self.transaction_status == crate::protocol::TransactionStatus::Idle;
        if need_transaction {
            self.query("BEGIN").await?;
        }

        self.transition(ConnectionState::ActiveExtendedQuery)?;

        let param_values = encode_params_text(params)?;
        let portal_name = format!("__pg_portal_{}", self.statement_counter);
        self.statement_counter += 1;

        // Parse (unnamed statement)
        self.codec
            .encode_and_write(
                &mut self.transport,
                &FrontendMessage::Parse {
                    name: String::new(),
                    sql: sql.to_string(),
                    param_types: vec![],
                },
            )
            .await?;

        // Bind (named portal)
        self.codec
            .encode_and_write(
                &mut self.transport,
                &FrontendMessage::Bind {
                    portal: portal_name.clone(),
                    statement: String::new(),
                    param_formats: vec![crate::protocol::FormatCode::Text],
                    params: param_values,
                    result_formats: vec![crate::protocol::FormatCode::Binary],
                },
            )
            .await?;

        // Describe the named portal so the server sends RowDescription
        // (or NoData for non-SELECT statements).
        self.codec
            .encode_and_write(
                &mut self.transport,
                &FrontendMessage::Describe {
                    variant: b'P',
                    name: portal_name.clone(),
                },
            )
            .await?;

        // Sync to complete the sub-protocol
        self.codec
            .encode_and_write(&mut self.transport, &FrontendMessage::Sync)
            .await?;

        // Flush the batch
        self.transport.flush().await.map_err(PgError::Transport)?;

        let mut columns: Option<Arc<Vec<FieldDescription>>> = None;

        loop {
            let msg = self.codec.read_message(&mut self.transport).await?;
            if self.handle_async_message(&msg) {
                continue;
            }
            match msg {
                BackendMessage::ParseComplete => {}
                BackendMessage::BindComplete => {}
                BackendMessage::NoData => {
                    // Non-SELECT query opened as cursor
                }
                BackendMessage::RowDescription(body) => {
                    columns = Some(Arc::new(read_row_description(body)?));
                }
                BackendMessage::ReadyForQuery(body) => {
                    self.transaction_status = TransactionStatus::from_u8(body.status())
                        .unwrap_or(TransactionStatus::Idle);
                    self.state = ConnectionState::Idle;
                    break;
                }
                BackendMessage::ErrorResponse(body) => {
                    let server_err = PgServerError::from_error_body(&body).map_err(PgError::Io)?;
                    self.read_until_ready().await?;
                    self.state = ConnectionState::Idle;
                    return Err(PgError::Server(Box::new(server_err)));
                }
                _ => {}
            }
        }

        Ok(CursorStream::new(
            self,
            portal_name,
            columns.unwrap_or_default(),
            fetch_size,
            need_transaction,
        ))
    }
}