@@ -161,9 +161,7 @@ func (qe *QRepQueryExecutor) processRowsStream(
161
161
record , err := qe .mapRowToQRecord (rows , fieldDescriptions )
162
162
if err != nil {
163
163
qe .logger .Error ("[pg_query_executor] failed to map row to QRecord" , slog .Any ("error" , err ))
164
- err := fmt .Errorf ("failed to map row to QRecord: %w" , err )
165
- stream .Close (err )
166
- return 0 , err
164
+ return 0 , fmt .Errorf ("failed to map row to QRecord: %w" , err )
167
165
}
168
166
169
167
stream .Records <- record
@@ -189,12 +187,10 @@ func (qe *QRepQueryExecutor) processFetchedRows(
189
187
) (int , error ) {
190
188
rows , err := qe .executeQueryInTx (ctx , tx , cursorName , fetchSize )
191
189
if err != nil {
192
- stream .Close (err )
193
190
qe .logger .Error ("[pg_query_executor] failed to execute query in tx" ,
194
191
slog .Any ("error" , err ), slog .String ("query" , query ))
195
192
return 0 , fmt .Errorf ("[pg_query_executor] failed to execute query in tx: %w" , err )
196
193
}
197
-
198
194
defer rows .Close ()
199
195
200
196
fieldDescriptions := rows .FieldDescriptions ()
@@ -210,7 +206,6 @@ func (qe *QRepQueryExecutor) processFetchedRows(
210
206
}
211
207
212
208
if err := rows .Err (); err != nil {
213
- stream .Close (err )
214
209
qe .logger .Error ("[pg_query_executor] row iteration failed" ,
215
210
slog .String ("query" , query ), slog .Any ("error" , err ))
216
211
return 0 , fmt .Errorf ("[pg_query_executor] row iteration failed '%s': %w" , query , err )
@@ -225,7 +220,8 @@ func (qe *QRepQueryExecutor) ExecuteAndProcessQuery(
225
220
args ... interface {},
226
221
) (* model.QRecordBatch , error ) {
227
222
stream := model .NewQRecordStream (1024 )
228
- errors := make (chan error , 1 )
223
+ errors := make (chan struct {})
224
+ var errorsError error
229
225
qe .logger .Info ("Executing and processing query" , slog .String ("query" , query ))
230
226
231
227
// must wait on errors to close before returning to maintain qe.conn exclusion
@@ -234,23 +230,28 @@ func (qe *QRepQueryExecutor) ExecuteAndProcessQuery(
234
230
_ , err := qe .ExecuteAndProcessQueryStream (ctx , stream , query , args ... )
235
231
if err != nil {
236
232
qe .logger .Error ("[pg_query_executor] failed to execute and process query stream" , slog .Any ("error" , err ))
237
- errors <- err
233
+ errorsError = err
238
234
}
239
235
}()
240
236
241
237
select {
242
- case err := <- errors :
243
- return nil , err
238
+ case <- errors :
239
+ return nil , errorsError
244
240
case <- stream .SchemaChan ():
241
+ schema , err := stream .Schema ()
242
+ if err != nil {
243
+ return nil , err
244
+ }
245
245
batch := & model.QRecordBatch {
246
- Schema : stream . Schema () ,
246
+ Schema : schema ,
247
247
Records : nil ,
248
248
}
249
249
for record := range stream .Records {
250
250
batch .Records = append (batch .Records , record )
251
251
}
252
- if err := <- errors ; err != nil {
253
- return nil , err
252
+ <- errors
253
+ if errorsError != nil {
254
+ return nil , errorsError
254
255
}
255
256
if err := stream .Err (); err != nil {
256
257
return nil , fmt .Errorf ("[pg] failed to get record from stream: %w" , err )
@@ -288,10 +289,16 @@ func (qe *QRepQueryExecutor) ExecuteQueryIntoSink(
288
289
})
289
290
if err != nil {
290
291
qe .logger .Error ("[pg_query_executor] failed to begin transaction" , slog .Any ("error" , err ))
291
- return 0 , fmt .Errorf ("[pg_query_executor] failed to begin transaction: %w" , err )
292
+ err := fmt .Errorf ("[pg_query_executor] failed to begin transaction: %w" , err )
293
+ sink .Close (err )
294
+ return 0 , err
292
295
}
293
296
294
- return sink .ExecuteQueryWithTx (ctx , qe , tx , query , args ... )
297
+ totalRecords , err := sink .ExecuteQueryWithTx (ctx , qe , tx , query , args ... )
298
+ if err != nil {
299
+ sink .Close (err )
300
+ }
301
+ return totalRecords , err
295
302
}
296
303
297
304
func (qe * QRepQueryExecutor ) ExecuteQueryIntoSinkGettingCurrentSnapshotXmin (
@@ -310,16 +317,21 @@ func (qe *QRepQueryExecutor) ExecuteQueryIntoSinkGettingCurrentSnapshotXmin(
310
317
})
311
318
if err != nil {
312
319
qe .logger .Error ("[pg_query_executor] failed to begin transaction" , slog .Any ("error" , err ))
313
- return 0 , currentSnapshotXmin .Int64 , fmt .Errorf ("[pg_query_executor] failed to begin transaction: %w" , err )
320
+ err := fmt .Errorf ("[pg_query_executor] failed to begin transaction: %w" , err )
321
+ sink .Close (err )
322
+ return 0 , currentSnapshotXmin .Int64 , err
314
323
}
315
324
316
- err = tx .QueryRow (ctx , "select txid_snapshot_xmin(txid_current_snapshot())" ).Scan (& currentSnapshotXmin )
317
- if err != nil {
325
+ if err := tx .QueryRow (ctx , "select txid_snapshot_xmin(txid_current_snapshot())" ).Scan (& currentSnapshotXmin ); err != nil {
318
326
qe .logger .Error ("[pg_query_executor] failed to get current snapshot xmin" , slog .Any ("error" , err ))
327
+ sink .Close (err )
319
328
return 0 , currentSnapshotXmin .Int64 , err
320
329
}
321
330
322
331
totalRecordsFetched , err := sink .ExecuteQueryWithTx (ctx , qe , tx , query , args ... )
332
+ if err != nil {
333
+ sink .Close (err )
334
+ }
323
335
return totalRecordsFetched , currentSnapshotXmin .Int64 , err
324
336
}
325
337
0 commit comments