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: no good way to set execution deadlines #8874

gopherbot opened this issue Oct 6, 2014 · 6 comments

database/sql: no good way to set execution deadlines #8874

gopherbot opened this issue Oct 6, 2014 · 6 comments


Copy link

@gopherbot gopherbot commented Oct 6, 2014

by wmorgan.masanjin:

There's currently (circa go 1.3.3) no good way to set execution deadlines on database
operations as provided by database/sql. Systems that e.g. use to
represent end-to-end request deadlines must either stop enforcing deadlines at the
database layer, or make use of system-specific hacks.

For example, a system that uses for requests that are backed by database
operations with database/sql might have a function like this:

  func getName(ctx context.Context, db *sql.DB, id int) (name string, err error) { 
    err = db.QueryRow("select name from things where id=? limit 1", id).Scan(&name)

Note that ctx is ignored in this implementation. With the current database/sql API,
there are at least two ways of making use of it, but they both have problems:

1. Run QueryRow() in a goroutine, emit the results to a channel, and select() across
that channel and ctx.Done(). This would bound the execution time of the function, but
would leave the query dangling on the database side, where it would continue to consume
database resources.

2. Wrap every query in a transaction and execute a system-specific timeout command prior
to the query as part of the transaction. For example, Postgres allows setting timeouts
on a per-transaction basis via a 'set local statement_timeout' command. This approach
allows database-side timeouts, but necessitates the overhead of adding a transaction to
every query, which may be significant.

Some alternatives that are not currently possible, but which would allow for the desired

3. An alternative (parallel?) API for database/sql that makes use of
directly, and requires drivers to implement these constraints when supported by the
underlying database.

4. Additional APIs for exclusive access to individual connections outside of
transactions. This would allow setting per-connection timeouts (and other settings) on
databases that support it, at the application level, without incurring the overhead of

5. Something else?

See!topic/golang-nuts/c7gfPQ8gIvM for some discussion.
Copy link

@ianlancetaylor ianlancetaylor commented Oct 6, 2014

Comment 1:

Labels changed: added repo-main, release-none.

@gopherbot gopherbot added new labels Oct 6, 2014
@bradfitz bradfitz removed the new label Dec 18, 2014
Copy link

@c4milo c4milo commented Mar 18, 2015

In the mailing list was mentioned that this could probably make it into Go 1.5, does that still hold true?

Copy link

@johto johto commented Mar 18, 2015

I wrote a CL:, but it's sort of waiting for my other patches go through first, and those are waiting on bradfitz, who's anxiously waiting for his vacation to end. I can't comment on its chances of getting into 1.5, but I'm certainly willing to put in the work to get it there.

Copy link

@rajeshucsb rajeshucsb commented Mar 28, 2016

@johto this is really useful feature, is there any way of getting your CL checked in.

Copy link

@kardianos kardianos commented Aug 12, 2016

This should be merged into #15123

Copy link

@bradfitz bradfitz commented Sep 9, 2016

Indeed, done.

@bradfitz bradfitz closed this Sep 9, 2016
@golang golang locked and limited conversation to collaborators Sep 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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