Skip to content

Support parallel consistent reads via Postgres snapshot export #50

Description

Summary

Add support for parallel consistent reads across multiple connections using Postgres's snapshot export mechanism. This enables structured concurrency patterns where a parent transaction fans out read-only queries to child virtual threads, each with their own JDBC Connection, all seeing an identical consistent view of the database.

Mechanism

-- Connection A (parent)
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT pg_export_snapshot(); -- → '00000003-1'

-- Connection B (child, before A commits)
BEGIN ISOLATION LEVEL REPEATABLE READ;
SET TRANSACTION SNAPSHOT '00000003-1';
-- now runs parallel query with same view as A

pg_export_snapshot() returns a snapshot ID that another transaction can import via SET TRANSACTION SNAPSHOT '<id>'. Both transactions then see an identical consistent view of the DB.

When to use

  • Parallel queries + cross-connection consistency required + queries are long enough that the parallelism benefit outweighs ~2 extra roundtrips (BEGIN + SET TRANSACTION SNAPSHOT per child)
  • Avoid on high-write tables where the snapshot hold time would be long
  • Don't use if slight inconsistency is acceptable — which it often is for analytical/reporting queries

Constraints

  • Child connections must use REPEATABLE READ or SERIALIZABLE isolation level
  • Children are read-only — writes in children are not part of the parent's transaction
  • Each child requires its own Connection from the pool — pool must be sized for fan-out
  • Snapshot is only valid while the exporting transaction is open

Prerequisites

This feature builds on top of the transaction system (once implemented). The parent transaction scope would export the snapshot, and a structured concurrency API would provide child scopes that automatically import it.

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions