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

database/sql: collect database metric information using dbstats #39161

4orty opened this issue May 20, 2020 · 3 comments

database/sql: collect database metric information using dbstats #39161

4orty opened this issue May 20, 2020 · 3 comments


Copy link

@4orty 4orty commented May 20, 2020

What version of Go are you using (go version)?

$ go version 1.14

Does this issue reproduce with the latest release?


What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

Purpose: I want to get database metric information in go application.
So here is a source code.

db, err := sql.Open("postgres", connectionInfo)

dbstat := db.Stats()

What did you expect to see?

I expected the printed number is 20

What did you see instead?

The result of the code was 0 (default value)

When i took a look at dbstat struct, I realized that all the type of field assigned int or string, not pointer.
Is there any problem that change fields of DBstat struct to pointer value?
If it assigned pointer value, it seems like can collect database metric information in real time.

Copy link

@seankhliao seankhliao commented May 20, 2020

db.Stats() should be called when you need the data,
this is the same as the other metrics exposed by the stdlib, ex in runtime

having unprotected pointers to mutable data races possible

Copy link

@cagedmantis cagedmantis commented May 20, 2020

As mentioned by @seankhliao, db.Stats() should be called at the point when you are ready to collect the data. It should not be able to mutate the state of DB.

I'm going to close this issue since this is working as expected. Please feel free to follow up if this was done in error.

Copy link

@4orty 4orty commented May 21, 2020

@cagedmantis Thanks for reply. If i want to collect DB metrics every second, is it ok to call db.Stats() every second?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.