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
Attempting a query with an input parameter of type jsonb not null, using driver pgx/v4, and with prefer_simple_protocol=true results in the query failing with:
ERROR: invalid input syntax for type json (SQLSTATE 22P02)
func (plan*encodePlanDriverValuer) Encode(valueany, buf []byte) (newBuf []byte, errerror) {
dv:=value.(driver.Valuer)
ifdv==nil {
returnnil, nil
}
v, err:=dv.Value() // hereiferr!=nil {
returnnil, err
}
ifv==nil {
returnnil, nil
}
newBuf, err=plan.m.Encode(plan.oid, plan.formatCode, v, buf)
iferr==nil {
returnnewBuf, nil
}
s, ok:=v.(string)
if!ok {
returnnil, err
}
varscannedValueanyscanErr:=plan.m.Scan(plan.oid, TextFormatCode, []byte(s), &scannedValue)
ifscanErr!=nil {
returnnil, err
}
// Prevent infinite loop. We can't encode this. See https://github.com/jackc/pgx/issues/1331.ifreflect.TypeOf(value) ==reflect.TypeOf(scannedValue) {
returnnil, fmt.Errorf("tried to encode %v via encoding to text and scanning but failed due to receiving same type back", value)
}
varerr2errornewBuf, err2=plan.m.Encode(plan.oid, BinaryFormatCode, scannedValue, buf)
iferr2!=nil {
returnnil, err
}
returnnewBuf, nil
}
I think that the value should be determined by sql.Scanner.Value and should not be guessed by pgx. If I choose to use a struct in place of json.RawMessage, pgx will mistakenly translate the JSON string to '\x...'
@kyleconroy I believe this is fine in pgx/v5, since it uses different types here. I'm in the process of migrating our uses to pgx/v5, so I believe this will eventually not be a problem for me personally (but may be for others still of course!)
JSONB query support with pgx/v4 and prefer_simple_protocol results in ERROR: invalid input syntax for type json (SQLSTATE 22P02) · Issue #2085 · sqlc-dev/sqlc
Activity
jinlongchen commentedon May 2, 2023
I think that the value should be determined by sql.Scanner.Value and should not be guessed by pgx. If I choose to use a struct in place of json.RawMessage, pgx will mistakenly translate the JSON string to '\x...'
kyleconroy commentedon Sep 26, 2023
Is this still an issue with
pgx/v5
? I don't have a way to reproduce this issue, so if you're still seeing this with v5, please add a schema and query.samavos commentedon Sep 26, 2023
@kyleconroy I believe this is fine in
pgx/v5
, since it uses different types here. I'm in the process of migrating our uses topgx/v5
, so I believe this will eventually not be a problem for me personally (but may be for others still of course!)geekodour commentedon Apr 9, 2024
@kyleconroy still having this issue on v5
just spent 3+ hours with this, so dumping things here. Too bad I didn't see this issue before.
Then I am using sqlc to construct this query (only relevant snippet)
I've query logging on: the actual query (has some actual data)
Error that I get:
All of this disappears if I don't use
pgx.QueryExecModeSimpleProtocol
as theDefaultQueryExecMode
Arttii commentedon May 16, 2024
Is there any fix for this when using SimpleProtocol with pg bouncer?