douque9815 2013-07-12 11:58
浏览 197
已采纳

pq驱动程序:准备好的语句不存在

I'm trying to connect to a postresql database with the pq driver in Go. When I do it on a local copy of the database, with a connection string like

DB, err = sql.Open("postgres", "user=user password=pwd dbname=mydb sslmode=disable")

it all works well.

However, when I switch to a production server where the connection goes through pgbouncer:

DB, err = sql.Open("postgres", "user=user password=pwd host=/var/run/pgbouncer port=port dbname=mydb sslmode=disable")

I keep getting the same error for all queries, however simple:

Database error: pq: S:"ERROR" M:"prepared statement \"1\" does not exist" C:"26000" F:"prepare.c" L:"519" R:"FetchPreparedStatement"

(it's always "prepared statement \"1\"", independent of the query I'm trying to pass)

The query in both cases is run simply as follows:

res_rows, err := DB.Query(query)
if err != nil {
    log.Printf("Database error: %s
", err)
}
for res_rows.Next() {
    ...
}

Googling suggests to turn off prepared statements, but I don't know how to do that in Go and I'm not sure it is supported at all. Any help (even a suggestion to use something else entirely) would be greatly appreciated.

  • 写回答

3条回答 默认 最新

  • doupang9614 2013-07-12 15:52
    关注

    Package driver

    type Queryer

    type Queryer interface {
        Query(query string, args []Value) (Rows, error)
    }
    

    Queryer is an optional interface that may be implemented by a Conn.

    If a Conn does not implement Queryer, the sql package's DB.Query will first prepare a query, execute the statement, and then close the statement.

    I don't see where the lib/pq PostgreSQL driver implements Queryer. Therefore, the DB.Query query is prepared before execution.

    PgBouncer doesn't support the PREPARE feature for all pooling methods: Feature matrix for pooling modes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?