Skip to content

[Performance]: Profile and optimize PostgreSQL task/push write saturation #206

Closed

Description

@MisterVVP

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:

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

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_upsert and push_config_upsert latency 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.

Activity

  1. converted this from a draft issue on Aug 11, 2026
  2. added
    performance
    on Aug 11, 2026
  3. self-assigned this
    on Aug 16, 2026
  4. moved this from Todo to In Progress in a2a-cpp SDK Roadmapon Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

performance

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions