Restore pg_pread and friends.

Commits cf112c12 and a0dc8271 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
This commit is contained in:
Thomas Munro
2022-09-29 13:12:11 +13:00
parent 3a5817695a
commit b6d8a60aba
19 changed files with 75 additions and 55 deletions

View File

@ -378,11 +378,11 @@ extern void XLogReaderResetError(XLogReaderState *state);
/*
* Error information from WALRead that both backend and frontend caller can
* process. Currently only errors from pread can be reported.
* process. Currently only errors from pg_pread can be reported.
*/
typedef struct WALReadError
{
int wre_errno; /* errno set by the last pread() */
int wre_errno; /* errno set by the last pg_pread() */
int wre_off; /* Offset we tried to read from. */
int wre_req; /* Bytes requested to be read. */
int wre_read; /* Bytes read by the last read(). */

View File

@ -213,6 +213,15 @@ extern int pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2,
extern int pg_vprintf(const char *fmt, va_list args) pg_attribute_printf(1, 0);
extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
#ifndef WIN32
/*
* We add a pg_ prefix as a warning that the Windows implementations have the
* non-standard side-effect of changing the current file position.
*/
#define pg_pread pread
#define pg_pwrite pwrite
#endif
/*
* We use __VA_ARGS__ for printf to prevent replacing references to
* the "printf" format archetype in format() attribute declarations.

View File

@ -35,12 +35,21 @@ struct iovec
/* Define a reasonable maximum that is safe to use on the stack. */
#define PG_IOV_MAX Min(IOV_MAX, 32)
#if !HAVE_DECL_PREADV
extern ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
/*
* Note that pg_preadv and pg_writev have a pg_ prefix as a warning that the
* Windows implementations have the side-effect of changing the file position.
*/
#if HAVE_DECL_PREADV
#define pg_preadv preadv
#else
extern ssize_t pg_preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);
#endif
#if !HAVE_DECL_PWRITEV
extern ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
#if HAVE_DECL_PWRITEV
#define pg_pwritev pwritev
#else
extern ssize_t pg_pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
#endif
#endif /* PG_IOVEC_H */

View File

@ -564,9 +564,9 @@ typedef unsigned short mode_t;
#endif
/* in port/win32pread.c */
extern ssize_t pread(int fd, void *buf, size_t nbyte, off_t offset);
extern ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset);
/* in port/win32pwrite.c */
extern ssize_t pwrite(int fd, const void *buf, size_t nbyte, off_t offset);
extern ssize_t pg_pwrite(int fd, const void *buf, size_t nbyte, off_t offset);
#endif /* PG_WIN32_PORT_H */