@@ -3353,10 +3353,11 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
33533353{
33543354 char path [MAXPGPATH ];
33553355 char tmppath [MAXPGPATH ];
3356+ char * zbuffer ;
33563357 XLogSegNo installed_segno ;
33573358 int max_advance ;
33583359 int fd ;
3359- bool zero_fill = true ;
3360+ int nbytes ;
33603361
33613362 XLogFilePath (path , ThisTimeLineID , logsegno );
33623363
@@ -3390,6 +3391,16 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
33903391
33913392 unlink (tmppath );
33923393
3394+ /*
3395+ * Allocate a buffer full of zeros. This is done before opening the file
3396+ * so that we don't leak the file descriptor if palloc fails.
3397+ *
3398+ * Note: palloc zbuffer, instead of just using a local char array, to
3399+ * ensure it is reasonably well-aligned; this may save a few cycles
3400+ * transferring data to the kernel.
3401+ */
3402+ zbuffer = (char * ) palloc0 (XLOG_BLCKSZ );
3403+
33933404 /* do not use get_sync_bit() here --- want to fsync only at end of fill */
33943405 fd = BasicOpenFile (tmppath , O_RDWR | O_CREAT | O_EXCL | PG_BINARY ,
33953406 S_IRUSR | S_IWUSR );
@@ -3398,66 +3409,38 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
33983409 (errcode_for_file_access (),
33993410 errmsg ("could not create file \"%s\": %m" , tmppath )));
34003411
3401- #ifdef HAVE_POSIX_FALLOCATE
34023412 /*
3403- * If posix_fallocate() is available and succeeds, then the file is
3404- * properly allocated and we don't need to zero-fill it (which is less
3405- * efficient). In case of an error, fall back to writing zeros, because on
3406- * some platforms posix_fallocate() is available but will not always
3407- * succeed in cases where zero-filling will.
3413+ * Zero-fill the file. We have to do this the hard way to ensure that all
3414+ * the file space has really been allocated --- on platforms that allow
3415+ * "holes" in files, just seeking to the end doesn't allocate intermediate
3416+ * space. This way, we know that we have all the space and (after the
3417+ * fsync below) that all the indirect blocks are down on disk. Therefore,
3418+ * fdatasync(2) or O_DSYNC will be sufficient to sync future writes to the
3419+ * log file.
34083420 */
3409- if (posix_fallocate (fd , 0 , XLogSegSize ) == 0 )
3410- zero_fill = false;
3411- #endif /* HAVE_POSIX_FALLOCATE */
3412-
3413- if (zero_fill )
3421+ for (nbytes = 0 ; nbytes < XLogSegSize ; nbytes += XLOG_BLCKSZ )
34143422 {
3415- /*
3416- * Allocate a buffer full of zeros. This is done before opening the
3417- * file so that we don't leak the file descriptor if palloc fails.
3418- *
3419- * Note: palloc zbuffer, instead of just using a local char array, to
3420- * ensure it is reasonably well-aligned; this may save a few cycles
3421- * transferring data to the kernel.
3422- */
3423-
3424- char * zbuffer = (char * ) palloc0 (XLOG_BLCKSZ );
3425- int nbytes ;
3426-
3427- /*
3428- * Zero-fill the file. We have to do this the hard way to ensure that
3429- * all the file space has really been allocated --- on platforms that
3430- * allow "holes" in files, just seeking to the end doesn't allocate
3431- * intermediate space. This way, we know that we have all the space
3432- * and (after the fsync below) that all the indirect blocks are down on
3433- * disk. Therefore, fdatasync(2) or O_DSYNC will be sufficient to sync
3434- * future writes to the log file.
3435- */
3436- for (nbytes = 0 ; nbytes < XLogSegSize ; nbytes += XLOG_BLCKSZ )
3423+ errno = 0 ;
3424+ if ((int ) write (fd , zbuffer , XLOG_BLCKSZ ) != (int ) XLOG_BLCKSZ )
34373425 {
3438- errno = 0 ;
3439- if ((int ) write (fd , zbuffer , XLOG_BLCKSZ ) != (int ) XLOG_BLCKSZ )
3440- {
3441- int save_errno = errno ;
3426+ int save_errno = errno ;
34423427
3443- /*
3444- * If we fail to make the file, delete it to release disk space
3445- */
3446- unlink (tmppath );
3428+ /*
3429+ * If we fail to make the file, delete it to release disk space
3430+ */
3431+ unlink (tmppath );
34473432
3448- close (fd );
3433+ close (fd );
34493434
3450- /* if write didn't set errno, assume no disk space */
3451- errno = save_errno ? save_errno : ENOSPC ;
3435+ /* if write didn't set errno, assume problem is no disk space */
3436+ errno = save_errno ? save_errno : ENOSPC ;
34523437
3453- ereport (ERROR ,
3454- (errcode_for_file_access (),
3455- errmsg ("could not write to file \"%s\": %m" ,
3456- tmppath )));
3457- }
3438+ ereport (ERROR ,
3439+ (errcode_for_file_access (),
3440+ errmsg ("could not write to file \"%s\": %m" , tmppath )));
34583441 }
3459- pfree (zbuffer );
34603442 }
3443+ pfree (zbuffer );
34613444
34623445 if (pg_fsync (fd ) != 0 )
34633446 {
0 commit comments