I use psycopg2 to query the Postgresql database and try to process all the rows from a table with approximately 380M rows. There are only 3 columns (id1, id2, count) of the whole integer type. However, when I run the simple select query below, the Python process starts consuming more and more memory until it is killed by the OS.
A minimal working example (assuming mydatabase exists and contains a table called mytable):
import psycopg2 conn = psycopg2.connect("dbname=mydatabase") cur = conn.cursor() cur.execute("SELECT * FROM mytable;")
At this point, the program begins to consume memory.
I looked and the Postgresql process is behaving well. It uses a fair processor bit, which is good and a very limited amount of memory.
I expected psycopg2 to return an iterator without trying to unload all the results from the selection. Then I could use cur.fetchone() to process all the lines.
So, how can I select 380M from the row table without using the available memory?
python postgresql psycopg2
Carl
source share