Skip to content

Commit

Permalink
sql: fix prepared statement query summary
Browse files Browse the repository at this point in the history
This commit sets the StmtSummary metadata field for prepared
statements to the StatementSummary value in the prepared statement
metadata for prepared statements at execution.

Fixes #79152.

Release note (bug fix): Previously, the querySummary metadata field in
the crdb_internal.statement_statistics table was inconsistent with the
query metadata field for executed prepared statements. These fields
are now consistent for prepared statements.
  • Loading branch information
ericharmeling committed Jul 12, 2022
1 parent a69de1e commit 377e6fd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/sql/conn_executor_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ func (ex *connExecutor) execStmtInOpenState(
stmt.Prepared = ps
stmt.ExpectedTypes = ps.Columns
stmt.StmtNoConstants = ps.StatementNoConstants
stmt.StmtSummary = ps.StatementSummary
res.ResetStmtType(ps.AST)

if e.DiscardRows {
Expand Down
37 changes: 37 additions & 0 deletions pkg/sql/conn_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,43 @@ func TestAppNameStatisticsInitialization(t *testing.T) {
}
}

func TestPrepareStatisticsMetadata(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

s, sqlDB, _ := serverutils.StartServer(t, base.TestServerArgs{Insecure: true})
defer s.Stopper().Stop(context.Background())
defer sqlDB.Close()

// PREPARE a prepared statement.
stmt, err := sqlDB.Prepare("SELECT $1::int")
require.NoError(t, err)

// EXECUTE the prepared statement
_, err = stmt.Exec(3)
require.NoError(t, err)

// Verify that query and querySummary are equal in crdb_internal.statement_statistics.metadata.
rows, err := sqlDB.Query(`SELECT metadata->>'query', metadata->>'querySummary' FROM crdb_internal.statement_statistics WHERE metadata->>'query' LIKE 'SELECT $1::INT8'`)
if err != nil {
t.Fatal(err)
}
defer rows.Close()

var query, summary string
for rows.Next() {
if err := rows.Scan(&query, &summary); err != nil {
t.Fatal(err)
}
}
if len(query) < 1 {
t.Fatal("unable to retrieve query metadata")
}
if query != summary {
t.Fatalf("query is not consistent with querySummary")
}
}

func TestQueryProgress(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
Expand Down
3 changes: 1 addition & 2 deletions pkg/sql/prepared_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ type PreparedStatement struct {
createdAt time.Time
// origin is the protocol in which this prepare statement was created.
// Used for reporting on `pg_prepared_statements`.
origin PreparedStatementOrigin
StatementSummary string
origin PreparedStatementOrigin
}

// MemoryEstimate returns a rough estimate of the PreparedStatement's memory
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/querycache/prepared_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type PrepareMetadata struct {
// suitable for recording in statement statistics.
StatementNoConstants string

// StatementSummary is a summarized version of the query.
StatementSummary string

// Provides TypeHints and Types fields which contain placeholder typing
// information.
tree.PlaceholderTypesInfo
Expand Down

0 comments on commit 377e6fd

Please sign in to comment.