14

I'm looking for advice on efficient ways to stream data incrementally from a Postgres table into Python. I'm in the process of implementing an online learning algorithm and I want to read batches of training examples from the database table into memory to be processed. Any thoughts on good ways to maximize throughput? Thanks for your suggestions.

2
  • Please elaborate how the date will be structured, "streaming" it somewhere could just mean dumping the table and reading that from stdout (which is fast, and probably mostly limited by your I/O capabilities). But I suspect you want some structure, and what one should do is heavily dependent on that, Commented Feb 24, 2014 at 23:06
  • Nothing fancy here. Each row corresponds to a particular feature vector often with integer or floating point values. I am just scanning through the rows of a single table. Having it in Postgres is a convenience for query when additional attribute data is available. Commented Feb 25, 2014 at 0:00

2 Answers 2

23

If you are using psycopg2, then you will want to use a named cursor, otherwise it will try to read the entire query data into memory at once.

cursor = conn.cursor("some_unique_name")
cursor.execute("SELECT aid FROM pgbench_accounts")
for record in cursor:
    something(record)

This will fetch the records from the server in batches of 2000 (default value of itersize) and then parcel them out to the loop one at a time.

Sign up to request clarification or add additional context in comments.

4 Comments

Note that you you should set itersize; see initd.org/psycopg/docs/cursor.html
only if you want to customise the size. By default itersize for named cursors is 2000
any ideas what happens in node-postgres library
Thanks a lot for this! It have been days I am trying with offset/limit but it is much more slower!
0

You may want to look into the Postgres LISTEN/NOTIFY functionality https://www.postgresql.org/docs/9.1/static/sql-notify.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.