Repository navigation
Description
I was experiencing some flaky bulk insert (COPY) behavior when using Npgsql that I could not track down.
The behavior was that a COPY bulk insert would intermittently finish/fail silently, without an exception, yet in my Postgresql database, there were 0 rows copied.
When I dove into the Npgsql and @bytefish bytefish/PostgreSQLCopyHelper code I was using to insert data with COPY, I realized that the number of rows inserted by Npgsql's COPY implementation was not being returned in the Complete() method signature.
Because of the failure mode did not throw an exception and because the number of rows inserted was not being returned, there was no way to detect the silent failure.
I first focused on adding a rows inserted count to Npgsql and PostgreSQLCopyHelper, which would help debug and also have lasting utility in every call to COPY to verify that rows inserted matched the expected number.
The underlying bug went away after some experimentation, and I could not isolate the cause or what had changed to solve it (possibly upgrading Postgresql).
I am submitting this issue and a Pull request as an enhancement to Npgsql - to return the number of rows inserted by COPY in NpgsqlBinaryImporter.Complete().
I am also submitting a related downstream enhancement to bytefish/PostgreSQLCopyHelper).
Steps to reproduce
Unfortunately, I did not isolate the bug when a COPY process wrote 0 rows into Postgresql without throwing an exception, because:
- As part of debugging, and in general, I realized downstream code needed to validate how many rows had been written, and
- by the time I implemented the return of the number of rows in Npgsql and PostgreSQLCopyHelper, the silent failure problem had stopped occurring.
It is possible that the bug existed in an older Npgsql or PostgreSQLCopyHelper or even Postgresql installation that I had upgraded during the time I was investigating this problem (was just pushing to get past the issue and get to other tasks at the time).
The issue
NpgsqlBinaryImporter.Complete() is where the COPY operation finishes, and where the number of affected/inserted rows should be returned.
Currently, the NpgsqlBinaryImporter.Complete() signature is:
public void Complete()
The suggested signature is:
public int Complete()
Additionally, in my pull request I update the BackendMessages/CommandCompleteMessage.cs to check a Postgresql command code of (byte)'C' for COPY.
Further technical details
Npgsql version: 4.0
PostgreSQL version: 10.3 (but I had been using 9.6 and 10.1, so the bug might have occurred in one of those versions)
Operating system: Postgresql on NixOS 18.03; Client Npgsql and PostgreSQLCopyHelper running on Windows 10 64-bit.
Other details about my project setup:
Client code was running in F#. This could be another source of the original bug, but I think less likely than either a slightly older Postgresql or Npgsql.
Motivation
Npgsql and Postgresql can be used for high performance bulk inserts programmatically using the NpgsqlBinaryImporter and Postgresql's COPY feature.
However, the implementation is incomplete to verify that the user's inserts were fully accepted by the database.
One corner case requiring COPY row count is when a (likely rare) bug causes COPY to fail without throwing an Exception in Npgsql.
Additionally, routine client code often wants to verify if the data inserted is of the size/count expected.
And Postgresql anticipates this by providing the COPY row count; all that is needed is for Npgsql to expose what Postgresql provides.
Activity
Modified return code from void to int in the NpgsqlBinaryImporter.Com…
Added int return value to NpgsqlBinaryImporter.Complete(). This will
Fixed typo. Fixes npgsql#2112
roji commentedon Aug 15, 2018
@chrisatx I'm OK with the feature idea and have reviewed your PR. However, note that using the rows inserted as a way to detect silent failures is not a good idea... We should be fixing the silent failure instead. Can you please try to reproduce this silent failure and open a separate issue for it?
Regardless, since PostgreSQL returns the number of rows imported in CommandComplete, it makes sense to expose that to the user.
chrisatx commentedon Aug 16, 2018
@roji, I share the strong desire to identify the silent failure, as those are worrisome and hard to notice until they impact another downstream process.
When I next have a chance, I will try to dig up any logs and try to figure out how I might be able to repro on the system where it occurred.
Bulk Copy Return Rows (#2150)
chrisatx commentedon Nov 11, 2018
@roji can we add Milestone 4.1 to this and #2150 please :-)
YohDeadfall commentedon Nov 11, 2018
Done.
roji commentedon Sep 30, 2019
Although this was released in 4.1.0, I'm reverting this because a signature change in Complete() is a breaking change (note: only the signature itself was reverted, the mechanism is still there). I'm assuming this is not a critical API that can wait for 5.0, please post here if otherwise.
bytefish commentedon Oct 1, 2019
@roji I understand the reasons to revert the changes. Just a question. We have a library, that uses this specific feature of 4.1.0. I don't think 4.1.0 will be unlisted, but I want to be sure and ask: Will Npgsql 4.1.0 still be on NuGet with this API? So we could stay on 4.1.0 for now and later update to 5.0.
roji commentedon Oct 1, 2019
Thanks for understanding.
Once a package has been published on nuget.org, it basically stays there forever and cannot change (aside from some very exceptional circumstances). So you can definitely stay on 4.1.0, although I'd highly recommend getting latest patches.
Since this revert is trivial (only a cast), it's probably better for you to maintain your own fork, always based on the latest published version, but with this feature. Once 5.0 comes out you can switch to it.
Revert breaking changes around NpgsqlStatement.Rows
Revert breaking changes around NpgsqlStatement.Rows (#3205)