From 759f6dadc758800c4afc060bf22bf111f57b3de4 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 11 Feb 2020 17:08:40 +1300
Subject: [PATCH 1/3] Add a wrapper for fstat() for when you just want the
 size.

We'll use this in a couple of places to replace lseek(SEEK_END)
for the same effect without moving the file position.
---
 src/backend/storage/file/fd.c | 14 ++++++++++++++
 src/include/storage/fd.h      |  1 +
 2 files changed, 15 insertions(+)

diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index b5f4df6a48..82ca0fc18e 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -322,6 +322,20 @@ static void unlink_if_exists_fname(const char *fname, bool isdir, int elevel);
 static int	fsync_parent_path(const char *fname, int elevel);
 
 
+/*
+ * pg_fstat_size --- fstat wrapper that just returns the size, or -1 for error
+ */
+off_t
+pg_fstat_size(int fd)
+{
+	struct stat st;
+
+	if (fstat(fd, &st) < 0)
+		return -1;
+	else
+		return st.st_size;
+}
+
 /*
  * pg_fsync --- do fsync with or without writethrough
  */
diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h
index 51e2ece3c9..6f4a432317 100644
--- a/src/include/storage/fd.h
+++ b/src/include/storage/fd.h
@@ -139,6 +139,7 @@ extern void RemovePgTempFilesInDir(const char *tmpdirname, bool missing_ok,
 								   bool unlink_all);
 extern bool looks_like_temp_rel_name(const char *name);
 
+extern off_t pg_fstat_size(int fd);
 extern int	pg_fsync(int fd);
 extern int	pg_fsync_no_writethrough(int fd);
 extern int	pg_fsync_writethrough(int fd);
-- 
2.23.0

