Skip to content

palloc_bench

Benchmark function for PostgreSQL palloc memory allocation

palloc_bench · Benchmark function for PostgreSQL palloc memory allocation

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100422palloc_benchpalloc_bench1.0.0FEATUnknownC

Usage

Sources:

palloc_bench is a tiny developer microbenchmark for PostgreSQL memory allocation. It creates nested allocation contexts, repeatedly calls palloc and pfree in the deepest context, and reports elapsed wall-clock time as a warning; it does not return a structured benchmark result.

Core Workflow

Use only in an isolated test database with conservative inputs and no competing workload.

CREATE EXTENSION palloc_bench;

REVOKE ALL ON FUNCTION palloc_bench(integer, integer, integer) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION palloc_bench(integer, integer, integer) TO benchmark_admin;

SET client_min_messages = warning;
SELECT palloc_bench(8, 100000, 64);

The parameters are the number of nested memory contexts, the allocation/free iteration count, and the allocation size in bytes. The function returns void; read the emitted WARNING: duration = ... ms message. Context construction is outside the timed loop, while each iteration includes both allocation and free.

Safety and Interpretation

The implementation does not validate negative or excessive inputs. A large context count consumes memory and stack, a large iteration count monopolizes a backend, and a negative or huge allocation size can become an invalid or enormous allocation request. The function has no privilege checks and receives public execute permission by default, so revoke it from PUBLIC to prevent a trivial resource-exhaustion surface.

The timer uses gettimeofday, is not monotonic, and the result includes PostgreSQL build, allocator, CPU, scheduler, cache, and concurrent-load effects. Run repeated controlled trials and do not treat a single warning as a production performance guarantee. The pinned source dates from 2014 and uses old memory-context APIs; compile and test it against the exact PostgreSQL source version before use. No preload is required.

Table reader