Contents

Valgrind and Postgres

Warning: The techniques described here are the subject of ongoing research - your mileage may vary

Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile programs in detail. In particular, Valgrind's Memcheck tool is useful for detecting these bugs. However, it is non-trivial to use with Postgres, and requires modification to Postgres source files to instrument memory allocation and memory context infrastructure with various Valgrind macros.

It is hoped that at some point in the future, Postgres will directly support Valgrind through the use of a configure option, which is possible due to the fact that the header file valgrind.h is under a BSD license, unlike the rest of Valgrind which is under the GPL 2. In the meantime, this wiki page is the place to obtain an unofficial patch that adds the necessary calls. It is not as comprehensive as it possibly could be, and there are probably other places where specific checks could be usefully injected.

The modifications themselves

Peter Geoghegan maintains a feature branch that has the necessary modifications to Postgres:

Per recommendations in the Valgrind documentation, the modifications just copy valgrind.h into the PostgreSQL tree. It is current for the master branch, as of April 5, 2012.

It is recommended that when running Valgrind that you disable CLOBBER_FREED_MEMORY and MEMORY_CONTEXT_CHECKING; they add additional valgrind hook traffic and are redundant with the testing valgrind performs. The patch actually switches the pg_config_manual.h defaults for those settings.

Co-ordination when running tests

postgresql.conf should include a timestamp and PID in log_line_prefix, as well as having a log_min_duration_statement of 0, so that all statements are logged. Since the valgrind logs included timestamps and were split by PID, they can be used to correlate valgrind errors with particular test suite commands. Once the test cases yielding valgrind errors are tracked-down, you can rerun the valgrind-ed postmaster with "--track-origins=yes --read-var-info=yes" to get more-specific diagnostics. Valgrind 3.6.0 should be used to get good pinpointing of the error source. At time of writing, version 3.7.0 is the latest stable release.

For reasons that have yet to be ascertained, it is necessary to run the regression tests with autovacuum = 'off'. Otherwise, Postgres will segfault within an autovacuum worker's elog() call.