memory-contexts

Manage PostgreSQL backend memory allocation and lifecycle using palloc and MemoryContext types.

Updated Jun 1, 2026
One-click install
npx skills add https://github.com/matejformanek/postgres-claude --skill memory-contexts
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: memory-contexts
Source: https://github.com/matejformanek/postgres-claude/tree/main/.claude/skills/memory-contexts
Command: npx skills add https://github.com/matejformanek/postgres-claude --skill memory-contexts

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This skill prevents common memory corruption and leak bugs in PostgreSQL backend development by enforcing strict adherence to memory context allocation rules and lifecycle management.

Core Features & Use Cases

  • Allocation Discipline: Provides authoritative guidance on using palloc, palloc0, and context-specific allocation macros to avoid OOM errors and null-pointer dereferences.
  • Lifecycle Scoping: Helps developers choose the correct MemoryContext (e.g., per-tuple, per-query, or cache-level) to ensure data persists exactly as long as needed.
  • Debugging & Cleanup: Offers actionable rules for using MemoryContextReset and MemoryContextDelete to prevent permanent leaks in long-running backend processes.

Quick Start

Use the memory-contexts skill to review my current implementation of a custom AllocSetContextCreate call and verify that I am correctly switching contexts for long-lived allocations.

Frequently Asked Questions about memory-contexts

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent memory leaks in PostgreSQL backend C extensions?

Prevent memory leaks in PostgreSQL backend C extensions by enforcing strict adherence to memory context allocation rules, using MemoryContextReset and MemoryContextDelete to manage allocation lifecycles and avoid permanent leaks in long-running backend processes.

When should I use AllocSet, Slab, or Generation memory contexts in PostgreSQL?

Use AllocSet, Slab, or Generation memory contexts in PostgreSQL to control data lifecycle scoping, ensuring data persists exactly as long as needed across per-tuple, per-query, or cache-level operations while avoiding premature deallocation.

How do I correctly switch MemoryContext for long-lived allocations in PostgreSQL C?

Switch MemoryContext for long-lived allocations in PostgreSQL C by selecting the correct context type and using context-specific allocation macros, verifying proper context switching to prevent data loss and null-pointer dereferences during backend patch development.

What is the palloc and pfree contract in PostgreSQL memory management?

The palloc and pfree contract in PostgreSQL memory management requires strict adherence to allocation discipline, using palloc, palloc0, and context-specific macros to avoid OOM errors and null-pointer dereferences during extension creation.

Can I debug memory context switching errors in PostgreSQL backend development?

Debug memory context switching errors in PostgreSQL backend development by applying actionable cleanup rules with MemoryContextReset and MemoryContextDelete, preventing permanent leaks and corruption bugs in long-running backend processes.

What are the limitations of using palloc for per-tuple memory allocation in PostgreSQL?

Limitations of using palloc for per-tuple memory allocation in PostgreSQL include the risk of data loss if allocated in the wrong context, requiring careful lifecycle scoping and proper MemoryContext selection to avoid corruption and permanent leaks.