jackc / pgx

PostgreSQL driver and toolkit for Go
MIT License
10.47k stars 828 forks source link

How to call pgx.Tx CopyFrom with sql.Tx #1904

Open firstone opened 7 months ago

firstone commented 7 months ago

pgx.Tx supports CopyFrom but there seems to be no way to use it from sql driver. Raw call will only produce pgx.Conn object but I don't see a way to call CopyFrom via sql.Tx

jackc commented 7 months ago

I don't know of any way to get a reference to a pgx object directly from a *sql.Tx. You could probably call Raw and BeginTx on the same *sql.Conn to start a transaction and be able to get the underlying *pgx.Conn -- but obviously that's a bit messy and error prone.

bushrod commented 1 month ago

FYI, I confirmed this does work by calling BeginTx, creating a temp table in the tx, and then doing CopyFrom into that temp table via Raw on the same conn (not referencing the tx directly).