473,385 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

psycopg2 & large result set

Hi All.

I'm using psycopg2 to retrieve results from a rather large query (it
returns 22m records); unsurprisingly this doesn't fit in memory all at
once. What I'd like to achieve is something similar to a .NET data
provider I have which allows you to set a 'FetchSize' property; it
then retrieves 'n' many rows at a time, and fetches the next 'chunk'
after you read past the end of the current chunk. I suppose I could
use Python for .NET or IronPython but I'd rather stick with CPython
2.5 if possible.

I'm not 100% sure if it's an interface or a server thing. Any ideas
are most welcome.

Cheers,

Jon.

May 25 '07 #1
2 2229

Jon Clements wrote:
Hi All.

I'm using psycopg2 to retrieve results from a rather large query (it
returns 22m records); unsurprisingly this doesn't fit in memory all at
once. What I'd like to achieve is something similar to a .NET data
provider I have which allows you to set a 'FetchSize' property; it
then retrieves 'n' many rows at a time, and fetches the next 'chunk'
after you read past the end of the current chunk. I suppose I could
use Python for .NET or IronPython but I'd rather stick with CPython
2.5 if possible.
psycopg2 is DB-API 2.0 [1]_ compliant, so you can use
``fetchmany`` method and set ``cursor.arraysize`` accordingly.

[1] .. http://www.python.org/dev/peps/pep-0249/

--
HTH,
Rob

May 25 '07 #2
On 25 May, 11:16, Jon Clements <jon...@googlemail.comwrote:
>
I'm using psycopg2 to retrieve results from a rather large query (it
returns 22m records); unsurprisingly this doesn't fit in memory all at
once. What I'd like to achieve is something similar to a .NET data
provider I have which allows you to set a 'FetchSize' property; it
then retrieves 'n' many rows at a time, and fetches the next 'chunk'
after you read past the end of the current chunk. I suppose I could
use Python for .NET or IronPython but I'd rather stick with CPython
2.5 if possible.

I'm not 100% sure if it's an interface or a server thing. Any ideas
are most welcome.
It's an interface thing. The DB-API has fetchone, fetchmany and
(optionally) iteration methods on cursor objects; PostgreSQL supports
what you have in mind; pyPgSQL supports it at the interface level, but
psycopg2 only supports it if you use "named cursors", which is not
part of the DB-API specification as far as I recall, and not
particularly convenient if you're thinking of targeting more than one
database system with the same code. See this bug filed against
psycopg2 and the resulting discussion:

http://www.initd.org/tracker/psycopg/ticket/158

I've been running a patched version of psycopg2, but haven't developed
the patch further since it may be more convenient for me to switch
back to pyPgSQL eventually.

Paul

May 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mvanier | last post by:
There was a thread a while back dealing with an error message the psycopg2 Postgres interface gives when trying to convert some mxDateTime values: "can't adapt". The answer given was that psycopg2...
2
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I've inserted a couple hundred rows in a table in Postgres via psycopg2. The first field of each row is a certain unix time (since epoch)...
0
by: Sandro Dentella | last post by:
Hi all, while building an applycation in pygtk I noticed that psycopg2 returns the floats rouded (eg: 4.123 -> 4.0). This turns out to be a problem of psycopg2 (psycopg behaves correctly) when...
1
by: Maxim Sloyko | last post by:
Hello, clp and all people reading it! Recently I was porting my (small) app from psycopg to psycopg2 (they got me with this "2"). I read, that psycopg2 supports all features of psycopg and plus...
12
by: Dale Strickland-Clark | last post by:
Now that OIDs have been deprecated in PostgreSQL, how do you find the key of a newly inserted record? I've tried three Python client libraries, including psycopg2, and where they support cursor...
2
by: Luis P. Mendes | last post by:
Hi, I've installed psycopg2 under Slacware 11.0 along with PostgreSQL 8.2.4. When I run the python shell I get the following error: lupe@lince ~$ python Python 2.4.3 (#1, Jul 26 2006,...
0
by: David Michael Schruth, | last post by:
Hi, I am sort of in a jam here. I am using the PsycoPG2 library to read data out of a windows XP based PostGIS / PostGreSQL database but I am apparently unable to write (update or insert) even...
1
by: George Sakkis | last post by:
I have a simple DB table that stores md5 signature pairs: Table "public.duplicate" Column | Type | Modifiers ----------+-------+----------- sig | bytea | not null orig_sig | bytea | not...
0
by: RossGK | last post by:
I've been using pydev for a short while successfully, and Django with postgresql as well. psycopg2 is part of that behind the scenes I would imagine, to make django work. Now I'm trying to use...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.