You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Need NpgsqlLargeObjectManager but batch/bulk mode #4755
Hello @roji
The API of NpgsqlLargeObjectManager is not conducive to bulk operations. It executes too many roundtrips, one roundtrip to open a handle, then one roundtrip per chunk, then one more to close.
If I need to bulk insert 1000 products with 3000 images each stored as a large object, this takes ages even on localhost due to the large amount of roundtrips.
This also needs to be done in 1 transaction, so using multiple parallel connections is not an option. The idea I have, is some kind of thing where I can open 1000 handles at once. And send chunks of the 1000 files in a single round one lo_write to each file handle, then close them all at once. To reduce the roundtrips or something like that.
I could probably implement something like this by hand but it would be a bit awkward. Perhaps when I get some time I can do it and contribute it to Npgsql. This issue is an enhancement request for consideration.
Thanks once more for the amazing reliable work you've done all these years.
@rwasef1830 you're right that this API isn't batching-compatible. However, NpgsqlLargeObjectManager in general is just a higher-level API built over the basic Npgsql operations - it just executes function calls, nothing more. That means there's basically no advantage in using it over just executing the appropriate functions yourself
To be honest, NpgsqlLargeObjectManager is a very old "feature" of Npgsql, which I don't believe should exist in the driver... I've considered obsoleting/removing it over the years but haven't bothered (just opened #4756 to think about this for 8.0). My recommendation is either for people to just call the functions, or an API could be implemented in a separate nuget package using Npgsql (though honestly, I'm not sure why bother).
There is an advantage which is a vastly simplified API. I see the appeal for it, since as a Stream abstraction it can fit into many use cases. I don't have an issue with the existing API, I just wish for a new API that can also handle batching.
You are probably right in that this "simplification" can be an external nuget to allow it to get updated and improved independently of the core driver.
The fact that NpgsqlLargeObjectManager will stream a large object in many smaller round trips automatically rather than force the database or the client to allocate the whole memory space for the large file at once. (or maybe I don't understand DbDataReader.GetStream() does), in addition what alternative is there to the writing part ? Even if I send a stream DbParameter, won't postgres have to basically buffer the entire input before writing it to the large object store (or maybe Npgsql itself internally) ?
AFAICT, this simply calls the lowrite function repeatedly, passing it ArraySegments (slices) of the array; that's something you can easily do. As an aside, I'm not sure what the advantage is of breaking large binary data into several roundtrips (MaxTransferBlockSize is 4MB by default), rather than just doing it in a single one... I'd be very surprised if on the PG side this makes things more efficient (why would it?). Maybe if there's some failure in the middle, not everything is rolled back; I'd be surprised - but otherwise all it does is hurt performance by adding more network roundtrips.
Stepping back, for now I'm not seeing anything in Npgsql's large object API that a user can't do directly against Npgsql's APIs; it really seems purely like a layer on top that's meant to provide some convenience - though I'm really not convinced it's actually better to use it instead of just invoking the functions yourself. I may be mistaken (I don't know this old part of the code very well), but if you think there's some advantage in using these APIs over doing things yourself, let me know.
That's why I'm pretty reluctant to start thinking about evolving this API with features like batching... If you can already use the regular Npgsql APIs to achieve the same thing, you can already batch there.
I'm not sure what the advantage is of breaking large binary data into several roundtrips (MaxTransferBlockSize is 4MB by default), rather than just doing it in a single one
A web server serving visitors where each one uploads files that are stored in blobs. If we buffer the whole file for each one, we need an amount of RAM for buffers proportional to simultaneous uploads. It is a cost-control trade off.
Stepping back, for now I'm not seeing anything in Npgsql's large object API that a user can't do directly against Npgsql's APIs; it really seems purely like a layer on top that's meant to provide some convenience - though I'm really not convinced it's actually better to use it instead of just invoking the functions yourself. I may be mistaken (I don't know this old part of the code very well), but if you think there's some advantage in using these APIs over doing things yourself, let me know.
It is purely a convenience, I ended writing my own loop of sending segments of multiple files together to upload them all in less roundtrips and it worked fine. I have to improve/package it and make it more robust to make it at the needed quality level of a nuget package though.
I guess you can just deprecate the current one and recommend an alternative one, when such one is released. (I will try to do it but I don't have time now).
I'm not sure what the advantage is of breaking large binary data into several roundtrips (MaxTransferBlockSize is 4MB by default), rather than just doing it in a single one
A web server serving visitors where each one uploads files that are stored in blobs. If we buffer the whole file for each one, we need an amount of RAM for buffers proportional to simultaneous uploads. It is a cost-control trade off.
I'm not sure I understand... NpgsqlLargeObjectStream seems like a standard stream to which you write binary data. When you invoke write on it, you give it a byte[]; it then writes that chunk in potentially multiple 4MB roundtrips to the database. I'm not sure why it wouldn't just write the entire byte[] you gave it in one roundtrip. Note again that I'm talking about the internal implementation of NpgsqlLargeObjectStream, and not how a user should use it; since the Write method accepts a byte[], that byte[] is already by definition in-memory when Write is called.
In other words, rather than using NpgsqlLargeObjectStream and calling its Write method repeatedly, you can just invoke lowrite yourself repeatedly, each time passing it a byte[] to be written.
Yes, you're right. I didn't pay attention that NpgsqlLargeObjectStream itself does internal chunking. NpgsqlLargeObjectStream doesn't provide a lot of value in case of a single file upload. I was thinking about the case when a user tries to write a large byte array directly, but failed to think that the source will be read in chunks anyway, in case of real world usage.
In other words, rather than using NpgsqlLargeObjectStream and calling its Write method repeatedly, you can just invoke lowrite yourself repeatedly, each time passing it a byte[] to be written.
Yes that's what I did (many lowrite calls with different pieces of different files in one roundtrip). But it was awkward, because you have to create the blobs in one pass all of them together and get the blob ids, then read a piece from each file, and lo_write to all the blob ids in roundtrip and so on until the files end one by one then commit at the end.
An API to make this easier is what I originally asked for. But I ended up writing it myself, but it's not yet at the robustness level to be make it a proper nuget package.
I guess you can deprecate this class since a better alternative is needed anyway and it doesn't provide a lot of value for single file upload.
I was thinking about the case when a user tries to write a large byte array directly, but failed to think that the source will be read in chunks anyway, in case of real world usage.
OK. Though I'm not sure NpgsqlLargeObjectStream helps much here - it's just a stream that accepts byte arrays for writing, so whether the user decides to read source data in chunks or not seems orthogonal to whether they use NpgsqlLargeObjectStream to write the data or not.
Yes that's what I did (many lowrite calls with different pieces of different files in one roundtrip). But it was awkward, because you have to create the blobs in one pass all of them together and get the blob ids, then read a piece from each file, and lo_write to all the blob ids in roundtrip and so on until the files end one by one then commit at the end.
Thanks for the extra detail.
Yeah, I think deprecating this API is the right thing to do... I've generally received very few issues/questions about the API so I think very few people are actually using it (it's also not documented at all on the Npgsql site). I get that it may be a bit more work to manage multiple blobs, but usage patterns in this area may vary and I don't think a single layer necessarily makes sense.
Of course, as we discussed above, since NpgsqlLargeObjectManager/NpgsqlLargeObjectStream are a pure layer above the standard APIs, anyone can do something like this as an additional nuget package or whatever.
Activity
roji commentedon Nov 15, 2022
@rwasef1830 you're right that this API isn't batching-compatible. However, NpgsqlLargeObjectManager in general is just a higher-level API built over the basic Npgsql operations - it just executes function calls, nothing more. That means there's basically no advantage in using it over just executing the appropriate functions yourself
To be honest, NpgsqlLargeObjectManager is a very old "feature" of Npgsql, which I don't believe should exist in the driver... I've considered obsoleting/removing it over the years but haven't bothered (just opened #4756 to think about this for 8.0). My recommendation is either for people to just call the functions, or an API could be implemented in a separate nuget package using Npgsql (though honestly, I'm not sure why bother).
rwasef1830 commentedon Nov 15, 2022
There is an advantage which is a vastly simplified API. I see the appeal for it, since as a Stream abstraction it can fit into many use cases. I don't have an issue with the existing API, I just wish for a new API that can also handle batching.
You are probably right in that this "simplification" can be an external nuget to allow it to get updated and improved independently of the core driver.
roji commentedon Nov 15, 2022
DbDataReader already exposes a GetStream() method, so I'm not sure what this really brings to the table...
rwasef1830 commentedon Nov 15, 2022
The fact that NpgsqlLargeObjectManager will stream a large object in many smaller round trips automatically rather than force the database or the client to allocate the whole memory space for the large file at once. (or maybe I don't understand DbDataReader.GetStream() does), in addition what alternative is there to the writing part ? Even if I send a stream DbParameter, won't postgres have to basically buffer the entire input before writing it to the large object store (or maybe Npgsql itself internally) ?
roji commentedon Dec 5, 2022
Here's the writing part of NpgsqlLargeObjectStream:
AFAICT, this simply calls the
lowritefunction repeatedly, passing it ArraySegments (slices) of the array; that's something you can easily do. As an aside, I'm not sure what the advantage is of breaking large binary data into several roundtrips (MaxTransferBlockSize is 4MB by default), rather than just doing it in a single one... I'd be very surprised if on the PG side this makes things more efficient (why would it?). Maybe if there's some failure in the middle, not everything is rolled back; I'd be surprised - but otherwise all it does is hurt performance by adding more network roundtrips.Stepping back, for now I'm not seeing anything in Npgsql's large object API that a user can't do directly against Npgsql's APIs; it really seems purely like a layer on top that's meant to provide some convenience - though I'm really not convinced it's actually better to use it instead of just invoking the functions yourself. I may be mistaken (I don't know this old part of the code very well), but if you think there's some advantage in using these APIs over doing things yourself, let me know.
That's why I'm pretty reluctant to start thinking about evolving this API with features like batching... If you can already use the regular Npgsql APIs to achieve the same thing, you can already batch there.
rwasef1830 commentedon Dec 5, 2022
A web server serving visitors where each one uploads files that are stored in blobs. If we buffer the whole file for each one, we need an amount of RAM for buffers proportional to simultaneous uploads. It is a cost-control trade off.
It is purely a convenience, I ended writing my own loop of sending segments of multiple files together to upload them all in less roundtrips and it worked fine. I have to improve/package it and make it more robust to make it at the needed quality level of a nuget package though.
I guess you can just deprecate the current one and recommend an alternative one, when such one is released. (I will try to do it but I don't have time now).
roji commentedon Dec 5, 2022
I'm not sure I understand... NpgsqlLargeObjectStream seems like a standard stream to which you write binary data. When you invoke write on it, you give it a byte[]; it then writes that chunk in potentially multiple 4MB roundtrips to the database. I'm not sure why it wouldn't just write the entire byte[] you gave it in one roundtrip. Note again that I'm talking about the internal implementation of NpgsqlLargeObjectStream, and not how a user should use it; since the Write method accepts a byte[], that byte[] is already by definition in-memory when Write is called.
In other words, rather than using NpgsqlLargeObjectStream and calling its Write method repeatedly, you can just invoke
lowriteyourself repeatedly, each time passing it a byte[] to be written.Am I missing something?
rwasef1830 commentedon Dec 5, 2022
Yes, you're right. I didn't pay attention that NpgsqlLargeObjectStream itself does internal chunking. NpgsqlLargeObjectStream doesn't provide a lot of value in case of a single file upload. I was thinking about the case when a user tries to write a large byte array directly, but failed to think that the source will be read in chunks anyway, in case of real world usage.
Yes that's what I did (many lowrite calls with different pieces of different files in one roundtrip). But it was awkward, because you have to create the blobs in one pass all of them together and get the blob ids, then read a piece from each file, and lo_write to all the blob ids in roundtrip and so on until the files end one by one then commit at the end.
An API to make this easier is what I originally asked for. But I ended up writing it myself, but it's not yet at the robustness level to be make it a proper nuget package.
I guess you can deprecate this class since a better alternative is needed anyway and it doesn't provide a lot of value for single file upload.
roji commentedon Dec 5, 2022
OK. Though I'm not sure NpgsqlLargeObjectStream helps much here - it's just a stream that accepts byte arrays for writing, so whether the user decides to read source data in chunks or not seems orthogonal to whether they use NpgsqlLargeObjectStream to write the data or not.
Thanks for the extra detail.
Yeah, I think deprecating this API is the right thing to do... I've generally received very few issues/questions about the API so I think very few people are actually using it (it's also not documented at all on the Npgsql site). I get that it may be a bit more work to manage multiple blobs, but usage patterns in this area may vary and I don't think a single layer necessarily makes sense.
Of course, as we discussed above, since NpgsqlLargeObjectManager/NpgsqlLargeObjectStream are a pure layer above the standard APIs, anyone can do something like this as an additional nuget package or whatever.
Thanks for all your input here!