sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.36k stars 803 forks source link

Bulk Inserts #2254

Open philon123 opened 1 year ago

philon123 commented 1 year ago

What do you want to change?

I would like to use queries like

INSERT INTO books(id, name) VALUES
(NULL, 'Notebook'),
(NULL, 'MacBook'),
(NULL, 'BookBook')
..;

We have support for COPY here, but it's not as flexible or as easy to use as the above syntax. Also, the above syntax allows for ON CONFLICT(id) UPDATE name = EXCLUDED.name, which is great for bulk updates / upserts.

The generated Go code should take a list of Param objects as input. I'm not quite sure how the SQL template should look, but maybe something like

-- name: BulkInsertBooks :bulk
INSERT INTO books(id, name) VALUES @bulkBookRows;

What database engines need to be changed?

PostgreSQL

What programming language backends need to be changed?

Go

Jille commented 1 year ago

I've started a Discussion about how to implement this in https://github.com/kyleconroy/sqlc/discussions/2385

adi-kmt commented 7 months ago

Are we expecting this to be dynamic as well? Because Even subqueries and conflicts don't seem to work with copy from