pgx: 4.1.2 Can't query with with PGBouncer?

Connecting to PGBouncer, without SSL, and attempting to run any query fails with:

ERROR: unsupported pkt type: 80 (SQLSTATE 08P01)

I also tried 4.0.0 and it seems that this error is not new. Looks quite similar to #512. I attempted both the sql.DB interface and the native pgx interface to no avail.

Versions:

  • PGBouncer 1.11.0
  • pgx 4.0.0, 4.1.2

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 17 (6 by maintainers)

Commits related to this issue

Most upvoted comments

For everyone, who used this but migrated to V5.

Right now, simple protocol can be enabled in a different way.

// ParseConfig creates a ConnConfig from a connection string. ParseConfig handles all options that pgconn.ParseConfig
// does. In addition, it accepts the following options:
//
//   - default_query_exec_mode.
//     Possible values: "cache_statement", "cache_describe", "describe_exec", "exec", and "simple_protocol". See
//     QueryExecMode constant documentation for the meaning of these values. Default: "cache_statement".

There are a few restrictions when using PgBouncer. One is that you cannot use prepared statements. So if you have any explicit prepared statement usage that would be a problem. But also pgx automatically uses prepared statements internally. So that needs to be disabled for pgx to work with pgbouncer.

There are two ways of doing that:

  1. Set the statement cache to describe mode (include statement_cache_mode=describe in your connection string is the simplest way to set this)
  2. Set PreferSimpleProtocol to true in your pgx.ConnConfig

Describe statement cache mode allows pgx to remember the argument types and result types from a query. If a column type changes then query errors or even data corruption could occur. For example, if the server changed a column from an int4 to float4 data corruption could occur as the binary representation of an integer would be stored into a float.

@pavelpatrin thanks for that