Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix prepared statement query summary #83673

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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