@mastra/pg 1.36.0-alpha.3 → 1.36.0-alpha.5

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.
@@ -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.36.0-alpha.3"
6
+ version: "1.36.0-alpha.5"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,161 +1,161 @@
1
1
  {
2
- "version": "1.36.0-alpha.3",
2
+ "version": "1.36.0-alpha.5",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {
5
5
  "AgentsPG": {
6
6
  "types": "dist/storage/domains/agents/index.d.ts",
7
7
  "implementation": "dist/index.js",
8
- "line": 4371
8
+ "line": 4404
9
9
  },
10
10
  "BackgroundTasksPG": {
11
11
  "types": "dist/storage/domains/background-tasks/index.d.ts",
12
12
  "implementation": "dist/index.js",
13
- "line": 5302
13
+ "line": 5335
14
14
  },
15
15
  "BlobsPG": {
16
16
  "types": "dist/storage/domains/blobs/index.d.ts",
17
17
  "implementation": "dist/index.js",
18
- "line": 5693
18
+ "line": 5726
19
19
  },
20
20
  "ChannelsPG": {
21
21
  "types": "dist/storage/domains/channels/index.d.ts",
22
22
  "implementation": "dist/index.js",
23
- "line": 5795
23
+ "line": 5828
24
24
  },
25
25
  "DatasetsPG": {
26
26
  "types": "dist/storage/domains/datasets/index.d.ts",
27
27
  "implementation": "dist/index.js",
28
- "line": 6037
28
+ "line": 6070
29
29
  },
30
30
  "ExperimentsPG": {
31
31
  "types": "dist/storage/domains/experiments/index.d.ts",
32
32
  "implementation": "dist/index.js",
33
- "line": 7294
33
+ "line": 7327
34
34
  },
35
35
  "FavoritesPG": {
36
36
  "types": "dist/storage/domains/favorites/index.d.ts",
37
37
  "implementation": "dist/index.js",
38
- "line": 8266
38
+ "line": 8299
39
39
  },
40
40
  "KnowledgePG": {
41
41
  "types": "dist/storage/domains/knowledge/index.d.ts",
42
42
  "implementation": "dist/index.js",
43
- "line": 10144
43
+ "line": 10177
44
44
  },
45
45
  "MCPClientsPG": {
46
46
  "types": "dist/storage/domains/mcp-clients/index.d.ts",
47
47
  "implementation": "dist/index.js",
48
- "line": 10301
48
+ "line": 10334
49
49
  },
50
50
  "MCPServersPG": {
51
51
  "types": "dist/storage/domains/mcp-servers/index.d.ts",
52
52
  "implementation": "dist/index.js",
53
- "line": 10870
53
+ "line": 10903
54
54
  },
55
55
  "MemoryPG": {
56
56
  "types": "dist/storage/domains/memory/index.d.ts",
57
57
  "implementation": "dist/index.js",
58
- "line": 11665
58
+ "line": 11698
59
59
  },
60
60
  "NotificationsPG": {
61
61
  "types": "dist/storage/domains/notifications/index.d.ts",
62
62
  "implementation": "dist/index.js",
63
- "line": 14562
63
+ "line": 14605
64
64
  },
65
65
  "ObservabilityPG": {
66
66
  "types": "dist/storage/domains/observability/index.d.ts",
67
67
  "implementation": "dist/index.js",
68
- "line": 14984
68
+ "line": 15027
69
69
  },
70
70
  "ObservabilityStoragePostgresVNext": {
71
71
  "types": "dist/storage/domains/observability/v-next/index.d.ts",
72
72
  "implementation": "dist/index.js",
73
- "line": 21941
73
+ "line": 21985
74
74
  },
75
75
  "PGVECTOR_PROMPT": {
76
76
  "types": "dist/vector/prompt.d.ts",
77
77
  "implementation": "dist/index.js",
78
- "line": 28617
78
+ "line": 28689
79
79
  },
80
80
  "PgFactoryStorage": {
81
81
  "types": "dist/storage/factory-storage.d.ts",
82
82
  "implementation": "dist/index.js",
83
- "line": 28103
83
+ "line": 28169
84
84
  },
85
85
  "PgVector": {
86
86
  "types": "dist/vector/index.d.ts",
87
87
  "implementation": "dist/index.js",
88
- "line": 593
88
+ "line": 591
89
89
  },
90
90
  "PoolAdapter": {
91
91
  "types": "dist/storage/client.d.ts",
92
92
  "implementation": "dist/index.js",
93
- "line": 2069
93
+ "line": 2067
94
94
  },
95
95
  "PostgresStore": {
96
96
  "types": "dist/storage/index.d.ts",
97
97
  "implementation": "dist/index.js",
98
- "line": 28318
98
+ "line": 28389
99
99
  },
100
100
  "PostgresStoreVNext": {
101
101
  "types": "dist/storage/index.d.ts",
102
102
  "implementation": "dist/index.js",
103
- "line": 28536
103
+ "line": 28608
104
104
  },
105
105
  "PromptBlocksPG": {
106
106
  "types": "dist/storage/domains/prompt-blocks/index.d.ts",
107
107
  "implementation": "dist/index.js",
108
- "line": 22391
108
+ "line": 22435
109
109
  },
110
110
  "SchedulesPG": {
111
111
  "types": "dist/storage/domains/schedules/index.d.ts",
112
112
  "implementation": "dist/index.js",
113
- "line": 23018
113
+ "line": 23064
114
114
  },
115
115
  "ScorerDefinitionsPG": {
116
116
  "types": "dist/storage/domains/scorer-definitions/index.d.ts",
117
117
  "implementation": "dist/index.js",
118
- "line": 23339
118
+ "line": 23394
119
119
  },
120
120
  "ScoresPG": {
121
121
  "types": "dist/storage/domains/scores/index.d.ts",
122
122
  "implementation": "dist/index.js",
123
- "line": 23962
123
+ "line": 24017
124
124
  },
125
125
  "SkillsPG": {
126
126
  "types": "dist/storage/domains/skills/index.d.ts",
127
127
  "implementation": "dist/index.js",
128
- "line": 24405
128
+ "line": 24460
129
129
  },
130
130
  "ThreadStatePG": {
131
131
  "types": "dist/storage/domains/thread-state/index.d.ts",
132
132
  "implementation": "dist/index.js",
133
- "line": 25096
133
+ "line": 25151
134
134
  },
135
135
  "ToolProviderConnectionsPG": {
136
136
  "types": "dist/storage/domains/tool-provider-connections/index.d.ts",
137
137
  "implementation": "dist/index.js",
138
- "line": 25270
138
+ "line": 25325
139
139
  },
140
140
  "WorkflowDefinitionsPG": {
141
141
  "types": "dist/storage/domains/workflow-definitions/index.d.ts",
142
142
  "implementation": "dist/index.js",
143
- "line": 25534
143
+ "line": 25589
144
144
  },
145
145
  "WorkflowsPG": {
146
146
  "types": "dist/storage/domains/workflows/index.d.ts",
147
147
  "implementation": "dist/index.js",
148
- "line": 25917
148
+ "line": 25972
149
149
  },
150
150
  "WorkspacesPG": {
151
151
  "types": "dist/storage/domains/workspaces/index.d.ts",
152
152
  "implementation": "dist/index.js",
153
- "line": 27203
153
+ "line": 27258
154
154
  },
155
155
  "exportSchemas": {
156
156
  "types": "dist/storage/index.d.ts",
157
157
  "implementation": "dist/index.js",
158
- "line": 28285
158
+ "line": 28356
159
159
  }
160
160
  },
161
161
  "modules": {}
package/dist/index.cjs CHANGED
@@ -273,31 +273,29 @@ function buildElemMatchConditions(value, paramIndex) {
273
273
  if (typeof value !== "object" || Array.isArray(value)) throw new Error("$elemMatch requires an object with conditions");
274
274
  const conditions = [];
275
275
  const values = [];
276
+ function buildFieldCondition(field, operator, operatorValue) {
277
+ if (operator === "$not") {
278
+ const nested = Object.entries(operatorValue);
279
+ if (nested.length === 0) throw new Error("$not requires at least one operator");
280
+ return `NOT (COALESCE(${nested.map(([nestedOp, nestedValue]) => buildFieldCondition(field, nestedOp, nestedValue)).join(" AND ")}, FALSE))`;
281
+ }
282
+ const operatorFn = FILTER_OPERATORS[operator];
283
+ if (!operatorFn) throw new Error(`Invalid operator: ${operator}`);
284
+ const scalarElement = field.startsWith("$");
285
+ const result = operatorFn(scalarElement ? "" : field, paramIndex + values.length, operatorValue);
286
+ const sql = result.sql.replaceAll("metadata->>", "elem->>").replaceAll("metadata->", "elem->").replaceAll("metadata#>>", "elem#>>").replaceAll("metadata#>", "elem#>").replaceAll("metadata ?", "elem ?");
287
+ const elementSql = scalarElement ? sql.replaceAll("elem->>''", "elem #>> '{}'").replaceAll("elem->''", "elem") : sql;
288
+ if (result.needsValue) {
289
+ const transformedValue = result.transformValue ? result.transformValue() : operatorValue;
290
+ if (operator === "$elemMatch") values.push(...transformedValue);
291
+ else values.push(transformedValue);
292
+ }
293
+ return elementSql;
294
+ }
276
295
  Object.entries(value).forEach(([field, val]) => {
277
- const nextParamIndex = paramIndex + values.length;
278
- let paramOperator;
279
- let paramKey;
280
- let paramValue;
281
- if (field.startsWith("$")) {
282
- paramOperator = field;
283
- paramKey = "";
284
- paramValue = val;
285
- } else if (typeof val === "object" && !Array.isArray(val)) {
286
- const [op, opValue] = Object.entries(val || {})[0] || [];
287
- paramOperator = op;
288
- paramKey = field;
289
- paramValue = opValue;
290
- } else {
291
- paramOperator = "$eq";
292
- paramKey = field;
293
- paramValue = val;
294
- }
295
- const operatorFn = FILTER_OPERATORS[paramOperator];
296
- if (!operatorFn) throw new Error(`Invalid operator: ${paramOperator}`);
297
- const result = operatorFn(paramKey, nextParamIndex, paramValue);
298
- const sql = result.sql.replaceAll("metadata->>", "elem->>").replaceAll("metadata->", "elem->").replaceAll("metadata#>>", "elem#>>").replaceAll("metadata#>", "elem#>");
299
- conditions.push(sql);
300
- if (result.needsValue) values.push(paramValue);
296
+ const operators = field.startsWith("$") ? [[field, val]] : typeof val === "object" && val !== null && !Array.isArray(val) ? Object.entries(val) : [["$eq", val]];
297
+ if (operators.length === 0) throw new Error(`$elemMatch field ${field} requires at least one operator`);
298
+ for (const [operator, operatorValue] of operators) conditions.push(buildFieldCondition(field, operator, operatorValue));
301
299
  });
302
300
  return {
303
301
  sql: conditions.join(" AND "),
@@ -2091,8 +2089,10 @@ function truncateQuery(query, maxLength = 100) {
2091
2089
  */
2092
2090
  var PoolAdapter = class {
2093
2091
  $pool;
2094
- constructor($pool) {
2092
+ onRollbackError;
2093
+ constructor($pool, onRollbackError) {
2095
2094
  this.$pool = $pool;
2095
+ this.onRollbackError = onRollbackError;
2096
2096
  }
2097
2097
  connect() {
2098
2098
  return this.$pool.connect();
@@ -2129,8 +2129,14 @@ var PoolAdapter = class {
2129
2129
  }
2130
2130
  async tx(callback) {
2131
2131
  const client = await this.$pool.connect();
2132
+ let destroyClient = false;
2132
2133
  try {
2133
- await client.query("BEGIN");
2134
+ try {
2135
+ await client.query("BEGIN");
2136
+ } catch (error) {
2137
+ destroyClient = true;
2138
+ throw error;
2139
+ }
2134
2140
  const txClient = new TransactionClient(client);
2135
2141
  try {
2136
2142
  const result = await callback(txClient);
@@ -2142,12 +2148,16 @@ var PoolAdapter = class {
2142
2148
  try {
2143
2149
  await client.query("ROLLBACK");
2144
2150
  } catch (rollbackError) {
2145
- console.error("Transaction rollback failed:", rollbackError);
2151
+ destroyClient = true;
2152
+ try {
2153
+ if (this.onRollbackError) await this.onRollbackError(rollbackError);
2154
+ else console.error("Failed to rollback transaction:", rollbackError);
2155
+ } catch {}
2146
2156
  }
2147
2157
  throw error;
2148
2158
  }
2149
2159
  } finally {
2150
- client.release();
2160
+ client.release(destroyClient || void 0);
2151
2161
  }
2152
2162
  }
2153
2163
  };
@@ -2254,6 +2264,7 @@ var TransactionClient = class {
2254
2264
  var PinnedClientAdapter = class {
2255
2265
  $pool;
2256
2266
  pinnedClient;
2267
+ onRollbackError;
2257
2268
  /**
2258
2269
  * Serialization tail. Domain init() methods fire via Promise.all in
2259
2270
  * MastraCompositeStore.#runInit(), so without our own gate every domain's
@@ -2264,9 +2275,14 @@ var PinnedClientAdapter = class {
2264
2275
  * the reliance on pg's internal queue.
2265
2276
  */
2266
2277
  #tail = Promise.resolve();
2267
- constructor($pool, pinnedClient) {
2278
+ #rollbackError;
2279
+ constructor($pool, pinnedClient, onRollbackError) {
2268
2280
  this.$pool = $pool;
2269
2281
  this.pinnedClient = pinnedClient;
2282
+ this.onRollbackError = onRollbackError;
2283
+ }
2284
+ get shouldDestroyClient() {
2285
+ return this.#rollbackError !== void 0;
2270
2286
  }
2271
2287
  /**
2272
2288
  * Run `fn` after any previously enqueued work on this pinned client has
@@ -2274,7 +2290,11 @@ var PinnedClientAdapter = class {
2274
2290
  * resume on the next caller's turn.
2275
2291
  */
2276
2292
  #enqueue(fn) {
2277
- const next = this.#tail.then(fn, fn);
2293
+ const run = async () => {
2294
+ if (this.shouldDestroyClient) throw this.#rollbackError;
2295
+ return fn();
2296
+ };
2297
+ const next = this.#tail.then(run, run);
2278
2298
  this.#tail = next.catch(() => void 0);
2279
2299
  return next;
2280
2300
  }
@@ -2326,7 +2346,12 @@ var PinnedClientAdapter = class {
2326
2346
  }
2327
2347
  tx(callback) {
2328
2348
  return this.#enqueue(async () => {
2329
- await this.pinnedClient.query("BEGIN");
2349
+ try {
2350
+ await this.pinnedClient.query("BEGIN");
2351
+ } catch (error) {
2352
+ this.#rollbackError = error;
2353
+ throw error;
2354
+ }
2330
2355
  const txClient = new TransactionClient(this.pinnedClient);
2331
2356
  try {
2332
2357
  const result = await callback(txClient);
@@ -2338,7 +2363,11 @@ var PinnedClientAdapter = class {
2338
2363
  try {
2339
2364
  await this.pinnedClient.query("ROLLBACK");
2340
2365
  } catch (rollbackError) {
2341
- console.error("Transaction rollback failed:", rollbackError);
2366
+ this.#rollbackError = rollbackError;
2367
+ try {
2368
+ if (this.onRollbackError) await this.onRollbackError(rollbackError);
2369
+ else console.error("Failed to rollback transaction:", rollbackError);
2370
+ } catch {}
2342
2371
  }
2343
2372
  throw error;
2344
2373
  }
@@ -3428,12 +3457,16 @@ var PgDB = class extends _mastra_core_base.MastraBase {
3428
3457
  }
3429
3458
  if (snapshot) {
3430
3459
  snapshot.tables.add(tableName);
3431
- if (isNewTable) snapshot.createdTables.add(tableName);
3432
- const created = this.snapshotColumns(snapshot, tableName);
3433
- for (const [name, def] of Object.entries(schema)) {
3434
- const parsedName = (0, _mastra_core_utils.parseSqlIdentifier)(name, "column name");
3435
- created.add(parsedName);
3436
- if (def.type === "timestamp") created.add(`${parsedName}Z`);
3460
+ if (isNewTable) {
3461
+ snapshot.createdTables.add(tableName);
3462
+ snapshot.columns.set(tableName, new Set(await this.getTableColumns(tableName)));
3463
+ } else {
3464
+ const created = this.snapshotColumns(snapshot, tableName);
3465
+ for (const [name, def] of Object.entries(schema)) {
3466
+ const parsedName = (0, _mastra_core_utils.parseSqlIdentifier)(name, "column name");
3467
+ created.add(parsedName);
3468
+ if (def.type === "timestamp") created.add(`${parsedName}Z`);
3469
+ }
3437
3470
  }
3438
3471
  }
3439
3472
  }
@@ -11822,15 +11855,25 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
11822
11855
  * @param schemaPrefix - Prefix for index names (e.g. "my_schema_" or "")
11823
11856
  */
11824
11857
  static getDefaultIndexDefs(schemaPrefix) {
11825
- return [{
11826
- name: `${schemaPrefix}mastra_threads_resourceid_createdat_idx`,
11827
- table: _mastra_core_storage.TABLE_THREADS,
11828
- columns: ["resourceId", "createdAt DESC"]
11829
- }, {
11830
- name: `${schemaPrefix}mastra_messages_thread_id_createdat_idx`,
11831
- table: _mastra_core_storage.TABLE_MESSAGES,
11832
- columns: ["thread_id", "createdAt DESC"]
11833
- }];
11858
+ return [
11859
+ {
11860
+ name: `${schemaPrefix}mastra_threads_resourceid_createdat_idx`,
11861
+ table: _mastra_core_storage.TABLE_THREADS,
11862
+ columns: ["resourceId", "createdAt DESC"]
11863
+ },
11864
+ {
11865
+ name: `${schemaPrefix}mastra_threads_metadata_gin_idx`,
11866
+ table: _mastra_core_storage.TABLE_THREADS,
11867
+ columns: ["metadata"],
11868
+ method: "gin",
11869
+ opclass: "jsonb_path_ops"
11870
+ },
11871
+ {
11872
+ name: `${schemaPrefix}mastra_messages_thread_id_createdat_idx`,
11873
+ table: _mastra_core_storage.TABLE_MESSAGES,
11874
+ columns: ["thread_id", "createdAt DESC"]
11875
+ }
11876
+ ];
11834
11877
  }
11835
11878
  /**
11836
11879
  * Returns all DDL statements for this domain: tables (threads, messages, resources, OM), indexes.
@@ -21949,6 +21992,7 @@ async function dangerouslyClearTracing(client, schema) {
21949
21992
  const spanQueryFeatures = typeof _mastra_core_storage.planSpanQuery === "function" ? [
21950
21993
  "span-query",
21951
21994
  "span-query-context-fields",
21995
+ ..._mastra_core_storage.SPAN_QUERY_ORDER_FIELDS?.includes("durationMs") ? ["span-query-duration-sort"] : [],
21952
21996
  ..."model" in (_mastra_core_storage.SPAN_QUERY_FIELD_REGISTRY ?? {}) ? ["span-query-model-fields"] : []
21953
21997
  ] : [];
21954
21998
  const spanAggregateFeatures = typeof _mastra_core_storage.planSpanAggregate === "function" ? ["span-aggregate", ..."model" in (_mastra_core_storage.SPAN_AGGREGATE_DIMENSION_REGISTRY ?? {}) ? ["span-aggregate-model-dimensions"] : []] : [];
@@ -23020,6 +23064,8 @@ function rowToSchedule(row) {
23020
23064
  if (metadata !== void 0) schedule.metadata = metadata;
23021
23065
  if (row.owner_type != null) schedule.ownerType = String(row.owner_type);
23022
23066
  if (row.owner_id != null) schedule.ownerId = String(row.owner_id);
23067
+ if (row.run_at != null) schedule.runAt = Number(row.run_at);
23068
+ if (row.end_at != null) schedule.endAt = Number(row.end_at);
23023
23069
  return schedule;
23024
23070
  }
23025
23071
  function rowToTrigger(row) {
@@ -23079,6 +23125,11 @@ var SchedulesPG = class SchedulesPG extends _mastra_core_storage.SchedulesStorag
23079
23125
  tableName: _mastra_core_storage.TABLE_SCHEDULES,
23080
23126
  schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_SCHEDULES]
23081
23127
  });
23128
+ await this.#db.alterTable({
23129
+ tableName: _mastra_core_storage.TABLE_SCHEDULES,
23130
+ schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_SCHEDULES],
23131
+ ifNotExists: ["run_at", "end_at"]
23132
+ });
23082
23133
  await this.#db.createTable({
23083
23134
  tableName: _mastra_core_storage.TABLE_SCHEDULE_TRIGGERS,
23084
23135
  schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_SCHEDULE_TRIGGERS]
@@ -23206,7 +23257,9 @@ var SchedulesPG = class SchedulesPG extends _mastra_core_storage.SchedulesStorag
23206
23257
  updated_at: schedule.updatedAt,
23207
23258
  metadata: schedule.metadata ?? null,
23208
23259
  owner_type: schedule.ownerType ?? null,
23209
- owner_id: schedule.ownerId ?? null
23260
+ owner_id: schedule.ownerId ?? null,
23261
+ run_at: schedule.runAt ?? null,
23262
+ end_at: schedule.endAt ?? null
23210
23263
  }
23211
23264
  });
23212
23265
  return schedule;
@@ -23276,6 +23329,8 @@ var SchedulesPG = class SchedulesPG extends _mastra_core_storage.SchedulesStorag
23276
23329
  if ("metadata" in patch) push("metadata = ?::jsonb", patch.metadata != null ? toPgJson(patch.metadata) : null);
23277
23330
  if ("ownerType" in patch) push("owner_type = ?", patch.ownerType ?? null);
23278
23331
  if ("ownerId" in patch) push("owner_id = ?", patch.ownerId ?? null);
23332
+ if ("runAt" in patch) push("run_at = ?", patch.runAt ?? null);
23333
+ if ("endAt" in patch) push("end_at = ?", patch.endAt ?? null);
23279
23334
  push("updated_at = ?", Date.now());
23280
23335
  if (setClauses.length === 1) {
23281
23336
  const existing = await this.#getSchedule(this.#client, id);
@@ -28100,18 +28155,29 @@ var PgFactoryStorageOps = class {
28100
28155
  };
28101
28156
  if (this.#transactionClient) return run(this.#transactionClient);
28102
28157
  const client = await this.#queryable.connect();
28158
+ let releaseError;
28103
28159
  try {
28104
- await client.query("BEGIN");
28160
+ try {
28161
+ await client.query("BEGIN");
28162
+ } catch (error) {
28163
+ releaseError = error instanceof Error ? error : new Error(String(error));
28164
+ throw error;
28165
+ }
28105
28166
  try {
28106
28167
  const result = await run(client);
28107
28168
  await client.query("COMMIT");
28108
28169
  return result;
28109
28170
  } catch (error) {
28110
- await client.query("ROLLBACK");
28171
+ try {
28172
+ await client.query("ROLLBACK");
28173
+ } catch (rollbackError) {
28174
+ releaseError = rollbackError instanceof Error ? rollbackError : new Error(String(rollbackError));
28175
+ throw new AggregateError([error, rollbackError], "Factory transaction and rollback both failed");
28176
+ }
28111
28177
  throw error;
28112
28178
  }
28113
28179
  } finally {
28114
- client.release();
28180
+ client.release(releaseError);
28115
28181
  }
28116
28182
  }
28117
28183
  };
@@ -28168,7 +28234,12 @@ var PgFactoryStorage = class extends _mastra_core_storage.FactoryStorage {
28168
28234
  let transactionOpen = false;
28169
28235
  let releaseError;
28170
28236
  try {
28171
- await client.query(options?.isolationLevel === "serializable" ? "BEGIN ISOLATION LEVEL SERIALIZABLE" : "BEGIN");
28237
+ try {
28238
+ await client.query(options?.isolationLevel === "serializable" ? "BEGIN ISOLATION LEVEL SERIALIZABLE" : "BEGIN");
28239
+ } catch (error) {
28240
+ releaseError = error instanceof Error ? error : new Error(String(error));
28241
+ throw error;
28242
+ }
28172
28243
  transactionOpen = true;
28173
28244
  const result = await fn(new PgFactoryStorageOps(client, this.#schemas, client));
28174
28245
  await client.query("COMMIT");
@@ -28370,8 +28441,9 @@ var PostgresStore = class extends _mastra_core_storage.MastraCompositeStore {
28370
28441
  this.#ownsWritePool = true;
28371
28442
  }
28372
28443
  this.#readPool = config.readPool ?? this.#writePool;
28373
- this.#db = new RoutingDbClient(new PoolAdapter(this.#writePool));
28374
- this.#readDb = this.#readPool === this.#writePool ? this.#db : new PoolAdapter(this.#readPool);
28444
+ const onRollbackError = (error) => this.logger?.error?.("Transaction rollback failed", { error });
28445
+ this.#db = new RoutingDbClient(new PoolAdapter(this.#writePool, onRollbackError));
28446
+ this.#readDb = this.#readPool === this.#writePool ? this.#db : new PoolAdapter(this.#readPool, onRollbackError);
28375
28447
  const domainConfig = {
28376
28448
  client: this.#db,
28377
28449
  readClient: this.#readDb,
@@ -28440,7 +28512,7 @@ var PostgresStore = class extends _mastra_core_storage.MastraCompositeStore {
28440
28512
  let pinned;
28441
28513
  try {
28442
28514
  pinnedClient = await this.#writePool.connect();
28443
- pinned = new PinnedClientAdapter(this.#writePool, pinnedClient);
28515
+ pinned = new PinnedClientAdapter(this.#writePool, pinnedClient, (error) => this.logger?.error?.("Transaction rollback failed", { error }));
28444
28516
  this.#db.pin(pinned);
28445
28517
  for (let attempt = 1; attempt <= 3; attempt++) {
28446
28518
  this.#db.setSchemaSnapshot(await loadSchemaSnapshot(pinned, this.schema));
@@ -28465,7 +28537,7 @@ var PostgresStore = class extends _mastra_core_storage.MastraCompositeStore {
28465
28537
  this.#db.setSchemaSnapshot(null);
28466
28538
  if (pinnedClient) {
28467
28539
  this.#db.unpin();
28468
- pinnedClient.release();
28540
+ pinnedClient.release(pinned?.shouldDestroyClient || void 0);
28469
28541
  }
28470
28542
  }
28471
28543
  }