Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(postgres-driver): an idle pooled connection keeps nothing from its last statement

garrytan43040814a8bd
diff --git a/patches/[email protected] b/patches/[email protected]
index 7367ed773a..254ec5c740 100644
--- a/patches/[email protected]
+++ b/patches/[email protected]
@@ -1,5 +1,5 @@
diff --git a/cjs/src/connection.js b/vendor/postgres/cjs/src/connection.js
-index 07f6716..a15e780 100644
+index 07f6716..5a7b7eb 100644
--- a/cjs/src/connection.js
+++ b/vendor/postgres/cjs/src/connection.js
@@ -20,6 +20,7 @@ const Sync = b().S().end()
@@ -95,7 +95,7 @@ index 07f6716..a15e780 100644
+ mayPipeline = mayPipeline || q.options.onexecute(connection)
+ noteBudget(q)
+ lastWritten = q
-+ lastExecuted = q
++ lastExecuted = { string: q.string, strings: q.strings }
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
@@ -255,7 +255,17 @@ index 07f6716..a15e780 100644
if (incomings) {
incomings.push(x)
remaining -= x.length
-@@ -379,20 +520,35 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -330,6 +471,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+ remaining = 0
+ incomings = null
+ }
++
++ if (incoming.length === 0)
++ incoming = Buffer.alloc(0)
+ }
+
+ async function connect() {
+@@ -379,20 +523,35 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
}
function error(err) {
@@ -291,7 +301,7 @@ index 07f6716..a15e780 100644
function queryError(query, err) {
if (query.reserve)
return query.reject(err)
-@@ -438,19 +594,47 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -438,19 +597,47 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
remaining = 0
incomings = null
clearImmediate(nextWriteTimer)
@@ -340,7 +350,7 @@ index 07f6716..a15e780 100644
closedTime = performance.now()
hadError && options.shared.retries++
delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000
-@@ -459,6 +643,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -459,6 +646,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
/* Handlers */
function handle(xs, x = xs[0]) {
@@ -351,7 +361,7 @@ index 07f6716..a15e780 100644
(
x === 68 ? DataRow : // D
x === 100 ? CopyData : // d
-@@ -489,6 +677,39 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -489,6 +680,39 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
)(xs)
}
@@ -391,7 +401,7 @@ index 07f6716..a15e780 100644
function DataRow(x) {
let index = 7
let length
-@@ -533,13 +754,25 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -533,13 +757,25 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
}
function ReadyForQuery(x) {
@@ -418,7 +428,7 @@ index 07f6716..a15e780 100644
} else {
query.resolve(results || result)
}
-@@ -547,9 +780,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -547,9 +783,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
errored(errorResponse)
}
@@ -435,7 +445,7 @@ index 07f6716..a15e780 100644
if (initial) {
if (target_session_attrs) {
-@@ -564,23 +804,43 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -564,23 +807,43 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
return fetchArrayTypes()
}
@@ -485,7 +495,7 @@ index 07f6716..a15e780 100644
: connection.reserved()
: ending
? terminate()
-@@ -630,7 +890,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -630,7 +893,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))
query.prepare && (statements[query.signature] = query.statement)
@@ -497,7 +507,7 @@ index 07f6716..a15e780 100644
}
function RowDescription(x) {
-@@ -767,14 +1031,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -767,14 +1034,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
async function fetchArrayTypes() {
needsTypes = false
@@ -527,7 +537,7 @@ index 07f6716..a15e780 100644
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
}
-@@ -821,7 +1092,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -821,7 +1095,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
function retry(q, error) {
delete statements[q.signature]
q.retried = error
@@ -545,7 +555,7 @@ index 07f6716..a15e780 100644
}
function NotificationResponse(x) {
-@@ -1039,6 +1319,59 @@ function xor(a, b) {
+@@ -1039,6 +1322,59 @@ function xor(a, b) {
return buffer
}
@@ -1107,7 +1117,7 @@ index 0578284..e80a868 100644
return '$' + (types.push(
diff --git a/src/connection.js b/vendor/postgres/src/connection.js
-index 1b1cccd..7b6372c 100644
+index 1b1cccd..65ce1f6 100644
--- a/src/connection.js
+++ b/vendor/postgres/src/connection.js
@@ -20,6 +20,7 @@ const Sync = b().S().end()
@@ -1203,7 +1213,7 @@ index 1b1cccd..7b6372c 100644
+ mayPipeline = mayPipeline || q.options.onexecute(connection)
+ noteBudget(q)
+ lastWritten = q
-+ lastExecuted = q
++ lastExecuted = { string: q.string, strings: q.strings }
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
@@ -1363,7 +1373,17 @@ index 1b1cccd..7b6372c 100644
if (incomings) {
incomings.push(x)
remaining -= x.length
-@@ -379,20 +520,35 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -330,6 +471,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+ remaining = 0
+ incomings = null
+ }
++
++ if (incoming.length === 0)
++ incoming = Buffer.alloc(0)
+ }
+
+ async function connect() {
+@@ -379,20 +523,35 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
}
function error(err) {
@@ -1399,7 +1419,7 @@ index 1b1cccd..7b6372c 100644
function queryError(query, err) {
if (query.reserve)
return query.reject(err)
-@@ -438,19 +594,47 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -438,19 +597,47 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
remaining = 0
incomings = null
clearImmediate(nextWriteTimer)
@@ -1448,7 +1468,7 @@ index 1b1cccd..7b6372c 100644
closedTime = performance.now()
hadError && options.shared.retries++
delay = (typeof backoff === 'function' ? backoff(options.shared.retries) : backoff) * 1000
-@@ -459,6 +643,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -459,6 +646,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
/* Handlers */
function handle(xs, x = xs[0]) {
@@ -1459,7 +1479,7 @@ index 1b1cccd..7b6372c 100644
(
x === 68 ? DataRow : // D
x === 100 ? CopyData : // d
-@@ -489,6 +677,39 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -489,6 +680,39 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
)(xs)
}
@@ -1499,7 +1519,7 @@ index 1b1cccd..7b6372c 100644
function DataRow(x) {
let index = 7
let length
-@@ -533,13 +754,25 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -533,13 +757,25 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
}
function ReadyForQuery(x) {
@@ -1526,7 +1546,7 @@ index 1b1cccd..7b6372c 100644
} else {
query.resolve(results || result)
}
-@@ -547,9 +780,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -547,9 +783,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
errored(errorResponse)
}
@@ -1543,7 +1563,7 @@ index 1b1cccd..7b6372c 100644
if (initial) {
if (target_session_attrs) {
-@@ -564,23 +804,43 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -564,23 +807,43 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
return fetchArrayTypes()
}
@@ -1593,7 +1613,7 @@ index 1b1cccd..7b6372c 100644
: connection.reserved()
: ending
? terminate()
-@@ -630,7 +890,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -630,7 +893,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
!query.statement.types[i] && (query.statement.types[i] = x.readUInt32BE(7 + i * 4))
query.prepare && (statements[query.signature] = query.statement)
@@ -1605,7 +1625,7 @@ index 1b1cccd..7b6372c 100644
}
function RowDescription(x) {
-@@ -767,14 +1031,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -767,14 +1034,21 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
async function fetchArrayTypes() {
needsTypes = false
@@ -1635,7 +1655,7 @@ index 1b1cccd..7b6372c 100644
types.forEach(({ oid, typarray }) => addArrayType(oid, typarray))
}
-@@ -821,7 +1092,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
+@@ -821,7 +1095,16 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
function retry(q, error) {
delete statements[q.signature]
q.retried = error
@@ -1653,7 +1673,7 @@ index 1b1cccd..7b6372c 100644
}
function NotificationResponse(x) {
-@@ -1039,6 +1319,59 @@ function xor(a, b) {
+@@ -1039,6 +1322,59 @@ function xor(a, b) {
return buffer
}
diff --git a/test/large-pages/segmented-prior-view.test.ts b/test/large-pages/segmented-prior-view.test.ts
index 4dd9099d1e..c44cdf66c0 100644
--- a/test/large-pages/segmented-prior-view.test.ts
+++ b/test/large-pages/segmented-prior-view.test.ts
@@ -11,10 +11,13 @@
* keeps every entry of the prior body (key, summary, detail and slug copies: 12.8x the body for a 6 MiB page).
* Seams: segmentedPassWork.bytes (segment-blocks.ts) counts bytes read; the heap is sampled after a full collection
* when classification first reads the new body, with the prior view built and alive, against a baseline taken after a
- * warm-up call on another snapshot (so driver buffers a segment read leaves allocated count in both). The warm-up's
- * new body is small: an in-flight concurrent JIT compilation keeps the values its profiles saw alive as a GC root
- * outside the heap graph, so a sliced line of the warm-up's text could pin that whole text at the sample and not at
- * the baseline (with the 6 MiB new body, its sanitized copy: 8.4 MiB held in about one run in ten).
+ * warm-up call on another snapshot and one small statement on every pooled connection. The pool hands statements out in
+ * turn, so the warm-up and the measured call end on different connections: a connection that kept its last segment
+ * read (its receive buffer, or the statement and its rows) held up to 3 MiB of the prior body that the baseline did not,
+ * so the driver keeps neither (vendor/postgres connection.js). The warm-up's new body is small: an in-flight concurrent
+ * JIT compilation keeps the values its profiles saw alive as a GC root outside the heap graph, so a sliced line of the
+ * warm-up's text could pin that whole text at the sample and not at the baseline (with the 6 MiB new body, its
+ * sanitized copy: 8.4 MiB held in about one run in ten).
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import type { BrainEngine } from '../../src/core/engine.ts';
@@ -97,6 +100,7 @@ for (const kind of testBackends()) {
peak = Math.max(peak, process.memoryUsage().heapUsed);
return parsed.timeline;
} };
+ await Promise.all(Array.from({ length: 8 }, () => engine.executeRaw('SELECT 1')));
Bun.gc(true);
const base = process.memoryUsage().heapUsed;
expect((await timelineRowsToRemove(engine, body, snapshot, SLUG, 'editing'))?.count).toBe(DROPPED);
diff --git a/vendor/README.md b/vendor/README.md
index 70436cc3e0..ecbd5a5d5e 100644
--- a/vendor/README.md
+++ b/vendor/README.md
@@ -90,6 +90,15 @@ while it is still producing them. The next statement on that connection then
stored its rows after that many empty slots, so a one-row read came back as
`[empty, row]` (`test/e2e/postgres-driver-row-count-postgres.test.ts`).
+An idle connection keeps nothing from its last statement. Stock postgres.js
+left its receive buffer as an empty view over the last message it joined from
+several socket reads, so the buffer stayed allocated until the connection's
+next read. For a 1 MiB bytea row, that buffer is the 2 MiB hex DataRow. The
+desync tripwire keeps only the last statement's text, not the query, whose
+promise holds its rows. Pools hand statements out in turn, so before this fix
+each pooled connection held one segment read
+(`test/large-pages/segmented-prior-view.test.ts`).
+
## Reproduce and update
Run `bash vendor/update-postgres.sh --check` to download the pinned upstream
diff --git a/vendor/postgres/cf/src/connection.js b/vendor/postgres/cf/src/connection.js
index 0d102fe1f3..56bac11cc4 100644
--- a/vendor/postgres/cf/src/connection.js
+++ b/vendor/postgres/cf/src/connection.js
@@ -194,7 +194,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
mayPipeline = mayPipeline || q.options.onexecute(connection)
noteBudget(q)
lastWritten = q
- lastExecuted = q
+ lastExecuted = { string: q.string, strings: q.strings }
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
@@ -473,6 +473,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
remaining = 0
incomings = null
}
+
+ if (incoming.length === 0)
+ incoming = Buffer.alloc(0)
}
async function connect() {
diff --git a/vendor/postgres/cjs/src/connection.js b/vendor/postgres/cjs/src/connection.js
index a15e780e19..5a7b7eb9ba 100644
--- a/vendor/postgres/cjs/src/connection.js
+++ b/vendor/postgres/cjs/src/connection.js
@@ -192,7 +192,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
mayPipeline = mayPipeline || q.options.onexecute(connection)
noteBudget(q)
lastWritten = q
- lastExecuted = q
+ lastExecuted = { string: q.string, strings: q.strings }
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
@@ -471,6 +471,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
remaining = 0
incomings = null
}
+
+ if (incoming.length === 0)
+ incoming = Buffer.alloc(0)
}
async function connect() {
diff --git a/vendor/postgres/src/connection.js b/vendor/postgres/src/connection.js
index 7b6372cbe4..65ce1f6aca 100644
--- a/vendor/postgres/src/connection.js
+++ b/vendor/postgres/src/connection.js
@@ -192,7 +192,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
mayPipeline = mayPipeline || q.options.onexecute(connection)
noteBudget(q)
lastWritten = q
- lastExecuted = q
+ lastExecuted = { string: q.string, strings: q.strings }
return write(toBuffer(q))
&& !q.describeFirst
&& !q.cursorFn
@@ -471,6 +471,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
remaining = 0
incomings = null
}
+
+ if (incoming.length === 0)
+ incoming = Buffer.alloc(0)
}
async function connect() {