diff --git a/server/src/db/engine/postgres/plugin-data-store.ts b/server/src/db/engine/postgres/plugin-data-store.ts index dd8de9ab8..a3f75640d 100644 --- a/server/src/db/engine/postgres/plugin-data-store.ts +++ b/server/src/db/engine/postgres/plugin-data-store.ts @@ -91,6 +91,33 @@ const DEFAULT_LIMITS: PostgresPluginDataLimits = { now: () => Date.now(), }; +/** Words whose parenthesis takes an array: `= ANY($1)`, `<> ALL($1)`. */ +const ARRAY_CALLS = new Set(['any', 'all', 'some']); + +/** Whether a placeholder of `statement` takes an array: inside ANY, ALL or SOME, or cast to an array type. */ +function takesArray(statement: PluginStatement): boolean { + const { tokens } = statement; + return tokens.some((token, i) => { + if (token.kind !== 'param') return false; + const before = tokens[i - 2]; + const after = tokens[i + 1]; + const called = tokens[i - 1]?.kind === 'punct' && before?.kind === 'word' && ARRAY_CALLS.has(before.value); + const cast = after?.kind === 'punct' && after.value === '::'; + return called || (cast && tokens.slice(i + 2, i + 5).some((t) => t.kind === 'punct' && t.value === '[')); + }); +} + +/** + * The values a statement is bound with. The single-array form, args [[a, b]], + * is spread as better-sqlite3 spreads it when the array fills the statement's + * placeholders and none of them takes an array. + */ +function bindValues(statement: PluginStatement, args: readonly unknown[]): readonly unknown[] { + const [only] = args; + if (args.length !== 1 || !Array.isArray(only) || only.length !== statement.params) return args; + return takesArray(statement) ? args : only; +} + /** Postgres's SQLSTATE for a statement cancelled by `statement_timeout`. */ const QUERY_CANCELED = '57014'; /** Rows a cursor fetches at once. */ @@ -219,7 +246,7 @@ export class PostgresPluginDataStore implements PluginDataStore { return this.call('query', (budget) => this.transaction(budget.ms, [statement], (client) => { const rows = { count: 0, max: maxRows, message: PLUGIN_DATA_ERRORS.queryTooManyRows(maxRows) }; - return this.stream(client, statement, args, rows, budget.check); + return this.stream(client, statement, bindValues(statement, args), rows, budget.check); }), ); } @@ -229,7 +256,7 @@ export class PostgresPluginDataStore implements PluginDataStore { const statement = this.single(sql, args); return this.call('exec', (budget) => this.transaction(budget.ms, [statement], async (client) => ({ - changes: await this.run(client, statement, args), + changes: await this.run(client, statement, bindValues(statement, args)), })), ); } @@ -261,7 +288,7 @@ export class PostgresPluginDataStore implements PluginDataStore { this.transaction(budget.ms, statements, async (client) => { const rows = { count: 0, max: maxRows, message: PLUGIN_DATA_ERRORS.txTooManyRows(maxRows) }; const results = await inOrder(statements, async (statement, index) => { - const args = ops[index].args ?? []; + const args = bindValues(statement, ops[index].args ?? []); // One budget for the whole batch: each statement after the first gets what is left of it. if (index > 0) await this.setTimeout(client, budget.remaining()); const result = statement.returnsRows diff --git a/server/tests/unit/db/engine/postgres/plugin-data-store.test.ts b/server/tests/unit/db/engine/postgres/plugin-data-store.test.ts index 1d29ab1f9..d83375365 100644 --- a/server/tests/unit/db/engine/postgres/plugin-data-store.test.ts +++ b/server/tests/unit/db/engine/postgres/plugin-data-store.test.ts @@ -354,6 +354,28 @@ describe('Postgres plugin data store', () => { expect(configs.filter((q) => q.queryMode === 'extended')).toHaveLength(plugin.length); }); + it('PGSTORE-021: the single-array bind form is spread as on SQLite when it fills the placeholders, and an array a placeholder takes stays whole', async () => { + const { store: s, configs, cursors } = store({ changes: 1 }); + const bytes = Buffer.from([0, 1, 255]); + await s.query('SELECT v FROM kv WHERE k = $1', [['key']]); + await s.exec('INSERT INTO blobs (k, v) VALUES ($1, $2)', [['key', bytes]]); + await s.tx([{ sql: 'DELETE FROM kv WHERE k = $1 AND v = $2', args: [['key', 2]] }]); + await s.query('SELECT v FROM kv WHERE k = ANY($1)', [['key']]); + await s.query('SELECT v FROM kv WHERE k = $1::text[]', [['key']]); + await s.exec('INSERT INTO blobs (k, v) VALUES ($1, $2)', ['key', 'v']); + + expect(cursors.filter((c) => c.text.startsWith('SELECT v')).map((c) => c.values)).toEqual([ + ['key'], + ['{"key"}'], + ['{"key"}'], + ]); + expect(configs.filter((q) => q.queryMode === 'extended').map((q) => q.values)).toEqual([ + ['key', bytes], + ['key', 2], + ['key', 'v'], + ]); + }); + it('PGSTORE-013: every read goes through a cursor on the same client, closed however the read ends', async () => { // EXTQ-002 before the store checked out its own clients: what is not a plugin write reaches pg as it was. const rows = Array.from({ length: 7 }, (_, i) => ({ i }));