Skip to content

Commit 79f29a1

Browse files
macdicepull[bot]
authored andcommitted
Remove dead pread and pwrite replacement code.
pread() and pwrite() are in SUSv2, and all targeted Unix systems have them. Previously, we defined pg_pread and pg_pwrite to emulate these function with lseek() on old Unixen. The names with a pg_ prefix were a reminder of a portability hazard: they might change the current file position. That hazard is gone, so we can drop the prefixes. Since the remaining replacement code is Windows-only, move it into src/port/win32p{read,write}.c, and move the declarations into src/include/port/win32_port.h. No need for vestigial HAVE_PREAD, HAVE_PWRITE macros as they were only used for declarations in port.h which have now moved into win32_port.h. Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Greg Stark <stark@mit.edu> Reviewed-by: Robert Haas <robertmhaas@gmail.com> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/CA+hUKGJ3LHeP9w5Fgzdr4G8AnEtJ=z=p6hGDEm4qYGEUX5B6fQ@mail.gmail.com
1 parent 768d22a commit 79f29a1

File tree

23 files changed

+76
-137
lines changed

23 files changed

+76
-137
lines changed

configure

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16728,32 +16728,6 @@ esac
1672816728

1672916729
fi
1673016730

16731-
ac_fn_c_check_func "$LINENO" "pread" "ac_cv_func_pread"
16732-
if test "x$ac_cv_func_pread" = xyes; then :
16733-
$as_echo "#define HAVE_PREAD 1" >>confdefs.h
16734-
16735-
else
16736-
case " $LIBOBJS " in
16737-
*" pread.$ac_objext "* ) ;;
16738-
*) LIBOBJS="$LIBOBJS pread.$ac_objext"
16739-
;;
16740-
esac
16741-
16742-
fi
16743-
16744-
ac_fn_c_check_func "$LINENO" "pwrite" "ac_cv_func_pwrite"
16745-
if test "x$ac_cv_func_pwrite" = xyes; then :
16746-
$as_echo "#define HAVE_PWRITE 1" >>confdefs.h
16747-
16748-
else
16749-
case " $LIBOBJS " in
16750-
*" pwrite.$ac_objext "* ) ;;
16751-
*) LIBOBJS="$LIBOBJS pwrite.$ac_objext"
16752-
;;
16753-
esac
16754-
16755-
fi
16756-
1675716731
ac_fn_c_check_func "$LINENO" "strlcat" "ac_cv_func_strlcat"
1675816732
if test "x$ac_cv_func_strlcat" = xyes; then :
1675916733
$as_echo "#define HAVE_STRLCAT 1" >>confdefs.h
@@ -17015,6 +16989,18 @@ esac
1701516989
;;
1701616990
esac
1701716991

16992+
case " $LIBOBJS " in
16993+
*" win32pread.$ac_objext "* ) ;;
16994+
*) LIBOBJS="$LIBOBJS win32pread.$ac_objext"
16995+
;;
16996+
esac
16997+
16998+
case " $LIBOBJS " in
16999+
*" win32pwrite.$ac_objext "* ) ;;
17000+
*) LIBOBJS="$LIBOBJS win32pwrite.$ac_objext"
17001+
;;
17002+
esac
17003+
1701817004
case " $LIBOBJS " in
1701917005
*" win32security.$ac_objext "* ) ;;
1702017006
*) LIBOBJS="$LIBOBJS win32security.$ac_objext"

configure.ac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,8 +1876,6 @@ AC_REPLACE_FUNCS(m4_normalize([
18761876
getpeereid
18771877
inet_aton
18781878
mkdtemp
1879-
pread
1880-
pwrite
18811879
strlcat
18821880
strlcpy
18831881
strnlen
@@ -1943,6 +1941,8 @@ if test "$PORTNAME" = "win32"; then
19431941
AC_LIBOBJ(win32error)
19441942
AC_LIBOBJ(win32link)
19451943
AC_LIBOBJ(win32ntdll)
1944+
AC_LIBOBJ(win32pread)
1945+
AC_LIBOBJ(win32pwrite)
19461946
AC_LIBOBJ(win32security)
19471947
AC_LIBOBJ(win32setlocale)
19481948
AC_LIBOBJ(win32stat)

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 (pg_pwrite(fd, query, query_len, off) != query_len)
2106+
if (pwrite(fd, query, query_len, off) != query_len)
21072107
goto error;
2108-
if (pg_pwrite(fd, "\0", 1, off + query_len) != 1)
2108+
if (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
@@ -1149,7 +1149,7 @@ heap_xlog_logical_rewrite(XLogReaderState *r)
11491149
/* write out tail end of mapping file (again) */
11501150
errno = 0;
11511151
pgstat_report_wait_start(WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE);
1152-
if (pg_pwrite(fd, data, len, xlrec->offset) != len)
1152+
if (pwrite(fd, data, len, xlrec->offset) != len)
11531153
{
11541154
/* if write didn't set errno, assume problem is no disk space */
11551155
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 (pg_pread(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
721+
if (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 (pg_pwrite(fd, shared->page_buffer[slotno], BLCKSZ, offset) != BLCKSZ)
876+
if (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
@@ -2189,7 +2189,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
21892189
INSTR_TIME_SET_CURRENT(start);
21902190

21912191
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
2192-
written = pg_pwrite(openLogFile, from, nleft, startoffset);
2192+
written = pwrite(openLogFile, from, nleft, startoffset);
21932193
pgstat_report_wait_end();
21942194

21952195
/*
@@ -3011,7 +3011,7 @@ XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
30113011
* enough.
30123012
*/
30133013
errno = 0;
3014-
if (pg_pwrite(fd, zbuffer.data, 1, wal_segment_size - 1) != 1)
3014+
if (pwrite(fd, zbuffer.data, 1, wal_segment_size - 1) != 1)
30153015
{
30163016
/* if write didn't set errno, assume no disk space */
30173017
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
@@ -1514,7 +1514,7 @@ WALRead(XLogReaderState *state,
15141514

15151515
/* Reset errno first; eases reporting non-errno-affecting errors */
15161516
errno = 0;
1517-
readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1517+
readbytes = pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
15181518

15191519
#ifndef FRONTEND
15201520
pgstat_report_wait_end();

src/backend/access/transam/xlogrecovery.c

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

32623262
pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
3263-
r = pg_pread(readFile, readBuf, XLOG_BLCKSZ, (off_t) readOff);
3263+
r = pread(readFile, readBuf, XLOG_BLCKSZ, (off_t) readOff);
32643264
if (r != XLOG_BLCKSZ)
32653265
{
32663266
char fname[MAXFNAMELEN];

src/backend/replication/basebackup.c

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

18241824
pgstat_report_wait_start(WAIT_EVENT_BASEBACKUP_READ);
1825-
rc = pg_pread(fd, buf, nbytes, offset);
1825+
rc = pread(fd, buf, nbytes, offset);
18261826
pgstat_report_wait_end();
18271827

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

src/backend/storage/file/fd.c

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

20642064
retry:
20652065
pgstat_report_wait_start(wait_event_info);
2066-
returnCode = pg_pread(vfdP->fd, buffer, amount, offset);
2066+
returnCode = pread(vfdP->fd, buffer, amount, offset);
20672067
pgstat_report_wait_end();
20682068

20692069
if (returnCode < 0)
@@ -2145,7 +2145,7 @@ FileWrite(File file, char *buffer, int amount, off_t offset,
21452145
retry:
21462146
errno = 0;
21472147
pgstat_report_wait_start(wait_event_info);
2148-
returnCode = pg_pwrite(VfdCache[file].fd, buffer, amount, offset);
2148+
returnCode = pwrite(VfdCache[file].fd, buffer, amount, offset);
21492149
pgstat_report_wait_end();
21502150

21512151
/* if write didn't set errno, assume problem is no disk space */

src/backend/utils/init/miscinit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ AddToDataDirLockFile(int target_line, const char *str)
14291429
len = strlen(destbuffer);
14301430
errno = 0;
14311431
pgstat_report_wait_start(WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE);
1432-
if (pg_pwrite(fd, destbuffer, len, 0) != len)
1432+
if (pwrite(fd, destbuffer, len, 0) != len)
14331433
{
14341434
pgstat_report_wait_end();
14351435
/* if write didn't set errno, assume problem is no disk space */

src/bin/pg_test_fsync/pg_test_fsync.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,10 @@ test_sync(int writes_per_op)
312312
for (ops = 0; alarm_triggered == false; ops++)
313313
{
314314
for (writes = 0; writes < writes_per_op; writes++)
315-
if (pg_pwrite(tmpfile,
316-
buf,
317-
XLOG_BLCKSZ,
318-
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
315+
if (pwrite(tmpfile,
316+
buf,
317+
XLOG_BLCKSZ,
318+
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
319319
die("write failed");
320320
}
321321
STOP_TIMER;
@@ -338,10 +338,10 @@ test_sync(int writes_per_op)
338338
for (ops = 0; alarm_triggered == false; ops++)
339339
{
340340
for (writes = 0; writes < writes_per_op; writes++)
341-
if (pg_pwrite(tmpfile,
342-
buf,
343-
XLOG_BLCKSZ,
344-
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
341+
if (pwrite(tmpfile,
342+
buf,
343+
XLOG_BLCKSZ,
344+
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
345345
die("write failed");
346346
fdatasync(tmpfile);
347347
}
@@ -363,10 +363,10 @@ test_sync(int writes_per_op)
363363
for (ops = 0; alarm_triggered == false; ops++)
364364
{
365365
for (writes = 0; writes < writes_per_op; writes++)
366-
if (pg_pwrite(tmpfile,
367-
buf,
368-
XLOG_BLCKSZ,
369-
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
366+
if (pwrite(tmpfile,
367+
buf,
368+
XLOG_BLCKSZ,
369+
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
370370
die("write failed");
371371
if (fsync(tmpfile) != 0)
372372
die("fsync failed");
@@ -387,10 +387,10 @@ test_sync(int writes_per_op)
387387
for (ops = 0; alarm_triggered == false; ops++)
388388
{
389389
for (writes = 0; writes < writes_per_op; writes++)
390-
if (pg_pwrite(tmpfile,
391-
buf,
392-
XLOG_BLCKSZ,
393-
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
390+
if (pwrite(tmpfile,
391+
buf,
392+
XLOG_BLCKSZ,
393+
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
394394
die("write failed");
395395
if (pg_fsync_writethrough(tmpfile) != 0)
396396
die("fsync failed");
@@ -419,10 +419,10 @@ test_sync(int writes_per_op)
419419
for (ops = 0; alarm_triggered == false; ops++)
420420
{
421421
for (writes = 0; writes < writes_per_op; writes++)
422-
if (pg_pwrite(tmpfile,
423-
buf,
424-
XLOG_BLCKSZ,
425-
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
422+
if (pwrite(tmpfile,
423+
buf,
424+
XLOG_BLCKSZ,
425+
writes * XLOG_BLCKSZ) != XLOG_BLCKSZ)
426426

427427
/*
428428
* This can generate write failures if the filesystem has
@@ -484,10 +484,10 @@ test_open_sync(const char *msg, int writes_size)
484484
for (ops = 0; alarm_triggered == false; ops++)
485485
{
486486
for (writes = 0; writes < 16 / writes_size; writes++)
487-
if (pg_pwrite(tmpfile,
488-
buf,
489-
writes_size * 1024,
490-
writes * writes_size * 1024) !=
487+
if (pwrite(tmpfile,
488+
buf,
489+
writes_size * 1024,
490+
writes * writes_size * 1024) !=
491491
writes_size * 1024)
492492
die("write failed");
493493
}
@@ -586,7 +586,7 @@ test_non_sync(void)
586586
START_TIMER;
587587
for (ops = 0; alarm_triggered == false; ops++)
588588
{
589-
if (pg_pwrite(tmpfile, buf, XLOG_BLCKSZ, 0) != XLOG_BLCKSZ)
589+
if (pwrite(tmpfile, buf, XLOG_BLCKSZ, 0) != XLOG_BLCKSZ)
590590
die("write failed");
591591
}
592592
STOP_TIMER;

src/include/access/xlogreader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,11 @@ extern bool XLogReaderValidatePageHeader(XLogReaderState *state,
375375

376376
/*
377377
* Error information from WALRead that both backend and frontend caller can
378-
* process. Currently only errors from pg_pread can be reported.
378+
* process. Currently only errors from pread can be reported.
379379
*/
380380
typedef struct WALReadError
381381
{
382-
int wre_errno; /* errno set by the last pg_pread() */
382+
int wre_errno; /* errno set by the last pread() */
383383
int wre_off; /* Offset we tried to read from. */
384384
int wre_req; /* Bytes requested to be read. */
385385
int wre_read; /* Bytes read by the last read(). */

src/include/pg_config.h.in

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,6 @@
388388
/* Define to 1 if you have the `ppoll' function. */
389389
#undef HAVE_PPOLL
390390

391-
/* Define to 1 if you have the `pread' function. */
392-
#undef HAVE_PREAD
393-
394391
/* Define to 1 if the PS_STRINGS thing exists. */
395392
#undef HAVE_PS_STRINGS
396393

@@ -406,9 +403,6 @@
406403
/* Have PTHREAD_PRIO_INHERIT. */
407404
#undef HAVE_PTHREAD_PRIO_INHERIT
408405

409-
/* Define to 1 if you have the `pwrite' function. */
410-
#undef HAVE_PWRITE
411-
412406
/* Define to 1 if you have the <readline.h> header file. */
413407
#undef HAVE_READLINE_H
414408

src/include/port.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -417,25 +417,6 @@ extern char *mkdtemp(char *path);
417417
extern int inet_aton(const char *cp, struct in_addr *addr);
418418
#endif
419419

420-
/*
421-
* Windows and older Unix don't have pread(2) and pwrite(2). We have
422-
* replacement functions, but they have slightly different semantics so we'll
423-
* use a name with a pg_ prefix to avoid confusion.
424-
*/
425-
#ifdef HAVE_PREAD
426-
#define pg_pread pread
427-
#else
428-
extern ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset);
429-
#endif
430-
431-
#ifdef HAVE_PWRITE
432-
#define pg_pwrite pwrite
433-
#else
434-
extern ssize_t pg_pwrite(int fd, const void *buf, size_t nbyte, off_t offset);
435-
#endif
436-
437-
/* For pg_pwritev() and pg_preadv(), see port/pg_iovec.h. */
438-
439420
#if !HAVE_DECL_STRLCAT
440421
extern size_t strlcat(char *dst, const char *src, size_t siz);
441422
#endif

src/include/port/win32_port.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,10 @@ typedef unsigned short mode_t;
562562
#define HAVE_BUGGY_STRTOF 1
563563
#endif
564564

565+
/* in port/win32pread.c */
566+
extern ssize_t pread(int fd, void *buf, size_t nbyte, off_t offset);
567+
568+
/* in port/win32pwrite.c */
569+
extern ssize_t pwrite(int fd, const void *buf, size_t nbyte, off_t offset);
570+
565571
#endif /* PG_WIN32_PORT_H */

src/port/preadv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pg_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
3030
{
3131
#ifdef HAVE_READV
3232
if (iovcnt == 1)
33-
return pg_pread(fd, iov[0].iov_base, iov[0].iov_len, offset);
33+
return pread(fd, iov[0].iov_base, iov[0].iov_len, offset);
3434
if (lseek(fd, offset, SEEK_SET) < 0)
3535
return -1;
3636
return readv(fd, iov, iovcnt);
@@ -40,7 +40,7 @@ pg_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset)
4040

4141
for (int i = 0; i < iovcnt; ++i)
4242
{
43-
part = pg_pread(fd, iov[i].iov_base, iov[i].iov_len, offset);
43+
part = pread(fd, iov[i].iov_base, iov[i].iov_len, offset);
4444
if (part < 0)
4545
{
4646
if (i == 0)

src/port/pwritev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pg_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
3030
{
3131
#ifdef HAVE_WRITEV
3232
if (iovcnt == 1)
33-
return pg_pwrite(fd, iov[0].iov_base, iov[0].iov_len, offset);
33+
return pwrite(fd, iov[0].iov_base, iov[0].iov_len, offset);
3434
if (lseek(fd, offset, SEEK_SET) < 0)
3535
return -1;
3636
return writev(fd, iov, iovcnt);
@@ -40,7 +40,7 @@ pg_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset)
4040

4141
for (int i = 0; i < iovcnt; ++i)
4242
{
43-
part = pg_pwrite(fd, iov[i].iov_base, iov[i].iov_len, offset);
43+
part = pwrite(fd, iov[i].iov_base, iov[i].iov_len, offset);
4444
if (part < 0)
4545
{
4646
if (i == 0)

0 commit comments

Comments
 (0)