pub enum PgMemoryContexts {
Show 13 variants CurrentMemoryContext, TopMemoryContext, PortalContext, ErrorContext, PostmasterContext, CacheMemoryContext, MessageContext, TopTransactionContext, CurTransactionContext, For(MemoryContext), Owned(OwnedMemoryContext), Of(void_ptr), Transient { parent: MemoryContext, name: &'static str, min_context_size: u32, initial_block_size: u32, max_block_size: u32, },
}
Expand description

An Enumeration of Postgres top-level MemoryContexts. Each have their own use and “lifetimes” as defined by Postgres’ memory management model.

It’s possible to deference any one of these (except Transient) via the ::value() method if it’s necessary to pass the raw pointer to a Postgres function.

Additionally, the ::switch_to() function, which takes a closure as its argument, executes the closure within that MemoryContext

Variants

CurrentMemoryContext

Because it would be too much notational overhead to always pass an appropriate memory context to called routines, there always exists the notion of the current memory context CurrentMemoryContext. Without it, for example, the copyObject routines would need to be passed a context, as would function execution routines that return a pass-by-reference datatype. Similarly for routines that temporarily allocate space internally, but don’t return it to their caller? We certainly don’t want to clutter every call in the system with “here is a context to use for any temporary memory allocation you might want to do”.

The upshot of that reasoning, though, is that CurrentMemoryContext should generally point at a short-lifespan context if at all possible. During query execution it usually points to a context that gets reset after each tuple. Only in very circumscribed code should it ever point at a context having greater than transaction lifespan, since doing so risks permanent memory leaks.

TopMemoryContext

this is the actual top level of the context tree; every other context is a direct or indirect child of this one. Allocating here is essentially the same as “malloc”, because this context will never be reset or deleted. This is for stuff that should live forever, or for stuff that the controlling module will take care of deleting at the appropriate time. An example is fd.c’s tables of open files. Avoid allocating stuff here unless really necessary, and especially avoid running with CurrentMemoryContext pointing here.

PortalContext

this is not actually a separate context, but a global variable pointing to the per-portal context of the currently active execution portal. This can be used if it’s necessary to allocate storage that will live just as long as the execution of the current portal requires.

ErrorContext

this permanent context is switched into for error recovery processing, and then reset on completion of recovery. We arrange to have a few KB of memory available in it at all times. In this way, we can ensure that some memory is available for error recovery even if the backend has run out of memory otherwise. This allows out-of-memory to be treated as a normal ERROR condition, not a FATAL error.

PostmasterContext

this is the postmaster’s normal working context. After a backend is spawned, it can delete PostmasterContext to free its copy of memory the postmaster was using that it doesn’t need. Note that in non-EXEC_BACKEND builds, the postmaster’s copy of pg_hba.conf and pg_ident.conf data is used directly during authentication in backend processes; so backends can’t delete PostmasterContext until that’s done. (The postmaster has only TopMemoryContext, PostmasterContext, and ErrorContext — the remaining top-level contexts are set up in each backend during startup.)

CacheMemoryContext

permanent storage for relcache, catcache, and related modules. This will never be reset or deleted, either, so it’s not truly necessary to distinguish it from TopMemoryContext. But it seems worthwhile to maintain the distinction for debugging purposes. (Note: CacheMemoryContext has child contexts with shorter lifespans. For example, a child context is the best place to keep the subsidiary storage associated with a relcache entry; that way we can free rule parsetrees and so forth easily, without having to depend on constructing a reliable version of freeObject().)

MessageContext

this context holds the current command message from the frontend, as well as any derived storage that need only live as long as the current message (for example, in simple-Query mode the parse and plan trees can live here). This context will be reset, and any children deleted, at the top of each cycle of the outer loop of PostgresMain. This is kept separate from per-transaction and per-portal contexts because a query string might need to live either a longer or shorter time than any single transaction or portal.

TopTransactionContext

this holds everything that lives until end of the top-level transaction. This context will be reset, and all its children deleted, at conclusion of each top-level transaction cycle. In most cases you don’t want to allocate stuff directly here, but in CurTransactionContext; what does belong here is control information that exists explicitly to manage status across multiple subtransactions. Note: this context is NOT cleared immediately upon error; its contents will survive until the transaction block is exited by COMMIT/ROLLBACK.

CurTransactionContext

this holds data that has to survive until the end of the current transaction, and in particular will be needed at top-level transaction commit. When we are in a top-level transaction this is the same as TopTransactionContext, but in subtransactions it points to a child context. It is important to understand that if a subtransaction aborts, its CurTransactionContext is thrown away after finishing the abort processing; but a committed subtransaction’s CurTransactionContext is kept until top-level commit (unless of course one of the intermediate levels of subtransaction aborts). This ensures that we do not keep data from a failed subtransaction longer than necessary. Because of this behavior, you must be careful to clean up properly during subtransaction abort — the subtransaction’s state must be delinked from any pointers or lists kept in upper transactions, or you will have dangling pointers leading to a crash at top-level commit. An example of data kept here is pending NOTIFY messages, which are sent at top-level commit, but only if the generating subtransaction did not abort.

For(MemoryContext)

This represents a MemoryContext that was likely created via pg_sys::AllocSetContextCreateExtended.

That could be a MemoryContext you created yourself, or it could be one given to you from Postgres. For example, the TupleTableSlot struct has a field referencing the MemoryContext in which slots are allocated.

Owned(OwnedMemoryContext)

A MemoryContext owned by Rust that will be freed when when Dropped

Of(void_ptr)

Use the MemoryContext in which the specified pointer was allocated.

It’s incredibly important that the specified pointer be one actually allocated by Postgres’ memory management system. Otherwise, it’s undefined behavior and will absolutely crash Postgres

Transient

Fields

parent: MemoryContext
name: &'static str
min_context_size: u32
initial_block_size: u32
max_block_size: u32

Create a temporary MemoryContext for use with ::switch_to(). It gets deleted as soon as ::switch_to() exits.

Trying to use this context through [::value{}] will result in a panic!().

Implementations

Create a new PgMemoryContext::Owned

Retrieve the underlying Postgres *mut MemoryContextData

This works for every type except the ::Transient type.

Set this MemoryContext as the CurrentMemoryContext, returning whatever CurrentMemoryContext` is

Release all space allocated within a context and delete all its descendant contexts (but not the context itself).

Run the specified function “within” the MemoryContext represented by this enum.

The important implementation detail is that Postgres’ CurrentMemoryContext is changed to be this context, the function is run so that all Postgres memory allocations happen within that context, and then CurrentMemoryContext is restored to what it was before we started.

Examples
use pgx::*;

#[pg_guard]
pub fn do_something() -> pg_sys::ItemPointer {
    PgMemoryContexts::TopTransactionContext.switch_to(|context| {
        // allocate a new ItemPointerData, but inside the TopTransactionContext
        let tid = PgBox::<pg_sys::ItemPointerData>::alloc();
         
        // do something with the tid and then return it.
        // Note that it stays allocated here in the TopTransactionContext
        tid.into_pg()
    })
}

Duplicate a Rust &str into a Postgres-allocated “char *”

Examples
use pgx::PgMemoryContexts;
let copy = PgMemoryContexts::CurrentMemoryContext.pstrdup("make a copy of this");

Copies len bytes, starting at src into this memory context and returns a raw *mut T pointer to the newly allocated location

Allocate memory in this context, which will be free’d whenever Postgres deletes this MemoryContext

Allocate a slice in this context, which will be free’d whenever Postgres deletes this MemoryContext

Allocate a slice in this context, where the memory is zero’d, which will be free’d whenever Postgres deletes this MemoryContext

Allocate memory in this context, which will be free’d whenever Postgres deletes this MemoryContext

The allocated memory is zero’d

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Set the foreground color generically Read more

Set the background color generically. Read more

Change the foreground color to black

Change the background color to black

Change the foreground color to red

Change the background color to red

Change the foreground color to green

Change the background color to green

Change the foreground color to yellow

Change the background color to yellow

Change the foreground color to blue

Change the background color to blue

Change the foreground color to magenta

Change the background color to magenta

Change the foreground color to purple

Change the background color to purple

Change the foreground color to cyan

Change the background color to cyan

Change the foreground color to white

Change the background color to white

Change the foreground color to the terminal default

Change the background color to the terminal default

Change the foreground color to bright black

Change the background color to bright black

Change the foreground color to bright red

Change the background color to bright red

Change the foreground color to bright green

Change the background color to bright green

Change the foreground color to bright yellow

Change the background color to bright yellow

Change the foreground color to bright blue

Change the background color to bright blue

Change the foreground color to bright magenta

Change the background color to bright magenta

Change the foreground color to bright purple

Change the background color to bright purple

Change the foreground color to bright cyan

Change the background color to bright cyan

Change the foreground color to bright white

Change the background color to bright white

Make the text bold

Make the text dim

Make the text italicized

Make the text italicized

Make the text blink

Make the text blink (but fast!)

Swap the foreground and background colors

Hide the text

Cross out the text

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either OwoColorize::fg or a color-specific method, such as OwoColorize::green, Read more

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either OwoColorize::bg or a color-specific method, such as OwoColorize::on_yellow, Read more

Set the foreground color to a specific RGB value.

Set the background color to a specific RGB value.

Sets the foreground color to an RGB value.

Sets the background color to an RGB value.

Apply a runtime-determined style

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more