mirror of https://github.com/jackc/pgx.git
Add ConnConfig.PreferSimpleProtocol
Allows configuring on a connection basis to prefer the simple protocol / disable implicit prepared statements. refs #331prefer-simple-protocol
parent
50627257f9
commit
bd76a96882
13
conn.go
13
conn.go
|
@ -75,6 +75,17 @@ type ConnConfig struct {
|
||||||
RuntimeParams map[string]string // Run-time parameters to set on connection as session default values (e.g. search_path or application_name)
|
RuntimeParams map[string]string // Run-time parameters to set on connection as session default values (e.g. search_path or application_name)
|
||||||
OnNotice NoticeHandler // Callback function called when a notice response is received.
|
OnNotice NoticeHandler // Callback function called when a notice response is received.
|
||||||
CustomConnInfo func(*Conn) (*pgtype.ConnInfo, error) // Callback function to implement connection strategies for different backends. crate, pgbouncer, pgpool, etc.
|
CustomConnInfo func(*Conn) (*pgtype.ConnInfo, error) // Callback function to implement connection strategies for different backends. crate, pgbouncer, pgpool, etc.
|
||||||
|
|
||||||
|
// PreferSimpleProtocol disables implicit prepared statement usage. By default
|
||||||
|
// pgx automatically uses the unnamed prepared statement for Query and
|
||||||
|
// QueryRow. It also uses a prepared statement when Exec has arguments. This
|
||||||
|
// can improve performance due to being able to use the binary format. It also
|
||||||
|
// does not rely on client side parameter sanitization. However, it does incur
|
||||||
|
// two round-trips per query and may be incompatible proxies such as
|
||||||
|
// PGBouncer. Setting PreferSimpleProtocol causes the simple protocol to be
|
||||||
|
// used by default. The same functionality can be controlled on a per query
|
||||||
|
// basis by setting QueryExOptions.SimpleProtocol.
|
||||||
|
PreferSimpleProtocol bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *ConnConfig) networkAddress() (network, address string) {
|
func (cc *ConnConfig) networkAddress() (network, address string) {
|
||||||
|
@ -1586,7 +1597,7 @@ func (c *Conn) execEx(ctx context.Context, sql string, options *QueryExOptions,
|
||||||
err = c.termContext(err)
|
err = c.termContext(err)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if options != nil && options.SimpleProtocol {
|
if (options == nil && c.config.PreferSimpleProtocol) || (options != nil && options.SimpleProtocol) {
|
||||||
err = c.sanitizeAndSendSimpleQuery(sql, arguments...)
|
err = c.sanitizeAndSendSimpleQuery(sql, arguments...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
25
conn_test.go
25
conn_test.go
|
@ -255,6 +255,31 @@ func TestConnectWithConnectionRefused(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnectWithPreferSimpleProtocol(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
connConfig := *defaultConnConfig
|
||||||
|
connConfig.PreferSimpleProtocol = true
|
||||||
|
|
||||||
|
conn := mustConnect(t, connConfig)
|
||||||
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
|
// If simple protocol is used we should be able to correctly scan the result
|
||||||
|
// into a pgtype.Text as the integer will have been encoded in text.
|
||||||
|
|
||||||
|
var s pgtype.Text
|
||||||
|
err := conn.QueryRow("select $1::int4", 42).Scan(&s)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.Get() != "42" {
|
||||||
|
t.Fatalf(`expected "42", got %v`, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureConnValid(t, conn)
|
||||||
|
}
|
||||||
|
|
||||||
func TestConnectCustomDialer(t *testing.T) {
|
func TestConnectCustomDialer(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
2
query.go
2
query.go
|
@ -393,7 +393,7 @@ func (c *Conn) QueryEx(ctx context.Context, sql string, options *QueryExOptions,
|
||||||
return rows, rows.err
|
return rows, rows.err
|
||||||
}
|
}
|
||||||
|
|
||||||
if options != nil && options.SimpleProtocol {
|
if (options == nil && c.config.PreferSimpleProtocol) || (options != nil && options.SimpleProtocol) {
|
||||||
err = c.sanitizeAndSendSimpleQuery(sql, args...)
|
err = c.sanitizeAndSendSimpleQuery(sql, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rows.fatal(err)
|
rows.fatal(err)
|
||||||
|
|
Loading…
Reference in New Issue