Skip to content

[Research/Performance]: Evaluate asynchronous PostgreSQL I/O for SDK stores #181

Open

Description

@MisterVVP

Problem

PostgreSQL-backed SDK stores currently use synchronous libpq calls such as PQexec()/PQexecParams(). A connection lease and calling thread remain occupied while a query waits for database I/O. Pool-size tuning changes the available connection count but does not make those calls non-blocking.

A fully asynchronous database layer is significantly more complex: it changes how connections are leased, transactions survive suspension, cancellation is applied, and shutdown handles in-flight work. Before committing to that complexity, the project needs evidence that it solves a meaningful bottleneck.

Goal

Produce a small, measured prototype and a clear go / limited scope / no-go recommendation for native non-blocking libpq integration. This is a research issue, not authorization to replace production stores.

Ownership and issue boundaries (v0.6.0)

Owns: Research-only baseline, architecture evaluation and minimal genuine non-blocking libpq prototype; produce a documented go/limited/no-go recommendation for #266.

Prerequisites and integration: May begin in parallel with #268/#169/#269. Align future cancellation API with #268 PR 1 but do not wait for its gRPC implementation. #266 remains blocked until a positive evidenced decision.

Must not absorb: No shipping production async store, public store API migration, SQL/schema tuning, pool auto-resizing, pipeline mode or thread-pool wrappers falsely labeled non-blocking.

Issue management: Retain this issue and use separate independently reviewable PRs for the checkpoints below. Close only when final acceptance criteria pass. Do not pull future checkpoints into earlier PRs.

Implementation PR checklist

  • PR 1 — Reproducible controlled synchronous PostgreSQL baseline, identify pool contention/SQL locks/worker waits; architecture options and lifecycle invariants document.
  • PR 2 — Isolated real non-blocking libpq prototype for lookup, write and multi-step transaction; verify lease ownership, cancellation, rollback/discard and shutdown under failure.
  • PR 3 — Apples-to-apples SQL/schema/pool/concurrency measurements (throughput, p50/p95/p99, threads, CPU, connection use, errors, cancellation) plus go/limited/no-go and explicit impact on [Feature]: Implement asynchronous PostgreSQL stores if #181 validates the design #266.

PR merge rule: Narrow changes, deterministic scope-specific tests, documented lifecycle ownership, and applicable AGENTS.md validation. Measure performance-sensitive behavior against equivalent baseline workloads. Preserve synchronous behavior.

Detailed technical requirements — scope and investigation phases

Phase 1 — Baseline and architecture decision

  • Establish controlled PostgreSQL benchmarks after the configurable pool work ([Performance]: Make PostgreSQL connection pool size configurable #177) and relevant diagnostics. Distinguish pool contention, SQL/locks, benchmark harness overhead and blocked SDK threads.
  • Examine the existing TaskStore, PushNotificationStore, transaction/advisory-lock flows, and thread/cancellation model.
  • Compare candidate API shapes: parallel async store interfaces, async internals with compatibility wrappers, and separate async PostgreSQL implementations.
  • Document a portable socket-readiness/continuation integration approach, connection lease ownership, cancellation, and shutdown. Prefer not to design a new shared reactor solely for this experiment.

Checkpoint: Written architecture and controlled sync baseline; no production store behavior changes.

Phase 2 — Minimal non-blocking prototype

  • Use real libpq non-blocking I/O (PQsetnonblocking, PQsendQueryParams, PQsocket, PQconsumeInput, PQisBusy, PQgetResult, or appropriate equivalents). Handle partial protocol progress and failure.
  • Prototype one point lookup, one write, one multi-step transaction, cancellation/deadline during work, and an in-flight connection failure.
  • Demonstrate that leases cannot be concurrently used by unrelated operations, rollback finishes or a damaged connection is discarded, and shutdown does not abandon in-flight transactions.
  • Test both successful completion and cancellation/connection failure at suspension boundaries.

Checkpoint: Prototype correctness and resource ownership are demonstrated independently of a new production API.

Phase 3 — Comparative results and recommendation

  • Compare identical SQL, schema, pool size, PostgreSQL config, request counts and concurrency on the same machine/environment. Include throughput, p50/p95/p99/max, errors, CPU, worker threads, connection utilization, relevant database wait attribution, and cancel latency.
  • Measure slow queries and realistic saturation; do not infer throughput improvement merely from using an async API.
  • Document complexity, API migration cost, maintainability and portability tradeoffs, and whether async is justified by: fewer blocked threads at equal work, higher throughput/resource efficiency, better cancellation/deadline behavior, or improved behavior under slow queries.
  • Recommend proceed with production, limit to selected operations, or keep synchronous stores. Explicitly state what would have to change in [Feature]: Implement asynchronous PostgreSQL stores if #181 validates the design #266.

Relationship to other issues

  • #266 remains blocked until this investigation recommends implementation.
  • #268 provides server-side cancellation/deadline semantics to align with; do not require its entire gRPC implementation for a standalone prototype.
  • #270 and #269 do not depend on async PostgreSQL research.

Out of scope

  • Shipping production async stores, converting all store interfaces, replacing PostgreSQL/libpq, schema/SQL tuning, adaptive pool sizing or implementing pipeline mode.
  • Calling a pool of blocked PQexec() operations "asynchronous PostgreSQL I/O".

Acceptance criteria and deliverables

  • Controlled current synchronous baseline and architecture/tradeoff document.
  • A limited real non-blocking libpq prototype covering lookup, write, transaction, cancel/deadline and broken connection.
  • No lease leak, accidental concurrent reuse or ambiguous transaction state on failure/shutdown.
  • Reproducible comparable benchmarks including worker-thread cost and error/cancel metrics.
  • A concise written go / limited / no-go decision with the evidence and clear implications for [Feature]: Implement asynchronous PostgreSQL stores if #181 validates the design #266.
  • Repository checks appropriate to the prototype and touched files pass; no production interface change is included in the research PR by default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions