As a large system software, if Memory Leakage is one of the key issues facing the system, PostgreSQL uses MemoryContext concept to solve this problem, first take a look at the definition of several data structures:
Note: In PostgreSQL code, general Struct will add suffix "data" with name, and the Struct's pointer is the name.
Typedef
Struct MemoryContextMethods
{
MemoryContext Context, Size Size;
/ * Call this free_p in case someone # define's free () * /
MemoryContext Context, Void * Pointer;
Void * (* Realloc) (MemoryContext Context, Void * Pointer, Size Size);
MemoryContext Context;
MemoryContext Context;
MemoryContext Context;
Size (* get_chunk_space) (MemoryContext Context, Void * Pointer);
(* is_empty) (MemoryContext Context);
"MemoryContext Context);
#ifdef memory_context_checking
MemoryContext Context;
#ENDIF
MemoryContextMethods;
Typedef
Struct MemoryContextData
{
Nodetag type; / * identifies exact kind of context * /
MemoryContextMethods * Methods; / * Virtual function table * /
MemoryContext Parent; / * null if no parent (topLEVEVEL Context) * /
MemoryContext firstchild; / * Head of linked list of children * /
MemoryContext Nextchild; / * Next child of Same Parent * /
Char * name; / * context name (just for debugging) * /
MemoryContextdata;
PostgreSQL's comment is ""