database/sql prepared statement names are deterministically generated

stdlib now uses the functionality introduced in
bbe2653bc5 for prepared statements. This
means that the prepared statement name is stable for a given query even
across connections and program executions.

It also makes tracing easier.

See https://github.com/jackc/pgx/issues/1754
pull/1762/head
Jack Christensen 2023-10-07 10:16:25 -05:00
parent c6c50110db
commit 0f0d236599
1 changed files with 4 additions and 8 deletions

View File

@ -397,7 +397,6 @@ func UnregisterConnConfig(connStr string) {
type Conn struct { type Conn struct {
conn *pgx.Conn conn *pgx.Conn
close func(context.Context) error close func(context.Context) error
psCount int64 // Counter used for creating unique prepared statement names
driver *Driver driver *Driver
connConfig pgx.ConnConfig connConfig pgx.ConnConfig
resetSessionFunc func(context.Context, *pgx.Conn) error // Function is called before a connection is reused resetSessionFunc func(context.Context, *pgx.Conn) error // Function is called before a connection is reused
@ -418,10 +417,7 @@ func (c *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
return nil, driver.ErrBadConn return nil, driver.ErrBadConn
} }
name := fmt.Sprintf("pgx_%d", c.psCount) sd, err := c.conn.Prepare(ctx, query, query)
c.psCount++
sd, err := c.conn.Prepare(ctx, name, query)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -558,7 +554,7 @@ type Stmt struct {
func (s *Stmt) Close() error { func (s *Stmt) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel() defer cancel()
return s.conn.conn.Deallocate(ctx, s.sd.Name) return s.conn.conn.Deallocate(ctx, s.sd.SQL)
} }
func (s *Stmt) NumInput() int { func (s *Stmt) NumInput() int {
@ -570,7 +566,7 @@ func (s *Stmt) Exec(argsV []driver.Value) (driver.Result, error) {
} }
func (s *Stmt) ExecContext(ctx context.Context, argsV []driver.NamedValue) (driver.Result, error) { func (s *Stmt) ExecContext(ctx context.Context, argsV []driver.NamedValue) (driver.Result, error) {
return s.conn.ExecContext(ctx, s.sd.Name, argsV) return s.conn.ExecContext(ctx, s.sd.SQL, argsV)
} }
func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error) { func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error) {
@ -578,7 +574,7 @@ func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error) {
} }
func (s *Stmt) QueryContext(ctx context.Context, argsV []driver.NamedValue) (driver.Rows, error) { func (s *Stmt) QueryContext(ctx context.Context, argsV []driver.NamedValue) (driver.Rows, error) {
return s.conn.QueryContext(ctx, s.sd.Name, argsV) return s.conn.QueryContext(ctx, s.sd.SQL, argsV)
} }
type rowValueFunc func(src []byte) (driver.Value, error) type rowValueFunc func(src []byte) (driver.Value, error)