Skip to content

Commit 0f0d236

Browse files
committed
database/sql prepared statement names are deterministically generated
stdlib now uses the functionality introduced in bbe2653 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 #1754
1 parent c6c5011 commit 0f0d236

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

stdlib/sql.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,6 @@ func UnregisterConnConfig(connStr string) {
397397
type Conn struct {
398398
conn *pgx.Conn
399399
close func(context.Context) error
400-
psCount int64 // Counter used for creating unique prepared statement names
401400
driver *Driver
402401
connConfig pgx.ConnConfig
403402
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
418417
return nil, driver.ErrBadConn
419418
}
420419

421-
name := fmt.Sprintf("pgx_%d", c.psCount)
422-
c.psCount++
423-
424-
sd, err := c.conn.Prepare(ctx, name, query)
420+
sd, err := c.conn.Prepare(ctx, query, query)
425421
if err != nil {
426422
return nil, err
427423
}
@@ -558,7 +554,7 @@ type Stmt struct {
558554
func (s *Stmt) Close() error {
559555
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
560556
defer cancel()
561-
return s.conn.conn.Deallocate(ctx, s.sd.Name)
557+
return s.conn.conn.Deallocate(ctx, s.sd.SQL)
562558
}
563559

564560
func (s *Stmt) NumInput() int {
@@ -570,15 +566,15 @@ func (s *Stmt) Exec(argsV []driver.Value) (driver.Result, error) {
570566
}
571567

572568
func (s *Stmt) ExecContext(ctx context.Context, argsV []driver.NamedValue) (driver.Result, error) {
573-
return s.conn.ExecContext(ctx, s.sd.Name, argsV)
569+
return s.conn.ExecContext(ctx, s.sd.SQL, argsV)
574570
}
575571

576572
func (s *Stmt) Query(argsV []driver.Value) (driver.Rows, error) {
577573
return nil, errors.New("Stmt.Query deprecated and not implemented")
578574
}
579575

580576
func (s *Stmt) QueryContext(ctx context.Context, argsV []driver.NamedValue) (driver.Rows, error) {
581-
return s.conn.QueryContext(ctx, s.sd.Name, argsV)
577+
return s.conn.QueryContext(ctx, s.sd.SQL, argsV)
582578
}
583579

584580
type rowValueFunc func(src []byte) (driver.Value, error)

0 commit comments

Comments
 (0)