pgx/pgconn
Evan Jones 11d892dfcf pgconn.CancelRequest: Fix unix sockets: don't use RemoteAddr()
The tests for cancelling requests were failing when using unix
sockets. The reason is that net.Conn.RemoteAddr() calls getpeername()
to get the address. For Unix sockets, this returns the address that
was passed to bind() by the *server* process, not the address that
was passed to connect() by the *client*. For postgres, this is always
relative to the server's directory, so is a path like:

    ./.s.PGSQL.5432

Since it does not return the full absolute path, this function cannot
connect, so it cannot cancel requests. To fix it, use the connection's
config for Unix sockets. I think this should be okay, since a system
using unix sockets should not have "fallbacks". If that is incorrect,
we will need to save the address on PgConn.

Fixes the following failed tests when using Unix sockets:

--- FAIL: TestConnCancelRequest (2.00s)
    pgconn_test.go:2056:
          Error Trace:  /Users/evan.jones/pgx/pgconn/pgconn_test.go:2056
                              /Users/evan.jones/pgx/pgconn/asm_arm64.s:1172
          Error:        Received unexpected error:
                        dial unix ./.s.PGSQL.5432: connect: no such file or directory
          Test:         TestConnCancelRequest
    pgconn_test.go:2063:
          Error Trace:  /Users/evan.jones/pgx/pgconn/pgconn_test.go:2063
          Error:        Object expected to be of type *pgconn.PgError, but was <nil>
          Test:         TestConnCancelRequest
--- FAIL: TestConnContextCanceledCancelsRunningQueryOnServer (5.10s)
    pgconn_test.go:2109:
          Error Trace:  /Users/evan.jones/pgx/pgconn/pgconn_test.go:2109
          Error:        Received unexpected error:
                        timeout: context already done: context deadline exceeded
          Test:         TestConnContextCanceledCancelsRunningQueryOnServer
2023-05-20 08:08:47 -05:00
..
internal/ctxwatch Unwatch at end of test 2023-02-14 09:03:41 -06:00
README.md Improve testing / contributing instructions 2022-10-29 17:14:09 -05:00
auth_scram.go Merge remote-tracking branch 'pgconn/master' into v5-dev 2022-07-12 06:45:54 -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 Use Go 1.19's lists for proper formatting 2023-01-23 19:54:30 -06:00
config_test.go Apply `gofmt -s` 2023-01-24 07:55:00 -06: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 Add pipeline mode to pgconn 2022-07-02 21:48:16 -05:00
errors.go pgconn.Timeout() no longer considers `context.Canceled` as a timeout error. 2022-09-17 10:18:06 -05:00
errors_test.go Bump module version to v5 2021-12-11 13:29:03 -06:00
export_test.go Import to pgx main repo in pgconn subdir 2021-12-04 13:51:24 -06:00
helper_test.go Bump module version to v5 2021-12-11 13:29:03 -06:00
krb5.go Merge remote-tracking branch 'pgconn/master' into v5-dev 2022-07-12 06:45:54 -05:00
pgconn.go pgconn.CancelRequest: Fix unix sockets: don't use RemoteAddr() 2023-05-20 08:08:47 -05: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 Apply `gofmt -s` 2023-01-24 07:55:00 -06: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.