Skip to content

The number of affected rows is not captured/exposed when a COPY completes in NpgsqlBinaryImporter #2112

Closed

Description

@chrisatx

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:

  1. As part of debugging, and in general, I realized downstream code needed to validate how many rows had been written, and
  2. 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

  1. added 3 commits that reference this issue on Aug 15, 2018
    f75eba0
    b2987b2
    0ae282c
  2. added this to the Backlog milestone on Aug 15, 2018
  3. self-assigned this
    on Aug 15, 2018
  4. roji commented on Aug 15, 2018

    @roji
    Member

    @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.

  5. chrisatx commented on Aug 16, 2018

    @chrisatx
    Author

    @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.

  6. added a commit that references this issue on Sep 18, 2018
    f142df0
  7. chrisatx commented on Nov 11, 2018

    @chrisatx
    Author

    @roji can we add Milestone 4.1 to this and #2150 please :-)

  8. modified the milestones: Backlog, 4.1 on Nov 11, 2018
  9. YohDeadfall commented on Nov 11, 2018

    @YohDeadfall
    Contributor

    Done.

  10. modified the milestones: 4.1.0, 5.0 on Sep 30, 2019
  11. roji commented on Sep 30, 2019

    @roji
    Member

    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.

  12. bytefish commented on Oct 1, 2019

    @bytefish

    @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.

  13. roji commented on Oct 1, 2019

    @roji
    Member

    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.

  14. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions