@mastra/pg 1.33.0 → 1.34.0-alpha.1

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.
Files changed (59) hide show
  1. package/README.md +1 -1
  2. package/dist/docs/SKILL.md +2 -2
  3. package/dist/docs/assets/SOURCE_MAP.json +1 -1
  4. package/dist/docs/references/docs-memory-semantic-recall.md +20 -20
  5. package/dist/docs/references/integrations-databases-neon.md +1 -1
  6. package/dist/docs/references/integrations-databases-postgresql.md +31 -0
  7. package/dist/docs/references/{reference-vectors-pg.md → integrations-vectors-pg.md} +12 -0
  8. package/dist/docs/references/reference-memory-memory-class.md +77 -0
  9. package/dist/docs/references/reference-rag-metadata-filters.md +11 -11
  10. package/dist/docs/references/reference-rag-vector-databases.md +3 -3
  11. package/dist/index.cjs +641 -304
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.js +642 -305
  14. package/dist/index.js.map +1 -1
  15. package/dist/storage/client.d.ts +1 -0
  16. package/dist/storage/client.d.ts.map +1 -1
  17. package/dist/storage/db/index-build.d.ts +5 -0
  18. package/dist/storage/db/index-build.d.ts.map +1 -0
  19. package/dist/storage/db/index.d.ts +0 -8
  20. package/dist/storage/db/index.d.ts.map +1 -1
  21. package/dist/storage/db/pg-errors.d.ts +6 -0
  22. package/dist/storage/db/pg-errors.d.ts.map +1 -1
  23. package/dist/storage/db/schema-snapshot.d.ts +4 -7
  24. package/dist/storage/db/schema-snapshot.d.ts.map +1 -1
  25. package/dist/storage/domains/background-tasks/index.d.ts +1 -0
  26. package/dist/storage/domains/background-tasks/index.d.ts.map +1 -1
  27. package/dist/storage/domains/experiments/index.d.ts +3 -3
  28. package/dist/storage/domains/experiments/index.d.ts.map +1 -1
  29. package/dist/storage/domains/observability/v-next/ddl.d.ts +2 -1
  30. package/dist/storage/domains/observability/v-next/ddl.d.ts.map +1 -1
  31. package/dist/storage/domains/observability/v-next/feedback.d.ts +3 -2
  32. package/dist/storage/domains/observability/v-next/feedback.d.ts.map +1 -1
  33. package/dist/storage/domains/observability/v-next/index.d.ts +9 -1
  34. package/dist/storage/domains/observability/v-next/index.d.ts.map +1 -1
  35. package/dist/storage/domains/observability/v-next/logs.d.ts +2 -1
  36. package/dist/storage/domains/observability/v-next/logs.d.ts.map +1 -1
  37. package/dist/storage/domains/observability/v-next/metrics.d.ts +2 -1
  38. package/dist/storage/domains/observability/v-next/metrics.d.ts.map +1 -1
  39. package/dist/storage/domains/observability/v-next/partitioning.d.ts +29 -19
  40. package/dist/storage/domains/observability/v-next/partitioning.d.ts.map +1 -1
  41. package/dist/storage/domains/observability/v-next/preview-compat.d.ts +4 -0
  42. package/dist/storage/domains/observability/v-next/preview-compat.d.ts.map +1 -1
  43. package/dist/storage/domains/observability/v-next/scores.d.ts +3 -2
  44. package/dist/storage/domains/observability/v-next/scores.d.ts.map +1 -1
  45. package/dist/storage/domains/observability/v-next/span-query.d.ts +4 -0
  46. package/dist/storage/domains/observability/v-next/span-query.d.ts.map +1 -1
  47. package/dist/storage/domains/observability/v-next/sql.d.ts +12 -0
  48. package/dist/storage/domains/observability/v-next/sql.d.ts.map +1 -1
  49. package/dist/storage/domains/observability/v-next/thread-query.d.ts +1 -0
  50. package/dist/storage/domains/observability/v-next/thread-query.d.ts.map +1 -1
  51. package/dist/storage/domains/observability/v-next/trace-aggregate.d.ts.map +1 -1
  52. package/dist/storage/domains/observability/v-next/trace-query.d.ts +10 -1
  53. package/dist/storage/domains/observability/v-next/trace-query.d.ts.map +1 -1
  54. package/dist/storage/domains/observability/v-next/tracing.d.ts +3 -2
  55. package/dist/storage/domains/observability/v-next/tracing.d.ts.map +1 -1
  56. package/dist/storage/index.d.ts +2 -0
  57. package/dist/storage/index.d.ts.map +1 -1
  58. package/dist/storage/test-utils.d.ts.map +1 -1
  59. package/package.json +4 -4
package/README.md CHANGED
@@ -28,7 +28,7 @@ const vectorStore = new PgVector({
28
28
 
29
29
  ## Documentation
30
30
 
31
- - [@mastra/pg documentation](https://mastra.ai/reference/vectors/pg)
31
+ - [@mastra/pg documentation](https://mastra.ai/integrations/vectors/pg)
32
32
 
33
33
  ## Changelog
34
34
 
@@ -3,7 +3,7 @@ name: mastra-pg
3
3
  description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/pg"
6
- version: "1.33.0"
6
+ version: "1.34.0-alpha.1"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -27,6 +27,7 @@ Read the individual reference documents for detailed explanations and code examp
27
27
  - [DynamoDB](references/integrations-databases-dynamodb.md) - Persist Mastra data in Amazon DynamoDB with a single-table ElectroDB design, configurable indexes, TTL support, credentials, and table initialization.
28
28
  - [Neon Postgres](references/integrations-databases-neon.md) - Connect Mastra to Neon Postgres with PostgresStore, pooled production connections, schema initialization, vector search, and Neon agent skills.
29
29
  - [PostgreSQL](references/integrations-databases-postgresql.md) - Persist Mastra application data in PostgreSQL with PostgresStore, configure connections and pools, initialize schemas, and enable observability.
30
+ - [PG](references/integrations-vectors-pg.md) - Run pgvector similarity search inside PostgreSQL from Mastra with HNSW or IVFFlat indexing, metadata filtering, and direct connection pool access.
30
31
 
31
32
  ### Reference
32
33
 
@@ -42,7 +43,6 @@ Read the individual reference documents for detailed explanations and code examp
42
43
  - [Reference: Composite storage](references/reference-storage-composite.md) - MastraCompositeStore can compose storage domains from different providers. Use it when you need different databases for different purposes.
43
44
  - [Reference: Storage retention (prune)](references/reference-storage-retention.md) - prune() deletes rows. It caps growth and is safe to run against large tables (batched, bounded, resumable, cancellable).
44
45
  - [Reference: createVectorQueryTool()](references/reference-tools-vector-query-tool.md) - The createVectorQueryTool() function creates a tool for semantic search over vector stores.
45
- - [Reference: PG vector store](references/reference-vectors-pg.md) - The PgVector class provides vector search using PostgreSQL with pgvector extension. It provides reliable vector similarity search capabilities within your existing PostgreSQL database.
46
46
 
47
47
 
48
48
  Read [assets/SOURCE_MAP.json](assets/SOURCE_MAP.json) for source code references.
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.33.0",
2
+ "version": "1.34.0-alpha.1",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {},
5
5
  "modules": {}
@@ -141,24 +141,24 @@ const agent = new Agent({
141
141
 
142
142
  Each vector store page below includes installation instructions, configuration parameters, and usage examples:
143
143
 
144
- - [Astra](https://mastra.ai/reference/vectors/astra)
145
- - [Chroma](https://mastra.ai/reference/vectors/chroma)
146
- - [Cloudflare Vectorize](https://mastra.ai/reference/vectors/vectorize)
147
- - [Convex](https://mastra.ai/reference/vectors/convex)
148
- - [Couchbase](https://mastra.ai/reference/vectors/couchbase)
149
- - [DuckDB](https://mastra.ai/reference/vectors/duckdb)
150
- - [Elasticsearch](https://mastra.ai/reference/vectors/elasticsearch)
151
- - [LanceDB](https://mastra.ai/reference/vectors/lance)
152
- - [libSQL](https://mastra.ai/reference/vectors/libsql)
153
- - [MongoDB](https://mastra.ai/reference/vectors/mongodb)
154
- - [OpenSearch](https://mastra.ai/reference/vectors/opensearch)
155
- - [OracleDB](https://mastra.ai/reference/vectors/oracledb)
156
- - [Pinecone](https://mastra.ai/reference/vectors/pinecone)
157
- - [PostgreSQL](https://mastra.ai/reference/vectors/pg)
158
- - [Qdrant](https://mastra.ai/reference/vectors/qdrant)
159
- - [S3 Vectors](https://mastra.ai/reference/vectors/s3vectors)
160
- - [Turbopuffer](https://mastra.ai/reference/vectors/turbopuffer)
161
- - [Upstash](https://mastra.ai/reference/vectors/upstash)
144
+ - [Astra](https://mastra.ai/integrations/vectors/astra)
145
+ - [Chroma](https://mastra.ai/integrations/vectors/chroma)
146
+ - [Cloudflare Vectorize](https://mastra.ai/integrations/vectors/vectorize)
147
+ - [Convex](https://mastra.ai/integrations/vectors/convex)
148
+ - [Couchbase](https://mastra.ai/integrations/vectors/couchbase)
149
+ - [DuckDB](https://mastra.ai/integrations/vectors/duckdb)
150
+ - [Elasticsearch](https://mastra.ai/integrations/vectors/elasticsearch)
151
+ - [LanceDB](https://mastra.ai/integrations/vectors/lance)
152
+ - [libSQL](https://mastra.ai/integrations/vectors/libsql)
153
+ - [MongoDB](https://mastra.ai/integrations/vectors/mongodb)
154
+ - [OpenSearch](https://mastra.ai/integrations/vectors/opensearch)
155
+ - [OracleDB](https://mastra.ai/integrations/vectors/oracledb)
156
+ - [Pinecone](https://mastra.ai/integrations/vectors/pinecone)
157
+ - [PostgreSQL](https://mastra.ai/integrations/vectors/pg)
158
+ - [Qdrant](https://mastra.ai/integrations/vectors/qdrant)
159
+ - [S3 Vectors](https://mastra.ai/integrations/vectors/s3vectors)
160
+ - [Turbopuffer](https://mastra.ai/integrations/vectors/turbopuffer)
161
+ - [Upstash](https://mastra.ai/integrations/vectors/upstash)
162
162
 
163
163
  ## Recall configuration
164
164
 
@@ -250,7 +250,7 @@ const options = {
250
250
 
251
251
  Some vector stores generate the embeddings themselves. With one of those, semantic recall runs without an `embedder`: the database embeds each message on write and the search string on arrival. The embedding provider stays out of your application entirely.
252
252
 
253
- MongoDB does this through [Automated Embedding](https://mastra.ai/reference/vectors/mongodb): name a Voyage AI model on the vector store and leave the `embedder` out.
253
+ MongoDB does this through [Automated Embedding](https://mastra.ai/integrations/vectors/mongodb): name a Voyage AI model on the vector store and leave the `embedder` out.
254
254
 
255
255
  ```typescript
256
256
  import { Memory } from '@mastra/memory'
@@ -467,7 +467,7 @@ const agent = new Agent({
467
467
  })
468
468
  ```
469
469
 
470
- For detailed information about index configuration options and performance tuning, see the [PgVector configuration guide](https://mastra.ai/reference/vectors/pg).
470
+ For detailed information about index configuration options and performance tuning, see the [PgVector configuration guide](https://mastra.ai/integrations/vectors/pg).
471
471
 
472
472
  ## Disable semantic recall
473
473
 
@@ -207,7 +207,7 @@ See [Neon connection pooling](https://neon.com/docs/connect/connection-pooling)
207
207
 
208
208
  ## Vector search
209
209
 
210
- Neon supports the [`pgvector` extension](https://neon.com/docs/extensions/pgvector). Use the direct `DATABASE_URL_UNPOOLED` connection while creating the extension and indexes. After setup, configure [`PgVector`](https://mastra.ai/reference/vectors/pg) with the pooled connection for runtime queries:
210
+ Neon supports the [`pgvector` extension](https://neon.com/docs/extensions/pgvector). Use the direct `DATABASE_URL_UNPOOLED` connection while creating the extension and indexes. After setup, configure [`PgVector`](https://mastra.ai/integrations/vectors/pg) with the pooled connection for runtime queries:
211
211
 
212
212
  ```typescript
213
213
  import { PgVector } from '@mastra/pg'
@@ -174,6 +174,37 @@ const storage = new PostgresStoreVNext({
174
174
 
175
175
  Lower `lookbackSeconds` if refreshes are slow, and raise `ttlSeconds` if they run more often than your filter values change. Only one process refreshes a given cache entry at a time, so adding server instances doesn't multiply the work.
176
176
 
177
+ #### Day partitions
178
+
179
+ `PostgresStoreVNext` stores observability events in tables partitioned by day. If TimescaleDB or pg_partman is installed, that extension creates partitions for you. Without an extension, `init()` creates the partitions for yesterday, today, and the next `futureDays` days (default 14), and a write that lands on a day inside that window with no partition creates it before it retries, so writes keep working after the pre-created window runs out. Rows dated outside the window are still rejected.
180
+
181
+ Creating a partition is DDL. If the role your app runs with cannot create tables, or does not own the observability tables, the write fails with an error that names the table and the missing day, and the exporter drops that batch after its retries. In that setup, call `ensurePartitions()` on a schedule, at least once every `futureDays` days, with a role that can create tables and owns the observability tables. It only extends the native day-partition window, and does nothing with TimescaleDB or pg_partman. You can also install TimescaleDB, or pg_partman with its background worker or a scheduled `run_maintenance()` call, so the database maintains the partitions itself.
182
+
183
+ A larger `futureDays` gives the scheduled job more time between runs:
184
+
185
+ ```typescript
186
+ import { PostgresStoreVNext } from '@mastra/pg'
187
+
188
+ const storage = new PostgresStoreVNext({
189
+ id: 'pg-storage',
190
+ connectionString: process.env.DATABASE_URL,
191
+ observability: {
192
+ connectionString: process.env.OBSERVABILITY_DATABASE_URL,
193
+ partitioning: {
194
+ futureDays: 60, // pre-create 60 days of partitions on init()
195
+ },
196
+ },
197
+ })
198
+ ```
199
+
200
+ Then call `ensurePartitions()` from the scheduled job:
201
+
202
+ ```typescript
203
+ // OBSERVABILITY_DATABASE_URL connects as a role that owns the observability tables
204
+ await storage.ensurePartitions()
205
+ await storage.close()
206
+ ```
207
+
177
208
  ### Initialization
178
209
 
179
210
  When you pass storage to the Mastra class, `init()` is called automatically before any storage operation:
@@ -8,6 +8,8 @@ The PgVector class provides vector search using [PostgreSQL](https://www.postgre
8
8
 
9
9
  ## Constructor options
10
10
 
11
+ **id** (`string`): Unique identifier for this vector store instance
12
+
11
13
  **connectionString** (`string`): PostgreSQL connection URL
12
14
 
13
15
  **host** (`string`): PostgreSQL server host
@@ -182,6 +184,16 @@ interface PGIndexStats {
182
184
 
183
185
  **indexName** (`string`): Name of the index to delete
184
186
 
187
+ ### `truncateIndex()`
188
+
189
+ Deletes every vector in an index while keeping the index itself.
190
+
191
+ **indexName** (`string`): Name of the index to clear
192
+
193
+ ```typescript
194
+ await vectorStore.truncateIndex({ indexName: 'my_vectors' })
195
+ ```
196
+
185
197
  ### `updateVector()`
186
198
 
187
199
  Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
@@ -61,6 +61,83 @@ export const agent = new Agent({
61
61
 
62
62
  **memory** (`Memory`): A new Memory instance with the specified configuration.
63
63
 
64
+ ## `saveMessages()`
65
+
66
+ `agent.generate()` and `agent.stream()` persist messages for you. Call `memory.saveMessages()` when you need to write messages yourself, for example to seed a thread, to import a transcript, or to store messages that originated outside Mastra. Each message needs a `threadId` and a `resourceId`, so create the thread with [`createThread()`](https://mastra.ai/reference/memory/createThread) before saving into it.
67
+
68
+ ```typescript
69
+ await memory.saveMessages({
70
+ messages: [
71
+ {
72
+ id: 'msg-1',
73
+ threadId: 'thread-123',
74
+ resourceId: 'user-456',
75
+ role: 'user',
76
+ createdAt: new Date(),
77
+ content: {
78
+ format: 2,
79
+ parts: [{ type: 'text', text: 'Where is my order?' }],
80
+ },
81
+ },
82
+ ],
83
+ })
84
+ ```
85
+
86
+ ### Parameters
87
+
88
+ **messages** (`MastraDBMessage[]`): The messages to save. Every message must include a threadId and a resourceId.
89
+
90
+ **memoryConfig** (`MemoryConfig`): Per-call memory options. Merged over the options passed to the constructor.
91
+
92
+ **observabilityContext** (`Partial<ObservabilityContext>`): Tracing context attached to the save operation.
93
+
94
+ Returns `Promise<{ messages: MastraDBMessage[]; usage?: { tokens: number } }>`. `usage` is present when the embedder reports token usage while indexing messages for semantic recall.
95
+
96
+ ### `MastraDBMessage`
97
+
98
+ Storage uses a single message shape, `MastraDBMessage`. The methods that hand messages back or take them in use this shape, including `saveMessages()` and [`recall()`](https://mastra.ai/reference/memory/recall).
99
+
100
+ **id** (`string`): Message identifier.
101
+
102
+ **role** (`'user' | 'assistant' | 'system' | 'signal'`): Who produced the message. System messages and transient signals are not persisted.
103
+
104
+ **threadId** (`string`): Thread the message belongs to. Required when saving.
105
+
106
+ **resourceId** (`string`): Resource (usually a user or tenant) the message belongs to. Required when saving.
107
+
108
+ **createdAt** (`Date`): Message timestamp.
109
+
110
+ **content** (`MastraMessageContentV2`): The message body. Its parts array holds the text, reasoning, tool calls, and attachments.
111
+
112
+ `content.parts` is the part the agent actually reads. `content.format` is always `2` for stored messages, and `content.metadata` carries arbitrary custom data. `content.toolInvocations`, `content.reasoning`, and `content.experimental_attachments` are flat mirrors kept for AI SDK v4 compatibility; write to `parts` instead.
113
+
114
+ ```typescript
115
+ const assistantMessage = {
116
+ id: 'msg-2',
117
+ threadId: 'thread-123',
118
+ resourceId: 'user-456',
119
+ role: 'assistant',
120
+ createdAt: new Date(),
121
+ content: {
122
+ format: 2,
123
+ parts: [
124
+ { type: 'text', text: 'It ships tomorrow.' },
125
+ {
126
+ type: 'tool-invocation',
127
+ toolInvocation: {
128
+ state: 'result',
129
+ toolCallId: 'call-1',
130
+ toolName: 'lookupOrder',
131
+ args: { orderId: 'A-1' },
132
+ result: { eta: '2026-10-30' },
133
+ },
134
+ },
135
+ ],
136
+ metadata: { source: 'import' },
137
+ },
138
+ }
139
+ ```
140
+
64
141
  ## Extended usage example
65
142
 
66
143
  ```typescript
@@ -225,14 +225,14 @@ await store.createIndex({
225
225
 
226
226
  ## Related
227
227
 
228
- - [Astra](https://mastra.ai/reference/vectors/astra)
229
- - [Chroma](https://mastra.ai/reference/vectors/chroma)
230
- - [Cloudflare Vectorize](https://mastra.ai/reference/vectors/vectorize)
231
- - [libSQL](https://mastra.ai/reference/vectors/libsql)
232
- - [MongoDB](https://mastra.ai/reference/vectors/mongodb)
233
- - [OracleDB](https://mastra.ai/reference/vectors/oracledb)
234
- - [PgVector](https://mastra.ai/reference/vectors/pg)
235
- - [Pinecone](https://mastra.ai/reference/vectors/pinecone)
236
- - [Qdrant](https://mastra.ai/reference/vectors/qdrant)
237
- - [Upstash](https://mastra.ai/reference/vectors/upstash)
238
- - [Amazon S3 Vectors](https://mastra.ai/reference/vectors/s3vectors)
228
+ - [Astra](https://mastra.ai/integrations/vectors/astra)
229
+ - [Chroma](https://mastra.ai/integrations/vectors/chroma)
230
+ - [Cloudflare Vectorize](https://mastra.ai/integrations/vectors/vectorize)
231
+ - [libSQL](https://mastra.ai/integrations/vectors/libsql)
232
+ - [MongoDB](https://mastra.ai/integrations/vectors/mongodb)
233
+ - [OracleDB](https://mastra.ai/integrations/vectors/oracledb)
234
+ - [PgVector](https://mastra.ai/integrations/vectors/pg)
235
+ - [Pinecone](https://mastra.ai/integrations/vectors/pinecone)
236
+ - [Qdrant](https://mastra.ai/integrations/vectors/qdrant)
237
+ - [Upstash](https://mastra.ai/integrations/vectors/upstash)
238
+ - [Amazon S3 Vectors](https://mastra.ai/integrations/vectors/s3vectors)
@@ -35,7 +35,7 @@ MongoDB Vector Search is a good solution for teams who want to consolidate vecto
35
35
 
36
36
  ### Using VoyageAI with MongoDB
37
37
 
38
- MongoDB works directly with VoyageAI's embedding models, which are optimized for retrieval tasks. For complete examples and specialized models, see the [VoyageAI embeddings documentation](https://mastra.ai/models/embeddings) and [MongoDB vector reference](https://mastra.ai/reference/vectors/mongodb).
38
+ MongoDB works directly with VoyageAI's embedding models, which are optimized for retrieval tasks. For complete examples and specialized models, see the [VoyageAI embeddings documentation](https://mastra.ai/models/embeddings) and [MongoDB vector reference](https://mastra.ai/integrations/vectors/mongodb).
39
39
 
40
40
  ### Hybrid Search (Vector + Full-Text)
41
41
 
@@ -52,7 +52,7 @@ const results = await store.hybridQuery({
52
52
  })
53
53
  ```
54
54
 
55
- See the [MongoDB vector reference](https://mastra.ai/reference/vectors/mongodb) for details on `createSearchIndex()`, `textQuery()`, and `hybridQuery()`.
55
+ See the [MongoDB vector reference](https://mastra.ai/integrations/vectors/mongodb) for details on `createSearchIndex()`, `textQuery()`, and `hybridQuery()`.
56
56
 
57
57
  ### Automated Embedding
58
58
 
@@ -75,7 +75,7 @@ const results = await store.query({
75
75
  })
76
76
  ```
77
77
 
78
- Automated Embedding is a MongoDB Preview feature and needs a deployment where it's available. See the [MongoDB vector reference](https://mastra.ai/reference/vectors/mongodb) for the supported options and requirements.
78
+ Automated Embedding is a MongoDB Preview feature and needs a deployment where it's available. See the [MongoDB vector reference](https://mastra.ai/integrations/vectors/mongodb) for the supported options and requirements.
79
79
 
80
80
  **PgVector**:
81
81