Repository navigation
Description
Benchmark signal
PR #204 exposed a repeatable PostgreSQL write-saturation pattern between concurrency 16 and 64 while using a fixed PostgreSQL pool size of 64.
Evidence source:
- PR: Add HTTP/1.1 persistent connections with connection-scoped buffering and diagnostics #204
- successful CI run: https://github.com/MisterVVP/a2a-cpp/actions/runs/31485493689
- normal performance artifact:
perf-artifacts-normal - commit:
f62b66e26cf65aa4fa7606ec72746b17ffda1032
The in-process PostgreSQL rows isolate the store/service path from wire-transport costs:
| Scenario | c16 throughput | c64 throughput | Throughput change | c16 p95 | c64 p95 | p95 change |
|---|---|---|---|---|---|---|
SendMessage_CreateTask |
3,230 ops/s | 3,138 ops/s | -2.8% | 9.16 ms | 48.15 ms | +426% |
SendMessage_FollowUpExistingTask |
2,593 ops/s | 2,393 ops/s | -7.7% | 10.39 ms | 52.07 ms | +401% |
PushConfig_Create |
6,631 ops/s | 4,980 ops/s | -24.9% | 4.36 ms | 35.49 ms | +714% |
PushConfig_CreateMany |
914 ops/s | 816 ops/s | -10.7% | 26.29 ms | 123.01 ms | +368% |
PostgreSQL phase diagnostics point at query/write execution rather than connection acquisition:
| Phase / scenario | c16 p95 | c64 p95 |
|---|---|---|
task_upsert / SendMessage_CreateTask |
4.12 ms | 13.34 ms |
task_upsert / SendMessage_FollowUpExistingTask |
3.94 ms | 20.84 ms |
push_config_upsert / PushConfig_Create |
4.35 ms | 35.48 ms |
push_config_upsert / PushConfig_CreateMany |
26.22 ms | 122.95 ms |
At the same time, connection-acquisition p95 remains negligible:
SendMessage_CreateTask: ~0.34 µs at c16 → ~0.37 µs at c64;PushConfig_Create: ~0.12 µs → ~0.17 µs;PushConfig_CreateMany: ~0.87 µs → ~1.04 µs.
The corresponding wire scenarios show the same c64 latency amplification across gRPC, HTTP+JSON, and JSON-RPC, so this does not look transport-specific.
Goal
Identify the dominant PostgreSQL-side cause of task and push write saturation, then implement only evidence-backed SDK/SQL/schema optimizations that improve high-concurrency behavior without degrading correctness, security, or lower-concurrency performance.
Do not assume that a larger connection pool or asynchronous I/O is the solution. The current signal shows available pool capacity with negligible acquisition wait while SQL-phase latency rises sharply.
Scope
1. Reproduce the saturation with focused workloads
Run controlled PostgreSQL profiles for at least:
SendMessage_CreateTask;SendMessage_FollowUpExistingTask;PushConfig_Create;PushConfig_CreateMany.
Use the same schema, PostgreSQL configuration, pool size, fixtures, request count/duration, and host for each concurrency comparison. Include at least c1, c4, c16, and c64.
Keep an in-process profile as the primary attribution signal. Wire measurements may be used as a regression/control layer, not as the primary database profile.
2. Add enough database-side observability to attribute the wait
Capture or derive, where practical:
- PostgreSQL wait-event type/event for active benchmark sessions;
- statement execution time and call counts for the task/push write statements;
- lock waits and conflicting lock types;
- transaction state and concurrent active writers;
- WAL generation / write / sync behavior relevant to the workload;
- buffer/cache activity relevant to the affected statements;
- connection utilisation and acquisition wait alongside database-side waits.
Prefer machine-readable diagnostics that can be correlated with the existing performance rows.
3. Profile the affected statements
Inspect the actual task and push write SQL under representative benchmark data, including the relevant INSERT ... ON CONFLICT / update paths.
Use PostgreSQL profiling tools such as EXPLAIN (ANALYZE, BUFFERS, WAL) where safe and meaningful, and determine whether the dominant cost is caused by, for example:
- row/index contention;
- conflict/update behavior;
- index maintenance;
- unnecessary round trips or repeated writes;
- lock ordering or transaction scope;
- WAL/fsync pressure;
- query-plan or schema/index choices;
- another PostgreSQL server bottleneck.
Do not optimize based on an assumed cause before the measurements identify one.
4. Implement measured optimizations
If profiling identifies an SDK/SQL/schema bottleneck, implement the smallest safe optimization and compare it against the controlled baseline.
Potential directions may include reducing redundant work/round trips, narrowing transaction or lock scope, improving statement/index shape, batching only where semantics permit, or another measured change. These are investigation options, not predetermined solutions.
Preserve existing task/push semantics, transactional correctness, least-privilege behavior, and cross-store consistency.
5. Re-run the focused and normal performance suites
For each accepted optimization, report:
- throughput;
- p50 / p95 / p99 / max latency;
- PostgreSQL phase latency;
- PostgreSQL wait attribution;
- connection acquisition wait;
- operation error rate;
- c16 → c64 scaling behavior.
Use multiple repetitions or another robust comparison method so hosted-runner noise is not mistaken for an optimization.
Relationship to existing issues
- [Performance]: Add adaptive PostgreSQL connection pool resizing #180 (adaptive PostgreSQL pool resizing) targets connection-acquisition contention. The PR Add HTTP/1.1 persistent connections with connection-scoped buffering and diagnostics #204 evidence above does not currently show acquisition contention at pool size 64, so this saturation should be understood before using adaptive pool growth as a remedy.
- [Research/Performance]: Evaluate asynchronous PostgreSQL I/O for SDK stores #181 (asynchronous PostgreSQL I/O) explicitly requires confirming that remaining limitations are not primarily caused by pool capacity, SQL execution plans, or benchmark infrastructure. This issue should provide that evidence for the write paths.
Out of scope
- Increasing benchmark thresholds to hide the saturation.
- Blindly increasing PostgreSQL pool size.
- Implementing adaptive pool resizing as part of this issue.
- Implementing asynchronous libpq I/O as part of this issue.
- Changing transport protocols.
- Large schema rewrites without profiling evidence.
- Benchmark-only changes that improve reported numbers without improving the measured write path.
Acceptance criteria
- Focused c1/c4/c16/c64 baselines are reproducible for representative task and push write scenarios.
- PostgreSQL-side wait attribution is captured for the affected high-concurrency writes.
- The dominant cause of
task_upsertandpush_config_upsertlatency growth is documented with evidence. - Connection acquisition is measured separately so pool starvation cannot be confused with database execution saturation.
- Any production optimization is justified by the profile rather than by speculation.
- At least one representative task-write path and one push-write path are re-measured after the optimization attempt.
- Accepted optimizations materially improve the identified hotspot at high concurrency without materially regressing c1/c4/c16 behavior.
- No correctness, transaction, security, TCK, or operation-error regression is introduced.
- Normal performance, PostgreSQL tail/c1 profiles, benchmarks, sanitizers, tests, clang-format, clang-tidy,
./scripts/verify_changes.sh, Docs, and TCK gates pass. - If profiling shows the limit is primarily PostgreSQL host/configuration rather than an SDK/SQL/schema defect, document that result and create narrowly scoped follow-ups instead of forcing an SDK change.
Expected outcome
After this work, we should know whether the c16 → c64 plateau is caused by an optimizable SDK/SQL/schema path or by the PostgreSQL server's write-capacity/locking/WAL limits under the current CI environment, and we should have a measured basis for deciding whether #180, #181, SQL/schema work, or database deployment guidance is the correct next step.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
perf(postgres): optimize task and push write paths under high concurr…