Skip to content
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

Question regarding result.rowCount #2182

Closed
MelissaSnell opened this issue Apr 28, 2020 · 4 comments
Closed

Question regarding result.rowCount #2182

MelissaSnell opened this issue Apr 28, 2020 · 4 comments

Comments

@MelissaSnell
Copy link

Hi there

I just wanted to clarify something in the documentation.

On this page for the Result object you say that rowCount is not set for the number of rows returned from a query.

I am finding that for a pool.query, rowCount does seem to be > 0 if result.rows.length>0.

Does rowCount reflect the number of rows returned for a pool.query but not for a client.query?

Many thanks in advance.

@sehrope
Copy link
Contributor

sehrope commented Apr 28, 2020

The rowCount is populated from the command tag supplied by the PostgreSQL server. It's generally of the form: COMMAND [OID] [ROWS]

Here's the piece of pg that parses the value into the rowCount field:

if (match) {
this.command = match[1]
if (match[3]) {
// COMMMAND OID ROWS
this.oid = parseInt(match[2], 10)
this.rowCount = parseInt(match[3], 10)
} else if (match[2]) {
// COMMAND ROWS
this.rowCount = parseInt(match[2], 10)
}
}

For DML commands (INSERT, UPDATE, etc), it reflects how many rows the server modified to process the command. For SELECT or COPY commands it reflects how many rows were retrieved or copied. More info on the specifics here: https://www.postgresql.org/docs/current/protocol-message-formats.html (search for CommandComplete for the message type)

The note in the docs about the difference is because that value is controlled by the server. It's possible for a non-standard server (ex: PostgreSQL fork) or a server version in the future to provide different information in some situations so it'd be best not to rely on it to assume that the rows array length matches the rowCount. It's fine to use it for DML counts though.

@MelissaSnell
Copy link
Author

Many thanks for the explanation. Much appreciated

@brianc
Copy link
Owner

brianc commented Apr 28, 2020

holy smokes this description is great - mind if I add it to the docs w/ credit @sehrope ?

@sehrope
Copy link
Contributor

sehrope commented Apr 28, 2020

Thanks! Sure go for it!

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

3 participants