# pgbreaker Agent Guidelines ## Language Write all source code, comments, error messages, SQL definitions and comments, tests, commit messages, `README.md`, and documentation in **English**. ## Architectural Redlines `pgbreaker` is a minimal, lock-free PostgreSQL (PG 14–18) circuit breaker and SQL concurrency/QPS limiter in Pure C99 + PGXS. - **Kernel scope**: implement only fast-fail integer-key `(query_id, db_oid, role_oid)` concurrency/trip control and lock-free GCRA QPS limiting. Leave adaptive heuristics, SQL auditing, string/regex/AST matching, wait queues, cross-node rule sync, business scoring, and TTL scheduling to proxies or external control planes. - **Hard core seam**: keep `src/core/` free of `postgres.h` and PG headers so it compiles standalone under ASan, UBSan, and TSan (C99 & C11). Keep `src/pg/` as a thin adapter for PG hooks, fixed shared memory, atomic file persistence, and SQL functions. - **Hot-path budget**: execute warmed `ExecutorStart`/`ExecutorEnd` with zero `LWLock` acquisitions, zero `malloc`/`palloc`, and zero string operations; read a backend-local snapshot refreshed only on `rule_epoch` change, and read `CLOCK_MONOTONIC` only when the matched rule sets `max_qps > 0` (or on reject when `log_rejects = on`). - **Node-local state**: persist rules atomically to `$PGDATA/pg_stat/pgbreaker.rules` (unique temp file + `fsync` + `rename` with CRC32C) without a Background Worker, SPI, or catalog table writes, so primary and read-only hot standby nodes both support immediate local `trip()` and restart recovery. - **Safety invariants**: - **No self-lock**: exempt effective superusers, roles inheriting `pg_use_reserved_connections` (PG 16+), parallel workers, `EXPLAIN`-only execution, nested executor statements, and `ProcessUtility` maintenance/DDL commands; keep `pgbreaker.enabled = off` (`SIGHUP`) as an immediate hot bypass. - **Zero slot leaks**: record every acquired concurrency lease in the fixed-depth (32) per-backend ledger and release across `ExecutorStart`/`Run`/`Finish` `PG_CATCH`, `ExecutorEnd`, xact/subxact callbacks, and `before_shmem_exit` with `eval_state` pinning, `rule_gen` validation, and saturating decrement. - **Fail-Open**: on unreadable, non-regular, or corrupt persistence files, emit `WARNING` and start with zero rules—never block PostgreSQL startup (fresh `ENOENT` starts silently). Suppress `53P01` server error spam via `emit_log_hook` and rate-limit `LOG` output. - **Control-plane discipline**: enforce `superuser()` in C plus least-privilege SQL grants (`REVOKE ALL FROM PUBLIC`, `pg_read_all_stats` for views); `reset_stats()` clears cumulative counters only and preserves `active_count`. ## Verification Commands - **Core tests (C99 & C11 ASan+UBSan, TSan, `-O2` release; no PostgreSQL required)**: `make test-core` (override compilers with `SAN_CC=clang CORE_CC=gcc`). - **Extension PGXS build & install**: `make && make install` (pass `PG_CONFIG=/path/to/pg_config` when non-default). - **PostgreSQL integration, chaos, and benchmark suite**: `make test-pg` (`SKIP_BENCH=1 make test-pg` for fast CI mode). ## Reference Pointers - **Architecture, safety invariants, and non-goals** — read before modifying shared memory layout, atomic concurrency/GCRA logic, persistence format, or scope boundaries: `docs/architecture.md`. - **SQL API, GUCs, matching precedence, and error codes** — read before modifying SQL functions, views, GUC behavior, or rejection error details: `docs/reference.md`.