Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions docs-main/sdks-tools/development-tools/pqs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import DamlDocsSdksToolsDevelopmentToolsPqsL62 from "/snippets/daml-docs/sdks-to
import DamlDocsSdksToolsDevelopmentToolsPqsL73 from "/snippets/daml-docs/sdks-tools_development-tools_pqs_L73.mdx";
import DamlDocsSdksToolsDevelopmentToolsPqsL85 from "/snippets/daml-docs/sdks-tools_development-tools_pqs_L85.mdx";
import DamlDocsSdksToolsDevelopmentToolsPqsL96 from "/snippets/daml-docs/sdks-tools_development-tools_pqs_L96.mdx";
import DamlDocsSdksToolsDevelopmentToolsPqsL108 from "/snippets/daml-docs/sdks-tools_development-tools_pqs_L108.mdx";


PQS (Participant Query Store) subscribes to a validator's transaction stream and projects contract data into a PostgreSQL database. Your backend queries this database with standard SQL, enabling: filtered queries, aggregations, joins, and full-text search that would be impractical through the Ledger API alone.
Expand Down Expand Up @@ -294,15 +293,17 @@ PQS is tested for compatibility with multiple versions of dependencies, as follo

## SQL Query Examples

These examples use the PQS SQL API — a set of provisioned functions that are the only database artifacts applications should query. See the [PQS SQL API reference](/appdev/reference/pqs-sql-reference) for the full function list and returned columns.

### Active Contracts

Query all active contracts for a specific template:

<DamlDocsSdksToolsDevelopmentToolsPqsL62 />

### Transaction History
### Recent Create Events

View recent transactions for a party:
View recent create events for a party:

<DamlDocsSdksToolsDevelopmentToolsPqsL73 />

Expand All @@ -314,27 +315,23 @@ Count active contracts by template:

### Party Filtering

Find all contracts visible to a specific party:
Find contracts where a specific party is a signatory:

<DamlDocsSdksToolsDevelopmentToolsPqsL96 />

## Performance Optimization

### Indexes

Add indexes on columns you query frequently. PQS creates basic indexes on startup, but your application may benefit from additional ones:

<DamlDocsSdksToolsDevelopmentToolsPqsL108 />
To speed up payload filters, create JSONB indexes with the supported `create_index_for_contract` procedure ([JSONB indexing](/appdev/reference/pqs-sql-reference#jsonb-indexing));
for host and PostgreSQL tuning, see [Optimize](/sdks-tools/development-tools/pqs/optimize).

### Connection Pooling

Use a connection pool (like HikariCP for Java or `pg-pool` for Node.js) between your backend and the PQS database. PQS itself maintains a separate connection to PostgreSQL for writes.

### Query Patterns

- Avoid `SELECT *` on large tables; specify the columns you need
- Use `template_id` filters to narrow the dataset before applying payload filters
- For time-range queries, add indexes on `effective_at` or `created_at` columns
- Avoid `SELECT *` on large result sets; specify the columns you need
- Use the `name` argument of the table functions (for example `active('Licensing:License')`) to narrow the dataset before applying payload filters

## High Availability and Database Sharing

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
```sql
SELECT *
FROM active_contracts
WHERE template_id = 'Licensing:License'
AND payload->>'owner' = 'Alice::1234...';
SELECT contract_id, payload
FROM active('Licensing:License')
WHERE payload->>'owner' = 'Alice::1234...';
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
```sql
SELECT event_id, template_id, event_type, effective_at
FROM events
WHERE witness_parties @> ARRAY['Alice::1234...']
ORDER BY effective_at DESC
SELECT contract_id, template_fqn, created_effective_at
FROM creates()
WHERE witnesses @> ARRAY['Alice::1234...']
ORDER BY created_effective_at DESC
LIMIT 50;
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
```sql
SELECT template_id, COUNT(*) as contract_count
FROM active_contracts
GROUP BY template_id
ORDER BY contract_count DESC;
SELECT template_fqn, count
FROM summary_active()
ORDER BY count DESC;
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```sql
SELECT contract_id, template_id, payload
FROM active_contracts
WHERE stakeholders @> ARRAY['Alice::1234...'];
SELECT contract_id, template_fqn, payload
FROM active()
WHERE signatories @> ARRAY['Alice::1234...'];
```