Using github.com/lib/pq Timeouts in Go does not work

Dear,

using github.com/lib/pq lib it seems that context.WithTimeout does not work in GO:

ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

// Pass the child context (the one with the timeout) as the first
// parameter to ExecContext().
_, err := db.ExecContext(ctx, "SELECT pg_sleep(10)")

CockroachDB does not support the cancellation protocol used by lib/pq. That is tracked in this issue: https://github.com/cockroachdb/cockroach/issues/41335

As a workaround, you can do one of the following:

  1. use a different driver like pgx (we recommend pgx instead of lib/pq)
  2. use the statement_timeout session variable
  3. start a timer in your application and close the connection after the timer elapses

Ok, we are using option 2).