@@ -17,16 +17,19 @@ import (
17
17
"testing"
18
18
"time"
19
19
20
+ "github.com/stretchr/testify/assert"
21
+ "github.com/stretchr/testify/require"
22
+
20
23
"github.com/jackc/pgx/v5"
21
24
"github.com/jackc/pgx/v5/internal/pgio"
22
25
"github.com/jackc/pgx/v5/internal/pgmock"
23
26
"github.com/jackc/pgx/v5/pgconn"
24
27
"github.com/jackc/pgx/v5/pgproto3"
25
28
"github.com/jackc/pgx/v5/pgtype"
26
- "github.com/stretchr/testify/assert"
27
- "github.com/stretchr/testify/require"
28
29
)
29
30
31
+ const pgbouncerConnStringEnvVar = "PGX_TEST_PGBOUNCER_CONN_STRING"
32
+
30
33
func TestConnect (t * testing.T ) {
31
34
tests := []struct {
32
35
name string
@@ -2256,18 +2259,44 @@ func TestConnCancelRequest(t *testing.T) {
2256
2259
func TestConnContextCanceledCancelsRunningQueryOnServer (t * testing.T ) {
2257
2260
t .Parallel ()
2258
2261
2262
+ t .Run ("postgres" , func (t * testing.T ) {
2263
+ t .Parallel ()
2264
+
2265
+ testConnContextCanceledCancelsRunningQueryOnServer (t , os .Getenv ("PGX_TEST_DATABASE" ), "postgres" )
2266
+ })
2267
+
2268
+ t .Run ("pgbouncer" , func (t * testing.T ) {
2269
+ t .Parallel ()
2270
+
2271
+ connString := os .Getenv (pgbouncerConnStringEnvVar )
2272
+ if connString == "" {
2273
+ t .Skipf ("Skipping due to missing environment variable %v" , pgbouncerConnStringEnvVar )
2274
+ }
2275
+
2276
+ testConnContextCanceledCancelsRunningQueryOnServer (t , connString , "pgbouncer" )
2277
+ })
2278
+ }
2279
+
2280
+ func testConnContextCanceledCancelsRunningQueryOnServer (t * testing.T , connString , dbType string ) {
2259
2281
ctx , cancel := context .WithTimeout (context .Background (), 120 * time .Second )
2260
2282
defer cancel ()
2261
2283
2262
- pgConn , err := pgconn .Connect (ctx , os . Getenv ( "PGX_TEST_DATABASE" ) )
2284
+ pgConn , err := pgconn .Connect (ctx , connString )
2263
2285
require .NoError (t , err )
2264
2286
defer closeConn (t , pgConn )
2265
2287
2266
- pid := pgConn .PID ()
2267
-
2268
2288
ctx , cancel = context .WithTimeout (ctx , 100 * time .Millisecond )
2269
2289
defer cancel ()
2270
- multiResult := pgConn .Exec (ctx , "select 'Hello, world', pg_sleep(30)" )
2290
+
2291
+ // Getting the actual PostgreSQL server process ID (PID) from a query executed through pgbouncer is not straightforward
2292
+ // because pgbouncer abstracts the underlying database connections, and it doesn't expose the PID of the PostgreSQL
2293
+ // server process to clients. However, we can check if the query is running by checking the generated query ID.
2294
+ queryID := fmt .Sprintf ("%s testConnContextCanceled %d" , dbType , time .Now ().UnixNano ())
2295
+
2296
+ multiResult := pgConn .Exec (ctx , fmt .Sprintf (`
2297
+ -- %v
2298
+ select 'Hello, world', pg_sleep(30)
2299
+ ` , queryID ))
2271
2300
2272
2301
for multiResult .NextResult () {
2273
2302
}
@@ -2283,7 +2312,7 @@ func TestConnContextCanceledCancelsRunningQueryOnServer(t *testing.T) {
2283
2312
ctx , cancel = context .WithTimeout (context .Background (), 10 * time .Second )
2284
2313
defer cancel ()
2285
2314
2286
- otherConn , err := pgconn .Connect (ctx , os . Getenv ( "PGX_TEST_DATABASE" ) )
2315
+ otherConn , err := pgconn .Connect (ctx , connString )
2287
2316
require .NoError (t , err )
2288
2317
defer closeConn (t , otherConn )
2289
2318
@@ -2292,8 +2321,8 @@ func TestConnContextCanceledCancelsRunningQueryOnServer(t *testing.T) {
2292
2321
2293
2322
for {
2294
2323
result := otherConn .ExecParams (ctx ,
2295
- `select 1 from pg_stat_activity where pid= $1` ,
2296
- [][]byte {[]byte (strconv . FormatInt ( int64 ( pid ), 10 ) )},
2324
+ `select 1 from pg_stat_activity where query like $1` ,
2325
+ [][]byte {[]byte ("%" + queryID + "%" )},
2297
2326
nil ,
2298
2327
nil ,
2299
2328
nil ,
0 commit comments