@@ -2256,11 +2256,9 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
22562256{
22572257 char path [MAXPGPATH ];
22582258 char tmppath [MAXPGPATH ];
2259- char * zbuffer ;
22602259 XLogSegNo installed_segno ;
22612260 int max_advance ;
22622261 int fd ;
2263- int nbytes ;
22642262
22652263 XLogFilePath (path , ThisTimeLineID , logsegno );
22662264
@@ -2294,16 +2292,6 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
22942292
22952293 unlink (tmppath );
22962294
2297- /*
2298- * Allocate a buffer full of zeros. This is done before opening the file
2299- * so that we don't leak the file descriptor if palloc fails.
2300- *
2301- * Note: palloc zbuffer, instead of just using a local char array, to
2302- * ensure it is reasonably well-aligned; this may save a few cycles
2303- * transferring data to the kernel.
2304- */
2305- zbuffer = (char * ) palloc0 (XLOG_BLCKSZ );
2306-
23072295 /* do not use get_sync_bit() here --- want to fsync only at end of fill */
23082296 fd = BasicOpenFile (tmppath , O_RDWR | O_CREAT | O_EXCL | PG_BINARY ,
23092297 S_IRUSR | S_IWUSR );
@@ -2312,38 +2300,73 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock)
23122300 (errcode_for_file_access (),
23132301 errmsg ("could not create file \"%s\": %m" , tmppath )));
23142302
2315- /*
2316- * Zero-fill the file. We have to do this the hard way to ensure that all
2317- * the file space has really been allocated --- on platforms that allow
2318- * "holes" in files, just seeking to the end doesn't allocate intermediate
2319- * space. This way, we know that we have all the space and (after the
2320- * fsync below) that all the indirect blocks are down on disk. Therefore,
2321- * fdatasync(2) or O_DSYNC will be sufficient to sync future writes to the
2322- * log file.
2323- */
2324- for (nbytes = 0 ; nbytes < XLogSegSize ; nbytes += XLOG_BLCKSZ )
2303+ #ifdef HAVE_POSIX_FALLOCATE
23252304 {
2326- errno = 0 ;
2327- if ((int ) write (fd , zbuffer , XLOG_BLCKSZ ) != (int ) XLOG_BLCKSZ )
2328- {
2329- int save_errno = errno ;
2305+ errno = posix_fallocate (fd , 0 , XLogSegSize );
23302306
2331- /*
2332- * If we fail to make the file, delete it to release disk space
2333- */
2334- unlink (tmppath );
2307+ if (errno )
2308+ {
2309+ int errno_saved = errno ;
23352310
23362311 close (fd );
2337-
2338- /* if write didn't set errno, assume problem is no disk space */
2339- errno = save_errno ? save_errno : ENOSPC ;
2312+ unlink (tmppath );
2313+ errno = errno_saved ;
23402314
23412315 ereport (ERROR ,
23422316 (errcode_for_file_access (),
2343- errmsg ("could not write to file \"%s\": %m" , tmppath )));
2317+ errmsg ("could not allocate space for file \"%s\" using posix_fallocate: %m" ,
2318+ tmppath )));
2319+ }
2320+ }
2321+ #else /* !HAVE_POSIX_FALLOCATE */
2322+ {
2323+ /*
2324+ * Allocate a buffer full of zeros. This is done before opening the
2325+ * file so that we don't leak the file descriptor if palloc fails.
2326+ *
2327+ * Note: palloc zbuffer, instead of just using a local char array, to
2328+ * ensure it is reasonably well-aligned; this may save a few cycles
2329+ * transferring data to the kernel.
2330+ */
2331+
2332+ char * zbuffer = (char * ) palloc0 (XLOG_BLCKSZ );
2333+ int nbytes ;
2334+
2335+ /*
2336+ * Zero-fill the file. We have to do this the hard way to ensure that
2337+ * all the file space has really been allocated --- on platforms that
2338+ * allow "holes" in files, just seeking to the end doesn't allocate
2339+ * intermediate space. This way, we know that we have all the space
2340+ * and (after the fsync below) that all the indirect blocks are down on
2341+ * disk. Therefore, fdatasync(2) or O_DSYNC will be sufficient to sync
2342+ * future writes to the log file.
2343+ */
2344+ for (nbytes = 0 ; nbytes < XLogSegSize ; nbytes += XLOG_BLCKSZ )
2345+ {
2346+ errno = 0 ;
2347+ if ((int ) write (fd , zbuffer , XLOG_BLCKSZ ) != (int ) XLOG_BLCKSZ )
2348+ {
2349+ int save_errno = errno ;
2350+
2351+ /*
2352+ * If we fail to make the file, delete it to release disk space
2353+ */
2354+ unlink (tmppath );
2355+
2356+ close (fd );
2357+
2358+ /* if write didn't set errno, assume no disk space */
2359+ errno = save_errno ? save_errno : ENOSPC ;
2360+
2361+ ereport (ERROR ,
2362+ (errcode_for_file_access (),
2363+ errmsg ("could not write to file \"%s\": %m" ,
2364+ tmppath )));
2365+ }
23442366 }
2367+ pfree (zbuffer );
23452368 }
2346- pfree ( zbuffer );
2369+ #endif /* HAVE_POSIX_FALLOCATE */
23472370
23482371 if (pg_fsync (fd ) != 0 )
23492372 {
0 commit comments