Routing hints aka virtual parameters
The SPQR router supports runtime hints and behavior configuration parameters. They behave differently depending on how users apply them in their work. There are two main ways to work with virtual parameters:-
Comment in the SQL statement:
-
Via SET-RESET interface:
Scope and priority
Many routing hints can override corresponding configuration settings from the router config file. When multiple sources define the same parameter, SPQR uses the following priority order (highest to lowest):- Transaction-level SET - SET statements executed inside an explicit transaction (BEGIN…COMMIT)
- SQL comment hints - Hints specified as SQL comments in the query (e.g.,
/* __spqr__execute_on: shard2 */) - Session-level SET - SET statements executed outside a transaction
- Router configuration - Default values from the router config file (e.g.,
query_routing.default_route_behaviour)
Example
List of hints
__spqr__engine_v2
Enables v2 routing engine functionality. This hint is currently under development and may be removed in future updates.__spqr__target_session_attrs
Thetarget-session-attrs parameter in PostgreSQL is used to specify the desired session state when connecting to a database. It is particularly useful in scenarios with multiple servers and failover configurations, where you want to ensure your session connects to the server with the desired role or state.
target_session_attrs and target-session-attrs are both aliases for __spqr__target_session_attrs.target-session-attrs:
read-write: Connects to a server in read-write mode, and fails if the primary is unavailable.smart-read-write: Connects to a server in read-write mode, and fails if the primary is unavailable. Automatically routes read-only queries to standbys (with prefer-standby semantics), if query_routing: auto_route_ro_on_standby is set to true in configure file.read-only: Connects only to read-only (in recovery) servers. Fails if none available.prefer-standbyorprefer-replica: Prefers connecting to standby servers. If none are available, it connects to primary.any: Connect to any available server, regardless of state.
__spqr__default_route_behaviour
Controls whether multi-shard queries are allowed or blocked. Possible values:BLOCK- Rejects queries that cannot be routed to a single shard (default in most configurations)ALLOW- Permits multi-shard query execution
- DDL commands that need to run on all shards
- Administrative queries
- Development and testing scenarios
__spqr__execute_on
The__spqr__execute_on parameter is used to specify the shard on which a query should be executed.
This is a runtime-only hint with no corresponding router configuration setting. It can only be specified via SQL comments or SET statements.
__spqr__reply_notice
Whether to show notice messages.__spqr__sharding_key
The__spqr__sharding_key explicitly pass the sharding key to the SPQR Router.
This is a runtime-only hint with no corresponding router configuration setting. It can only be specified via SQL comments or SET statements.
__spqr__distribution
The__spqr__distribution explicitly specify which distribution the SPQR router should use for query routing.
This is a runtime-only hint with no corresponding router configuration setting. It can only be specified via SQL comments or SET statements.
__spqr__distributed_relation
The__spqr__distributed_relation is name of distributed relation in context of which query meant to be executed. Is takes effect when distribution contains both CITY and MURMUR hash based sharding
This is a runtime-only hint with no corresponding router configuration setting. It can only be specified via SQL comments or SET statements.
__spqr__auto_distribution
The__spqr__auto_distribution parameter is used to automatically adding new table in SPQR metadata without going into the spqr-console.
This is a runtime-only hint with no corresponding router configuration setting. It can only be specified via SQL comments or SET statements.
__spqr__distribution_key
The__spqr__distribution_key parameter is used to automatically add a new table to the SPQR metadata without having to go into the spqr console. This parameter can only be used with the __spqr__auto_distribution hint.
This is a runtime-only hint with no corresponding router configuration setting. It can only be specified via SQL comments or SET statements.
__spqr__scatter_query
Forces a query to execute on all shards in the cluster, regardless of the sharding key.This is a runtime-only hint with no corresponding router configuration setting. It can only be specified via SQL comments or SET statements.
true- Execute query on all shardsfalse- Normal routing behavior (default)
__spqr__commit_strategy
Specifies the commit strategy for distributed transactions. This parameter is critical for ensuring data consistency when transactions span multiple shards. Possible values:best-effort(default) - One-phase commit with no coordination between shards1pc- Alias forbest-effort2pc- Two-phase commit with atomicity guarantees across shards
- Enable
__spqr__engine_v2hint - Set max_prepared_transactions > 0 on all shards
- 1pc/best-effort: Commits each shard independently. Fast but no atomicity across shards.
- 2pc: Uses PostgreSQL’s prepared transactions to ensure all-or-nothing commits across shards.
__spqr__preferred_engine
This is developer-only option. Only possible value currently is ‘v2’This is a runtime-only hint with no corresponding router configuration setting. It is intended for internal development use only.