You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
An "invalid input syntax for type json" error occurs when attempting to pass a []byte as an argument for a query.
Specifically, the error message is:
ERROR: invalid input syntax for type json (SQLSTATE 22P02)
To Reproduce
Create a table with a jsonb field and attempt to insert values into it. The code below demonstrates the issue.
package main
import (
"context""database/sql""encoding/json""github.com/jackc/pgx/v5""github.com/jackc/pgx/v5/stdlib""github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
funcmain() {
conf, err:=pgx.ParseConfig("postgresql://test:test123456@localhost:5432/postgres?default_query_exec_mode=simple_protocol")
// conf, err := pgx.ParseConfig("postgresql://test:test123456@localhost:5432/postgres")iferr!=nil {
panic(err)
}
connector:=stdlib.GetConnector(*conf)
db:=sqlx.NewDb(sql.OpenDB(connector), "pgx")
deferdb.Close()
pqDb, err:=sqlx.Connect("postgres", "postgresql://test:test123456@localhost:5432/postgres?binary_parameters=yes&sslmode=disable")
iferr!=nil {
panic(err)
}
deferpqDb.Close()
_, err=db.Exec("CREATE TABLE IF NOT EXISTS test (id serial PRIMARY KEY, data jsonb NOT NULL);")
iferr!=nil {
panic(err)
}
b, _:=json.Marshal(map[string]int{"test": 1})
query:=`INSERT INTO test (data) VALUES ($1)`_, err=pqDb.ExecContext(context.Background(), query, append([]byte{0x01}, b...)) // this one is required for pq to work with binary_protocol=yesiferr!=nil {
panic("pq_nonempty_map: "+err.Error())
}
_, err=db.ExecContext(context.Background(), query, append([]byte{0x01}, b...)) // it does not matter if I add 1 byte in fornt of b or not. result will be the sameiferr!=nil {
panic("pgx_nonempty_map: "+err.Error())
}
b, _=json.Marshal(map[string]int{})
_, err=pqDb.ExecContext(context.Background(), query, append([]byte{0x01}, b...))
iferr!=nil {
panic("pq_empty_map: "+err.Error())
}
_, err=db.ExecContext(context.Background(), query, b)
iferr!=nil {
panic("pgx_empty_map: "+err.Error())
}
}
Expected behaviour
Data should be inserted into the table without any issues, as it does when using lib/pq.
Actual behaviour
An error appears when inserting data into the table.
Version
Go: go version go1.23.1 darwin/arm64
PostgreSQL: PostgreSQL 14.10 (Debian 14.10-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
pgx: v5.7.2
Additional context
Postgres 14 behind pgbouncer for production environment
Project is running using lib/pq+sqlx. It works as intended.
Local testing is done in an ordinary Docker container with PostgreSQL, but it is necessary to work with the simple protocol due to pgbouncer restrictions.
pgx/stdlib is working fine if default_query_exec_mode is not set to simple_protocol
The text was updated successfully, but these errors were encountered:
Describe the bug
An "invalid input syntax for type json" error occurs when attempting to pass a
[]byte
as an argument for a query.Specifically, the error message is:
To Reproduce
Create a table with a
jsonb
field and attempt to insert values into it. The code below demonstrates the issue.Expected behaviour
Data should be inserted into the table without any issues, as it does when using lib/pq.
Actual behaviour
An error appears when inserting data into the table.
Version
go version go1.23.1 darwin/arm64
PostgreSQL 14.10 (Debian 14.10-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
v5.7.2
Additional context
Local testing is done in an ordinary Docker container with PostgreSQL, but it is necessary to work with the simple protocol due to pgbouncer restrictions.
pgx/stdlib
is working fine ifdefault_query_exec_mode
is not set tosimple_protocol
The text was updated successfully, but these errors were encountered: