Fetching data from postgresql takes a ton of memory even when chunk loading

Viewed 123

I have a postgreSQL DB with around 600000 rows and 100 columns, which takes around 500 MB RAM. I have now tried loading it via

con = pyodbc.connect("DSN="+DSN)
df = pd.read_sql(query ,con)

ending up in 9+ GB of RAM usage and crashes due to memory error.

I have tried several work-arounds now:

"Manually" fetching the rows using fetchmany

eng = create_engine(f'postgresql://{self.user}:{self.pwd}@{self.host}:{self.port}/{self.db}',server_side_cursors=True)
#or 
#eng = create_engine(f'postgresql://{self.user}:{self.pwd}@{self.host}:{self.port}/{self.db}',execution_options={"stream_results":True})
con = eng.connect()
ex = con.execute(query)
           
def __fetch_iter__(self,ex,chunksize):
        """
        Generator for loading in chunks
        """
        fetch = True
        while fetch:
           fetch = ex.fetchmany(chunksize)
           
           yield pd.DataFrame(fetch)

df = pd.concat(__fetch_iter__(ex,chunksize))

which, according to this should indeed only load the given amount of rows each time.

even with chunksize=10 my memory goes above 6GB

0 Answers
Related