-
Notifications
You must be signed in to change notification settings - Fork 888
Efficient bulk imports via pq.CopyIn
#218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @kyleconroy, Any update on this. |
Nope, no update as of yet. It's low on my list, as I'm focusing on MySQL / SQLite support right now. |
Hi! Any updates on this? |
I found a way to do bulk insert and update with sqlc and Postgres: -- name: CreateAuthors :exec
INSERT INTO authors
SELECT unnest(@ids::bigint[]) AS id,
unnest(@names::text[]) AS name,
unnest(@bios::text[]) AS bio;
-- name: UpdateAuthors :exec
UPDATE authors AS a
SET name = tmp.name, bio = tmp.bio
FROM (
SELECT unnest(@ids::bigint[]) AS id,
unnest(@names::text[]) AS name,
unnest(@bios::text[]) AS bio
) AS tmp
WHERE a.id = tmp.id; It uses arrays to serialize multiple rows into 1 parameter and It felt a bit hacky, so I made a benchmark to ensure that it is working as expected:
By looking at If you want to try it, you can find the code of the benchmark here. |
Now that pgx support has landed, we'll also need support their Copy Protocol. |
@aitva Does this solution also work for fields which are Nullable? Because right now when I am creating my inserts this way I get []int64 for ::bigInt[] but I need to have []sql.NullInt64 because this field is nullable? Any ideas on how this could work? |
https://docs.sqlc.dev/en/latest/howto/insert.html#using-copyfrom has landed, so I think we can close this. |
@aitva We have been using the json_populate_recordset function in order to do bulk inserts which does allow for null values. There are other json functions you may want to use like json_to_record. Unfortunately the models generated by sqlc do not work well with these methods as instead of using NullInt64 you would want int64 with omit empty |
@Jille @kyleconroy, please consider reopening this issue to support the COPY instruction for bulk inserts with lib/pq, as originally mentioned above.
|
Let's make that a new feature request. Would you mind coming that? |
Sure, I've just opened a new issue for it: #3264. |
lib/pq
has support for efficiently inserting bulk items using COPY. sqlc currently can not generate code that exposes this functionality.See this StackOverflow answer for a great overview of the problem space.
The text was updated successfully, but these errors were encountered: