@mastra/pg 1.6.1 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +74 -0
- package/dist/docs/SKILL.md +40 -0
- package/dist/docs/assets/SOURCE_MAP.json +6 -0
- package/dist/docs/references/docs-memory-semantic-recall.md +272 -0
- package/dist/docs/references/docs-memory-storage.md +261 -0
- package/dist/docs/references/docs-memory-working-memory.md +400 -0
- package/dist/docs/references/docs-rag-overview.md +72 -0
- package/dist/docs/references/docs-rag-retrieval.md +515 -0
- package/dist/docs/references/docs-rag-vector-databases.md +645 -0
- package/dist/docs/references/reference-memory-memory-class.md +147 -0
- package/dist/docs/references/reference-processors-message-history-processor.md +85 -0
- package/dist/docs/references/reference-processors-semantic-recall-processor.md +117 -0
- package/dist/docs/references/reference-processors-working-memory-processor.md +152 -0
- package/dist/docs/references/reference-rag-metadata-filters.md +216 -0
- package/dist/docs/references/reference-storage-composite.md +235 -0
- package/dist/docs/references/reference-storage-dynamodb.md +282 -0
- package/dist/docs/references/reference-storage-postgresql.md +526 -0
- package/dist/docs/references/reference-tools-vector-query-tool.md +459 -0
- package/dist/docs/references/reference-vectors-pg.md +408 -0
- package/dist/index.cjs +62 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +62 -5
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts.map +1 -1
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
# PG Vector Store
|
|
2
|
+
|
|
3
|
+
The PgVector class provides vector search using [PostgreSQL](https://www.postgresql.org/) with [pgvector](https://github.com/pgvector/pgvector) extension. It provides robust vector similarity search capabilities within your existing PostgreSQL database.
|
|
4
|
+
|
|
5
|
+
## Constructor Options
|
|
6
|
+
|
|
7
|
+
**connectionString?:** (`string`): PostgreSQL connection URL
|
|
8
|
+
|
|
9
|
+
**host?:** (`string`): PostgreSQL server host
|
|
10
|
+
|
|
11
|
+
**port?:** (`number`): PostgreSQL server port
|
|
12
|
+
|
|
13
|
+
**database?:** (`string`): PostgreSQL database name
|
|
14
|
+
|
|
15
|
+
**user?:** (`string`): PostgreSQL user
|
|
16
|
+
|
|
17
|
+
**password?:** (`string`): PostgreSQL password
|
|
18
|
+
|
|
19
|
+
**ssl?:** (`boolean | ConnectionOptions`): Enable SSL or provide custom SSL configuration
|
|
20
|
+
|
|
21
|
+
**schemaName?:** (`string`): The name of the schema you want the vector store to use. Will use the default schema if not provided.
|
|
22
|
+
|
|
23
|
+
**max?:** (`number`): Maximum number of pool connections (default: 20)
|
|
24
|
+
|
|
25
|
+
**idleTimeoutMillis?:** (`number`): Idle connection timeout in milliseconds (default: 30000)
|
|
26
|
+
|
|
27
|
+
**pgPoolOptions?:** (`PoolConfig`): Additional pg pool configuration options
|
|
28
|
+
|
|
29
|
+
## Constructor Examples
|
|
30
|
+
|
|
31
|
+
### Connection String
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { PgVector } from '@mastra/pg'
|
|
35
|
+
|
|
36
|
+
const vectorStore = new PgVector({
|
|
37
|
+
id: 'pg-vector',
|
|
38
|
+
connectionString: 'postgresql://user:password@localhost:5432/mydb',
|
|
39
|
+
})
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Host/Port/Database Configuration
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const vectorStore = new PgVector({
|
|
46
|
+
id: 'pg-vector',
|
|
47
|
+
host: 'localhost',
|
|
48
|
+
port: 5432,
|
|
49
|
+
database: 'mydb',
|
|
50
|
+
user: 'postgres',
|
|
51
|
+
password: 'password',
|
|
52
|
+
})
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Advanced Configuration
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const vectorStore = new PgVector({
|
|
59
|
+
id: 'pg-vector',
|
|
60
|
+
connectionString: 'postgresql://user:password@localhost:5432/mydb',
|
|
61
|
+
schemaName: 'custom_schema',
|
|
62
|
+
max: 30,
|
|
63
|
+
idleTimeoutMillis: 60000,
|
|
64
|
+
pgPoolOptions: {
|
|
65
|
+
connectionTimeoutMillis: 5000,
|
|
66
|
+
allowExitOnIdle: true,
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Methods
|
|
72
|
+
|
|
73
|
+
### createIndex()
|
|
74
|
+
|
|
75
|
+
**indexName:** (`string`): Name of the index to create
|
|
76
|
+
|
|
77
|
+
**dimension:** (`number`): Vector dimension (must match your embedding model)
|
|
78
|
+
|
|
79
|
+
**metric?:** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search (Default: `cosine`)
|
|
80
|
+
|
|
81
|
+
**indexConfig?:** (`IndexConfig`): Index configuration (Default: `{ type: 'ivfflat' }`)
|
|
82
|
+
|
|
83
|
+
**buildIndex?:** (`boolean`): Whether to build the index (Default: `true`)
|
|
84
|
+
|
|
85
|
+
#### IndexConfig
|
|
86
|
+
|
|
87
|
+
**type:** (`'flat' | 'hnsw' | 'ivfflat'`): stringflat:flatSequential scan (no index) that performs exhaustive search.ivfflat:ivfflatClusters vectors into lists for approximate search.hnsw:hnswGraph-based index offering fast search times and high recall. (Default: `ivfflat`)
|
|
88
|
+
|
|
89
|
+
**ivf?:** (`IVFConfig`): objectlists?:numberNumber of lists. If not specified, automatically calculated based on dataset size. (Minimum 100, Maximum 4000)
|
|
90
|
+
|
|
91
|
+
**hnsw?:** (`HNSWConfig`): objectm?:numberMaximum number of connections per node (default: 8)efConstruction?:numberBuild-time complexity (default: 32)
|
|
92
|
+
|
|
93
|
+
#### Memory Requirements
|
|
94
|
+
|
|
95
|
+
HNSW indexes require significant shared memory during construction. For 100K vectors:
|
|
96
|
+
|
|
97
|
+
- Small dimensions (64d): \~60MB with default settings
|
|
98
|
+
- Medium dimensions (256d): \~180MB with default settings
|
|
99
|
+
- Large dimensions (384d+): \~250MB+ with default settings
|
|
100
|
+
|
|
101
|
+
Higher M values or efConstruction values will increase memory requirements significantly. Adjust your system's shared memory limits if needed.
|
|
102
|
+
|
|
103
|
+
### upsert()
|
|
104
|
+
|
|
105
|
+
**indexName:** (`string`): Name of the index to upsert vectors into
|
|
106
|
+
|
|
107
|
+
**vectors:** (`number[][]`): Array of embedding vectors
|
|
108
|
+
|
|
109
|
+
**metadata?:** (`Record<string, any>[]`): Metadata for each vector
|
|
110
|
+
|
|
111
|
+
**ids?:** (`string[]`): Optional vector IDs (auto-generated if not provided)
|
|
112
|
+
|
|
113
|
+
### query()
|
|
114
|
+
|
|
115
|
+
**indexName:** (`string`): Name of the index to query
|
|
116
|
+
|
|
117
|
+
**queryVector:** (`number[]`): Query vector
|
|
118
|
+
|
|
119
|
+
**topK?:** (`number`): Number of results to return (Default: `10`)
|
|
120
|
+
|
|
121
|
+
**filter?:** (`Record<string, any>`): Metadata filters
|
|
122
|
+
|
|
123
|
+
**includeVector?:** (`boolean`): Whether to include the vector in the result (Default: `false`)
|
|
124
|
+
|
|
125
|
+
**minScore?:** (`number`): Minimum similarity score threshold (Default: `0`)
|
|
126
|
+
|
|
127
|
+
**options?:** (`{ ef?: number; probes?: number }`): objectef?:numberHNSW search parameterprobes?:numberIVF search parameter
|
|
128
|
+
|
|
129
|
+
### listIndexes()
|
|
130
|
+
|
|
131
|
+
Returns an array of index names as strings.
|
|
132
|
+
|
|
133
|
+
### describeIndex()
|
|
134
|
+
|
|
135
|
+
**indexName:** (`string`): Name of the index to describe
|
|
136
|
+
|
|
137
|
+
Returns:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
interface PGIndexStats {
|
|
141
|
+
dimension: number
|
|
142
|
+
count: number
|
|
143
|
+
metric: 'cosine' | 'euclidean' | 'dotproduct'
|
|
144
|
+
type: 'flat' | 'hnsw' | 'ivfflat'
|
|
145
|
+
config: {
|
|
146
|
+
m?: number
|
|
147
|
+
efConstruction?: number
|
|
148
|
+
lists?: number
|
|
149
|
+
probes?: number
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### deleteIndex()
|
|
155
|
+
|
|
156
|
+
**indexName:** (`string`): Name of the index to delete
|
|
157
|
+
|
|
158
|
+
### updateVector()
|
|
159
|
+
|
|
160
|
+
Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
|
|
161
|
+
|
|
162
|
+
**indexName:** (`string`): Name of the index containing the vector
|
|
163
|
+
|
|
164
|
+
**id?:** (`string`): ID of the vector to update (mutually exclusive with filter)
|
|
165
|
+
|
|
166
|
+
**filter?:** (`Record<string, any>`): Metadata filter to identify vector(s) to update (mutually exclusive with id)
|
|
167
|
+
|
|
168
|
+
**update:** (`{ vector?: number[]; metadata?: Record<string, any>; }`): Object containing the vector and/or metadata to update
|
|
169
|
+
|
|
170
|
+
Updates an existing vector by ID or filter. At least one of vector or metadata must be provided in the update object.
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
// Update by ID
|
|
174
|
+
await pgVector.updateVector({
|
|
175
|
+
indexName: 'my_vectors',
|
|
176
|
+
id: 'vector123',
|
|
177
|
+
update: {
|
|
178
|
+
vector: [0.1, 0.2, 0.3],
|
|
179
|
+
metadata: { label: 'updated' },
|
|
180
|
+
},
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
// Update by filter
|
|
184
|
+
await pgVector.updateVector({
|
|
185
|
+
indexName: 'my_vectors',
|
|
186
|
+
filter: { category: 'product' },
|
|
187
|
+
update: {
|
|
188
|
+
metadata: { status: 'reviewed' },
|
|
189
|
+
},
|
|
190
|
+
})
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### deleteVector()
|
|
194
|
+
|
|
195
|
+
**indexName:** (`string`): Name of the index containing the vector
|
|
196
|
+
|
|
197
|
+
**id:** (`string`): ID of the vector to delete
|
|
198
|
+
|
|
199
|
+
Deletes a single vector by ID from the specified index.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
await pgVector.deleteVector({ indexName: 'my_vectors', id: 'vector123' })
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### deleteVectors()
|
|
206
|
+
|
|
207
|
+
Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
|
|
208
|
+
|
|
209
|
+
**indexName:** (`string`): Name of the index containing the vectors to delete
|
|
210
|
+
|
|
211
|
+
**ids?:** (`string[]`): Array of vector IDs to delete (mutually exclusive with filter)
|
|
212
|
+
|
|
213
|
+
**filter?:** (`Record<string, any>`): Metadata filter to identify vectors to delete (mutually exclusive with ids)
|
|
214
|
+
|
|
215
|
+
### disconnect()
|
|
216
|
+
|
|
217
|
+
Closes the database connection pool. Should be called when done using the store.
|
|
218
|
+
|
|
219
|
+
### buildIndex()
|
|
220
|
+
|
|
221
|
+
**indexName:** (`string`): Name of the index to define
|
|
222
|
+
|
|
223
|
+
**metric?:** (`'cosine' | 'euclidean' | 'dotproduct'`): Distance metric for similarity search (Default: `cosine`)
|
|
224
|
+
|
|
225
|
+
**indexConfig:** (`IndexConfig`): Configuration for the index type and parameters
|
|
226
|
+
|
|
227
|
+
Builds or rebuilds an index with specified metric and configuration. Will drop any existing index before creating the new one.
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
// Define HNSW index
|
|
231
|
+
await pgVector.buildIndex('my_vectors', 'cosine', {
|
|
232
|
+
type: 'hnsw',
|
|
233
|
+
hnsw: {
|
|
234
|
+
m: 8,
|
|
235
|
+
efConstruction: 32,
|
|
236
|
+
},
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
// Define IVF index
|
|
240
|
+
await pgVector.buildIndex('my_vectors', 'cosine', {
|
|
241
|
+
type: 'ivfflat',
|
|
242
|
+
ivf: {
|
|
243
|
+
lists: 100,
|
|
244
|
+
},
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
// Define flat index
|
|
248
|
+
await pgVector.buildIndex('my_vectors', 'cosine', {
|
|
249
|
+
type: 'flat',
|
|
250
|
+
})
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Response Types
|
|
254
|
+
|
|
255
|
+
Query results are returned in this format:
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
interface QueryResult {
|
|
259
|
+
id: string
|
|
260
|
+
score: number
|
|
261
|
+
metadata: Record<string, any>
|
|
262
|
+
vector?: number[] // Only included if includeVector is true
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Error Handling
|
|
267
|
+
|
|
268
|
+
The store throws typed errors that can be caught:
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
try {
|
|
272
|
+
await store.query({
|
|
273
|
+
indexName: 'index_name',
|
|
274
|
+
queryVector: queryVector,
|
|
275
|
+
})
|
|
276
|
+
} catch (error) {
|
|
277
|
+
if (error instanceof VectorStoreError) {
|
|
278
|
+
console.log(error.code) // 'connection_failed' | 'invalid_dimension' | etc
|
|
279
|
+
console.log(error.details) // Additional error context
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Index Configuration Guide
|
|
285
|
+
|
|
286
|
+
### Performance Optimization
|
|
287
|
+
|
|
288
|
+
#### IVFFlat Tuning
|
|
289
|
+
|
|
290
|
+
- **lists parameter**: Set to `sqrt(n) * 2` where n is the number of vectors
|
|
291
|
+
- More lists = better accuracy but slower build time
|
|
292
|
+
- Fewer lists = faster build but potentially lower accuracy
|
|
293
|
+
|
|
294
|
+
#### HNSW Tuning
|
|
295
|
+
|
|
296
|
+
- **m parameter**:
|
|
297
|
+
|
|
298
|
+
- 8-16: Moderate accuracy, lower memory
|
|
299
|
+
- 16-32: High accuracy, moderate memory
|
|
300
|
+
- 32-64: Very high accuracy, high memory
|
|
301
|
+
|
|
302
|
+
- **efConstruction**:
|
|
303
|
+
|
|
304
|
+
- 32-64: Fast build, good quality
|
|
305
|
+
- 64-128: Slower build, better quality
|
|
306
|
+
- 128-256: Slowest build, best quality
|
|
307
|
+
|
|
308
|
+
### Index Recreation Behavior
|
|
309
|
+
|
|
310
|
+
The system automatically detects configuration changes and only rebuilds indexes when necessary:
|
|
311
|
+
|
|
312
|
+
- Same configuration: Index is kept (no recreation)
|
|
313
|
+
- Changed configuration: Index is dropped and rebuilt
|
|
314
|
+
- This prevents the performance issues from unnecessary index recreations
|
|
315
|
+
|
|
316
|
+
## Best Practices
|
|
317
|
+
|
|
318
|
+
- Regularly evaluate your index configuration to ensure optimal performance.
|
|
319
|
+
- Adjust parameters like `lists` and `m` based on dataset size and query requirements.
|
|
320
|
+
- **Monitor index performance** using `describeIndex()` to track usage
|
|
321
|
+
- Rebuild indexes periodically to maintain efficiency, especially after significant data changes
|
|
322
|
+
|
|
323
|
+
## Direct Pool Access
|
|
324
|
+
|
|
325
|
+
The `PgVector` class exposes its underlying PostgreSQL connection pool as a public field:
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
pgVector.pool // instance of pg.Pool
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
This enables advanced usage such as running direct SQL queries, managing transactions, or monitoring pool state. When using the pool directly:
|
|
332
|
+
|
|
333
|
+
- You are responsible for releasing clients (`client.release()`) after use.
|
|
334
|
+
- The pool remains accessible after calling `disconnect()`, but new queries will fail.
|
|
335
|
+
- Direct access bypasses any validation or transaction logic provided by PgVector methods.
|
|
336
|
+
|
|
337
|
+
This design supports advanced use cases but requires careful resource management by the user.
|
|
338
|
+
|
|
339
|
+
## Usage Example
|
|
340
|
+
|
|
341
|
+
### Local embeddings with fastembed
|
|
342
|
+
|
|
343
|
+
Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords). This setup uses `@mastra/fastembed` to generate vector embeddings.
|
|
344
|
+
|
|
345
|
+
Install `fastembed` to get started:
|
|
346
|
+
|
|
347
|
+
**npm**:
|
|
348
|
+
|
|
349
|
+
```bash
|
|
350
|
+
npm install @mastra/fastembed@latest
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
**pnpm**:
|
|
354
|
+
|
|
355
|
+
```bash
|
|
356
|
+
pnpm add @mastra/fastembed@latest
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
**Yarn**:
|
|
360
|
+
|
|
361
|
+
```bash
|
|
362
|
+
yarn add @mastra/fastembed@latest
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
**Bun**:
|
|
366
|
+
|
|
367
|
+
```bash
|
|
368
|
+
bun add @mastra/fastembed@latest
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
Add the following to your agent:
|
|
372
|
+
|
|
373
|
+
```typescript
|
|
374
|
+
import { Memory } from '@mastra/memory'
|
|
375
|
+
import { Agent } from '@mastra/core/agent'
|
|
376
|
+
import { PostgresStore, PgVector } from '@mastra/pg'
|
|
377
|
+
import { fastembed } from '@mastra/fastembed'
|
|
378
|
+
|
|
379
|
+
export const pgAgent = new Agent({
|
|
380
|
+
id: 'pg-agent',
|
|
381
|
+
name: 'PG Agent',
|
|
382
|
+
instructions:
|
|
383
|
+
'You are an AI agent with the ability to automatically recall memories from previous interactions.',
|
|
384
|
+
model: 'openai/gpt-5.1',
|
|
385
|
+
memory: new Memory({
|
|
386
|
+
storage: new PostgresStore({
|
|
387
|
+
id: 'pg-agent-storage',
|
|
388
|
+
connectionString: process.env.DATABASE_URL!,
|
|
389
|
+
}),
|
|
390
|
+
vector: new PgVector({
|
|
391
|
+
id: 'pg-agent-vector',
|
|
392
|
+
connectionString: process.env.DATABASE_URL!,
|
|
393
|
+
}),
|
|
394
|
+
embedder: fastembed,
|
|
395
|
+
options: {
|
|
396
|
+
lastMessages: 10,
|
|
397
|
+
semanticRecall: {
|
|
398
|
+
topK: 3,
|
|
399
|
+
messageRange: 2,
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
}),
|
|
403
|
+
})
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
## Related
|
|
407
|
+
|
|
408
|
+
- [Metadata Filters](https://mastra.ai/reference/rag/metadata-filters)
|
package/dist/index.cjs
CHANGED
|
@@ -814,8 +814,12 @@ var PgVector = class extends vector.MastraVector {
|
|
|
814
814
|
}) {
|
|
815
815
|
try {
|
|
816
816
|
vector.validateTopK("PG", topK);
|
|
817
|
-
if (
|
|
818
|
-
|
|
817
|
+
if (queryVector !== void 0) {
|
|
818
|
+
if (!Array.isArray(queryVector) || !queryVector.every((x) => typeof x === "number" && Number.isFinite(x))) {
|
|
819
|
+
throw new Error("queryVector must be an array of finite numbers");
|
|
820
|
+
}
|
|
821
|
+
} else if (!filter || Object.keys(filter).length === 0) {
|
|
822
|
+
throw new Error("Either queryVector or filter must be provided");
|
|
819
823
|
}
|
|
820
824
|
} catch (error$1) {
|
|
821
825
|
const mastraError = new error.MastraError(
|
|
@@ -832,6 +836,46 @@ var PgVector = class extends vector.MastraVector {
|
|
|
832
836
|
this.logger?.trackException(mastraError);
|
|
833
837
|
throw mastraError;
|
|
834
838
|
}
|
|
839
|
+
if (queryVector === void 0) {
|
|
840
|
+
const client2 = await this.pool.connect();
|
|
841
|
+
try {
|
|
842
|
+
const translatedFilter = this.transformFilter(filter);
|
|
843
|
+
const { sql: filterQuery, values: filterValues } = buildDeleteFilterQuery(translatedFilter);
|
|
844
|
+
const { tableName } = this.getTableName(indexName);
|
|
845
|
+
const query = `
|
|
846
|
+
SELECT
|
|
847
|
+
vector_id as id,
|
|
848
|
+
metadata
|
|
849
|
+
${includeVector ? ", embedding" : ""}
|
|
850
|
+
FROM ${tableName}
|
|
851
|
+
${filterQuery}
|
|
852
|
+
ORDER BY vector_id
|
|
853
|
+
LIMIT $${filterValues.length + 1}`;
|
|
854
|
+
const result = await client2.query(query, [...filterValues, topK]);
|
|
855
|
+
return result.rows.map(({ id, metadata, embedding }) => ({
|
|
856
|
+
id,
|
|
857
|
+
score: 0,
|
|
858
|
+
metadata,
|
|
859
|
+
...includeVector && embedding && { vector: JSON.parse(embedding) }
|
|
860
|
+
}));
|
|
861
|
+
} catch (error$1) {
|
|
862
|
+
const mastraError = new error.MastraError(
|
|
863
|
+
{
|
|
864
|
+
id: storage.createVectorErrorId("PG", "QUERY", "FAILED"),
|
|
865
|
+
domain: error.ErrorDomain.MASTRA_VECTOR,
|
|
866
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
867
|
+
details: {
|
|
868
|
+
indexName
|
|
869
|
+
}
|
|
870
|
+
},
|
|
871
|
+
error$1
|
|
872
|
+
);
|
|
873
|
+
this.logger?.trackException(mastraError);
|
|
874
|
+
throw mastraError;
|
|
875
|
+
} finally {
|
|
876
|
+
client2.release();
|
|
877
|
+
}
|
|
878
|
+
}
|
|
835
879
|
const client = await this.pool.connect();
|
|
836
880
|
try {
|
|
837
881
|
await client.query("BEGIN");
|
|
@@ -2055,6 +2099,17 @@ function generateTableSQL({
|
|
|
2055
2099
|
ADD CONSTRAINT ${workflowSnapshotConstraint}
|
|
2056
2100
|
UNIQUE (workflow_name, run_id);
|
|
2057
2101
|
END IF;
|
|
2102
|
+
IF EXISTS (
|
|
2103
|
+
SELECT 1 FROM pg_index i
|
|
2104
|
+
JOIN pg_class c ON i.indexrelid = c.oid
|
|
2105
|
+
JOIN pg_namespace n ON c.relnamespace = n.oid
|
|
2106
|
+
WHERE c.relname = lower('${workflowSnapshotConstraint}')
|
|
2107
|
+
AND n.nspname = '${schemaFilter}'
|
|
2108
|
+
AND i.indisreplident = false
|
|
2109
|
+
) THEN
|
|
2110
|
+
ALTER TABLE ${getTableName({ indexName: tableName, schemaName: quotedSchemaName })}
|
|
2111
|
+
REPLICA IDENTITY USING INDEX ${workflowSnapshotConstraint};
|
|
2112
|
+
END IF;
|
|
2058
2113
|
END $$;
|
|
2059
2114
|
` : ""}
|
|
2060
2115
|
${// For spans table: Include PRIMARY KEY in exports, but not in runtime (handled after deduplication)
|
|
@@ -8594,11 +8649,13 @@ var MemoryPG = class _MemoryPG extends storage.MemoryStorage {
|
|
|
8594
8649
|
const maxOvershoot = retentionFloor * 0.95;
|
|
8595
8650
|
const overshoot = bestOverTokens - targetMessageTokens;
|
|
8596
8651
|
const remainingAfterOver = input.currentPendingTokens - bestOverTokens;
|
|
8597
|
-
|
|
8652
|
+
const remainingAfterUnder = input.currentPendingTokens - bestUnderTokens;
|
|
8653
|
+
const minRemaining = Math.min(1e3, retentionFloor);
|
|
8654
|
+
if (input.forceMaxActivation && bestOverBoundary > 0 && remainingAfterOver >= minRemaining) {
|
|
8598
8655
|
chunksToActivate = bestOverBoundary;
|
|
8599
|
-
} else if (bestOverBoundary > 0 && overshoot <= maxOvershoot &&
|
|
8656
|
+
} else if (bestOverBoundary > 0 && overshoot <= maxOvershoot && remainingAfterOver >= minRemaining) {
|
|
8600
8657
|
chunksToActivate = bestOverBoundary;
|
|
8601
|
-
} else if (bestUnderBoundary > 0) {
|
|
8658
|
+
} else if (bestUnderBoundary > 0 && remainingAfterUnder >= minRemaining) {
|
|
8602
8659
|
chunksToActivate = bestUnderBoundary;
|
|
8603
8660
|
} else if (bestOverBoundary > 0) {
|
|
8604
8661
|
chunksToActivate = bestOverBoundary;
|