-
-
Notifications
You must be signed in to change notification settings - Fork 954
Can't run two queries in a transaction? #81
Description
I have some data I've loaded into a table from a CSV file that I want to massage into a master table. When I try to do this in a transaction, pq just hangs on the second query I perform for no reason I can ascertain.
Here's my code. I chopped out everything after the second query; that query checks to see if the data is already migrated, and if not, runs all the create code and such. If you comment out the "select * from community_gnis" line, the loop runs to completion, printing every city in the table. If I leave the query in, it just hangs after the first city name.
if db, err := sql.Open("postgres", "user=postgres dbname=geo sslmode=disable"); err == nil {
defer db.Close()
if tx, err := db.Begin(); err == nil {
if cities, err := tx.Query("select name, state, id, lon, lat from noaa.cities where id is not null"); err == nil {
var name, state, gnis string
var longitude, latitude float32
for cities.Next() {
if err = cities.Scan(&name, &state, &gnis, &longitude, &latitude); err == nil {
fmt.Println("Got city", name, "(", gnis, ")")
tx.Query("select * from community_gnis where gnis = $1", gnis)
}
}
}
}
}
I'm new to Go, so maybe I'm doing things incorrectly here. However, if I move the "select * from community_gnis" up above the "select name, state..." statement, the community_gnis statement runs fine and the "select name, state..." query will then be the one that hangs.
If I remove the transaction code and run everything against "db", e.g. "db.Query" instead of "tx.Query", etc., everything runs completely fine (though it doesn't run in a transaction, which is what I want).
I've tried various combinations, using prepared statements, adding Close calls everywhere, etc. Nothing works. I'm 99% sure it's not the database, because I'm migrating this code from a Node JS app to test whether we should use Go or Node for our project, and the Node version runs completely in a transaction.