Skip to content

Insert multiple rows into a table using a single statement #1545

Answered by jackc
stasvo asked this question in Q&A
Discussion options

You must be logged in to vote

The COPY protocol is the fastest way to insert many rows at a time. https://pkg.go.dev/github.com/jackc/pgx/[email protected]#hdr-Copy_Protocol

Next in speed would be one insert statement with multiple rows as you have above. That is a little awkward to use because you have to build the SQL. You also are limited to ~64k parameters.

A batch operation that bundles bunch of simple inserts together would be next in performance. https://pkg.go.dev/github.com/jackc/pgx/[email protected]#Conn.SendBatch

And slowest, of course, would be inserting each row one at a time.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@stasvo
Comment options

@jackc
Comment options

@stasvo
Comment options

@stasvo
Comment options

@jackc
Comment options

Answer selected by stasvo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants