From 0c2ea03986b80d7593fe54cbd212acee2425fa6f Mon Sep 17 00:00:00 2001 From: Aidar Imamov Date: Fri, 17 Oct 2025 12:56:58 +0300 Subject: [PATCH 1/3] This fix adds protection against Linux-specific code related to the polar_monitor extension with no functional changes for Linux. In particular: - Removed inclusion of from procstat.h and related sources. Verified that no constants (e.g., PATH_MAX, NAME_MAX) from are used in procstat.h, procstat.c, or polar_monitor_io.c. - Wrapped usage of , TCP_INFO, SOL_TCP, SIOCOUTQ, and SIOCINQ with #ifdef __linux__ in polar_network_stats.c Provided safe fallbacks: set values to 0 or skip updates. --- external/polar_monitor/procstat.h | 1 - src/backend/libpq/polar_network_stats.c | 27 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/external/polar_monitor/procstat.h b/external/polar_monitor/procstat.h index 9da48525b82..3314ef56f11 100644 --- a/external/polar_monitor/procstat.h +++ b/external/polar_monitor/procstat.h @@ -30,7 +30,6 @@ #include #include #include -#include #include typedef enum PROCARGTYPE diff --git a/src/backend/libpq/polar_network_stats.c b/src/backend/libpq/polar_network_stats.c index 8bbb1110105..d32ae2f4f52 100644 --- a/src/backend/libpq/polar_network_stats.c +++ b/src/backend/libpq/polar_network_stats.c @@ -25,7 +25,9 @@ #include "postgres.h" +#ifdef __linux__ #include +#endif #include #include #include @@ -210,9 +212,13 @@ polar_network_stat_timer(void) static void polar_network_get_tcpinfo(void) { +#ifdef __linux__ struct tcp_info info; - int length = sizeof(struct tcp_info); + struct timeval now; int ret; + int index; + int length = sizeof(struct tcp_info); +#endif if (!polar_network_stat_array) return; @@ -220,12 +226,11 @@ polar_network_get_tcpinfo(void) if (!MyProcPort) return; +#ifdef __linux__ ret = getsockopt(MyProcPort->sock, SOL_TCP, TCP_INFO, (void *) &info, (socklen_t *) &length); if (ret == 0) { - struct timeval now; - int index = POLAR_NET_STAT_BACKEND_INDEX(); - + index = POLAR_NET_STAT_BACKEND_INDEX(); if (index < 0) return; @@ -238,6 +243,9 @@ polar_network_get_tcpinfo(void) polar_network_stat_array[index].tcpinfo_update_time = now.tv_sec; last_network_tcpinfo_update_time = now.tv_sec; } +#else + /* TCP_INFO with SOL_TCP are Linux-specific, skip updating tcpinfo */ +#endif } /* @@ -246,9 +254,11 @@ polar_network_get_tcpinfo(void) static void polar_network_get_qlen(void) { +#ifdef __linux__ int queue_len; - int index; int ret; +#endif + int index; if (!polar_network_stat_array) return; @@ -260,6 +270,8 @@ polar_network_get_qlen(void) if (index < 0) return; +#ifdef __linux__ + ret = ioctl(MyProcPort->sock, SIOCOUTQ, &queue_len); if (ret == 0) polar_network_stat_array[index].sendq = queue_len; @@ -267,4 +279,9 @@ polar_network_get_qlen(void) ret = ioctl(MyProcPort->sock, SIOCINQ, &queue_len); if (ret == 0) polar_network_stat_array[index].recvq = queue_len; +#else + /* SIOCOUTQ/SIOCINQ are Linux-specific, set values to 0 */ + polar_network_stat_array[index].sendq = 0; + polar_network_stat_array[index].recvq = 0; +#endif } From 56d2eca9416d86d4e6915318ad6664c6db7d82e3 Mon Sep 17 00:00:00 2001 From: Aidar Imamov Date: Fri, 17 Oct 2025 16:29:54 +0300 Subject: [PATCH 2/3] This commit adds protection against a platform-specific feature in polar_vfs. In particular: - Gate .vfs_posix_fallocate behind HAVE_POSIX_FALLOCATE across VFS managers. Wrap .vfs_posix_fallocate assignments with HAVE_POSIX_FALLOCATE and set to NULL when the platform does not provide posix_fallocate. --- src/backend/storage/file/polar_fd.c | 4 ++++ src/polar_vfs/polar_bufferio.c | 4 ++++ src/polar_vfs/polar_directio.c | 4 ++++ src/polar_vfs/polar_vfs_fe.c | 4 ++++ 4 files changed, 16 insertions(+) diff --git a/src/backend/storage/file/polar_fd.c b/src/backend/storage/file/polar_fd.c index a668752ff7f..21e3fd1cb44 100644 --- a/src/backend/storage/file/polar_fd.c +++ b/src/backend/storage/file/polar_fd.c @@ -104,7 +104,11 @@ vfs_mgr polar_vfs[] = .vfs_fsync = pg_fsync, .vfs_unlink = unlink, .vfs_rename = rename, +#ifdef HAVE_POSIX_FALLOCATE .vfs_posix_fallocate = posix_fallocate, +#else + .vfs_posix_fallocate = NULL, +#endif #ifdef __linux__ .vfs_fallocate = fallocate, #else diff --git a/src/polar_vfs/polar_bufferio.c b/src/polar_vfs/polar_bufferio.c index ae8b99b0067..d9a667e1353 100644 --- a/src/polar_vfs/polar_bufferio.c +++ b/src/polar_vfs/polar_bufferio.c @@ -57,7 +57,11 @@ const vfs_mgr polar_vfs_bio = #endif .vfs_unlink = unlink, .vfs_rename = rename, +#ifdef HAVE_POSIX_FALLOCATE .vfs_posix_fallocate = posix_fallocate, +#else + .vfs_posix_fallocate = NULL, +#endif #ifdef __linux__ .vfs_fallocate = fallocate, #else diff --git a/src/polar_vfs/polar_directio.c b/src/polar_vfs/polar_directio.c index 186743d05ee..342263a85e4 100644 --- a/src/polar_vfs/polar_directio.c +++ b/src/polar_vfs/polar_directio.c @@ -79,7 +79,11 @@ const vfs_mgr polar_vfs_dio = .vfs_fsync = polar_directio_fsync, .vfs_unlink = unlink, .vfs_rename = rename, +#ifdef HAVE_POSIX_FALLOCATE .vfs_posix_fallocate = posix_fallocate, +#else + .vfs_posix_fallocate = NULL, +#endif #ifdef __linux__ .vfs_fallocate = fallocate, #else diff --git a/src/polar_vfs/polar_vfs_fe.c b/src/polar_vfs/polar_vfs_fe.c index 8377920e13f..2a23ceb8389 100644 --- a/src/polar_vfs/polar_vfs_fe.c +++ b/src/polar_vfs/polar_vfs_fe.c @@ -105,7 +105,11 @@ vfs_mgr polar_vfs[] = .vfs_fsync = fsync, .vfs_unlink = unlink, .vfs_rename = rename, +#ifdef HAVE_POSIX_FALLOCATE .vfs_posix_fallocate = posix_fallocate, +#else + .vfs_posix_fallocate = NULL, +#endif #ifdef __linux__ .vfs_fallocate = fallocate, #else From c35f1c2a3c281c5a6fc38f67d3aff1b7be50e556 Mon Sep 17 00:00:00 2001 From: Aidar Imamov Date: Sun, 19 Oct 2025 21:42:36 +0300 Subject: [PATCH 3/3] Suggested patch edits the polar_drop_log_page_cache function. In particular: - an early return is added when open() fails to avoid calling fadvise()/close() on an invalid fd. - platform specific posix_fadvise() call was replaced with appropriate polar_posix_fadvise() call. The invocation is guarded with directives USE_POSIX_FADVISE and POSIX_FADV_DONTNEED. Falls back to no-op when the call is unavailable. This aligns the function's behavior with existing PolarDB practice in fd.c, xlog.c, etc. - the polar_fd.h header file was included to make the polar_posix_fadvise() function available. --- src/backend/postmaster/syslogger.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c index 29d1d333103..b26ba034b1d 100644 --- a/src/backend/postmaster/syslogger.c +++ b/src/backend/postmaster/syslogger.c @@ -56,6 +56,7 @@ /* POLAR */ #include "storage/fd.h" +#include "storage/polar_fd.h" #include "utils/builtins.h" /* @@ -2092,9 +2093,20 @@ polar_drop_log_page_cache(const char *filename) { ereport(LOG, (errmsg("the old log file doesn't exist"))); + + /* no need to go further */ + return; } - ret = posix_fadvise(last_log_fd, 0, 0, POSIX_FADV_DONTNEED); - /* return zero means success */ + /* + * on platforms that support posix_fadvise, advise the kernel to drop + * cached pages for the old log file. Otherwise, skip silently. + */ +#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED) + ret = polar_posix_fadvise(last_log_fd, 0, 0, POSIX_FADV_DONTNEED); +#else + ret = 0; +#endif + if (ret != 0) { ereport(LOG,