@google-cloud/spanner 1.2.0 » Namespace: v1

Enumerations

Kind

static

number

The kind of PlanNode. Distinguishes between the two different kinds of nodes that can appear in a query plan.

Value

KIND_UNSPECIFIED

Not specified.

RELATIONAL

Denotes a Relational operator node in the expression tree. Relational operators represent iterative processing of rows during query execution. For example, a TableScan operation that reads rows from a table.

QueryMode

Value

The default mode where only the query result, without any information about the query plan is returned.

PLAN

This mode returns only the query plan, without any result rows or execution statistics information.

PROFILE

This mode returns both the query plan and the execution statistics along with the result rows.

Properties

Kind

static

number

The kind of PlanNode. Distinguishes between the two different kinds of nodes that can appear in a query plan.

Value

KIND_UNSPECIFIED

Not specified.

RELATIONAL

Denotes a Relational operator node in the expression tree. Relational operators represent iterative processing of rows during query execution. For example, a TableScan operation that reads rows from a table.

ChildLink

static

Metadata associated with a parent-child relationship appearing in a PlanNode.

Properties

Parameter

childIndex

number

The node to which the link points.

type

string

The type of the link. For example, in Hash Joins this could be used to distinguish between the build child and the probe child, or in the case of the child being an output variable, to represent the tag associated with the output
variable.

variable

string

Only present if the child node is SCALAR and corresponds to an output variable of the parent node. The field carries the name of the output variable. For example, a TableScan operator that reads rows from a table will
have child links to the SCALAR nodes representing the output variables created for each column that is read by the operator. The corresponding
variable fields will be set to the variable names assigned to the columns.

CommitRequest

Properties

Parameter

session

string

Required. The session in which the transaction to be committed is running.

transactionId

string

Commit a previously-started transaction.

singleUseTransaction

Object

Execute mutations in a temporary transaction. Note that unlike commit of a previously-started transaction, commit with a temporary transaction is non-idempotent. That is, if the
CommitRequest is sent to Cloud Spanner more than once (for instance, due to retries in the application, or in the transport library), it is possible that the mutations are executed more than once. If this is undesirable,
use BeginTransaction and Commit instead.

The SQL query string can contain parameter placeholders. A parameter placeholder consists of '@' followed by the parameter name. Parameter names consist of any combination of letters, numbers, and underscores.

Parameters can appear anywhere that a literal value is expected. The same parameter name can be used more than once, for example:
"WHERE id > @msg_id AND id < @msg_id + 100"

It is an error to execute an SQL query with unbound parameters.

Parameter values are specified using params, which is a JSON object whose keys are parameter names, and whose values are the corresponding parameter values.

It is not always possible for Cloud Spanner to infer the right SQL type from a JSON value. For example, values of type BYTES and values of type STRING both appear in params as JSON strings.

In these cases, param_types can be used to specify the exact SQL type for some or all of the SQL query parameters. See the definition of Type for more information about SQL types.

resumeToken

string

If this request is resuming a previously interrupted SQL query execution, resume_token should be copied from the last PartialResultSet yielded before the interruption. Doing this enables the new SQL query execution
to resume where the last one left off. The rest of the request parameters must exactly match the request that yielded this token.

queryMode

number

Used to control the amount of debugging information returned in ResultSetStats. If partition_token is set, query_mode can only be set to QueryMode.NORMAL.

If present, results will be restricted to the specified partition previously created using PartitionQuery(). There must be an exact match for the values of fields common to this message and the PartitionQueryRequest message used
to create this partition_token.

Since the UserEvents table's PRIMARY KEY clause names two columns, each UserEvents key has two elements; the first is the
UserName, and the second is the EventDate.

Key ranges with multiple components are interpreted lexicographically by component using the table or index key's declared sort order. For example, the following range returns all events for user "Bob" that occurred
in the year 2015:

Start and end keys can omit trailing key components. This affects the inclusion and exclusion of rows that exactly match the provided key components: if the key is closed, then rows that exactly match the provided components are included;
if the key is open, then rows that exactly match are not included.

For example, the following range includes all events for "Bob" that occurred during and after the year 2000:

"start_closed": ["Bob", "2000-01-01"]
"end_closed": ["Bob"]

The next example retrieves all events for "Bob":

"start_closed": ["Bob"]
"end_closed": ["Bob"]

To retrieve events before the year 2000:

"start_closed": ["Bob"]
"end_open": ["Bob", "2000-01-01"]

The following range includes all rows in the table:

"start_closed": []
"end_closed": []

This range returns all users whose UserName begins with any character from A to C:

KeySet

KeySet defines a collection of Cloud Spanner keys and/or key ranges. All the keys are expected to be in the same table or index. The keys need not be sorted in any particular way.

If the same key is specified multiple times in the set (for example if two ranges, two keys, or a key and a range overlap), Cloud Spanner behaves as if the key were only specified once.

Properties

Parameter

keys

Array of Object

A list of specific keys. Entries in keys should have exactly as many elements as there are columns in the primary or index key with which this KeySet is used. Individual key values are encoded as described
here.

Like insert, except that if the row already exists, it is deleted, and the column values provided are inserted instead. Unlike insert_or_update, this means any values not explicitly written become NULL.

Parameter

A streamed result set consists of a stream of values, which might be split into many PartialResultSet messages to accommodate large rows and/or large values. Every N complete values defines a row, where N is equal
to the number of entries in metadata.row_type.fields.

Most values are encoded based on type as described here.

It is possible that the last value in values is "chunked", meaning that the rest of the value is sent in subsequent
PartialResultSet(s). This is denoted by the chunked_value field. Two or more chunked values can be merged to form a complete value as follows:

bool/number/null: cannot be chunked
string: concatenate the strings
list: concatenate the lists. If the last element in a list is a
string, list, or object, merge it with the first element in
the next list by applying these rules recursively.
object: concatenate the (field name, field value) pairs. If a
field name is duplicated, then apply these rules recursively
to merge the field values.

If true, then the final value in values is chunked, and must be combined with more values from subsequent PartialResultSets to obtain a complete field value.

resumeToken

string

Streaming calls might be interrupted for a variety of reasons, such as TCP connection loss. If this occurs, the stream of results can be resumed by re-sending the original request and including
resume_token. Note that executing any other transaction in the same session invalidates the token.

stats

Object

Query plan and execution statistics for the query that produced this streaming result set. These can be requested by setting ExecuteSqlRequest.query_mode and are sent only once with the last response in the stream.

PartitionOptions

static

Options for a PartitionQueryRequest and PartitionReadRequest.

Properties

Parameter

partitionSizeBytes

number

The desired data size for each partition generated. The default for this option is currently 1 GiB. This is only a hint. The actual size of each partition may be smaller or larger than this size request.

maxPartitions

number

The desired maximum number of partitions to return. For example, this may be set to the number of workers available. The default for this option is currently 10,000. The maximum value is currently 200,000. This is only a hint.
The actual number of partitions returned may be smaller or larger than this maximum count request.

The query request to generate partitions for. The request will fail if the query is not root partitionable. The query plan of a root partitionable query has a single distributed union operator. A distributed union operator conceptually
divides one or more tables into multiple splits, remotely evaluates a subquery independently on each split, and then unions all results.

params

Object

The SQL query string can contain parameter placeholders. A parameter placeholder consists of '@' followed by the parameter name. Parameter names consist of any combination of letters, numbers, and underscores.

Parameters can appear anywhere that a literal value is expected. The same parameter name can be used more than once, for example:
"WHERE id > @msg_id AND id < @msg_id + 100"

It is an error to execute an SQL query with unbound parameters.

Parameter values are specified using params, which is a JSON object whose keys are parameter names, and whose values are the corresponding parameter values.

If non-empty, the name of an index on table. This index is used instead of the table primary key when interpreting key_set and sorting result rows. See key_set for further information.

columns

Array of string

The columns of table to be returned for each row matching this request.

keySet

Object

Required. key_set identifies the rows to be yielded. key_set names the primary keys of the rows in table to be yielded, unless index is present. If index is present, then key_set instead names index keys
in index.

It is not an error for the key_set to name rows that do not exist in the database. Read yields nothing for nonexistent rows.

PlanNode

static

Node information for nodes appearing in a QueryPlan.plan_nodes.

Properties

Parameter

index

number

The PlanNode's index in node list.

kind

number

Used to determine the type of node. May be needed for visualizing different kinds of nodes differently. For example, If the node is a SCALAR node, it will have a condensed representation which can be used to directly embed a description
of the node in its parent.

The execution statistics associated with the node, contained in a group of key-value pairs. Only present if the plan was returned as a result of a profile query. For example, number of executions, number of rows/time per execution
etc.

Read data at a timestamp >= NOW - max_staleness seconds. Guarantees that all writes that have committed more than the specified number of seconds ago are visible. Because Cloud Spanner chooses the exact timestamp,
this mode works even if the client's local clock is substantially skewed from Cloud Spanner commit timestamps.

Useful for reading the freshest data available at a nearby replica, while bounding the possible staleness if the local replica has fallen behind.

Executes all reads at the given timestamp. Unlike other modes, reads at a specific timestamp are repeatable; the same read at the same timestamp always returns the same data. If the timestamp is in the future, the read will block
until the specified timestamp, modulo the read's deadline.

Useful for large scale consistent reads such as mapreduces, or for coordinating many reads against a consistent snapshot of the data.

Executes all reads at a timestamp that is exact_staleness old. The timestamp is chosen soon after the read is started.

Guarantees that all writes that have committed more than the specified number of seconds ago are visible. Because Cloud Spanner chooses the exact timestamp, this mode works even if the client's local clock is substantially skewed
from Cloud Spanner commit timestamps.

Useful for reading at nearby replicas without the distributed timestamp negotiation overhead of max_staleness.

If non-empty, the name of an index on table. This index is used instead of the table primary key when interpreting key_set and sorting result rows. See key_set for further information.

columns

Array of string

The columns of table to be returned for each row matching this request.

keySet

Object

Required. key_set identifies the rows to be yielded. key_set names the primary keys of the rows in table to be yielded, unless index is present. If index is present, then key_set instead names index keys
in index.

If the partition_token field is empty, rows are yielded in table primary key order (if index is empty) or index key order (if index is non-empty). If the partition_token field is not empty, rows will be yielded in an unspecified
order.

It is not an error for the key_set to name rows that do not exist in the database. Read yields nothing for nonexistent rows.

If greater than zero, only the first limit rows are yielded. If limit is zero, the default is no limit. A limit cannot be specified if
partition_token is set.

resumeToken

string

If this request is resuming a previously interrupted read,
resume_token should be copied from the last PartialResultSet yielded before the interruption. Doing this enables the new read to resume where the last read left off. The rest of the request parameters must exactly
match the request that yielded this token.

partitionToken

string

If present, results will be restricted to the specified partition previously created using PartitionRead(). There must be an exact match for the values of fields common to this message and the PartitionReadRequest message used
to create this partition_token.

Properties

Parameter

Each element in rows is a row whose format is defined by metadata.row_type. The ith element in each row matches the ith field in metadata.row_type. Elements are encoded based on type as described here.

ShortRepresentation

Condensed representation of a node and its subtree. Only present for
SCALAR PlanNode(s).

Properties

Parameter

description

string

A string representation of the expression subtree rooted at this node.

subqueries

Object with number properties

A mapping of (subquery variable name) -> (subquery node id) for cases where the description string of this node references a SCALAR subquery contained in the expression subtree rooted at this node.
The referenced SCALAR subquery may not necessarily be a direct child of this node.

TransactionOptions

static

Transactions

Each session can have at most one active transaction at a time. After the active transaction is completed, the session can immediately be re-used for the next transaction. It is not necessary to create a new session for each transaction.

Transaction Modes

Cloud Spanner supports two transaction modes:

Locking read-write. This type of transaction is the only way to write data into Cloud Spanner. These transactions rely on pessimistic locking and, if necessary, two-phase commit. Locking read-write transactions may abort, requiring the
application to retry.

Snapshot read-only. This transaction type provides guaranteed consistency across several reads, but does not allow writes. Snapshot read-only transactions can be configured to read at timestamps in the past. Snapshot read-only transactions
do not need to be committed.

For transactions that only read, snapshot read-only transactions provide simpler semantics and are almost always faster. In particular, read-only transactions do not take locks, so they do not conflict with read-write transactions. As a
consequence of not taking locks, they also do not abort, so retry loops are not needed.

Transactions may only read/write data in a single database. They may, however, read/write data in different tables within that database.

Locking Read-Write Transactions

Locking transactions may be used to atomically read-modify-write data anywhere in a database. This type of transaction is externally consistent.

Clients should attempt to minimize the amount of time a transaction is active. Faster transactions commit with higher probability and cause less contention. Cloud Spanner attempts to keep read locks active as long as the transaction continues
to do reads, and the transaction has not been terminated by Commit or Rollback. Long periods of inactivity at the client may cause Cloud Spanner to release a transaction's locks and abort it.

Reads performed within a transaction acquire locks on the data being read. Writes can only be done at commit time, after all reads have been completed. Conceptually, a read-write transaction consists of zero or more reads or SQL queries
followed by Commit. At any time before Commit, the client can send a Rollback request to abort the transaction.

Semantics

Cloud Spanner can commit the transaction if all read locks it acquired are still valid at commit time, and it is able to acquire write locks for all writes. Cloud Spanner can abort the transaction for any reason. If a commit attempt returns
ABORTED, Cloud Spanner guarantees that the transaction has not modified any user data in Cloud Spanner.

Unless the transaction commits, Cloud Spanner makes no guarantees about how long the transaction's locks were held for. It is an error to use Cloud Spanner locks for any sort of mutual exclusion other than between Cloud Spanner transactions
themselves.

Retrying Aborted Transactions

When a transaction aborts, the application can choose to retry the whole transaction again. To maximize the chances of successfully committing the retry, the client should execute the retry in the same session as the original attempt. The
original session's lock priority increases with each consecutive abort, meaning that each attempt has a slightly better chance of success than the previous.

Under some circumstances (e.g., many transactions attempting to modify the same row(s)), a transaction can abort many times in a short period before successfully committing. Thus, it is not a good idea to cap the number of retries a transaction
can attempt; instead, it is better to limit the total amount of wall time spent retrying.

Idle Transactions

A transaction is considered idle if it has no outstanding reads or SQL queries and has not started a read or SQL query within the last 10 seconds. Idle transactions can be aborted by Cloud Spanner so that they don't hold on to locks indefinitely.
In that case, the commit will fail with error ABORTED.

If this behavior is undesirable, periodically executing a simple SQL query in the transaction (e.g., SELECT 1) prevents the transaction from becoming idle.

Snapshot Read-Only Transactions

Snapshot read-only transactions provides a simpler method than locking read-write transactions for doing several consistent reads. However, this type of transaction does not support writes.

Snapshot transactions do not take locks. Instead, they work by choosing a Cloud Spanner timestamp, then executing all reads at that timestamp. Since they do not acquire locks, they do not block concurrent read-write transactions.

Unlike locking read-write transactions, snapshot read-only transactions never abort. They can fail if the chosen read timestamp is garbage collected; however, the default garbage collection policy is generous enough that most applications
do not need to worry about this in practice.

Snapshot read-only transactions do not need to call Commit or Rollback (and in fact are not permitted to do so).

To execute a snapshot transaction, the client specifies a timestamp bound, which tells Cloud Spanner how to choose a read timestamp.

The types of timestamp bound are:

Strong (the default).

Bounded staleness.

Exact staleness.

If the Cloud Spanner database to be read is geographically distributed, stale read-only transactions can execute more quickly than strong or read-write transaction, because they are able to execute far from the leader replica.

Each type of timestamp bound is discussed in detail below.

Strong

Strong reads are guaranteed to see the effects of all transactions that have committed before the start of the read. Furthermore, all rows yielded by a single read are consistent with each other -- if any part of the read observes a transaction,
all parts of the read see the transaction.

Strong reads are not repeatable: two consecutive strong read-only transactions might return inconsistent results if there are concurrent writes. If consistency across reads is required, the reads should be executed within a transaction or
at an exact read timestamp.

See TransactionOptions.ReadOnly.strong.

Exact Staleness

These timestamp bounds execute reads at a user-specified timestamp. Reads at a timestamp are guaranteed to see a consistent prefix of the global transaction history: they observe modifications done by all transactions with a commit timestamp
<= the read timestamp, and observe none of the modifications done by transactions with a larger commit timestamp. They will block until all conflicting transactions that may be assigned commit timestamps <= the read timestamp have
finished.

The timestamp can either be expressed as an absolute Cloud Spanner commit timestamp or a staleness relative to the current time.

These modes do not require a "negotiation phase" to pick a timestamp. As a result, they execute slightly faster than the equivalent boundedly stale concurrency modes. On the other hand, boundedly stale reads usually return fresher
results.

See TransactionOptions.ReadOnly.read_timestamp and TransactionOptions.ReadOnly.exact_staleness.

Bounded Staleness

Bounded staleness modes allow Cloud Spanner to pick the read timestamp, subject to a user-provided staleness bound. Cloud Spanner chooses the newest timestamp within the staleness bound that allows execution of the reads at the closest available
replica without blocking.

All rows yielded are consistent with each other -- if any part of the read observes a transaction, all parts of the read see the transaction. Boundedly stale reads are not repeatable: two stale reads, even if they use the same staleness
bound, can execute at different timestamps and thus return inconsistent results.

Boundedly stale reads execute in two phases: the first phase negotiates a timestamp among all replicas needed to serve the read. In the second phase, reads are executed at the negotiated timestamp.

As a result of the two phase execution, bounded staleness reads are usually a little slower than comparable exact staleness reads. However, they are typically able to return fresher results, and are more likely to execute at the closest
replica.

Because the timestamp negotiation requires up-front knowledge of which rows will be read, it can only be used with single-use read-only transactions.

See TransactionOptions.ReadOnly.max_staleness and TransactionOptions.ReadOnly.min_read_timestamp.

Old Read Timestamps and Garbage Collection

Cloud Spanner continuously garbage collects deleted and overwritten data in the background to reclaim storage space. This process is known as "version GC". By default, version GC reclaims versions after they are one hour old. Because
of this, Cloud Spanner cannot perform reads at read timestamps more than one hour in the past. This restriction also applies to in-progress reads and/or SQL queries whose timestamp become too old while executing. Reads and SQL queries
with too-old read timestamps fail with the error FAILED_PRECONDITION.

Properties

Parameter

readWrite

Object

Transaction may write.

Authorization to begin a read-write transaction requires
spanner.databases.beginOrRollbackReadWriteTransaction permission on the session resource.

Properties

Parameter

The list of columns must contain enough columns to allow Cloud Spanner to derive values for all primary key columns in the row(s) to be modified.

values

Array of Object

The values to be written. values can contain more than one list of values. If it does, then multiple rows are written, one for each entry in values. Each list in values must have exactly as
many entries as there are entries in columns above. Sending multiple lists is equivalent to sending multiple
Mutations, each containing one values entry and repeating table and columns. Individual values in each list are encoded as described here.