Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

perf(postgres): remember held page guards per transaction like PGLite so repeated guards cost no round trips (#5984)

garrytanea564c48931a
diff --git a/src/core/postgres-engine.ts b/src/core/postgres-engine.ts
index a52481e1e5..b8cf41e2a3 100644
--- a/src/core/postgres-engine.ts
+++ b/src/core/postgres-engine.ts
@@ -3,7 +3,7 @@ import { replaceDerivedLinks, type DerivedLinkOrigin, type DerivedLinkReplacemen
import { mutatePageTag } from './page-state/tags.ts';
import type { PageKey, PageSnapshot, PageSnapshotOptions, PageWriteOptions } from './page-state/types.ts';
import { assertPageRevision } from './page-state/types.ts';
-import { lockPageKeys as acquirePageKeys } from './page-state/guards.ts';
+import { lockUnheldPageKeys, withHeldPageKeys, type HeldPageKeys } from './page-state/guards.ts';
import { readPageSnapshot as readCanonicalPageSnapshot } from './page-state/snapshot.ts';
import { createPageVersion } from './page-state/versions.ts';
import { moveSlugBindings, recordRenameAlias } from './page-state/rename-alias.ts';
@@ -600,13 +600,15 @@ export class PostgresEngine implements BrainEngine {
// .begin), which would skip a chained .finally and leak the counter.
if (!this._pageTransaction) this.checkoutGauge.acquire('tx');
try {
- return await (conn.begin(async (handle) => {
+ // #5984: like PGLite, a transaction remembers the page guards it holds (a rolled-back savepoint's are released), so repeated guards cost no round trips.
+ return await withHeldPageKeys(this._pageTransaction ? this._heldPageKeys : null, held => conn.begin(async (handle) => {
if (!this._pageTransaction) this.checkoutGauge.checkedOut();
const tx = composablePostgresTransaction(handle);
// Create a scoped engine with tx as its connection, no shared state mutation
const txEngine = Object.create(this) as PostgresEngine;
Object.defineProperty(txEngine, '_chunkWritesInTransaction', { value: true });
Object.defineProperty(txEngine, '_pageTransaction', { value: true });
+ Object.defineProperty(txEngine, '_heldPageKeys', { value: held });
Object.defineProperty(txEngine, 'sql', { get: () => tx });
Object.defineProperty(txEngine, '_sql', { value: tx as unknown as ReturnType<typeof postgres>, writable: false });
return fn(txEngine);
@@ -710,8 +712,9 @@ export class PostgresEngine implements BrainEngine {
async lockPageKeys(keys: readonly PageKey[]): Promise<void> {
if (!this._pageTransaction) throw new Error('lockPageKeys requires engine.transaction()');
- await acquirePageKeys(this, keys);
+ await lockUnheldPageKeys(this, this._heldPageKeys!, keys);
}
+ private _heldPageKeys: HeldPageKeys | null = null;
/**
* v0.41.13 (#1309) — identity-based dedup pre-check.