Skip to content

Instantly share code, notes, and snippets.

@wkalt
Created April 30, 2019 04:15
Show Gist options
  • Save wkalt/08cfb70216a6420cae3eda83db209f02 to your computer and use it in GitHub Desktop.
Save wkalt/08cfb70216a6420cae3eda83db209f02 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
func main() {
db, err := sql.Open("postgres", "")
if err != nil {
panic(err)
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
panic(err)
}
// if you execute these in psql in a transaction, you get a rollback
query := `set local statement_timeout = '1s';
select pg_sleep(10)`
_, err = tx.Exec(query)
if err != nil {
tx.Rollback()
fmt.Println("timed out")
} else {
tx.Commit()
fmt.Println("failed to time out")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment