Skip to content

PgTransaction

Namespace: Ankus

Assembly: Ankus.Runtime.dll

Runs recoverable work and registers callbacks for the current PostgreSQL transaction.

public static class PgTransaction

Inheritance: object

RegisterCallback(PgTransactionEvent, Action)

Section titled “RegisterCallback(PgTransactionEvent, Action)”

Registers a one-shot callback for an outer-transaction phase.

public static PgTransactionCallback RegisterCallback(PgTransactionEvent @event, Action callback)

Parameters:

event — PgTransactionEvent

The phase that runs the callback.

callback — Action

The action to run in registration order.

Returns: PgTransactionCallback

A registration that can cancel the pending callback.

PreCommit and PrePrepare exceptions reject the transaction. Unhandled exceptions in completed commit, abort, and prepare phases cause PostgreSQL crash recovery; committed changes remain committed.

RegisterSubtransactionCallback(PgSubtransactionEvent, Action<PgSubtransactionId, PgSubtransactionId>)

Section titled “RegisterSubtransactionCallback(PgSubtransactionEvent, Action<PgSubtransactionId, PgSubtransactionId>)”

Registers a callback for every matching subtransaction in the current outer transaction.

public static PgSubtransactionCallback RegisterSubtransactionCallback(PgSubtransactionEvent @event, Action<PgSubtransactionId, PgSubtransactionId> callback)

Parameters:

event — PgSubtransactionEvent

The subtransaction phase that runs the callback.

callback — Action<PgSubtransactionId, PgSubtransactionId>

The action to run with the current subtransaction ID followed by its parent subtransaction ID.

Returns: PgSubtransactionCallback

A registration that can cancel future invocations.

Start and PreCommit exceptions reject the operation. A Commit exception becomes a PostgreSQL error, and PostgreSQL aborts the enclosing transaction. An Abort exception is reported as a warning, because the rollback must finish.

Runs synchronous work in an internal subtransaction, rolling it back if the callback fails.

public static void RunInSubtransaction(Action action)

Parameters:

action — Action

The work to run on the active backend thread.

Catch failures outside this callback, after native resources have been recovered. An unrecovered native error, including a value-operation error, prevents further backend work in this scope, even if caught. Success retains changes in the enclosing transaction; it does not commit that transaction. The callback must not perform transaction control or asynchronous work. This operation is unavailable during transaction callbacks and abort cleanup. Internal guard subtransactions do not invoke consumer subtransaction callbacks.

RunInSubtransaction(Action, PgSubtransactionMode)

Section titled “RunInSubtransaction(Action, PgSubtransactionMode)”

Runs synchronous work in an internal subtransaction with the selected statement recovery.

public static void RunInSubtransaction(Action action, PgSubtransactionMode mode)

Parameters:

action — Action

The work to run on the active backend thread.

mode — PgSubtransactionMode

Whether statements recover individually or share the scope’s subtransaction.

Exceptions:

Atomic runs every statement in the scope’s own subtransaction, as SPI does in pgrx, so a bulk write consumes one subtransaction ID. The first PostgreSQL error makes later backend calls in the scope rethrow it, then rolls back the whole scope before propagating; catch it outside the callback. A nested recoverable scope restores per-statement recovery for its own statements. The other rules of RunInSubtransaction(Action) apply.

RunInSubtransaction<TResult>(Func<TResult>)

Section titled “RunInSubtransaction<TResult>(Func<TResult>)”

Runs synchronous work in an internal subtransaction and returns its result after successful release.

public static TResult RunInSubtransaction<TResult>(Func<TResult> action)

Type parameters:

TResult

The callback result type.

Parameters:

action — Func<TResult>

The synchronous work to run on the active backend thread.

Returns: TResult

The result after the subtransaction succeeds.

Failures roll back this scope before propagating to the caller. Catch native errors outside the callback; catching one inside cannot turn a failed scope into a successful one. Nested scopes may recover independently. Native results retain PostgreSQL’s memory-context lifetimes; rollback invalidates allocations and resources owned by the aborted scope. Cleanup aggregates that contain the original native failure retain all their causes after rollback. Do not perform transaction control or asynchronous work in the callback. This operation is unavailable during transaction callbacks and abort cleanup.

RunInSubtransaction<TResult>(Func<TResult>, PgSubtransactionMode)

Section titled “RunInSubtransaction<TResult>(Func<TResult>, PgSubtransactionMode)”

Runs synchronous work in an internal subtransaction with the selected statement recovery and returns its result.

public static TResult RunInSubtransaction<TResult>(Func<TResult> action, PgSubtransactionMode mode)

Type parameters:

TResult

The callback result type.

Parameters:

action — Func<TResult>

The synchronous work to run on the active backend thread.

mode — PgSubtransactionMode

Whether statements recover individually or share the scope’s subtransaction.

Returns: TResult

The result after the subtransaction succeeds.

Exceptions:

See RunInSubtransaction(Action, PgSubtransactionMode).