@@ -1888,35 +1888,41 @@ def _finalize_executemany_returning_span(
18881888 }
18891889 span .set_status (Status (OTelStatusCode .ERROR , str (error )))
18901890 else :
1891- # Iterate through cursor.results() to capture all result sets
1891+ # Iterate through result sets using nextset() (compatible with psycopg 3.2+)
1892+ # Note: results() requires psycopg 3.3+, so we use nextset() for broader compatibility
18921893 result_sets = []
18931894 all_rows_collected = [] # For re-populating cursor
18941895
18951896 try :
1896- # cursor.results() yields the cursor itself for each result set
1897- for result_cursor in cursor .results ():
1897+
1898+ def capture_current_result_set ():
1899+ """Capture the current result set's description and rows.
1900+
1901+ Extracts column metadata from cursor.description, fetches all rows,
1902+ normalizes row data (handling dict, namedtuple, and tuple formats),
1903+ and serializes for storage. Appends raw rows to all_rows_collected
1904+ for later cursor re-population.
1905+ """
18981906 result_set_data = {}
18991907
1900- # Capture description for this result set
1901- if hasattr (result_cursor , "description" ) and result_cursor .description :
1908+ if hasattr (cursor , "description" ) and cursor .description :
19021909 description = [
19031910 {
19041911 "name" : desc [0 ] if hasattr (desc , "__getitem__" ) else desc .name ,
19051912 "type_code" : desc [1 ]
19061913 if hasattr (desc , "__getitem__" ) and len (desc ) > 1
19071914 else getattr (desc , "type_code" , None ),
19081915 }
1909- for desc in result_cursor .description
1916+ for desc in cursor .description
19101917 ]
19111918 result_set_data ["description" ] = description
19121919 column_names = [d ["name" ] for d in description ]
19131920 else :
19141921 description = None
19151922 column_names = None
19161923
1917- # Fetch all rows for this result set
19181924 rows = []
1919- raw_rows = result_cursor .fetchall ()
1925+ raw_rows = cursor .fetchall ()
19201926 all_rows_collected .append (raw_rows )
19211927
19221928 for row in raw_rows :
@@ -1933,15 +1939,18 @@ def _finalize_executemany_returning_span(
19331939 else :
19341940 rows .append (list (row ))
19351941
1936- result_set_data ["rowcount" ] = (
1937- result_cursor .rowcount if hasattr (result_cursor , "rowcount" ) else len (rows )
1938- )
1942+ result_set_data ["rowcount" ] = cursor .rowcount if hasattr (cursor , "rowcount" ) else len (rows )
19391943 result_set_data ["rows" ] = [[serialize_value (col ) for col in row ] for row in rows ]
19401944
1941- result_sets .append (result_set_data )
1945+ return result_set_data
1946+
1947+ result_sets .append (capture_current_result_set ())
1948+
1949+ while cursor .nextset ():
1950+ result_sets .append (capture_current_result_set ())
19421951
19431952 except Exception as results_error :
1944- logger .debug (f"Could not iterate results() : { results_error } " )
1953+ logger .debug (f"Could not iterate result sets : { results_error } " )
19451954 # Fallback: treat as single result set
19461955 result_sets = []
19471956
0 commit comments