Explicit type casting behaviors in pgx vs database/sql
- Dominant language
- Go
- Stars
- 14.3k
- Forks
- 1.1k
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 11
Description
**Describe the bug**
I found a slight difference in scanning behavior between pgx and database/sql. It appears pgx is more strict with type conversions.
For example, with this query:
```sql
SELECT
setting
FROM
pg_settings
WHERE
name = 'max_connections';
```
The standard library successfully scans into:
```go
type result struct {
MaxConnections int
}
var r result
err := row.Scan(
&r.MaxConnections,
)
```
But pgx's `row.Scan` fails with:
> can't scan into dest[0] (col: max_connections): cannot scan text (OID 25) in text format into *int
The fix is to cast in the query with `setting::int`, but should pgx behavior match the standard library more closely?
Specifically referring to the conversion in [`convertAssignRows`](https://github.com/golang/go/blob/3cefe69c5a5ad687f220696d4f8a2fe4d59241dd/src/database/sql/convert.go/#L225-L230)
ps. I don't think this is a bug (label automatically added). Mainly trying to understand the design decision and totally get why being _more_ strict is favorable.
Contributor guide
Research direction
Start by examining pgx's row.Scan conversion path and compare it with the linked database/sql convertAssignRows implementation. Use the provided pg_settings query and MaxConnections destination as the reproduction case. The issue needs a decided outcome—matching database/sql or documenting the stricter behavior—before completion can be defined.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, postgresql, sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100