Skip to content

memstat

Memory context statistics for local and instance backends

memstat · Memory context statistics for local and instance backends

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
100420memstatmemstat1.0STATUnknownC

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:

CREATE EXTENSION memstat;

SELECT name, level, totalspace, freespace
FROM local_memory_stats()
ORDER BY totalspace DESC;

For all backends, put memstat last in shared_preload_libraries, restart PostgreSQL, and query the instance view:

shared_preload_libraries = 'other_library,memstat'
memstat.period = 10s
SELECT * FROM memory_stats ORDER BY totalspace DESC;

Important Objects

  • local_memory_stats returns context name, nesting level, block and chunk counts, total bytes, and free bytes for the current backend.
  • instance_memory_stats adds the backend PID and reads statistics for live backends; preload is required.
  • memory_stats aggregates the instance data into a per-backend summary.
  • memstat.period sets 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.

Table reader