On Tue Jul 7, 2026 at 11:57 PM UTC, Tristan Partin wrote:
> On Tue Jul 7, 2026 at 6:54 PM CDT, Tom Lane wrote:
>> Michael Paquier <[email protected]> writes:
>>> Additional question.  Does this help with requirements like the one
>>> listed in fe-exec.c for PQfreemem() under WIN32?
>>
>> Yeah, I was wondering about that point too.  It would be really nice
>> to have automated checks for that.  Maybe the right thing to do here
>> (to start anyway) is to put in targeted annotations that address
>> specific pain points like that one.
>>
>> One issue that'd have to be dealt with is that that would involve
>> putting annotations into the public header file libpq-fe.h.  We'd need
>> to be sure we do not break things for applications using compilers
>> other than what we built libpq with.  That seems reasonably easy to do
>> with some macro trickery, but it's a point to keep in mind.
>
> Ok, I think the path forward is two-fold:
>
> 1. Add the annotations to specific pain points as Tom suggested 
>    (PGfreemem(), frontend memory allocators)
> 2. Add the annotations to the backend allocators knowing that the patch 
>    may not be committed

After talking to Peter off-list, I've come up with a new strategy by 
splitting up __attribute__((malloc)) and 
__attribute__((malloc(deallocator, arg))).

For those that are unaware, these two attributes, though named the same, 
have two separate meanings:

malloc:

> Attribute malloc indicates that a function is malloc-like, i.e., that 
> the pointer P returned by the function cannot alias any other pointer 
> valid when the function returns, and moreover no pointers to valid 
> objects occur in any storage addressed by P. In addition, GCC predicts 
> that a function with the attribute returns non-null in most cases.

malloc(deallocator, arg):

> Associating a function with a deallocator helps detect calls to 
> mismatched allocation and deallocation functions and diagnose them 
> under the control of options such as -Wmismatched-dealloc. It also 
> makes it possible to diagnose attempts to deallocate objects that were 
> not allocated dynamically, by -Wfree-nonheap-object. To indicate that 
> an allocation function both satisfies the nonaliasing property and has 
> a deallocator associated with it, both the plain form of the attribute 
> and the one with the deallocator argument must be used. The same 
> function can be both an allocator and a deallocator. Since inlining 
> one of the associated functions but not the other could result in 
> apparent mismatches, this form of attribute malloc is not accepted on 
> inline functions. For the same reason, using the attribute prevents 
> both the allocation and deallocation functions from being expanded 
> inline.

To summarize the second a bit, getting the extra analysis is not free. 
There are performance implications with the potential lack of inlining. 
I think adding the deallocator is important, but probably only in debug 
builds.

Attached are patches which add pg_attribute_malloc and apply it to 
various memory allocating functions.

I have the patches prepared for what I am calling 
pg_attribute_dealloc(...), which is defined to be 
__attribute__((malloc(deallocator, arg))). I'll hold off on inundating 
the list with those until this plain version is reviewed and committed. 

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)
From acc80849955f037e9833582decb803cccd0f6126 Mon Sep 17 00:00:00 2001
From: Tristan Partin <[email protected]>
Date: Thu, 12 Nov 2026 19:30:14 +0000
Subject: [PATCH v2 1/2] Add pg_attribute_malloc

The malloc function attribute tells the compiler that the pointer
returned by a function does not alias any other pointer that is valid
when the function returns, and that the storage it addresses contains no
pointers to valid objects. Compilers can optimize callers based on this,
and GCC can also assume that such functions rarely return NULL.

Clang also supports the attribute. A following commit marks the memory
allocation functions with it.

Author: Tristan Partin <[email protected]>
Signed-off-by: Tristan Partin <[email protected]>
---
 src/include/c.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/include/c.h b/src/include/c.h
index 4bbb0071289..e2c9a6f8824 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -293,6 +293,21 @@ extern "C++"
 #define pg_attribute_no_sanitize_function()
 #endif
 
+/*
+ * pg_attribute_malloc indicates that the function is malloc-like: the
+ * returned pointer does not alias any other pointer valid when the function
+ * returns, and the storage it addresses contains no pointers to valid
+ * objects. GCC can infer that functions marked as malloc also rarely return
+ * NULL.
+ *
+ * https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-malloc
+ */
+#if __has_attribute(malloc)
+#define pg_attribute_malloc __attribute__((malloc))
+#else
+#define pg_attribute_malloc
+#endif
+
 /*
  * pg_attribute_nonnull means the compiler should warn if the function is
  * called with the listed arguments set to NULL.  If no arguments are
-- 
Tristan Partin
https://tristan.partin.io

From 39b2d5f3d949dbc1f9edea76d38d8813b4e40696 Mon Sep 17 00:00:00 2001
From: Tristan Partin <[email protected]>
Date: Thu, 12 Nov 2026 19:30:14 +0000
Subject: [PATCH v2 2/2] Mark some memory allocation functions with
 pg_attribute_malloc

This list is going to be incomplete, but it aims to first tackle
functions that look like malloc, sprintf, calloc, strdup, etc.

Author: Tristan Partin <[email protected]>
Signed-off-by: Tristan Partin <[email protected]>
---
 src/backend/storage/ipc/shmem.c               |  2 +-
 src/include/common/fe_memutils.h              | 32 ++++++++--------
 src/include/executor/spi.h                    |  2 +-
 src/include/storage/shm_toc.h                 |  2 +-
 src/include/storage/shmem.h                   |  4 +-
 src/include/utils/guc.h                       |  4 +-
 src/include/utils/palloc.h                    | 38 +++++++++----------
 src/interfaces/ecpg/ecpglib/ecpglib_extern.h  |  6 +--
 .../ecpg/pgtypeslib/pgtypeslib_extern.h       |  4 +-
 src/interfaces/ecpg/preproc/preproc_extern.h  |  8 ++--
 10 files changed, 51 insertions(+), 51 deletions(-)

diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 8d81893b470..66f25333e1c 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -243,7 +243,7 @@ typedef struct ShmemAllocatorData
 
 #define ShmemIndexLock (&ShmemAllocator->index_lock)
 
-static void *ShmemAllocRaw(Size size, Size alignment, Size *allocated_size);
+pg_attribute_malloc static void *ShmemAllocRaw(Size size, Size alignment, Size *allocated_size);
 
 /* shared memory global variables */
 
diff --git a/src/include/common/fe_memutils.h b/src/include/common/fe_memutils.h
index 0db64ab4389..4f640f1340d 100644
--- a/src/include/common/fe_memutils.h
+++ b/src/include/common/fe_memutils.h
@@ -34,10 +34,10 @@
  * "Safe" memory allocation functions --- these exit(1) on failure
  * (except pg_malloc_extended with MCXT_ALLOC_NO_OOM)
  */
-pg_attribute_returns_nonnull extern char *pg_strdup(const char *in);
-pg_attribute_returns_nonnull extern void *pg_malloc(size_t size);
-pg_attribute_returns_nonnull extern void *pg_malloc0(size_t size);
-extern void *pg_malloc_extended(size_t size, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pg_strdup(const char *in);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *pg_malloc(size_t size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *pg_malloc0(size_t size);
+pg_attribute_malloc extern void *pg_malloc_extended(size_t size, int flags);
 pg_attribute_returns_nonnull extern void *pg_realloc(void *ptr, size_t size);
 extern void pg_free(void *ptr);
 
@@ -46,9 +46,9 @@ extern void pg_free(void *ptr);
  */
 extern Size add_size(Size s1, Size s2);
 extern Size mul_size(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *pg_malloc_mul(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *pg_malloc0_mul(Size s1, Size s2);
-extern void *pg_malloc_mul_extended(Size s1, Size s2, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *pg_malloc_mul(Size s1, Size s2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *pg_malloc0_mul(Size s1, Size s2);
+pg_attribute_malloc extern void *pg_malloc_mul_extended(Size s1, Size s2, int flags);
 pg_attribute_returns_nonnull extern void *pg_realloc_mul(void *p, Size s1, Size s2);
 
 /*
@@ -75,16 +75,16 @@ pg_attribute_returns_nonnull extern void *pg_realloc_mul(void *p, Size s1, Size
 #define pg_realloc_array(pointer, type, count) ((type *) pg_realloc_mul(pointer, sizeof(type), count))
 
 /* Equivalent functions, deliberately named the same as backend functions */
-pg_attribute_returns_nonnull extern char *pstrdup(const char *in);
-pg_attribute_returns_nonnull extern char *pnstrdup(const char *in, Size size);
-pg_attribute_returns_nonnull extern void *palloc(Size size);
-pg_attribute_returns_nonnull extern void *palloc0(Size size);
-extern void *palloc_extended(Size size, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pstrdup(const char *in);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pnstrdup(const char *in, Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc(Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc0(Size size);
+pg_attribute_malloc extern void *palloc_extended(Size size, int flags);
 pg_attribute_returns_nonnull extern void *repalloc(void *pointer, Size size);
 extern void pfree(void *pointer);
-pg_attribute_returns_nonnull extern void *palloc_mul(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *palloc0_mul(Size s1, Size s2);
-extern void *palloc_mul_extended(Size s1, Size s2, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc_mul(Size s1, Size s2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc0_mul(Size s1, Size s2);
+pg_attribute_malloc extern void *palloc_mul_extended(Size s1, Size s2, int flags);
 pg_attribute_returns_nonnull extern void *repalloc_mul(void *p, Size s1, Size s2);
 
 #define palloc_object(type) ((type *) palloc(sizeof(type)))
@@ -95,7 +95,7 @@ pg_attribute_returns_nonnull extern void *repalloc_mul(void *p, Size s1, Size s2
 #define repalloc_array(pointer, type, count) ((type *) repalloc_mul(pointer, sizeof(type), count))
 
 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
-pg_attribute_returns_nonnull extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
 extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
 
 #endif							/* FE_MEMUTILS_H */
diff --git a/src/include/executor/spi.h b/src/include/executor/spi.h
index c7a3b7abb26..1ddb553b90a 100644
--- a/src/include/executor/spi.h
+++ b/src/include/executor/spi.h
@@ -164,7 +164,7 @@ extern char *SPI_gettype(TupleDesc tupdesc, int fnumber);
 extern Oid	SPI_gettypeid(TupleDesc tupdesc, int fnumber);
 extern char *SPI_getrelname(Relation rel);
 extern char *SPI_getnspname(Relation rel);
-extern void *SPI_palloc(Size size);
+pg_attribute_malloc extern void *SPI_palloc(Size size);
 extern void *SPI_repalloc(void *pointer, Size size);
 extern void SPI_pfree(void *pointer);
 extern Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen);
diff --git a/src/include/storage/shm_toc.h b/src/include/storage/shm_toc.h
index 65e4f16eee9..3f597166a1a 100644
--- a/src/include/storage/shm_toc.h
+++ b/src/include/storage/shm_toc.h
@@ -29,7 +29,7 @@ typedef struct shm_toc shm_toc;
 
 extern shm_toc *shm_toc_create(uint64 magic, void *address, Size nbytes);
 extern shm_toc *shm_toc_attach(uint64 magic, void *address);
-extern void *shm_toc_allocate(shm_toc *toc, Size nbytes);
+pg_attribute_malloc extern void *shm_toc_allocate(shm_toc *toc, Size nbytes);
 extern Size shm_toc_freespace(shm_toc *toc);
 extern void shm_toc_insert(shm_toc *toc, uint64 key, void *address);
 extern void *shm_toc_lookup(shm_toc *toc, uint64 key, bool noError);
diff --git a/src/include/storage/shmem.h b/src/include/storage/shmem.h
index 43b636868c7..a5ab0e7a364 100644
--- a/src/include/storage/shmem.h
+++ b/src/include/storage/shmem.h
@@ -186,8 +186,8 @@ extern void ShmemRequestHashWithOpts(const ShmemHashOpts *options);
 extern void *ShmemInitStruct(const char *name, Size size, bool *foundPtr);
 extern HTAB *ShmemInitHash(const char *name, int64 nelems,
 						   HASHCTL *infoP, int hash_flags);
-extern void *ShmemAlloc(Size size);
-extern void *ShmemAllocNoError(Size size);
+pg_attribute_malloc extern void *ShmemAlloc(Size size);
+pg_attribute_malloc extern void *ShmemAllocNoError(Size size);
 
 extern PGDLLIMPORT Size pg_get_shmem_pagesize(void);
 
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index 164efba6b51..328d4d99a0c 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -472,9 +472,9 @@ extern ArrayType *GUCArrayAdd(ArrayType *array, const char *name, const char *va
 extern ArrayType *GUCArrayDelete(ArrayType *array, const char *name);
 extern ArrayType *GUCArrayReset(ArrayType *array);
 
-extern void *guc_malloc(int elevel, size_t size);
+pg_attribute_malloc extern void *guc_malloc(int elevel, size_t size);
 pg_nodiscard extern void *guc_realloc(int elevel, void *old, size_t size);
-extern char *guc_strdup(int elevel, const char *src);
+pg_attribute_malloc extern char *guc_strdup(int elevel, const char *src);
 extern void guc_free(void *ptr);
 
 #ifdef EXEC_BACKEND
diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h
index 0c7452fe30d..aa8243fa5b8 100644
--- a/src/include/utils/palloc.h
+++ b/src/include/utils/palloc.h
@@ -68,17 +68,17 @@ extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
 /*
  * Fundamental memory-allocation operations (more are in utils/memutils.h)
  */
-pg_attribute_returns_nonnull extern void *MemoryContextAlloc(MemoryContext context, Size size);
-pg_attribute_returns_nonnull extern void *MemoryContextAllocZero(MemoryContext context, Size size);
-extern void *MemoryContextAllocExtended(MemoryContext context,
-										Size size, int flags);
-extern void *MemoryContextAllocAligned(MemoryContext context,
-									   Size size, Size alignto, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *MemoryContextAlloc(MemoryContext context, Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *MemoryContextAllocZero(MemoryContext context, Size size);
+pg_attribute_malloc extern void *MemoryContextAllocExtended(MemoryContext context,
+															Size size, int flags);
+pg_attribute_malloc extern void *MemoryContextAllocAligned(MemoryContext context,
+														   Size size, Size alignto, int flags);
 
-pg_attribute_returns_nonnull extern void *palloc(Size size);
-pg_attribute_returns_nonnull extern void *palloc0(Size size);
-extern void *palloc_extended(Size size, int flags);
-extern void *palloc_aligned(Size size, Size alignto, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc(Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc0(Size size);
+pg_attribute_malloc extern void *palloc_extended(Size size, int flags);
+pg_attribute_malloc extern void *palloc_aligned(Size size, Size alignto, int flags);
 pg_nodiscard pg_attribute_returns_nonnull extern void *repalloc(void *pointer, Size size);
 pg_nodiscard extern void *repalloc_extended(void *pointer,
 											Size size, int flags);
@@ -90,9 +90,9 @@ extern void pfree(void *pointer);
  */
 extern Size add_size(Size s1, Size s2);
 extern Size mul_size(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *palloc_mul(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *palloc0_mul(Size s1, Size s2);
-extern void *palloc_mul_extended(Size s1, Size s2, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc_mul(Size s1, Size s2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc0_mul(Size s1, Size s2);
+pg_attribute_malloc extern void *palloc_mul_extended(Size s1, Size s2, int flags);
 pg_nodiscard pg_attribute_returns_nonnull extern void *repalloc_mul(void *p, Size s1, Size s2);
 pg_nodiscard extern void *repalloc_mul_extended(void *p, Size s1, Size s2,
 												int flags);
@@ -123,7 +123,7 @@ pg_nodiscard extern void *repalloc_mul_extended(void *p, Size s1, Size s2,
 #define repalloc_array_extended(pointer, type, count, flags) ((type *) repalloc_mul_extended(pointer, sizeof(type), count, flags))
 
 /* Higher-limit allocators. */
-pg_attribute_returns_nonnull extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
 pg_nodiscard pg_attribute_returns_nonnull extern void *repalloc_huge(void *pointer, Size size);
 
 /*
@@ -154,14 +154,14 @@ extern void MemoryContextUnregisterResetCallback(MemoryContext context,
  * These are like standard strdup() except the copied string is
  * allocated in a context, not with malloc().
  */
-pg_attribute_returns_nonnull extern char *MemoryContextStrdup(MemoryContext context, const char *string);
-pg_attribute_returns_nonnull extern char *pstrdup(const char *in);
-pg_attribute_returns_nonnull extern char *pnstrdup(const char *in, Size len);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *MemoryContextStrdup(MemoryContext context, const char *string);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pstrdup(const char *in);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pnstrdup(const char *in, Size len);
 
-pg_attribute_returns_nonnull extern char *pchomp(const char *in);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pchomp(const char *in);
 
 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
-pg_attribute_returns_nonnull extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
 extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
 
 #endif							/* PALLOC_H */
diff --git a/src/interfaces/ecpg/ecpglib/ecpglib_extern.h b/src/interfaces/ecpg/ecpglib/ecpglib_extern.h
index 0b8cea6196d..c54ec6407cb 100644
--- a/src/interfaces/ecpg/ecpglib/ecpglib_extern.h
+++ b/src/interfaces/ecpg/ecpglib/ecpglib_extern.h
@@ -168,14 +168,14 @@ bool		ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
 
 void		ecpg_pthreads_init(void);
 struct connection *ecpg_get_connection(const char *connection_name);
-char	   *ecpg_alloc(long size, int lineno);
-char	   *ecpg_auto_alloc(long size, int lineno);
+pg_attribute_malloc char *ecpg_alloc(long size, int lineno);
+pg_attribute_malloc char *ecpg_auto_alloc(long size, int lineno);
 char	   *ecpg_realloc(void *ptr, long size, int lineno);
 void		ecpg_free(void *ptr);
 bool		ecpg_init(const struct connection *con,
 					  const char *connection_name,
 					  const int lineno);
-char	   *ecpg_strdup(const char *string, int lineno, bool *alloc_failed);
+pg_attribute_malloc char *ecpg_strdup(const char *string, int lineno, bool *alloc_failed);
 const char *ecpg_type_name(enum ECPGttype typ);
 int			ecpg_dynamic_type(Oid type);
 int			sqlda_dynamic_type(Oid type, enum COMPAT_MODE compat);
diff --git a/src/interfaces/ecpg/pgtypeslib/pgtypeslib_extern.h b/src/interfaces/ecpg/pgtypeslib/pgtypeslib_extern.h
index ce2f14fab76..5e63c8a0b6e 100644
--- a/src/interfaces/ecpg/pgtypeslib/pgtypeslib_extern.h
+++ b/src/interfaces/ecpg/pgtypeslib/pgtypeslib_extern.h
@@ -39,7 +39,7 @@ int			pgtypes_fmt_replace(union un_fmt_comb replace_val,
 								int replace_type, char **output,
 								int *pstr_len);
 
-char	   *pgtypes_alloc(long size);
-char	   *pgtypes_strdup(const char *str);
+pg_attribute_malloc char *pgtypes_alloc(long size);
+pg_attribute_malloc char *pgtypes_strdup(const char *str);
 
 #endif							/* _ECPG_PGTYPESLIB_EXTERN_H */
diff --git a/src/interfaces/ecpg/preproc/preproc_extern.h b/src/interfaces/ecpg/preproc/preproc_extern.h
index c61f054873b..521cb0005ea 100644
--- a/src/interfaces/ecpg/preproc/preproc_extern.h
+++ b/src/interfaces/ecpg/preproc/preproc_extern.h
@@ -79,10 +79,10 @@ extern char *hashline_number(void);
 extern int	base_yyparse(void);
 extern int	base_yylex(void);
 extern void base_yyerror(const char *error);
-extern void *mm_alloc(size_t size);
-extern char *mm_strdup(const char *string);
-extern void *loc_alloc(size_t size);
-extern char *loc_strdup(const char *string);
+pg_attribute_malloc extern void *mm_alloc(size_t size);
+pg_attribute_malloc extern char *mm_strdup(const char *string);
+pg_attribute_malloc extern void *loc_alloc(size_t size);
+pg_attribute_malloc extern char *loc_strdup(const char *string);
 extern void reclaim_local_storage(void);
 extern char *cat2_str(const char *str1, const char *str2);
 extern char *cat_str(int count, ...);
-- 
Tristan Partin
https://tristan.partin.io

Reply via email to