Skip to content

Commit 79b02b7

Browse files
docs: Add docs for how to set runtime parameters
1 parent d150b59 commit 79b02b7

File tree

2 files changed

+154
-1
lines changed

2 files changed

+154
-1
lines changed

docs/06-concepts/06-database/06-filter.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ var filteredSimilarity = await Document.db.find(
249249
```
250250

251251
:::tip
252-
For optimal performance with vector similarity searches, consider creating specialized vector indexes (HNSW or IVFFLAT) on your vector fields. See the [Vector indexes](indexing#vector-indexes) section for more details.
252+
For optimal performance with vector similarity searches, consider creating specialized vector indexes (HNSW or IVFFLAT) on your vector fields. See the [Vector indexes](indexing#vector-indexes) section for more details. To fine-tune query execution, you can also set appropriate [runtime parameters](runtime-parameters) for vector queries.
253253
:::
254254

255255
## Relation operations
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Runtime parameters
2+
3+
Runtime parameters in Serverpod allow you to fine-tune the behavior of the database engine for specific queries or workloads. This can significantly improve performance, especially for complex queries or large datasets.
4+
5+
:::warning
6+
Setting runtime parameters affects PostgreSQL's query planning and execution. Always test different parameter combinations with your specific dataset and query patterns to find the optimal configuration.
7+
:::
8+
9+
## Parameter scopes
10+
11+
Runtime parameters can be applied with different scopes:
12+
13+
- **Global scope** (default): Parameters affect all subsequent queries in the session.
14+
- **Local scope**: Parameters only affect the current transaction.
15+
16+
```dart
17+
var hnswOptions = HnswIndexQueryOptions(efSearch: 40);
18+
19+
// Global scope - affects all queries in this session
20+
await session.db.setRuntimeParameters([hnswOptions]);
21+
22+
// Local scope - only affects current transaction
23+
await session.db.transaction((transaction) async {
24+
await session.db.setRuntimeParameters([hnswOptions], transaction: transaction);
25+
26+
var results = await Document.db.find(
27+
session,
28+
where: (t) => t.embedding.distanceCosine(queryVector) < 0.5,
29+
limit: 10,
30+
orderBy: (t) => t.embedding.distanceCosine(queryVector),
31+
transaction: transaction,
32+
);
33+
34+
return results;
35+
});
36+
```
37+
38+
## Combining multiple parameter groups
39+
40+
You can combine different runtime parameter groups for comprehensive optimization:
41+
42+
```dart
43+
// Create multiple parameter groups
44+
var hnswOptions = HnswIndexQueryOptions(efSearch: 64);
45+
var vectorOptions = VectorIndexQueryOptions(enableIndexScan: true);
46+
47+
// Apply all parameter groups (globally or locally)
48+
await session.db.setRuntimeParameters([vectorOptions, hnswOptions]);
49+
```
50+
51+
## Vector runtime parameters
52+
53+
When working with vector similarity searches, you can fine-tune query performance by setting runtime parameters that control the behavior of vector indexes.
54+
55+
:::info
56+
Runtime parameters are particularly useful when you need to balance between query speed and result quality. For real-time applications, you might prefer faster but less accurate results, while for analytical workloads, you might prioritize accuracy over speed.
57+
:::
58+
59+
### General vector query optimization
60+
61+
Use `VectorIndexQueryOptions` to control general PostgreSQL settings that affect vector query performance:
62+
63+
```dart
64+
// Configure general vector query parameters
65+
var vectorOptions = VectorIndexQueryOptions(
66+
enableIndexScan: true, // Enable/disable index scans
67+
enableSeqScan: false, // Enable/disable sequential scans
68+
minParallelTableScanSize: 1024, // Min number of 8KB chunks to scan before parallelizing scans
69+
parallelSetupCost: 1000, // Estimated cost of launching parallel worker processes
70+
maintenanceWorkMem: 65536, // Memory in KB for operations such as index creation
71+
maxParallelMaintenanceWorkers: 2, // Increase to speed up index creation on large tables
72+
maxParallelWorkersPerGather: 2, // Increase to speed up queries without an index
73+
);
74+
75+
// Apply runtime parameters to the session
76+
await session.db.setRuntimeParameters([vectorOptions]);
77+
78+
// Perform vector search with optimized PostgreSQL settings
79+
var results = await Document.db.find(
80+
session,
81+
where: (t) => t.embedding.distanceCosine(queryVector) < 0.7,
82+
orderBy: (t) => t.embedding.distanceCosine(queryVector),
83+
limit: 50,
84+
);
85+
```
86+
87+
### HNSW index optimization
88+
89+
For queries using HNSW indexes, use `HnswIndexQueryOptions` to control search behavior:
90+
91+
```dart
92+
import 'package:serverpod/serverpod.dart';
93+
94+
// Configure HNSW runtime parameters
95+
var hnswOptions = HnswIndexQueryOptions(
96+
efSearch: 40, // Higher values = better recall, slower search
97+
iterativeScan: IterativeScan.relaxed, // Relaxed scan for better recall
98+
maxScanTuples: 20000, // Limit number of tuples to scan
99+
scanMemMultiplier: 2, // Memory multiplier for scanning
100+
);
101+
102+
// Apply runtime parameters to the session before performing the query
103+
await session.db.setRuntimeParameters([hnswOptions]);
104+
```
105+
106+
### IVFFLAT index optimization
107+
108+
For queries using IVFFLAT indexes, use `IvfFlatIndexQueryOptions` to control search behavior:
109+
110+
```dart
111+
// Configure IVFFLAT runtime parameters
112+
var ivfFlatOptions = IvfFlatIndexQueryOptions(
113+
probes: 10, // Number of probes to search (higher = better recall)
114+
iterativeScan: IterativeScan.strict, // Strict scan for exact order by distance
115+
maxProbes: 20, // Maximum number of probes to use
116+
);
117+
118+
// Apply runtime parameters to the session before performing the query
119+
await session.db.setRuntimeParameters([ivfFlatOptions]);
120+
```
121+
122+
### Iterative scan modes
123+
124+
The `IterativeScan` enum controls how vector indexes handle scanning:
125+
126+
- **`IterativeScan.strict`**: Ensures results are in exact order by distance (better precision).
127+
- **`IterativeScan.relaxed`**: Allows slightly out-of-order results but provides better recall.
128+
129+
## Setting global runtime parameters on startup
130+
131+
To set global runtime parameters, add a call to `setRuntimeParameters` right at the start of the server, inside the `run` function of `lib/server.dart` module.
132+
133+
```dart
134+
// Add this import to expose the `internalSession` getter on `pod` variable.
135+
import 'package:serverpod/src/server/serverpod.dart';
136+
// ...
137+
138+
void run(List<String> args) async {
139+
// Initialize Serverpod and connect it with your generated code.
140+
final pod = Serverpod(
141+
args,
142+
Protocol(),
143+
Endpoints(),
144+
);
145+
146+
// ...
147+
// Set global runtime parameters for the session
148+
await pod.internalSession.db.setRuntimeParameters([
149+
VectorIndexQueryOptions(enableIndexScan: true),
150+
HnswIndexQueryOptions(efSearch: 64),
151+
]);
152+
}
153+
```

0 commit comments

Comments
 (0)