Skip to content

Commit b6d8a60

Browse files
committed
Restore pg_pread and friends.
Commits cf112c1 and a0dc827 were a little too hasty in getting rid of the pg_ prefixes where we use pread(), pwrite() and vectored variants. We dropped support for ancient Unixes where we needed to use lseek() to implement replacements for those, but it turns out that Windows also changes the current position even when you pass in an offset to ReadFile() and WriteFile() if the file handle is synchronous, despite its documentation saying otherwise. Switching to asynchronous file handles would fix that, but have other complications. For now let's just put back the pg_ prefix and add some comments to highlight the non-standard side-effect, which we can now describe as Windows-only. Reported-by: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> Reviewed-by: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> Discussion: https://postgr.es/m/20220923202439.GA1156054%40nathanxps13
1 parent 3a58176 commit b6d8a60

File tree

19 files changed

+75
-55
lines changed

19 files changed

+75
-55
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,9 +2103,9 @@ qtext_store(const char *query, int query_len,
21032103
if (fd < 0)
21042104
goto error;
21052105

2106-
if (pwrite(fd, query, query_len, off) != query_len)
2106+
if (pg_pwrite(fd, query, query_len, off) != query_len)
21072107
goto error;
2108-
if (pwrite(fd, "\0", 1, off + query_len) != 1)
2108+
if (pg_pwrite(fd, "\0", 1, off + query_len) != 1)
21092109
goto error;
21102110

21112111
CloseTransientFile(fd);

src/backend/access/heap/rewriteheap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ heap_xlog_logical_rewrite(XLogReaderState *r)
11501150
/* write out tail end of mapping file (again) */
11511151
errno = 0;
11521152
pgstat_report_wait_start(WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE);
1153-
if (pwrite(fd, data, len, xlrec->offset) != len)
1153+
if (pg_pwrite(fd, data, len, xlrec->offset) != len)
11541154
{
11551155
/* if write didn't set errno, assume problem is no disk space */
11561156
if (errno == 0)

src/backend/access/transam/slru.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ SlruPhysicalReadPage(SlruCtl ctl, int pageno, int slotno)
718718

719719
errno = 0;
720720
pgstat_report_wait_start(WAIT_EVENT_SLRU_READ);
721-
if (pread(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
721+
if (pg_pread(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
722722
{
723723
pgstat_report_wait_end();
724724
slru_errcause = SLRU_READ_FAILED;
@@ -873,7 +873,7 @@ SlruPhysicalWritePage(SlruCtl ctl, int pageno, int slotno, SlruWriteAll fdata)
873873

874874
errno = 0;
875875
pgstat_report_wait_start(WAIT_EVENT_SLRU_WRITE);
876-
if (pwrite(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
876+
if (pg_pwrite(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
877877
{
878878
pgstat_report_wait_end();
879879
/* if write didn't set errno, assume problem is no disk space */

src/backend/access/transam/xlog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,7 +2196,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
21962196
INSTR_TIME_SET_CURRENT(start);
21972197

21982198
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
2199-
written = pwrite(openLogFile, from, nleft, startoffset);
2199+
written = pg_pwrite(openLogFile, from, nleft, startoffset);
22002200
pgstat_report_wait_end();
22012201

22022202
/*
@@ -3018,7 +3018,7 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
30183018
* enough.
30193019
*/
30203020
errno = 0;
3021-
if (pwrite(fd, zbuffer.data, 1, wal_segment_size - 1) != 1)
3021+
if (pg_pwrite(fd, zbuffer.data, 1, wal_segment_size - 1) != 1)
30223022
{
30233023
/* if write didn't set errno, assume no disk space */
30243024
save_errno = errno ? errno : ENOSPC;

src/backend/access/transam/xlogreader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ WALRead(XLogReaderState *state,
15441544

15451545
/* Reset errno first; eases reporting non-errno-affecting errors */
15461546
errno = 0;
1547-
readbytes = pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1547+
readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
15481548

15491549
#ifndef FRONTEND
15501550
pgstat_report_wait_end();

src/backend/access/transam/xlogrecovery.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3271,7 +3271,7 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
32713271
readOff = targetPageOff;
32723272

32733273
pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
3274-
r = pread(readFile, readBuf, XLOG_BLCKSZ, (off_t) readOff);
3274+
r = pg_pread(readFile, readBuf, XLOG_BLCKSZ, (off_t) readOff);
32753275
if (r != XLOG_BLCKSZ)
32763276
{
32773277
char fname[MAXFNAMELEN];

src/backend/backup/basebackup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,7 @@ basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset,
18281828
int rc;
18291829

18301830
pgstat_report_wait_start(WAIT_EVENT_BASEBACKUP_READ);
1831-
rc = pread(fd, buf, nbytes, offset);
1831+
rc = pg_pread(fd, buf, nbytes, offset);
18321832
pgstat_report_wait_end();
18331833

18341834
if (rc < 0)

src/backend/replication/walreceiver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr, TimeLineID tli)
915915
/* OK to write the logs */
916916
errno = 0;
917917

918-
byteswritten = pwrite(recvFile, buf, segbytes, (off_t) startoff);
918+
byteswritten = pg_pwrite(recvFile, buf, segbytes, (off_t) startoff);
919919
if (byteswritten <= 0)
920920
{
921921
char xlogfname[MAXFNAMELEN];

src/backend/storage/file/fd.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ FileRead(File file, char *buffer, int amount, off_t offset,
20532053

20542054
retry:
20552055
pgstat_report_wait_start(wait_event_info);
2056-
returnCode = pread(vfdP->fd, buffer, amount, offset);
2056+
returnCode = pg_pread(vfdP->fd, buffer, amount, offset);
20572057
pgstat_report_wait_end();
20582058

20592059
if (returnCode < 0)
@@ -2135,7 +2135,7 @@ FileWrite(File file, char *buffer, int amount, off_t offset,
21352135
retry:
21362136
errno = 0;
21372137
pgstat_report_wait_start(wait_event_info);
2138-
returnCode = pwrite(VfdCache[file].fd, buffer, amount, offset);
2138+
returnCode = pg_pwrite(VfdCache[file].fd, buffer, amount, offset);
21392139
pgstat_report_wait_end();
21402140

21412141
/* if write didn't set errno, assume problem is no disk space */
@@ -3740,7 +3740,7 @@ data_sync_elevel(int elevel)
37403740
}
37413741

37423742
/*
3743-
* A convenience wrapper for pwritev() that retries on partial write. If an
3743+
* A convenience wrapper for pg_pwritev() that retries on partial write. If an
37443744
* error is returned, it is unspecified how much has been written.
37453745
*/
37463746
ssize_t
@@ -3760,7 +3760,7 @@ pg_pwritev_with_retry(int fd, const struct iovec *iov, int iovcnt, off_t offset)
37603760
for (;;)
37613761
{
37623762
/* Write as much as we can. */
3763-
part = pwritev(fd, iov, iovcnt, offset);
3763+
part = pg_pwritev(fd, iov, iovcnt, offset);
37643764
if (part < 0)
37653765
return -1;
37663766

src/backend/utils/init/miscinit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ AddToDataDirLockFile(int target_line, const char *str)
15271527
len = strlen(destbuffer);
15281528
errno = 0;
15291529
pgstat_report_wait_start(WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE);
1530-
if (pwrite(fd, destbuffer, len, 0) != len)
1530+
if (pg_pwrite(fd, destbuffer, len, 0) != len)
15311531
{
15321532
pgstat_report_wait_end();
15331533
/* if write didn't set errno, assume problem is no disk space */

0 commit comments

Comments
 (0)