File tree Expand file tree Collapse file tree 1 file changed +5
-2
lines changed Expand file tree Collapse file tree 1 file changed +5
-2
lines changed Original file line number Diff line number Diff line change @@ -776,7 +776,10 @@ def to_pyarrow_batches(
776
776
cursor .close ()
777
777
raise
778
778
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 ):
780
783
columns = schema .names
781
784
try :
782
785
while batch := cursor .fetchmany (chunk_size ):
@@ -791,4 +794,4 @@ def _batches(schema: pa.Schema):
791
794
con .commit ()
792
795
793
796
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 ))
You can’t perform that action at this time.
0 commit comments