From 5ce58b18992bbaf98eb4d0d0b9cf50e25a191aee Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Fri, 7 Nov 2025 14:18:31 +0000
Subject: [PATCH v8] Introduce pg_attribute_deprecated() and deprecate
 XLogRecPtrIsInvalid()

This commit creates a new macro pg_attribute_deprecated() to mark a declaration
as deprecated with a custom message. The compiler will emit a warning when the
deprecated entity is used.

Then it makes use of this new macro to emit a deprecated message about
XLogRecPtrIsInvalid() as of version 21 (no need to be more conservative and wait
until version 24 as XLogRecPtrIsValid() has been added in back branches).
---
 src/include/access/xlogdefs.h | 17 +++++++++++++++++
 src/include/c.h               | 14 ++++++++++++++
 2 files changed, 31 insertions(+)
  48.1% src/include/access/
  51.8% src/include/

diff --git a/src/include/access/xlogdefs.h b/src/include/access/xlogdefs.h
index 5f07e57c832..1ed8f1b413e 100644
--- a/src/include/access/xlogdefs.h
+++ b/src/include/access/xlogdefs.h
@@ -27,6 +27,23 @@ typedef uint64 XLogRecPtr;
  */
 #define InvalidXLogRecPtr		0
 #define XLogRecPtrIsValid(r)	((r) != InvalidXLogRecPtr)
+
+/*
+ * New code should use XLogRecPtrIsValid() instead of XLogRecPtrIsInvalid()
+ * for consistency with the affirmative form of macros used for other datatypes
+ * and to avoid awkward double negative.
+ * This function is retained for convenience of third-party code but is/will be
+ * deprecated as of version 21.
+ */
+#if PG_VERSION_NUM >= 210000
+pg_attribute_deprecated("use XLogRecPtrIsValid() instead")
+#endif
+static inline bool
+XLogRecPtrIsInvalid(XLogRecPtr ptr)
+{
+	return ptr == InvalidXLogRecPtr;
+}
+
 #define XLogRecPtrIsInvalid(r)	((r) == InvalidXLogRecPtr)
 
 /*
diff --git a/src/include/c.h b/src/include/c.h
index 757dfff4782..053635e984c 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -227,6 +227,20 @@
 #define PG_USED_FOR_ASSERTS_ONLY pg_attribute_unused()
 #endif
 
+/*
+ * Mark a declaration as deprecated with a custom message. The compiler will
+ * emit a warning when the deprecated entity is used.
+ */
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201402L)
+#define pg_attribute_deprecated(msg) [[deprecated(msg)]]
+#elif defined(__GNUC__)
+#define pg_attribute_deprecated(msg) __attribute__((deprecated(msg)))
+#elif defined(_MSC_VER)
+#define pg_attribute_deprecated(msg) __declspec(deprecated(msg))
+#else
+#define pg_attribute_deprecated(msg)
+#endif
+
 /* GCC supports format attributes */
 #if defined(__GNUC__)
 #define pg_attribute_format_arg(a) __attribute__((format_arg(a)))
-- 
2.34.1

