Re: Why our Valgrind reports suck

Alexander Law <exclusion@gmail.com>

From: Alexander Lakhin <exclusion@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>, Richard Guo <guofenglinux@gmail.com>
Cc: Andres Freund <andres@anarazel.de>, Álvaro Herrera <alvherre@kurilemu.de>, pgsql-hackers@lists.postgresql.org
Date: 2026-06-10T20:00:00Z
Lists: pgsql-hackers
Hello Tom and Richard,

10.07.2025 21:16, Tom Lane wrote:
> Richard Guo<guofenglinux@gmail.com> writes:
>> I'm just skimming through the changes and happened to spot two minor
>> things.
>> In 0008:
>>          if (pq_mq_handle != NULL)
>> +       {
>>              shm_mq_detach(pq_mq_handle);
>> +           pfree(pq_mq_handle);
>> +       }
>>          pq_mq_handle = NULL;
>> Maybe we could move "pq_mq_handle = NULL;" inside the if branch?
>> Though I see we're doing it in your way on master.
> Yeah, we could make it be like that.  I was just trying to do the
> minimal change from master.

Could you please look at an issue apparently related to the change shown
above? This simple modification:
--- a/src/backend/storage/ipc/shm_mq.c
+++ b/src/backend/storage/ipc/shm_mq.c
@@ -377,7 +377,7 @@ shm_mq_sendv(shm_mq_handle *mqh, shm_mq_iovec *iov, int iovcnt, bool nowait,
                 nbytes += iov[i].len;

         /* Prevent writing messages overwhelming the receiver. */
-       if (nbytes > MaxAllocSize)
+       if (nbytes > 4000)
                 ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                                  errmsg("cannot send a message of size %zu via shared memory queue",

makes the server crash during `make check` as below:
2026-06-10 15:24:41.568 EDT parallel worker[738010] STATEMENT: select (stringu1 || repeat('abcd', 5000))::int2 from 
tenk1 where unique1 = 1;
2026-06-10 15:24:41.568 EDT parallel worker[738010] ERROR:  cannot send a message of size 20110 via shared memory queue
2026-06-10 15:24:41.568 EDT parallel worker[738010] STATEMENT: select (stringu1 || repeat('abcd', 5000))::int2 from 
tenk1 where unique1 = 1;
2026-06-10 15:24:41.568 EDT parallel worker[738010] ERROR:  detected double pfree in Parallel worker 0xaaaae6a1d670
2026-06-10 15:24:41.622 EDT postmaster[736899] LOG:  background worker "parallel worker" (PID 738010) was terminated by 
signal 11: Segmentation fault

(That its, the error message added in 26ec6b594, triggers double pfree and
segfault now.)

Not reproduced at e78d1d6d4~1 with 095555daf added.

Best regards,
Alexander

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.