Skip to content

Row count #78

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

Closed
gkristjansson opened this issue Dec 10, 2011 · 8 comments
Closed

Row count #78

gkristjansson opened this issue Dec 10, 2011 · 8 comments

Comments

@gkristjansson
Copy link

This is not really an issue but more of a question. Is there any way of getting the number of rows returned by a query when using the 'row' event ?

@defunctzombie
Copy link
Contributor

The result object has a 'rowCount' variable which has the number of rows.

@gkristjansson
Copy link
Author

But how do I get the result object if I am using the evented api? I suppose it's possible to do both kind of callbacks but that sounds sort of weird. My concern is that the result set is huge so I would rather want to stick with the event model.

@booo
Copy link
Contributor

booo commented Dec 11, 2011

What about counting the row events?

query.rowEvents++?

@gkristjansson
Copy link
Author

Want to have an indicator on how far away the loading process is from finishing. So I need to know beforehand how many rows there are.

@booo
Copy link
Contributor

booo commented Dec 11, 2011

I'm not 100% sure but I think there is no way to get this information before. The only way I can think of is a another query before the actual query

SELECT count(*) FROM your_table;

@chowey
Copy link
Contributor

chowey commented Dec 11, 2011

You can also look at the "reltuples" field of the built-in "pg_class" table for an approximate number of rows. This would probably be fine to show how far away the loading process is from finishing.

SELECT reltuples FROM pg_class WHERE relname='your_table';

Reference is http://www.postgresql.org/docs/current/interactive/catalog-pg-class.html.

Since postgresql doesn't know itself ahead of time how many rows will be returned, the pg module also won't know unless it buffers the whole result. Use the callback api to buffer results, but stick with the evented api to not buffer results.

@booo's suggestion will give you a perfectly accurate count, but it is slower since postgresql has to walk the whole table first.

@brianc
Copy link
Owner

brianc commented Dec 12, 2011

You're not going to be able to get a count of rows until your last row is returned. Like @booo said you can do a query before the select to get a count of the results. I'm pretty sure postgresql itself is streaming the results as it grabs them. Anyway...the 'CommandComplete' postgres message contains the number of rows in the SELECT statement. And this message comes in after all 'RowData' messages come in...so there's technically no way I can see to support this at the postgres client level even if we wanted to.

@brianc brianc closed this as completed Dec 12, 2011
@gkristjansson
Copy link
Author

Thanks for all of comments

brianc pushed a commit that referenced this issue Dec 27, 2019
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

5 participants