Documentation Index
Fetch the complete documentation index at: https://docs.canton.network/llms.txt
Use this file to discover all available pages before exploring further.
How to query contracts and transactions using SQL
Query
How to query contracts that are in Active Contract Set (ACS)
To fetch contracts in the ACS use theactive() table function1 which is part of Participant Query Store’s (PQS) schema and can be used as any other PostgreSQL function.
How to query contracts that are in ACS by type
The previous example is very resource-consuming and unadvisable to use except on tiny datasets. A more typical (and efficient at the same time) use case is to limit the result set to a specific Daml template type. The reason for improved efficiency is PostgreSQL’s ability to prune unused partitions when planning the query execution. As long as there is no ambiguity in resolving the type, the following forms are equivalent.How to query contracts that are in ACS by information in payload
To query contracts by business information in the payload, use JSONB operators2 in theWHERE clause.
See also
pqs-how-to-add-an-index-on-expression-over-jsonb-payload.How to join contracts that are in ACS
To join contracts from multiple Daml template types use standardJOIN syntax.
How to aggregate data from contract values
Since PQS is backed by PostgreSQL database, one can use aggregate functions available as part of Structured Query Language (SQL) syntax.How to query state in the past
PQS uses Event Sourcing3 architecture and therefore users can peek into the state at an arbitrary point in ledger history by providing an offset explicitly.How to fetch a contract by its contract ID
Contracts may be fetched by their contract ID (regardless of their activeness state). Keep in mind, that interface views and contracts share the same contract ID so the function will return interface views as well.How to get a list of contracts created in an offset range
To get a sorted list of “contract created” events within a specific offset range.This query is potentially resource-heavy in the presence of multiple template types and is a poor substitute for Ledger API transaction stream.
How to get a list of contracts created in an open offset range
By default, offset edges increates() and archives() SQL functions represent closed ranges. To make either or both open, use WHERE clauses with desired refinements.
How to emulate a stream of “contract created” events with SQL
Although PQS is a poor substitute for Ledger API stream of events, it is sometimes justifiable to get a stream of “contract created” events directly from a PQS database. To emulate such a stream, one needs to implement client-side busy polling of the database using a similar query.Optimize
PQS is backed by PostgreSQL database. Any query optimization is essentially a database/SQL tuning concern. The usual SQL development best practices apply here:- Apply indexes for frequently used clauses
- Limit data fetching (remove unnecessary columns in final projection, avoid using
SELECT *) - Design Daml models to be read-friendly
- Avoid paginating with the help of
OFFSETSQL clause - Avoid using queries similar to
SELECT COUNT(*)if possible
pqs-optimize.
How to add an index on expression over JSONB payload
To optimize read performance of queries that useWHERE clauses with information from payload add indexes on expression5.
WHERE clauses (including operators and casts).
How to limit data fetching
List only columns with data required and avoid using*.
Most Read API functions return an abundance of data in the result set, including contract’s liveness, divulgence and disclosure metadata. Fetching this data involves joining several internal tables. However, if metadata is not required, the query will cause unnecessary burden on PostgreSQL resources. On the other hand, the query planner in PostgreSQL is smart to prune automatically unnecessary joins6.
Bad:
How to paginate efficiently
Make sure your paginated access7 to data is efficient and does not cause performance degradation. Bad:Footnotes
- https://www.postgresql.org/docs/current/xfunc-sql.html#XFUNC-SQL-TABLE-FUNCTIONS ↩
- https://www.postgresql.org/docs/current/functions-json.html ↩
- https://learn.microsoft.com/en-us/azure/architecture/patterns/event-sourcing ↩
- https://en.wikipedia.org/wiki/Memoization ↩
- https://www.postgresql.org/docs/current/indexes-expressional.html ↩
- https://www.oreilly.com/library/view/mastering-postgresql-12/9781838988821/736b4431-b85e-4879-9a93-e5133b42db1f.xhtml ↩
- https://www.postgresql.org/docs/current/queries-limit.html ↩