4 ms·
It is much faster than INSERT indeed and I’m using it in places where I need to insert a lot of data into Postgres. Can you get RETURNING from COPY, though?
by murkt 6y ago
It is much faster than INSERT indeed and I’m using it in places where I need to insert a lot of data into Postgres. Can you get RETURNING from COPY, though?
- morrbo 6y agoI don't believe you can...however one of the pieces of code I'm quite proud of was using a combination of c# reflection and postgres built in types to convert and arbitray list of objects into a postgres binary representation and then insert that in bulk. In our situation we are able to "reserve" primary keys by incrementing the sequencer directly by the number of objects we are inserting (separate statement beforehand) and assign the ID before we insert it...so whilst you can't get the IDe from using "returning" you can work around it. Hope that helps someone :-)
- Smaug123 6y agoYou can't RETURNING from a copy, but you can `CREATE TEMP TABLE foo ON COMMIT DROP`, `COPY` into foo, then `INSERT INTO bar RETURNING` from the temp table.