Skip to content

Commit f42ab45

Browse files
committed
fix(postgres): ensure that cursor lifetime is at least as long as the generator
1 parent 0bdb5a0 commit f42ab45

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

ibis/backends/postgres/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,10 @@ def to_pyarrow_batches(
776776
cursor.close()
777777
raise
778778

779-
def _batches(schema: pa.Schema):
779+
# cursor must be passed into the function, otherwise it's liable to be
780+
# cleaned up by GC when the function exists, due to the fact that `_batches`
781+
# is a generator
782+
def _batches(schema: pa.Schema, cursor: psycopg.ServerCursor):
780783
columns = schema.names
781784
try:
782785
while batch := cursor.fetchmany(chunk_size):
@@ -791,4 +794,4 @@ def _batches(schema: pa.Schema):
791794
con.commit()
792795

793796
pa_schema = schema.to_pyarrow()
794-
return pa.RecordBatchReader.from_batches(pa_schema, _batches(pa_schema))
797+
return pa.RecordBatchReader.from_batches(pa_schema, _batches(pa_schema, cursor))

0 commit comments

Comments
 (0)