Optimize PostgreSQL forwarding with phased deferred I/O

Merged
RE
11d ago
+6199-1133

Reduce PostgreSQL forwarding overhead by consuming socket readiness in session handlers while retaining phased scheduling.

  • Finish current-request cleanup and attempt nonblocking client writeout before handing released capacity to a waiter. Keep another buffered command pending until the scheduler selects it; preserve partial output and pause deadlines.
  • Restrict speculative checkout to compatible established connections, preserving ordinary recovery and deadlines.
  • Reduce classifier copies, tokenizer formatting, COPY regex work, result allocations and redundant session visits; batch bounded native DataRow spans and cache verified frontend TLS handshake completion.
  • Contain libpq notice allocation failures until C cleanup completes, retain COPY buffer ownership and prevent reuse of incomplete responses. Preserve plaintext output after retryable sends, including after writable readiness.

Scheduling order and diagrams explain the previous and current request boundaries.

Validation: release build, 143 focused scheduler/allocation/worker assertions, native and libpq wire ordering checks, and repository CI lint passed locally.

Latest focused benchmark: SELECT 1, no TLS, 500 clients, 10 pre-established backends, 16 pgbench jobs, one native worker. In old/new/new/old order, mean TPS was 42,640 for c94fbe056 and 41,960 for 4facb95bf: a 1.60% regression, with CPU/query 1.66% higher. No new backends or failed transactions. This performance regression remains unresolved. Results and artifacts.

Summary by CodeRabbit

  • New Features
    • PostgreSQL results can be collected in bounded batches.
    • Connection dispatch can reuse eligible established connections, while waiting requests can proceed as backend connections become available.
  • Bug Fixes
    • Improved TLS handshake handling and SQL command classification.
    • Result-processing allocation failures preserve buffered data and mark affected connections unhealthy; notice-processing failures prevent affected connections from being reused.
    • Temporary socket-send interruptions no longer incorrectly close connections.
  • Tests
    • Expanded coverage for row batching, scheduler behavior, command classification, and allocation failures.