mirror of https://github.com/jackc/pgx.git
Rename PreparedStatementCache to StatementCache
parent
61f0710101
commit
6508934508
|
@ -17,7 +17,7 @@ import (
|
||||||
|
|
||||||
func BenchmarkMinimalUnpreparedSelectWithoutStatementCache(b *testing.B) {
|
func BenchmarkMinimalUnpreparedSelectWithoutStatementCache(b *testing.B) {
|
||||||
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
config.BuildPreparedStatementCache = nil
|
config.BuildStatementCache = nil
|
||||||
|
|
||||||
conn := mustConnect(b, config)
|
conn := mustConnect(b, config)
|
||||||
defer closeConn(b, conn)
|
defer closeConn(b, conn)
|
||||||
|
@ -39,7 +39,7 @@ func BenchmarkMinimalUnpreparedSelectWithoutStatementCache(b *testing.B) {
|
||||||
|
|
||||||
func BenchmarkMinimalUnpreparedSelectWithStatementCacheModeDescribe(b *testing.B) {
|
func BenchmarkMinimalUnpreparedSelectWithStatementCacheModeDescribe(b *testing.B) {
|
||||||
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
config.BuildPreparedStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
config.BuildStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||||
return stmtcache.New(conn, stmtcache.ModeDescribe, 32)
|
return stmtcache.New(conn, stmtcache.ModeDescribe, 32)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ func BenchmarkMinimalUnpreparedSelectWithStatementCacheModeDescribe(b *testing.B
|
||||||
|
|
||||||
func BenchmarkMinimalUnpreparedSelectWithStatementCacheModePrepare(b *testing.B) {
|
func BenchmarkMinimalUnpreparedSelectWithStatementCacheModePrepare(b *testing.B) {
|
||||||
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
config := mustParseConfig(b, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
config.BuildPreparedStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
config.BuildStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||||
return stmtcache.New(conn, stmtcache.ModePrepare, 32)
|
return stmtcache.New(conn, stmtcache.ModePrepare, 32)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
24
conn.go
24
conn.go
|
@ -29,9 +29,9 @@ type ConnConfig struct {
|
||||||
Logger Logger
|
Logger Logger
|
||||||
LogLevel LogLevel
|
LogLevel LogLevel
|
||||||
|
|
||||||
// BuildPreparedStatementCache creates the stmtcache.Cache implementation for connections created with this config. Set
|
// BuildStatementCache creates the stmtcache.Cache implementation for connections created with this config. Set
|
||||||
// to nil to disable automatic prepared statements.
|
// to nil to disable automatic prepared statements.
|
||||||
BuildPreparedStatementCache BuildPreparedStatementCacheFunc
|
BuildStatementCache BuildStatementCacheFunc
|
||||||
|
|
||||||
// PreferSimpleProtocol disables implicit prepared statement usage. By default pgx automatically uses the extended
|
// PreferSimpleProtocol disables implicit prepared statement usage. By default pgx automatically uses the extended
|
||||||
// protocol. This can improve performance due to being able to use the binary format. It also does not rely on client
|
// protocol. This can improve performance due to being able to use the binary format. It also does not rely on client
|
||||||
|
@ -44,8 +44,8 @@ type ConnConfig struct {
|
||||||
createdByParseConfig bool // Used to enforce created by ParseConfig rule.
|
createdByParseConfig bool // Used to enforce created by ParseConfig rule.
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildPreparedStatementCacheFunc is a function that can be used to create a stmtcache.Cache implementation for connection.
|
// BuildStatementCacheFunc is a function that can be used to create a stmtcache.Cache implementation for connection.
|
||||||
type BuildPreparedStatementCacheFunc func(conn *pgconn.PgConn) stmtcache.Cache
|
type BuildStatementCacheFunc func(conn *pgconn.PgConn) stmtcache.Cache
|
||||||
|
|
||||||
// Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. Use a connection pool to manage access
|
// Conn is a PostgreSQL connection handle. It is not safe for concurrent usage. Use a connection pool to manage access
|
||||||
// to multiple database connections from multiple goroutines.
|
// to multiple database connections from multiple goroutines.
|
||||||
|
@ -138,7 +138,7 @@ func ParseConfig(connString string) (*ConnConfig, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var buildPreparedStatementCache BuildPreparedStatementCacheFunc
|
var buildStatementCache BuildStatementCacheFunc
|
||||||
statementCacheCapacity := 512
|
statementCacheCapacity := 512
|
||||||
statementCacheMode := stmtcache.ModePrepare
|
statementCacheMode := stmtcache.ModePrepare
|
||||||
if s, ok := config.RuntimeParams["statement_cache_capacity"]; ok {
|
if s, ok := config.RuntimeParams["statement_cache_capacity"]; ok {
|
||||||
|
@ -163,16 +163,16 @@ func ParseConfig(connString string) (*ConnConfig, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if statementCacheCapacity > 0 {
|
if statementCacheCapacity > 0 {
|
||||||
buildPreparedStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
buildStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||||
return stmtcache.New(conn, statementCacheMode, statementCacheCapacity)
|
return stmtcache.New(conn, statementCacheMode, statementCacheCapacity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
connConfig := &ConnConfig{
|
connConfig := &ConnConfig{
|
||||||
Config: *config,
|
Config: *config,
|
||||||
createdByParseConfig: true,
|
createdByParseConfig: true,
|
||||||
LogLevel: LogLevelInfo,
|
LogLevel: LogLevelInfo,
|
||||||
BuildPreparedStatementCache: buildPreparedStatementCache,
|
BuildStatementCache: buildStatementCache,
|
||||||
}
|
}
|
||||||
|
|
||||||
return connConfig, nil
|
return connConfig, nil
|
||||||
|
@ -217,8 +217,8 @@ func connect(ctx context.Context, config *ConnConfig) (c *Conn, err error) {
|
||||||
c.closedChan = make(chan error)
|
c.closedChan = make(chan error)
|
||||||
c.wbuf = make([]byte, 0, 1024)
|
c.wbuf = make([]byte, 0, 1024)
|
||||||
|
|
||||||
if c.config.BuildPreparedStatementCache != nil {
|
if c.config.BuildStatementCache != nil {
|
||||||
c.stmtcache = c.config.BuildPreparedStatementCache(c.pgConn)
|
c.stmtcache = c.config.BuildStatementCache(c.pgConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replication connections can't execute the queries to
|
// Replication connections can't execute the queries to
|
||||||
|
|
30
conn_test.go
30
conn_test.go
|
@ -111,28 +111,28 @@ func TestParseConfigExtractsStatementCacheOptions(t *testing.T) {
|
||||||
|
|
||||||
config, err := pgx.ParseConfig("statement_cache_capacity=0")
|
config, err := pgx.ParseConfig("statement_cache_capacity=0")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, config.BuildPreparedStatementCache)
|
require.Nil(t, config.BuildStatementCache)
|
||||||
|
|
||||||
config, err = pgx.ParseConfig("statement_cache_capacity=42")
|
config, err = pgx.ParseConfig("statement_cache_capacity=42")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, config.BuildPreparedStatementCache)
|
require.NotNil(t, config.BuildStatementCache)
|
||||||
c := config.BuildPreparedStatementCache(nil)
|
c := config.BuildStatementCache(nil)
|
||||||
require.NotNil(t, c)
|
require.NotNil(t, c)
|
||||||
require.Equal(t, 42, c.Cap())
|
require.Equal(t, 42, c.Cap())
|
||||||
require.Equal(t, stmtcache.ModePrepare, c.Mode())
|
require.Equal(t, stmtcache.ModePrepare, c.Mode())
|
||||||
|
|
||||||
config, err = pgx.ParseConfig("statement_cache_capacity=42 statement_cache_mode=prepare")
|
config, err = pgx.ParseConfig("statement_cache_capacity=42 statement_cache_mode=prepare")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, config.BuildPreparedStatementCache)
|
require.NotNil(t, config.BuildStatementCache)
|
||||||
c = config.BuildPreparedStatementCache(nil)
|
c = config.BuildStatementCache(nil)
|
||||||
require.NotNil(t, c)
|
require.NotNil(t, c)
|
||||||
require.Equal(t, 42, c.Cap())
|
require.Equal(t, 42, c.Cap())
|
||||||
require.Equal(t, stmtcache.ModePrepare, c.Mode())
|
require.Equal(t, stmtcache.ModePrepare, c.Mode())
|
||||||
|
|
||||||
config, err = pgx.ParseConfig("statement_cache_capacity=42 statement_cache_mode=describe")
|
config, err = pgx.ParseConfig("statement_cache_capacity=42 statement_cache_mode=describe")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, config.BuildPreparedStatementCache)
|
require.NotNil(t, config.BuildStatementCache)
|
||||||
c = config.BuildPreparedStatementCache(nil)
|
c = config.BuildStatementCache(nil)
|
||||||
require.NotNil(t, c)
|
require.NotNil(t, c)
|
||||||
require.Equal(t, 42, c.Cap())
|
require.Equal(t, 42, c.Cap())
|
||||||
require.Equal(t, stmtcache.ModeDescribe, c.Mode())
|
require.Equal(t, stmtcache.ModeDescribe, c.Mode())
|
||||||
|
@ -318,28 +318,28 @@ func TestExecExtendedProtocol(t *testing.T) {
|
||||||
ensureConnValid(t, conn)
|
ensureConnValid(t, conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExecPreparedStatementCacheModes(t *testing.T) {
|
func TestExecStatementCacheModes(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))
|
config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
buildPreparedStatementCache pgx.BuildPreparedStatementCacheFunc
|
buildStatementCache pgx.BuildStatementCacheFunc
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "disabled",
|
name: "disabled",
|
||||||
buildPreparedStatementCache: nil,
|
buildStatementCache: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "prepare",
|
name: "prepare",
|
||||||
buildPreparedStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
|
buildStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||||
return stmtcache.New(conn, stmtcache.ModePrepare, 32)
|
return stmtcache.New(conn, stmtcache.ModePrepare, 32)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "describe",
|
name: "describe",
|
||||||
buildPreparedStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
|
buildStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||||
return stmtcache.New(conn, stmtcache.ModeDescribe, 32)
|
return stmtcache.New(conn, stmtcache.ModeDescribe, 32)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -347,7 +347,7 @@ func TestExecPreparedStatementCacheModes(t *testing.T) {
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
func() {
|
func() {
|
||||||
config.BuildPreparedStatementCache = tt.buildPreparedStatementCache
|
config.BuildStatementCache = tt.buildStatementCache
|
||||||
conn := mustConnect(t, config)
|
conn := mustConnect(t, config)
|
||||||
defer closeConn(t, conn)
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ func TestPgbouncerStatementCacheDescribe(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
config := mustParseConfig(t, connString)
|
config := mustParseConfig(t, connString)
|
||||||
config.BuildPreparedStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
config.BuildStatementCache = func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||||
return stmtcache.New(conn, stmtcache.ModeDescribe, 1024)
|
return stmtcache.New(conn, stmtcache.ModeDescribe, 1024)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ func TestPgbouncerSimpleProtocol(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
config := mustParseConfig(t, connString)
|
config := mustParseConfig(t, connString)
|
||||||
config.BuildPreparedStatementCache = nil
|
config.BuildStatementCache = nil
|
||||||
config.PreferSimpleProtocol = true
|
config.PreferSimpleProtocol = true
|
||||||
|
|
||||||
testPgbouncer(t, config, 10, 100)
|
testPgbouncer(t, config, 10, 100)
|
||||||
|
|
|
@ -1566,28 +1566,28 @@ func TestConnSimpleProtocolRefusesNonStandardConformingStrings(t *testing.T) {
|
||||||
ensureConnValid(t, conn)
|
ensureConnValid(t, conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryPreparedStatementCacheModes(t *testing.T) {
|
func TestQueryStatementCacheModes(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))
|
config := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
buildPreparedStatementCache pgx.BuildPreparedStatementCacheFunc
|
buildStatementCache pgx.BuildStatementCacheFunc
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "disabled",
|
name: "disabled",
|
||||||
buildPreparedStatementCache: nil,
|
buildStatementCache: nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "prepare",
|
name: "prepare",
|
||||||
buildPreparedStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
|
buildStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||||
return stmtcache.New(conn, stmtcache.ModePrepare, 32)
|
return stmtcache.New(conn, stmtcache.ModePrepare, 32)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "describe",
|
name: "describe",
|
||||||
buildPreparedStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
|
buildStatementCache: func(conn *pgconn.PgConn) stmtcache.Cache {
|
||||||
return stmtcache.New(conn, stmtcache.ModeDescribe, 32)
|
return stmtcache.New(conn, stmtcache.ModeDescribe, 32)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1595,7 +1595,7 @@ func TestQueryPreparedStatementCacheModes(t *testing.T) {
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
func() {
|
func() {
|
||||||
config.BuildPreparedStatementCache = tt.buildPreparedStatementCache
|
config.BuildStatementCache = tt.buildStatementCache
|
||||||
conn := mustConnect(t, config)
|
conn := mustConnect(t, config)
|
||||||
defer closeConn(t, conn)
|
defer closeConn(t, conn)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue