Skip to content

Single-statement INSERT a variable number of rows without $1, $2, $3, $...N (since that N is variable) #1998

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

You must be logged in to vote

Ideally, I'd like to be able to insert multiple rows, but I'm not sure how to idiomatically do that without adding a variable number of placeholders. This is to say, I feel like the string I pass to pgx would have to change based on how many rows I'm upserting:

That's correct. It is rather awkward, but it is necessary to use a variable number of placeholders. (At least unless you did something really crazy like using an array of rows / structs that you used unnest to extract in PostgreSQL -- I don't recommend it 😉 )

And if the answer is "honestly, just use a batch, it's fine" then that's cool with me :)

Honestly, just use a batch. 😉 It's far simpler and the speed should be comparable,…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@wittekm
Comment options

Answer selected by wittekm
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