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
{{ message }}
Repository navigation
Running :copyfrom with a uuid causes ERROR: incorrect binary data format (SQLSTATE 22P03) with PGX #1507
I have encountered this problem too. I do not think this is a bug. But it is nice to write some documents to warn people who want to COPY UUID type.
The reason for this problem is neither google/uuid nor gofrs/uuid implements the BinaryEncoder interface (link) which needs by the pgx to encode your data during COPY.
A quick solution is to add overrides settings to sqlc.yaml to force using UUID type from github.com/jackc/pgtype
I'm running into this too with the following settings:
go version go1.19.4 darwin/amd64
sqlc version v1.17.0
pgx v4
query type :copyfrom
UUID type "github.com/google/uuid"
I noticed an extra detail here which is that the failure only happens for me when a NullUUID is present in the copyfrom query as one of the data rows to insert. Normal UUID behaves fine.
This is consistent with the OP's case it seems since ad_check_parent_id is also nullable there based on the posted schema and that's why the manual intervention hack of adding MarshalBinary() explicitly works.
Is there a better workaround here than overriding with the pgtype as suggested? We have far too many locations where we've been using Google's UUID to make this an easy swap by doing so and that just feels way too heavy-handed as a workaround. We can't afford to wait for pgx v5 to settle either (not to mention the cost of switching over to that).
Is the next best option to change the copyfrom query to a transacted loop of single inserts? I spent some time trying to get a custom type to work but it seemed to suffer the same problem which makes sense since it still doesn't really affect how the copyfrom code is being generated or used.
Activity
austincollinpena commentedon Mar 22, 2022
Updating my code from:
to:
Worked :)
CNLHC commentedon Sep 7, 2022
I have encountered this problem too. I do not think this is a bug. But it is nice to write some documents to warn people who want to
COPYUUID type.The reason for this problem is neither
google/uuidnorgofrs/uuidimplements theBinaryEncoderinterface (link) which needs by thepgxto encode your data duringCOPY.A quick solution is to add
overridessettings tosqlc.yamlto force usingUUIDtype fromgithub.com/jackc/pgtypeFor example
sudotliu commentedon Feb 16, 2023
I'm running into this too with the following settings:
:copyfromI noticed an extra detail here which is that the failure only happens for me when a
NullUUIDis present in the copyfrom query as one of the data rows to insert. Normal UUID behaves fine.This is consistent with the OP's case it seems since
ad_check_parent_idis also nullable there based on the posted schema and that's why the manual intervention hack of addingMarshalBinary()explicitly works.Is there a better workaround here than overriding with the pgtype as suggested? We have far too many locations where we've been using Google's UUID to make this an easy swap by doing so and that just feels way too heavy-handed as a workaround. We can't afford to wait for pgx v5 to settle either (not to mention the cost of switching over to that).
Is the next best option to change the copyfrom query to a transacted loop of single inserts? I spent some time trying to get a custom type to work but it seemed to suffer the same problem which makes sense since it still doesn't really affect how the copyfrom code is being generated or used.
kyleconroy commentedon Sep 22, 2023
I believe this has been fixed in
pgx/v5. Could you upgrade to v5 and let me know if you're still seeing the issue?sudotliu commentedon Sep 27, 2023
@kyleconroy , got some time to work on the upgrade today and this does appear to be fixed now in v5, thank you very much sir!
For anyone else migrating from v4, these were super helpful resources: