Created
December 23, 2021 09:40
-
-
Save arshamalh/632778becdac9f42ef263be3917999fb to your computer and use it in GitHub Desktop.
Cursor pagination in PostgreSQL and Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Written by Arsham Arya | |
# I'm not sure it's the best way, | |
# So any contribution makes me and other readers happy :) | |
from psycopg2.pool import ThreadedConnectionPool | |
import config | |
_CONNECTION_POOL = None | |
def execQuery(query: str, batch_size: int = 10000): | |
""" | |
Executes and streams the data | |
""" | |
global _CONNECTION_POOL | |
if not _CONNECTION_POOL: | |
_CONNECTION_POOL = ThreadedConnectionPool( | |
0, | |
100, | |
user=config.POSTGRES_USER, | |
password=config.POSTGRES_PASSWORD, | |
host=config.POSTGRES_HOST, | |
port=config.POSTGRES_PORT, | |
database=config.POSTGRES_DB, | |
) | |
connection = _CONNECTION_POOL.getconn() | |
cursor = connection.cursor(name="cursor_custom_name") | |
try: | |
cursor.execute(query) | |
except Exception as e: | |
print(e) | |
result = None | |
try: | |
while result := cursor.fetchmany(batch_size): | |
yield result | |
cursor.close() | |
connection.commit() | |
_CONNECTION_POOL.putconn(connection) | |
return | |
except Exception as e: | |
print(e) | |
cursor.close() | |
connection.commit() | |
_CONNECTION_POOL.putconn(connection) | |
yield result | |
def getData(): | |
query = """ | |
SELECT * FROM table_name | |
""" | |
data = execQuery(query, batch_size=50000) | |
for i, rows in enumerate(data): | |
for row in rows: | |
print(row) | |
print(row[0]) | |
getData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment