Contents
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 ofpostgres.hand PG headers so it compiles standalone under ASan, UBSan, and TSan (C99 & C11). Keepsrc/pg/as a thin adapter for PG hooks, fixed shared memory, atomic file persistence, and SQL functions. - Hot-path budget: execute warmed
ExecutorStart/ExecutorEndwith zeroLWLockacquisitions, zeromalloc/palloc, and zero string operations; read a backend-local snapshot refreshed only onrule_epochchange, and readCLOCK_MONOTONIConly when the matched rule setsmax_qps > 0(or on reject whenlog_rejects = on). - Node-local state: persist rules atomically to
$PGDATA/pg_stat/pgbreaker.rules(unique temp file +fsync+renamewith CRC32C) without a Background Worker, SPI, or catalog table writes, so primary and read-only hot standby nodes both support immediate localtrip()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, andProcessUtilitymaintenance/DDL commands; keeppgbreaker.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/FinishPG_CATCH,ExecutorEnd, xact/subxact callbacks, andbefore_shmem_exitwitheval_statepinning,rule_genvalidation, and saturating decrement. - Fail-Open: on unreadable, non-regular, or corrupt persistence files, emit
WARNINGand start with zero rules—never block PostgreSQL startup (freshENOENTstarts silently). Suppress53P01server error spam viaemit_log_hookand rate-limitLOGoutput. - Control-plane discipline: enforce
superuser()in C plus least-privilege SQL grants (REVOKE ALL FROM PUBLIC,pg_read_all_statsfor views);reset_stats()clears cumulative counters only and preservesactive_count.
- No self-lock: exempt effective superusers, roles inheriting
Verification Commands
- Core tests (C99 & C11 ASan+UBSan, TSan,
-O2release; no PostgreSQL required):make test-core(override compilers withSAN_CC=clang CORE_CC=gcc). - Extension PGXS build & install:
make && make install(passPG_CONFIG=/path/to/pg_configwhen non-default). - PostgreSQL integration, chaos, and benchmark suite:
make test-pg(SKIP_BENCH=1 make test-pgfor 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.