fix(db): run plugin SQL on a pool client of its own instead of patching the pool

Plugin statements on Postgres went over the extended query protocol through a
marker parameter and a hook that replaced query() on every client of TREK's
pool. That kept a plugin to one statement per round trip, but it changed
TREK's own hot path for a concern only plugin data has.

The plugin data store now checks a pg client out of the ORM's pool for each
transaction, through MikroORM's getNativeClient, and runs the transaction on
it directly. Every plugin write goes as a query with queryMode 'extended' and
every read through a pg-cursor on that client, so Postgres parses exactly one
statement either way. The settings the transaction makes are local, and
before the client goes back to the pool the store drops temporary tables,
cursors, listeners, session advisory locks and sequence state, or closes a
client it cannot bring back to that state. The row cap, quota, time budget,
untimed migrate, per-plugin queue, pool share and script commits are as
before, and plugin args are still sent without NUL.

The marker, the hook and its install in the Postgres driver are gone.
This commit is contained in:
Maurice
2026-11-17 21:50:30 +01:00
parent 633df91bd6
commit a884539a2b
13 changed files with 505 additions and 343 deletions
+2 -4
View File
@@ -14,7 +14,7 @@
import { buildApp } from '../../src/bootstrap';
import { buildOrmConfig, resolveEngine } from '../../src/db/engine';
import { readPostgresBaseline } from '../../src/db/engine/postgres/baseline';
import { PluginDataSlots, PostgresPluginDataStore } from '../../src/db/engine/postgres/plugin-data-store';
import { PluginDataSlots, poolClients, PostgresPluginDataStore } from '../../src/db/engine/postgres/plugin-data-store';
import { MoveRefusal, moveSqliteToPostgres, type MoveSummary } from '../../src/db/engine/postgres/sqlite-move/move';
import { SqliteEngine } from '../../src/db/engine/sqlite/sqlite-engine';
import { AppSettings } from '../../src/db/entities/AppSettings.entity';
@@ -45,7 +45,6 @@ import { makeTrip, readTripDays } from '../helpers/factories/trips';
import { makeUser } from '../helpers/factories/users';
import { databaseUrl, withAdmin } from './databases';
import { MikroORM, type EntityClass, type Options } from '@mikro-orm/core';
import type { AbstractSqlConnection } from '@mikro-orm/sql';
import type { INestApplication } from '@nestjs/common';
import Database from 'better-sqlite3';
@@ -270,8 +269,7 @@ describe('moving a SQLite install to Postgres', () => {
const highest = Math.max(...(await findRows(sqliteOrm, Users)).map((row) => row.id));
expect(user.id).toBeGreaterThan(highest);
const client = (pgOrm.em.getConnection() as AbstractSqlConnection).getClient<object>();
const store = new PostgresPluginDataStore(PLUGIN_ID, client, new PluginDataSlots(1));
const store = new PostgresPluginDataStore(PLUGIN_ID, poolClients(pgOrm.em.getConnection()), new PluginDataSlots(1));
expect(await store.query('select id, body, score from notes order by id')).toEqual([
{ id: 1, body: 'first', score: 1.5 },
{ id: 2, body: 'second', score: null },