Need help understanding CopyFrom
into simple enum
columns
#1519
-
Hi, I am facing difficulties in understanding
I have tried several ways of incorporating the enum value in the source Slice. I have tried a literal string (since it seems to work in the example above). I have tried a literal
I have gone through all examples as well as tests to see if I might find an example with an Each time I get the same error:
or some variation of this depending on what I send into the source Slice. For example:
Do I need an encode plan? If so, can you point me to an example of one such? And there is the question of why a literal string in the source works for a table with a few columns but not when the number of columns is bigger. I am using the latest version of Go (1.20.1) and the latest version of pgx/pgxpool (5.3.0) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
There are two formats to encode values to send to PostgreSQL, text and binary. Normal queries can mix and match the format of parameters. The copy protocol requires all data be in the same format. The reason your enums work for normal queries is it can fall back to the text format. However, pgx's You have the right idea with |
Beta Was this translation helpful? Give feedback.
-
I now loaded and registered the type in the AfterConnect hook and bulk-copying enum columns works fine. Thank you :) Indeed, I expected bulk copy to use binary encoding exclusively, and I am still not sure why it prefers text encoding for small tables. My sample code also uses the copy command as you can see from the first snippet. Thanks for the AfterConnect suggestion |
Beta Was this translation helpful? Give feedback.
There are two formats to encode values to send to PostgreSQL, text and binary. Normal queries can mix and match the format of parameters. The copy protocol requires all data be in the same format. The reason your enums work for normal queries is it can fall back to the text format. However, pgx's
CopyFrom
exclusively uses the binary format. That is why your enum fails there.You have the right idea with
LoadType
andRegisterType
, however, you are running it in the wrong place. It should be run in a AfterConnect hook (https://pkg.go.dev/github.com/jackc/pgx/[email protected]/pgxpool#Config).