pgx/pgconn
Anthonin Bonnefoy 228cfffc20 Unwatch and close connection on a batch write error
Previously, a conn.Write would simply unlock pgconn, leaving the
connection as Idle and reusable while the multiResultReader would be
closed. From this state, calling multiResultReader.Close won't try to
receiveMessage and thus won't unwatch and close the connection since it
is already closed. This leaves the connection "open" and the next time
it's used, a "Watch already in progress" panic could be triggered.

This patch fixes the issue by unwatching and closing the connection on a
batch write error. The same was done on Sync.Encode error even if the
path is unreachable as Sync.Error never returns an error.
2025-01-24 08:49:07 +01:00
..
ctxwatch Fix typo in test function name 2024-12-29 21:03:38 +08:00
internal/bgreader Allow customizing context canceled behavior for pgconn 2024-05-08 07:41:02 -05:00
README.md Improve testing / contributing instructions 2022-10-29 17:14:09 -05:00
auth_scram.go Spelling and grammar fixes 2023-10-07 09:26:23 -05:00
benchmark_private_test.go Use string internally for CommandTag 2022-07-11 21:09:03 -05:00
benchmark_test.go pgconn and pgproto use same environment variable for tests as pgx 2022-10-29 13:23:25 -05:00
config.go Switch from ExecParams to Exec in ValidateConnectTargetSessionAttrs functions 2024-11-19 19:42:49 +03:00
config_test.go Fix TestParseConfigEnvLibpq unsetting envars 2024-05-17 09:19:36 -05:00
defaults.go Make Chunkreader an internal implementation detail 2022-02-26 08:50:46 -06:00
defaults_windows.go Make Chunkreader an internal implementation detail 2022-02-26 08:50:46 -06:00
doc.go Replace DSN with keyword/value in comments and documentation 2024-05-11 14:33:35 -05:00
errors.go Replace DSN with keyword/value in comments and documentation 2024-05-11 14:33:35 -05:00
errors_test.go Replace DSN with keyword/value in comments and documentation 2024-05-11 14:33:35 -05:00
export_test.go Make pgconn.ConnectError and pgconn.ParseConfigError public 2024-01-12 17:52:25 -06:00
helper_test.go Drastically increase allowed test times for potato CI 2023-07-11 21:16:08 -05:00
krb5.go Handle writes that could deadlock with reads from the server 2023-06-12 09:39:26 -05:00
pgconn.go Unwatch and close connection on a batch write error 2025-01-24 08:49:07 +01:00
pgconn_private_test.go Use string internally for CommandTag 2022-07-11 21:09:03 -05:00
pgconn_stress_test.go pgconn and pgproto use same environment variable for tests as pgx 2022-10-29 13:23:25 -05:00
pgconn_test.go Unwatch and close connection on a batch write error 2025-01-24 08:49:07 +01:00

README.md

pgconn

Package pgconn is a low-level PostgreSQL database driver. It operates at nearly the same level as the C library libpq. It is primarily intended to serve as the foundation for higher level libraries such as https://github.com/jackc/pgx. Applications should handle normal queries with a higher level library and only use pgconn directly when required for low-level access to PostgreSQL functionality.

Example Usage

pgConn, err := pgconn.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
	log.Fatalln("pgconn failed to connect:", err)
}
defer pgConn.Close(context.Background())

result := pgConn.ExecParams(context.Background(), "SELECT email FROM users WHERE id=$1", [][]byte{[]byte("123")}, nil, nil, nil)
for result.NextRow() {
	fmt.Println("User 123 has email:", string(result.Values()[0]))
}
_, err = result.Close()
if err != nil {
	log.Fatalln("failed reading result:", err)
}

Testing

See CONTRIBUTING.md for setup instructions.