Transaction Behavior
Default Route Behaviour
query_routing.default_route_behaviour parameter determines what happens when SPQR cannot route a query to a single shard:BLOCK: Rejects queries that would need to execute on multiple shards (safer for consistency)ALLOW: Permits multi-shard queries to execute (useful for DDL and administrative queries)
BLOCK: Production environments where you want strict control over shard accessALLOW: Development, testing, or when executing DDL commands across all shards
Scatter Query
__spqr__scatter_query routing hint is useful when you need to run a query across all shards, regardless of sharding key:
Scatter queries do not provide consistent cross-shard snapshots, can have significant performance impact on large clusters, and should be used primarily for administrative or analytical purposes.
Commit Strategies
SPQR provides three commit strategies for distributed transactions, controlled by the default_commit_strategy parameter.1. One-Phase Commit
Value:1pc (alias: best-effort)
This is the default commit strategy. In this mode transactions are committed on each shard independently with no coordination between shards and is best used for single-shard transactions or when eventual consistency is acceptable.
If a transaction spans multiple shards, partial commits are possible if one shard fails.
2. Two-Phase Commit (2PC)
To use 2PC, you need to ensure the following:
- Set max_prepared_transactions on each shard.
- Enable enhanced_multishard_processing.
- Prepare Phase: SPQR sends
PREPARE TRANSACTIONto all involved shards with a unique transaction ID - Commit Phase: If all shards successfully prepare, SPQR sends
COMMIT PREPAREDto each shard - Rollback on Failure: If any shard fails to prepare, SPQR rolls back the entire transaction
2PC provides atomicity but has performance overhead. Prepared transactions consume resources on shards until committed or rolled back. Monitor prepared transactions with
pg_prepared_xacts on each shard and configure max_prepared_transactions based on your expected concurrent transaction volume.2PC requires a durable transaction coordinator that issues prepare/commit decisions and can recover them after failures; without a durable coordinator, prepared transactions may become orphaned and require manual intervention. SPQR’s router currently acts as the ephemeral coordinator for multi-shard transactions but there is no separate persistent coordinator service implemented yet.