-
Notifications
You must be signed in to change notification settings - Fork 886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSONB issue with default_query_exec_mode=simple_protocol #2231
Comments
it may or may not be related to #2020 or your proposal golang/go#67546 i can't define it yet |
From the docs regarding
You are passing a If you look at your PostgreSQL logs, you will see that the query actually sent to the server is something like:
Obviously, that is going to fail. With the simple protocol, you have to pass a The following example demonstrates it working: package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
_ "github.com/jackc/pgx/v5/stdlib"
)
func main() {
conf, err := pgx.ParseConfig(os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
conf.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
db := stdlib.OpenDB(*conf)
defer db.Close()
_, err = db.ExecContext(context.Background(), "create temporary table t (id int, data jsonb)")
if err != nil {
log.Fatal(err)
}
data, err := json.Marshal(map[string]int{"test": 1})
if err != nil {
log.Fatal(err)
}
_, err = db.ExecContext(context.Background(), "insert into t (id, data) values ($1, $2)", 1, string(data))
if err != nil {
log.Fatal(err)
}
var id int32
var dataBuf []byte
err = db.QueryRow("select * from t").Scan(&id, &dataBuf)
if err != nil {
log.Fatal(err)
}
fmt.Println(id, string(dataBuf))
} I've also updated the documentation to call out |
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: