Skip to content

ValidateConnect built in callbacks cannot have their errors type asserted. #2634

Description

@adriansmares

Is your feature request related to a problem? Please describe.

Hello, I am using pgx with a connection string which includes target_session_attrs=read-write. The built in callback correctly rejects connections to read only replicas:

pgx/pgconn/config.go

Lines 1070 to 1083 in b56d88f

// ValidateConnectTargetSessionAttrsReadWrite is a ValidateConnectFunc that implements libpq compatible
// target_session_attrs=read-write.
func ValidateConnectTargetSessionAttrsReadWrite(ctx context.Context, pgConn *PgConn) error {
result, err := pgConn.Exec(ctx, "show transaction_read_only").ReadAll()
if err != nil {
return err
}
if string(result[0].Rows[0][0]) == "on" {
return errors.New("read only connection")
}
return nil
}

However, I cannot use errors.Is with this error, and have to assert the error text.

Describe the solution you'd like

I would like for there to be dedicated errors such that I can do errors.Is(err, package.ErrConnectionReadOnly) in my transaction retry logic.

Describe alternatives you've considered

The error can either live in pgconn, or somewhere else. I am not sure if it should be a new set of errors or we should reuse existing errors in order to reduce duplication - any feedback is open.

Additional context

I can implement this change, but please let me know your thoughts on which errors we should use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions