-
Notifications
You must be signed in to change notification settings - Fork 415
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
Get number of rows in cursor ? (equivalent to psycopg2#cursor.rowcount) #359
Comments
A In your case, it seems like you need an explicit scrollable cursor: # declare the cursor
await conn.execute('DECLARE my_cursor SCROLL CURSOR FOR <query>')
# MOVE to the end to get the total row count
row_count = await conn.execute('MOVE LAST IN my_cursor')
# MOVE back to the start
await conn.execute('MOVE FIRST IN my_cursor')
# Fetch a page
results = await conn.fetch('FETCH RELATIVE <page_size> IN my_cursor') |
Hi @elprans Many thanks ! It's probably more a PostgreSQL question but I take the opportunity to challenge this (the scrollable cursor) with a secondary query (using Kind regards, |
I just tested your code and I have following notes: 1. Declare the cursor# declare the cursor
await conn.execute('DECLARE my_cursor SCROLL CURSOR FOR <query>') It's OK but it has to be done in a transaction. 2. MOVE to the end to get the total row count# MOVE to the end to get the total row count
row_count = await conn.execute('MOVE LAST IN my_cursor') It always responses with The only way (I guess) to get the number of rows is to add 3. MOVE back to the start# MOVE back to the start
await conn.execute('MOVE FIRST IN my_cursor') This is moving the cursor position to first row => row 0; so when we fetch next 10 rows its fetching from row 1. Instead we have to use: MOVE ABSOLUTE 0 IN my_cursor; 4. Fetch a page# Fetch a page
results = await conn.fetch('FETCH RELATIVE <page_size> IN my_cursor') According to PostgreSQL doc it's fetching the FETCH <page_size> IN my_cursor; |
Actually there is a way to get total number of rows (step 2). Instead of doing a For instance: MOVE FORWARD ALL IN my_cursor; (e.g. it returns Here is a complete example: -- Start a transaction
BEGIN;
-- Declare a new cursor with our query
DECLARE my_cursor SCROLL CURSOR FOR SELECT generate_series(0, 100);
-- Move forward to get number of rows
MOVE FORWARD ALL IN my_cursor;
-- Come back at the beginning of the cursor
MOVE ABSOLUTE 0 IN my_cursor;
-- Fetch the first page (e.g. 10)
FETCH 10 IN my_cursor;
-- Commit the transaction
COMMIT; Using async with connection.transaction():
await connection.execute('DECLARE my_cursor SCROLL CURSOR FOR SELECT generate_series(0, 100)')
row_count = await connection.execute('MOVE FORWARD ALL IN my_cursor')
await connection.execute('MOVE ABSOLUTE 0 IN my_cursor')
results = await connection.fetch('FETCH 10 IN my_cursor') Maybe one remaining question is: is there a function in asyncpg to parse query result (for instance
|
There is none currently.
Sure, I'm not against having a helper function for this. |
Warning: It's may be a duplicate of #311
I would like to get the number of rows from the last query (a
SELECT
query). An equivalent tocursor.rowcount
in psycopg2.I saw in #311 that we can get the query status line (as a string) from
execute
function. Actually, I'm not able to use it since I need a cursor. Don't know very well binary protocol of pg.. I don't even know if it is possible to get it.The purpose of this issue is to avoid doing multiple queries (for instance with
count(*)
statement).The text was updated successfully, but these errors were encountered: