Skip to main content
A commit timestamp represents the time (in nanoseconds) when a mutation commits. The value is not observable within the mutation, but with the db.vars.commitTs placeholder you can have the value inserted at commit time into documents or the return value. This can be used to to model a queue, or to implement an updatedAt field. You could define an index on a field that captures the commit time, then use that to iterate over new or updated documents without worrying about missing changes due to out-of-order commits. For tips on using CommitTs for efficient iteration, see the notes in the Batch Worker Component README.
db.vars.commitTs is a placeholder value while the mutation executes. When the mutation commits, the value gets resolved to the commit timestamp in nanoseconds.

Commit timestamp vs. _creationTime

Note that the commit timestamp is not the same as the _creationTime system field. Mutations execute concurrently before committing, so _creationTime does not necessarily reflect the order in which the mutations committed. This mean you can be reading the last committed document in a table, ordered by _creationTime, and then later a new document could be inserted with an earlier _creationTime. This can lead to missing documents when iterating over a table. With an index on db.vars.commitTs, you do not need to worry about this, as the timestamp is strictly increasing. Since inserted documents will always have a greater commit timestamp, the tombstones will always end where the new documents begin.

Using the commit timestamp

During function execution, new documents inserted with db.vars.commitTs will have the CommitTsPlaceholder. Resolved commit timestamps are Int64s (bigint in JS). You can use the v.commitTs() validator in argument and return validators and schema definitions. It accepts both resolved Int64 values and the commit timestamp placeholder. Fields with the commit timestamp are otherwise like any other field: they can be used in indexes, nested objects, arrays, and unions.
You can check for the placeholder before using the commit timestamp type.
You can query for documents inserted in the current transaction using a commit timestamp index. This can be useful inside components or nested function where you may not have context from the parent transaction.
The commit timestamp placeholder can be passed into or returned from a nested function.
Note that it is not possible to schedule a function with the commit timestamp as an argument. Furthermore, you cannot return the commit timestamp placeholder from a query, as queries are not committed.