Skip to content

rowCount not always equal to rows.length #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
spollack opened this issue Apr 4, 2016 · 4 comments
Open

rowCount not always equal to rows.length #979

spollack opened this issue Apr 4, 2016 · 4 comments

Comments

@spollack
Copy link
Contributor

spollack commented Apr 4, 2016

Since upgrading to pg 4.x javascript from pg 2.11.1 native, we have seen a variety of nasty failures in production under load. one of my engineers had a theory, and it proved correct: we saw in production over the weekend a case where the results returned from pg were internally inconsistent: results.rowCount was 1, but results.rows.length was 0. Our codebase has been using these two properties interchangeably, which led to some very nasty bugs. We do not currently have a repro case for this, but seems to happen on an error path under load. This was using pg 4.5.1 javascript, plus PR #961.

I would caution everyone to avoid using the rowCount property until we get to the bottom of this.

For now we have now rolled back to pg 2.11.1 native in production.

@vitaly-t
Copy link
Contributor

vitaly-t commented Apr 5, 2016

Property rowCount equals to the number of rows affected by the query. It is not the same as rows.length, which equals to the number of rows received.

For example, when you execute an UPDATE, you will get rowCount = number of records updated, while your rows.length would be 0.

They are NOT interchangeable.

@spollack
Copy link
Contributor Author

spollack commented Apr 5, 2016

Good info, thanks. We have already modified our codebase to no longer look at rowCount.

As further background, the query in question was an UPDATE ... RETURNING *. Under the native pg driver to the best of our knowledge it always returned rowCount == rows.length, but with the javascript driver under stress/failure conditions we experienced a mismatch. my suspicion is that the error handling may be somewhat different here.

@vitaly-t
Copy link
Contributor

vitaly-t commented Apr 5, 2016

They should match in this case indeed. It would take to dissect your stress test to understand the problem. Let us know if you find anything ;)

@vitaly-t
Copy link
Contributor

vitaly-t commented Apr 6, 2016

I have just added pg-native support into pg-promise, and would be very interested to know in which cases this error occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants