sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

Add PgBindIter for encoding and use it as the implementation encoding &[T]

Open tylerhawkes opened this issue 1 year ago • 4 comments

Partial fix for postgres of https://github.com/launchbadge/sqlx/issues/294

I've been using this implementation in several different projects and thought that I would see if it can be accepted upstream. I don't care about the name, but it is only useful when calling .bind(PgBindIter(iter...)), so that's what I've been calling it.

I use this for writing inserts using unnest in postgres and it can easily hit 100k rows/sec on any hardware I've run it on (my mac, ec2, gcp, etc.)

The only overhead this implementation has over the current one for &[T] is the count += 1 in the encoding loop. Hopefully this gets optimized out for slices due to the check in the calling function before delegating the call.

The existing tests for &[T] should cover testing this since it is now the implementation.

I have used a peeking iterator for the first stuff in the past, but that probably has some iteration overhead that is larger than just handling the first item separately.

tylerhawkes avatar Dec 22 '24 03:12 tylerhawkes

I added a few lines of documentation about why this is useful and how to best use it in postgres with the unnest operator. I also added a test that ensures it works as expected and is usable for owned and borrowed types returned in the iterator.

tylerhawkes avatar Dec 23 '24 06:12 tylerhawkes

Is there anything else I need to do to get this merged?

tylerhawkes avatar Dec 24 '24 00:12 tylerhawkes

I thought about it more and using an extension trait feels a lot nicer of an api to use and it lets us hide some implementation details that aren't important.

Instead of bind_iter as the fn name, maybe into_bind would work better or some other name. This feels nicer to write at the very least.

tylerhawkes avatar Dec 24 '24 07:12 tylerhawkes

@abonander Can we get this included or something like it for 0.9?

tylerhawkes avatar Jun 11 '25 23:06 tylerhawkes