@@ -51,7 +51,7 @@ static int outfd = -1;
5151static volatile sig_atomic_t time_to_abort = false;
5252static volatile sig_atomic_t output_reopen = false;
5353static int64 output_last_fsync = -1 ;
54- static bool output_unsynced = false;
54+ static bool output_needs_fsync = false;
5555static XLogRecPtr output_written_lsn = InvalidXLogRecPtr ;
5656static XLogRecPtr output_fsync_lsn = InvalidXLogRecPtr ;
5757
@@ -173,10 +173,10 @@ OutputFsync(int64 now)
173173 if (fsync_interval <= 0 )
174174 return true;
175175
176- if (!output_unsynced )
176+ if (!output_needs_fsync )
177177 return true;
178178
179- output_unsynced = false;
179+ output_needs_fsync = false;
180180
181181 /* Accept EINVAL, in case output is writing to a pipe or similar. */
182182 if (fsync (outfd ) != 0 && errno != EINVAL )
@@ -304,6 +304,17 @@ StreamLog(void)
304304 last_status = now ;
305305 }
306306
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+
307318 r = PQgetCopyData (conn , & copybuf , 1 );
308319 if (r == 0 )
309320 {
@@ -327,7 +338,7 @@ StreamLog(void)
327338 ((int64 ) 1000 );
328339
329340 /* 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 )
331342 fsync_target = output_last_fsync + (fsync_interval - 1 ) *
332343 ((int64 ) 1000 );
333344
@@ -468,28 +479,14 @@ StreamLog(void)
468479 output_written_lsn = Max (temp , output_written_lsn );
469480 }
470481
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 */
488483 if (outfd == -1 )
489484 {
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 );
493490 if (outfd == -1 )
494491 {
495492 fprintf (stderr ,
@@ -503,7 +500,7 @@ StreamLog(void)
503500 bytes_written = 0 ;
504501
505502 /* signal that a fsync is needed */
506- output_unsynced = true;
503+ output_needs_fsync = true;
507504
508505 while (bytes_left )
509506 {
0 commit comments