Google Cloud Client Libraries for Rust - Spanner
This crate implements the Spanner client library.
WARNING: this is a preview release of the crate. We believe the APIs to be stable. We also are seeking feedback about the APIs and may need to make breaking changes if we discover that some parts are hard to use.
We welcome feedback about the APIs, documentation, missing features, bugs, etc.
About Spanner
Spanner is a fully managed, mission-critical, relational database service that offers transactional consistency at global scale, schemas, SQL (ANSI 2011 with extensions), and automatic, synchronous replication for high availability.
To read, write, and query data, use the Spanner client to connect to the
service and create a DatabaseClient for your database.
Getting Started
Installation
To add this crate to your project, use cargo add:
To find the latest version and see how to add it manually to your Cargo.toml,
refer to the crate's page on docs.rs.
Authentication and Authorization
The Spanner client automatically uses Application Default Credentials (ADC). You
can also provide explicit credentials by calling
.with_credentials(credentials) on the client builder:
use Spanner;
// ...
# async
Ensure your authenticated principal has the required IAM roles to access Spanner resources. See the Authentication section in the workspace root for setup details.
Local Development with the Spanner Emulator
To develop locally against the
Spanner Emulator, set the
SPANNER_EMULATOR_HOST environment variable before creating the client:
The client builder automatically detects this variable, connects to the emulator endpoint, and configures anonymous credentials.
Session Management and Client Lifecycle
The Spanner Rust client manages a long-lived multiplexed session under the hood.
Because session creation is expensive, DatabaseClient is designed to be a
long-lived object.
- Create a single
DatabaseClientper database and reuse it across your application for all queries and transactions. DatabaseClientis thread-safe and cheap to clone, as it shares the underlying session pool and gRPC channels via reference counting (Arc).- Avoid creating a new
DatabaseClientfor individual requests.
Common Tasks
1. Creating a client
The following example shows how to initialize the Spanner client and build a
long-lived DatabaseClient for a specific database:
use Error;
use Spanner;
async
2. Executing a query using a single-use read-only transaction
A single-use read-only transaction (single_use()) is optimized for executing a
single read or query. It reduces latency by avoiding the overhead of multi-use
transaction initialization.
use Error;
use DatabaseClient;
use Statement;
async
3. Executing a read/write transaction
Read/write transactions execute queries and mutations atomically. Spanner may
abort a transaction if contention occurs or for other transient reasons. Use
TransactionRunner to execute read/write operations; it automatically retries
the closure when aborted.
use Error;
use DatabaseClient;
use Statement;
async
4. Executing a read-only transaction
A multi-use read-only transaction executes multiple reads or queries at a consistent snapshot in time without taking locks or blocking write operations.
use Error;
use DatabaseClient;
use Statement;
async
5. Executing a stale query
When reading slightly older data is acceptable, choosing a stale timestamp bound can reduce read latency.
use Duration;
use Error;
use DatabaseClient;
use Statement;
use TimestampBound;
async
Admin Operations
While DatabaseClient handles data-plane queries and transactions, you can
perform DDL and instance management operations using admin clients:
DatabaseAdmin: Create and drop databases, execute DDL schema updates.InstanceAdmin: Manage Spanner instances and configurations.
Admin builders automatically inherit endpoints, credentials, and emulator
settings from the parent Spanner instance:
use Error;
use Spanner;
async
Executing Schema Updates
When creating tables or applying schema changes, always combine related DDL
statements into a single update_database_ddl request. Splitting schema changes
into multiple individual operations is inefficient on Spanner and causes each
change to run as a separate long-running operation.
use Poller;
use Error;
use Spanner;
async
Features
default-rustls-provider: enabled by default. Usesaws-lc-rsfor TLS and authentication. Applications with specific cryptographic requirements (such as exclusively using theringcrate) should disable this default and callrustls::crypto::CryptoProvider::install_default().unstable-stream: enables the.into_stream()method on streaming types likeResultSetandExecuteStreamingSql, allowing them to be consumed as a standardfutures::Stream.