I have a very large table(3.7 GB) stored in PostgreSQL database which I am loading using psycopg2, with the following code:
conn = psycopg2.connect(host="localhost", port = 5432, database="postgres", user="postgres", password="root")
cur = conn.cursor('cursor1')
cur.itersize = 10000
cur.execute("""SELECT * FROM customers""")
query_results = cur.fetchall()
cur.close()
conn.close()
Python uses 13GB memory to load the table for the first time and every time I run this code again, the memory usage increases until it becomes 99%; then drops to 13GB level again. Also, each time the query takes longer time to execute: First time 34 seconds, second time 46 seconds and it stays around ~45 seconds in the next runs.
I have searched for a solution and this answer seemed to provide an answer, but adding itersize didn't work for me either; the memory usage stayed the same
What should I do to decrease the memory usage? I used gc.collect() and it didn't worked either.
I am using Python 3.9 with PostgreSQL 14 on Windows10 machine and I have 32GB RAM.