rust 准备好的语句pgBouncer使用'transaction' pool_mode时出现的问题

4xrmg8kj  于 2023-01-30  发布在  其他
关注(0)|答案(1)|浏览(122)

我已经准备好语句问题与pgBouncer在'事务'池模式。
此 rust 码:

use postgres::{Client, Error, NoTls};

fn main() -> Result<(), Error> {
    let mut client = Client::connect(
        "postgresql://haproxy@localhost:9435/haproxy",
        NoTls,
    )?;

for row in client.query("SELECT pg_is_in_recovery() as x;", &[])? {
    let x: bool = row.get(0);
    println!(
        "found app x: {}",
        x
    );
    }

    Ok(())
}

失败,错误为:预准备语句"s0"不存在

Error: Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState(E26000), message: "prepared statement \"s0\" does not exist", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("prepare.c"), line: Some(506), routine: Some("FetchPreparedStatement") }) }

有什么办法吗?

jobtbby3

jobtbby31#

快!
答案是使用simple_query

use postgres::{Client, Error, NoTls, SimpleQueryMessage};

fn main() -> Result<(), Error> {
    let mut client = Client::connect("postgresql://haproxy@localhost:9435/haproxy", NoTls)?;

    let mut res = false;

    let it = client.simple_query("SELECT pg_is_in_recovery()")?;

    for mm in it {
        match mm {
            SimpleQueryMessage::CommandComplete(_x) => {
                // println!("{:?}", x);
            }
            SimpleQueryMessage::Row(x) => {
                if x.get(0).as_ref().unwrap().contains('t') {
                    res = true;
                } else {
                    res = false;
                }
            }
            _ => panic!("n"),
        }
    }

    println!("Result is: {}", res);

    Ok(())
}

相关问题