Query argument intrepretation is not compatible with lib/pq
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 14.3k
- Forks
- 1.1k
- Avg merge
- 6d 9h
- Merged PRs (30d)
- 11
Description
**Describe the bug**
I'm trying to move from "lib/pq" to "jackc/pgx" due to lack of "target_session_attrs=read-write" support in "lib/pq".
But unlike "lib/pq", "jackc/pgx" fails to handle different data types passed to the same query.
**To Reproduce**
Run an example shown below (please adjust dbname/user/password etc. as needed), with different combinations of import and sql.Open().
```go
package main
import (
"database/sql"
"fmt"
//_ "github.com/jackc/pgx/v4/stdlib"
_ "github.com/jackc/pgx/v5/stdlib"
//_ "github.com/lib/pq"
)
func main() {
var value string
if db, err := sql.Open(`pgx`, `host=127.0.0.1 port=5432 dbname=test user=test password=test sslmode=disable`); err != nil {
fmt.Println(1, err)
} else if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS test(a VARCHAR(6) NOT NULL PRIMARY KEY)`); err != nil {
fmt.Println(2, err)
} else if err := db.QueryRow(`SELECT * FROM test WHERE a = $1`, `123456`).Scan(&value); err != sql.ErrNoRows {
fmt.Println(3, err)
} else if err := db.QueryRow(`SELECT * FROM test WHERE a = $1`, 123456).Scan(&value); err != sql.ErrNoRows {
fmt.Println(4, err)
}
}
```
**Expected behavior**
This program prints nothing, for stdlib is the compatibility layer.
**Actual behavior**
With import github.com/lib/pq and sql.Open("postgres"), this program prints nothing.
With import github.com/jackc/pgx/v4 and sql.Open("pgx"), this program prints "4 cannot convert 123456 to Text" message.
With import github.com/jackc/pgx/v5 and sql.Open("pgx"), this program prints "4 failed to encode args[0]: unable to encode 123456 into text format for text (OID 25): cannot find encode plan" message.
**Version**
- Go: go version go1.20.1 linux/amd64
- PostgreSQL: PostgreSQL 13.10 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
- pgx: v4.18.1 and v5.3.1
- pq: v1.10.7
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided program with github.com/lib/pq and github.com/jackc/pgx/v4 or v5 through database/sql, comparing the two argument-handling results. Read the pgx stdlib compatibility layer and its query argument encoding path. Done means both string and integer values produce the expected sql.ErrNoRows behavior without an encoding error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, postgresql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100