Skip to content

PgProcess

Namespace: Ankus

Assembly: Ankus.Runtime.dll

Runs code when the current PostgreSQL process exits, as a C extension’s before_shmem_exit callback does.

public static class PgProcess

Inheritance: object

Registers a callback that runs when this process exits, before it detaches from shared memory.

public static IDisposable OnExit(Action<int> callback)

Parameters:

callback — Action<int>

The callback, given the exit code: zero for a normal exit, nonzero after a FATAL error or a termination request.

Returns: IDisposable

A registration whose disposal removes the callback before it runs.

Callbacks run newest first, after a normal exit, after a FATAL error and when PostgreSQL stops a worker, but not when the process crashes or the server restarts after a crash. A background worker uses one to release the shared-memory slot it claimed, as pglogical's workers do.

A callback can use shared memory, lightweight locks and [PgLog](/api/ankus.pglog/) at LOG and below, as PostgreSQL's own exit callbacks do, but not SQL, because the transaction may be aborted and PostgreSQL is shutting the process down. It must not wait for a lock this process may already hold. An exception is reported as a warning and the remaining callbacks still run. Registration belongs to the calling process; register in the backend or worker that needs the callback, after the library has loaded there.