memstat
memstat · Memory context statistics for local and instance backends
Overview
Usage
Sources:
memstat reports PostgreSQL memory-context allocation statistics. It can inspect the current backend without preload, or collect per-backend summaries for the whole instance when loaded at server start. Use it for focused memory diagnostics, not permanent high-frequency monitoring.
Core Workflow
For the current session, create the extension and inspect its context tree:
For all backends, put memstat last in shared_preload_libraries, restart PostgreSQL, and query the instance view:
Important Objects
local_memory_statsreturns context name, nesting level, block and chunk counts, total bytes, and free bytes for the current backend.instance_memory_statsadds the backend PID and reads statistics for live backends; preload is required.memory_statsaggregates the instance data into a per-backend summary.memstat.periodsets the minimum seconds between instance-statistic collections.
Operational Notes
The preload hook collects memory statistics at the beginning of queries and can be expensive on a busy server. Increase memstat.period, sample briefly, and remove the module after diagnosis. Upstream says it must be last in the preload list because it wraps shared-memory setup. Context names and layouts are PostgreSQL implementation details and can change across major versions.