PostgreSQL GCRA
PostgresGcra stores GCRA state in PostgreSQL and makes each charge through a database function.
It is a shared policy: replicas that use the same database, namespace, policy name, and quota
enforce one client-key state. The adapter accepts a caller-owned sqlx::PgPool and is Tokio-only
through SQLx.
Setup and migration ownership
Enable the policy and provide SQLx in the application:
[dependencies]
sqlx = { version = "0.9", default-features = false, features = ["postgres", "runtime-tokio"] }
tower-rate-limiter = { version = "0.2", default-features = false, features = ["postgres-gcra"] }
Before serving traffic, apply the packaged, versioned migration with your application’s migration runner:
migrations/postgres/0001_gcra.sql
The same artifact is exported as POSTGRES_GCRA_MIGRATION_V1 for migration tooling that accepts
SQL text. File-oriented migration frameworks should copy the packaged SQL into the application’s
own migration directory so their normal checksum and ordering rules apply.
The crate deliberately never applies DDL or creates schema objects at runtime. Treat the migration
as part of service provisioning and roll it out before deploying code that enables PostgresGcra.
#![allow(unused)]
fn main() {
use std::time::Duration;
use tower_rate_limiter::{GcraQuota, GcraQuotaError, PostgresGcra};
fn configure(pool: sqlx::PgPool) -> Result<PostgresGcra, GcraQuotaError> {
let quota = GcraQuota::new(100, Duration::from_secs(60), 20)?;
let policy = PostgresGcra::new(pool, quota).with_namespace("my-service-v1");
Ok(policy)
}
}
See GCRA backends for the shared quota, Decision, and rollout contract.
Atomicity and time source
The migration installs tower_rate_limiter_gcra_v1.charge(...). It locks an existing key row
directly or creates a missing row, retrying the lookup on a concurrent creation or deletion.
It then captures clock_timestamp() after that lock and performs the GCRA decision and
accepted-state update in the same database function. A rejected charge intentionally does not
advance state.
Using database time avoids application-host clock skew for a decision. Requests for the same key serialize on that key’s row; different keys can proceed independently. This makes PostgreSQL a good fit only when its per-request database work and hot-key contention are acceptable for the protected endpoint.
The adapter executes one statement through the pool and is tested with PostgreSQL’s default
READ COMMITTED isolation. If the deployment changes the database or role default isolation,
validate concurrent fresh-key behavior and decide how Policy errors such as serialization failures
fit the application’s failure mode.
Permissions
Run the migration as a schema owner or migration principal. The runtime limiter principal needs access to the installed schema and function plus the table permissions used by this migration’s security-invoker function:
GRANT USAGE ON SCHEMA tower_rate_limiter_gcra_v1 TO app_limiter;
GRANT EXECUTE ON FUNCTION tower_rate_limiter_gcra_v1.charge(TEXT, TEXT, BIGINT, BIGINT) TO app_limiter;
GRANT SELECT, INSERT, UPDATE ON TABLE tower_rate_limiter_gcra_v1.state TO app_limiter;
Use a separate maintenance principal for deletion if the runtime role should not remove state.
Do not change the function to SECURITY DEFINER without a deliberate ownership and search_path
review.
Expired-state cleanup
The state table records expires_at and indexes it. This cleanup timestamp is rounded up to
whole seconds, reducing index changes and permitting HOT updates when page space is available.
It never precedes the precise TAT and can retain an inactive row for less than one additional
second. Admission, remaining capacity, retry, and reset calculations still use microsecond TAT.
The migration leaves fillfactor and autovacuum at PostgreSQL defaults.
Expired rows no longer affect a fresh GCRA decision, but PostgreSQL does not remove them
automatically. Schedule a bounded cleanup job under your operational ownership, for example
repeatedly deleting expired rows in small batches during off-peak periods. Grant that job DELETE
separately when the runtime principal does not have it.
One concurrency-safe batch shape is:
WITH expired AS (
SELECT namespace, client_key
FROM tower_rate_limiter_gcra_v1.state
WHERE expires_at <= statement_timestamp()
ORDER BY expires_at
LIMIT 1000
FOR UPDATE SKIP LOCKED
)
DELETE FROM tower_rate_limiter_gcra_v1.state AS state
USING expired
WHERE state.namespace = expired.namespace
AND state.client_key = expired.client_key;
statement_timestamp() fixes the cutoff for each batch so the expiry condition can use an index
range scan. The charge function still uses clock_timestamp() after acquiring the row lock.
Choose the interval, batch size, vacuum/monitoring policy, and retention budget for your workload; the library does not run a cleanup worker. Monitor table growth, index size, lock waits, database latency, and policy failures.
Namespace and quota rollouts
Use .with_namespace(...) to separate deployments sharing one database. The middleware-provided
policy_name is also part of the scoped client key. Every replica charging the same resulting
state identity must use the same quota. When changing a quota, version the namespace or policy
name—such as my-service-v2—rather than allowing old and new emission intervals to reinterpret
the same stored theoretical-arrival time. Retire the old identity only after its state has expired.
PostgreSQL’s primary-key index also imposes a practical bound on the combined namespace and client
key size. Built-in IP keys are small. If a custom extractor can produce long or attacker-controlled
values, use with_key_encoder to map the complete scoped key to a bounded digest before it reaches
the Policy.