Re: Why our Valgrind reports suck

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alexander Lakhin <exclusion@gmail.com>
Cc: Richard Guo <guofenglinux@gmail.com>, Andres Freund <andres@anarazel.de>, Álvaro Herrera <alvherre@kurilemu.de>, pgsql-hackers@lists.postgresql.org
Date: 2026-06-10T20:41:29Z
Lists: pgsql-hackers
Alexander Lakhin <exclusion@gmail.com> writes:
> Could you please look at an issue apparently related to the change shown
> above? This simple modification:
> ...
> (That its, the error message added in 26ec6b594, triggers double pfree and
> segfault now.)

Hah, nice one.  Apparently the "detach the queue" path in 
mq_putmessage() has never ever been tested, because it
contains a double pfree, and this fixes it:

diff --git a/src/backend/libpq/pqmq.c b/src/backend/libpq/pqmq.c
index 21ce180c78d..d038a9da515 100644
--- a/src/backend/libpq/pqmq.c
+++ b/src/backend/libpq/pqmq.c
@@ -140,7 +140,6 @@ mq_putmessage(char msgtype, const char *s, size_t len)
 		if (pq_mq_handle != NULL)
 		{
 			shm_mq_detach(pq_mq_handle);
-			pfree(pq_mq_handle);
 			pq_mq_handle = NULL;
 		}
 		return EOF;

The necessity of this can be observed by noting that the last
step in shm_mq_detach() is to pfree the pointer it was handed.

			regards, tom lane



Commits

  1. Undo thinko in commit e78d1d6d4.

  2. Avoid leakage of zero-length arrays in partition_bounds_copy().

  3. Fix MemoryContextAllocAligned's interaction with Valgrind.

  4. Fix assorted pretty-trivial memory leaks in the backend.

  5. Improve our support for Valgrind's leak tracking.

  6. Reduce leakage during PL/pgSQL function compilation.

  7. Silence Valgrind leakage complaints in more-or-less-hackish ways.

  8. Silence complaints about leaks in PlanCacheComputeResultDesc.

  9. Suppress complaints about leaks in TS dictionary loading.

  10. Suppress complaints about leaks in function cache loading.

  11. Fix per-relation memory leakage in autovacuum.

  12. Fix AlignedAllocRealloc to cope sanely with OOM.