Skip to content

common_rates.limiters.postgres

The PostgreSQL backend, through common-db: every process sharing the database shares the count.

One row per scope holds its state as JSON. A reservation is one short transaction on common-db’s async pool: an advisory lock on the scope, the row and the server’s clock read under it, the decision run, the row written, the lock released with the commit. The clock is always the server’s, so two processes never disagree on now.

Classes

PostgresLimiter

Bases: BaseLimiter

Shared state in one PostgreSQL table, reached through common-db’s DBClient.

Carried state: the base’s, the config, the client and whether it is owned. An owned client is built from the config’s connection and pool at open and closed at close; a borrowed one is used as given and left open. Opening proves the server answers; initialize() creates the schema and table explicitly. Reads never create or migrate.

__init__

def __init__(config: PostgresLimiterConfig | Mapping[str, object] | None = None, *, db: DBClient | None = None) -> None

Fields and properties declared here; inherited members belong to the bases above.

BACKEND

BACKEND: str = 'postgres'

db

Read-only property.

db: DBClient | None

The common-db client behind this limiter; None before an owned one is opened.

owns_db

Read-only property.

owns_db: bool

table

Read-only property.

table: str

initialize

async def initialize() -> None

Create the schema and the table when absent. Explicit, never on read.