Skip to content

sql: include placeholder values in SHOW QUERIES #66689

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

Merged
merged 2 commits into from
Jun 23, 2021
Merged

sql: include placeholder values in SHOW QUERIES #66689

merged 2 commits into from
Jun 23, 2021

Conversation

matthewtodd
Copy link
Contributor

@matthewtodd matthewtodd commented Jun 21, 2021

Resolves #61569

Given this test script, running against a cockroach demo cluster:

require "pg"
c = PG::Connection.new(host: "localhost", port: 26257, user: "root")
c.prepare("test", "SELECT pg_sleep(20), upper($1)")
c.exec_prepared("test", ["hello"])
c.close

Here's what we would see before this change, in particular the $1 as the argument to upper():

> select query from [show queries] where application_name like '%run%';
              query
----------------------------------
  SELECT pg_sleep(20), upper($1)
(1 row)

And here's what we now see, the string 'hello' filled in:

> select query from [show queries] where application_name like '%run%';
                 query
---------------------------------------
  SELECT pg_sleep(20), upper('hello')
(1 row)

Release note (sql change): The SHOW QUERIES command was extended for prepared statements to show the actual values in use at query time, rather than the previous $1, $2, etc., placeholders.

We expect showing these values will greatly improve the experience of debugging slow queries.

@matthewtodd matthewtodd requested a review from a team June 21, 2021 21:08
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: mod nits

Reviewed 5 of 5 files at r1, 2 of 2 files at r2.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @matthewtodd)


pkg/sql/conn_executor_exec.go, line 737 at r2 (raw file):

			d, err := placeholder.Eval(evalCtx)
			if err != nil {
				// fall back to the default behavior if something goes wrong

super nit: Comments should be sentences.


pkg/sql/show_test.go, line 636 at r2 (raw file):

}

// #61569 sql: placeholder values for prepared statements should be visible in SHOW QUERIES

My understanding of the party line is that you shouldn't need to follow github issues to understand the context. It's okay to reference them but I think it's unusual to lead with them. Also, sentences for comments (sorry, I know it's nitpicky).


pkg/sql/show_test.go, line 654 at r2 (raw file):

					// Only observe queries when we're in an application session,
					// to limit concurrent access to the recordedQueries map.
					if session.ApplicationName == "application" {

nit: define "application" as a constant and then use it here and elsewhere -- then, in the queries, you can use it as a placeholder?

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, 👋 and welcome!

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @matthewtodd)

This allows test code to make decisions based on, for example, the
session id or the application name.

Release note: none
@matthewtodd matthewtodd requested a review from a team June 22, 2021 14:03
Copy link
Contributor Author

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Thanks for looking, @ajwerner!

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @ajwerner)


pkg/sql/conn_executor_exec.go, line 737 at r2 (raw file):

Previously, ajwerner wrote…

super nit: Comments should be sentences.

Done.

Copy link
Contributor

@Azhng Azhng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! 🎉

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @ajwerner and @matthewtodd)


pkg/sql/show_test.go, line 742 at r4 (raw file):

				actual := recordedQueries[test.statement]

				if actual != test.expected {

nit: I think we can use require.Equal here

Resolves #61569

Given this test script, running against a `cockroach demo` cluster:

```ruby
require "pg"
c = PG::Connection.new(host: "localhost", port: 26257, user: "root")
c.prepare("test", "SELECT pg_sleep(20), upper($1)")
c.exec_prepared("test", ["hello"])
c.close
```

Here's what we would see before this change, in particular the $1 as the
argument to `upper()`:

```
> select query from [show queries] where application_name like '%run%';
              query
----------------------------------
  SELECT pg_sleep(20), upper($1)
(1 row)
```

And here's what we now see, the string 'hello' filled in:

```
> select query from [show queries] where application_name like '%run%';
                 query
---------------------------------------
  SELECT pg_sleep(20), upper('hello')
(1 row)
```

Release note (sql change): The SHOW QUERIES command was extended for
prepared statements to show the actual values in use at query time,
rather than the previous $1, $2, etc., placeholders.

We expect showing these values will greatly improve the experience of
debugging slow queries.
Copy link
Contributor Author

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @ajwerner and @Azhng)


pkg/sql/show_test.go, line 742 at r4 (raw file):

Previously, Azhng (Archer Zhang) wrote…

nit: I think we can use require.Equal here

Awesome, I was wondering about that! Done.

@matthewtodd
Copy link
Contributor Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jun 23, 2021

Build succeeded:

@craig craig bot merged commit c5c7424 into cockroachdb:master Jun 23, 2021
@matthewtodd matthewtodd deleted the 61569-prepared-statement-placeholder-values branch June 23, 2021 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: placeholder values for prepared statements should be visible in SHOW QUERIES
4 participants