Skip to content

Conversation

@niclaflamme
Copy link
Contributor

@niclaflamme niclaflamme commented Aug 7, 2025

This PR introduces two new modules to PgDog’s frontend: logical session management and logical transaction management. Both aim to enforce correct behavior across a sharded backend without leaking complexity to clients.

LogicalSession

  • New struct LogicalSession<'a> tracks all SET-style session variables.

  • Clears and re-fans out variable settings to relevant shards on change.

  • APIs:

    • set_variable(name, value) – record a new setting and clear shard sync state.
    • get_variable(name) – fetch current value, if any.
    • sync_shard(shard) / is_shard_synced(shard) – mark/check shard as up-to-date.
    • store_swapped_value / take_swapped_value – stash and retrieve prior shard values.
    • reset / reset_after_take – clear state, returning any swapped values.
  • Complete unit tests covering all methods and edge cases.

LogicalTransaction

  • New struct LogicalTransaction enforces one-shard-per-transaction discipline.

  • Tracks lifecycle: IdleBeginPendingInProgress → (Committed/RolledBack).

  • Shard routing:

    • soft_begin() – start a pending transaction.
    • execute_query(shard) – touch a shard (first statement flips to InProgress).
    • commit() / rollback() – finalize or abort.
    • set_manual_shard(shard) – pin transaction to a specific Shard::Direct(n).
    • reset() – clear all state safely at any point.
  • Errors enforce invalid transitions and multi-shard conflicts.

  • Exhaustive tests for all states, error paths, and shard-conflict rules.

Future improvements

  • Track per-variable synced shards to skip redundant SET fan-outs.

Todo

  • Replace client.in_transaction (bool) with client.transaction (LogicalTransaction) and follow the compiler.
  • in_transaction can be inferred.

@niclaflamme niclaflamme changed the title WIP Add Logical Session & Transaction Management Aug 7, 2025
@niclaflamme niclaflamme merged commit cf51417 into main Aug 8, 2025
5 checks passed
@niclaflamme niclaflamme deleted the nic/logical_x branch August 8, 2025 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants