@@ -51,7 +51,7 @@ static int outfd = -1;
51
51
static volatile sig_atomic_t time_to_abort = false;
52
52
static volatile sig_atomic_t output_reopen = false;
53
53
static int64 output_last_fsync = -1 ;
54
- static bool output_unsynced = false;
54
+ static bool output_needs_fsync = false;
55
55
static XLogRecPtr output_written_lsn = InvalidXLogRecPtr ;
56
56
static XLogRecPtr output_fsync_lsn = InvalidXLogRecPtr ;
57
57
@@ -173,10 +173,10 @@ OutputFsync(int64 now)
173
173
if (fsync_interval <= 0 )
174
174
return true;
175
175
176
- if (!output_unsynced )
176
+ if (!output_needs_fsync )
177
177
return true;
178
178
179
- output_unsynced = false;
179
+ output_needs_fsync = false;
180
180
181
181
/* Accept EINVAL, in case output is writing to a pipe or similar. */
182
182
if (fsync (outfd ) != 0 && errno != EINVAL )
@@ -304,6 +304,17 @@ StreamLog(void)
304
304
last_status = now ;
305
305
}
306
306
307
+ /* got SIGHUP, close output file */
308
+ if (outfd != -1 && output_reopen && strcmp (outfile , "-" ) != 0 )
309
+ {
310
+ now = feGetCurrentTimestamp ();
311
+ if (!OutputFsync (now ))
312
+ goto error ;
313
+ close (outfd );
314
+ outfd = -1 ;
315
+ }
316
+ output_reopen = false;
317
+
307
318
r = PQgetCopyData (conn , & copybuf , 1 );
308
319
if (r == 0 )
309
320
{
@@ -327,7 +338,7 @@ StreamLog(void)
327
338
((int64 ) 1000 );
328
339
329
340
/* Compute when we need to wakeup to fsync the output file. */
330
- if (fsync_interval > 0 && output_unsynced )
341
+ if (fsync_interval > 0 && output_needs_fsync )
331
342
fsync_target = output_last_fsync + (fsync_interval - 1 ) *
332
343
((int64 ) 1000 );
333
344
@@ -468,28 +479,14 @@ StreamLog(void)
468
479
output_written_lsn = Max (temp , output_written_lsn );
469
480
}
470
481
471
- /* redirect output to stdout */
472
- if (outfd == -1 && strcmp (outfile , "-" ) == 0 )
473
- {
474
- outfd = fileno (stdout );
475
- }
476
-
477
- /* got SIGHUP, close output file */
478
- if (outfd != -1 && output_reopen )
479
- {
480
- now = feGetCurrentTimestamp ();
481
- if (!OutputFsync (now ))
482
- goto error ;
483
- close (outfd );
484
- outfd = -1 ;
485
- output_reopen = false;
486
- }
487
-
482
+ /* open the output file, if not open yet */
488
483
if (outfd == -1 )
489
484
{
490
-
491
- outfd = open (outfile , O_CREAT | O_APPEND | O_WRONLY | PG_BINARY ,
492
- S_IRUSR | S_IWUSR );
485
+ if (strcmp (outfile , "-" ) == 0 )
486
+ outfd = fileno (stdout );
487
+ else
488
+ outfd = open (outfile , O_CREAT | O_APPEND | O_WRONLY | PG_BINARY ,
489
+ S_IRUSR | S_IWUSR );
493
490
if (outfd == -1 )
494
491
{
495
492
fprintf (stderr ,
@@ -503,7 +500,7 @@ StreamLog(void)
503
500
bytes_written = 0 ;
504
501
505
502
/* signal that a fsync is needed */
506
- output_unsynced = true;
503
+ output_needs_fsync = true;
507
504
508
505
while (bytes_left )
509
506
{
0 commit comments