Skip to content

Commit 701a51f

Browse files
committed
Use pg_pwrite() in more places.
This removes some lseek() system calls. Author: Thomas Munro Reviewed-by: Andres Freund Discussion: https://postgr.es/m/CA%2BhUKGJ%2BoHhnvqjn3%3DHro7xu-YDR8FPr0FL6LF35kHRX%3D_bUzg%40mail.gmail.com
1 parent 2102ba4 commit 701a51f

File tree

4 files changed

+7
-40
lines changed

4 files changed

+7
-40
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,12 +1868,9 @@ qtext_store(const char *query, int query_len,
18681868
if (fd < 0)
18691869
goto error;
18701870

1871-
if (lseek(fd, off, SEEK_SET) != off)
1871+
if (pg_pwrite(fd, query, query_len, off) != query_len)
18721872
goto error;
1873-
1874-
if (write(fd, query, query_len) != query_len)
1875-
goto error;
1876-
if (write(fd, "\0", 1) != 1)
1873+
if (pg_pwrite(fd, "\0", 1, off + query_len) != 1)
18771874
goto error;
18781875

18791876
CloseTransientFile(fd);

src/backend/access/heap/rewriteheap.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,21 +1156,14 @@ heap_xlog_logical_rewrite(XLogReaderState *r)
11561156
path, (uint32) xlrec->offset)));
11571157
pgstat_report_wait_end();
11581158

1159-
/* now seek to the position we want to write our data to */
1160-
if (lseek(fd, xlrec->offset, SEEK_SET) != xlrec->offset)
1161-
ereport(ERROR,
1162-
(errcode_for_file_access(),
1163-
errmsg("could not seek to end of file \"%s\": %m",
1164-
path)));
1165-
11661159
data = XLogRecGetData(r) + sizeof(*xlrec);
11671160

11681161
len = xlrec->num_mappings * sizeof(LogicalRewriteMappingData);
11691162

11701163
/* write out tail end of mapping file (again) */
11711164
errno = 0;
11721165
pgstat_report_wait_start(WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE);
1173-
if (write(fd, data, len) != len)
1166+
if (pg_pwrite(fd, data, len, xlrec->offset) != len)
11741167
{
11751168
/* if write didn't set errno, assume problem is no disk space */
11761169
if (errno == 0)

src/backend/replication/walreceiver.c

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,13 @@ WalReceiverFunctionsType *WalReceiverFunctions = NULL;
8585
#define NAPTIME_PER_CYCLE 100 /* max sleep time between cycles (100ms) */
8686

8787
/*
88-
* These variables are used similarly to openLogFile/SegNo/Off,
88+
* These variables are used similarly to openLogFile/SegNo,
8989
* but for walreceiver to write the XLOG. recvFileTLI is the TimeLineID
9090
* corresponding the filename of recvFile.
9191
*/
9292
static int recvFile = -1;
9393
static TimeLineID recvFileTLI = 0;
9494
static XLogSegNo recvSegNo = 0;
95-
static uint32 recvOff = 0;
9695

9796
/*
9897
* Flags set by interrupt handlers of walreceiver for later service in the
@@ -945,7 +944,6 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
945944
use_existent = true;
946945
recvFile = XLogFileInit(recvSegNo, &use_existent, true);
947946
recvFileTLI = ThisTimeLineID;
948-
recvOff = 0;
949947
}
950948

951949
/* Calculate the start offset of the received logs */
@@ -956,29 +954,10 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
956954
else
957955
segbytes = nbytes;
958956

959-
/* Need to seek in the file? */
960-
if (recvOff != startoff)
961-
{
962-
if (lseek(recvFile, (off_t) startoff, SEEK_SET) < 0)
963-
{
964-
char xlogfname[MAXFNAMELEN];
965-
int save_errno = errno;
966-
967-
XLogFileName(xlogfname, recvFileTLI, recvSegNo, wal_segment_size);
968-
errno = save_errno;
969-
ereport(PANIC,
970-
(errcode_for_file_access(),
971-
errmsg("could not seek in log segment %s to offset %u: %m",
972-
xlogfname, startoff)));
973-
}
974-
975-
recvOff = startoff;
976-
}
977-
978957
/* OK to write the logs */
979958
errno = 0;
980959

981-
byteswritten = write(recvFile, buf, segbytes);
960+
byteswritten = pg_pwrite(recvFile, buf, segbytes, (off_t) startoff);
982961
if (byteswritten <= 0)
983962
{
984963
char xlogfname[MAXFNAMELEN];
@@ -995,13 +974,12 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr)
995974
(errcode_for_file_access(),
996975
errmsg("could not write to log segment %s "
997976
"at offset %u, length %lu: %m",
998-
xlogfname, recvOff, (unsigned long) segbytes)));
977+
xlogfname, startoff, (unsigned long) segbytes)));
999978
}
1000979

1001980
/* Update state for write */
1002981
recptr += byteswritten;
1003982

1004-
recvOff += byteswritten;
1005983
nbytes -= byteswritten;
1006984
buf += byteswritten;
1007985

src/backend/utils/init/miscinit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,7 @@ AddToDataDirLockFile(int target_line, const char *str)
13331333
len = strlen(destbuffer);
13341334
errno = 0;
13351335
pgstat_report_wait_start(WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE);
1336-
if (lseek(fd, (off_t) 0, SEEK_SET) != 0 ||
1337-
(int) write(fd, destbuffer, len) != len)
1336+
if (pg_pwrite(fd, destbuffer, len, 0) != len)
13381337
{
13391338
pgstat_report_wait_end();
13401339
/* if write didn't set errno, assume problem is no disk space */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy