Skip to content

Commit 5ff0659

Browse files
authored
fix(duckdb): install the PostgreSQL extension on first use (#1118)
1 parent a961be8 commit 5ff0659

4 files changed

Lines changed: 28 additions & 9 deletions

File tree

‎__tests__/duckdb/loaders/postgresql.test.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
import { describe, it, expect, afterEach } from 'vitest';
1+
import { describe, it, expect, afterEach, vi } from 'vitest';
22

33
import { DuckDBEngine } from '../../../src/duckdb/engine';
4+
import { loadPostgreSQL } from '../../../src/duckdb/loaders/postgresql';
45
import { getLLMConfig } from '../../test-utils';
56

7+
it.each([
8+
{ installed: false, commands: [['INSTALL postgres'], ['LOAD postgres']] },
9+
{ installed: true, commands: [['LOAD postgres']] },
10+
])('loads the PostgreSQL extension with installed=$installed', async ({ installed, commands }) => {
11+
const source = await loadPostgreSQL({ host: 'localhost', database: 'analytics' });
12+
const conn = {
13+
run: vi.fn().mockResolvedValue(undefined),
14+
runAndReadAll: vi
15+
.fn()
16+
.mockResolvedValueOnce({ getRowObjectsJson: () => (installed ? [{ extension_name: 'postgres_scanner' }] : []) })
17+
.mockResolvedValue({ getRowObjectsJson: () => [] }),
18+
};
19+
20+
await source.register(conn);
21+
22+
expect(conn.run.mock.calls).toEqual([...commands, [expect.stringContaining('(TYPE postgres, READ_ONLY)')]]);
23+
});
24+
625
describe.skipIf(process.env.AVA_POSTGRESQL_TEST !== '1')('loaders/postgresql', () => {
726
let engine: DuckDBEngine | null = null;
827

‎src/duckdb/engine.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ import type {
3636
/**
3737
* Process-level security baseline: DuckDB extensions run with the same
3838
* privileges as the host process, so disable auto-install/auto-load and keep
39-
* community extensions off by default at instance creation. The mysql/postgres
40-
* loaders still work because those extensions are statically linked (built-in),
41-
* so an explicit `LOAD mysql`/`LOAD postgres` is unaffected by these flags.
39+
* community extensions off by default at instance creation. Data source
40+
* loaders can explicitly install and load the extensions they require.
4241
* https://duckdb.org/docs/current/operations_manual/securing_duckdb/securing_extensions
4342
*/
4443
const EXTENSION_LOCKDOWN = {

‎src/duckdb/loaders/excel.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ export async function loadExcel(options: ExcelSourceOptions): Promise<LoadedSour
5353
let sheetNames: string[] = [];
5454
return {
5555
register: async (conn, namespace) => {
56-
// The excel extension is dynamically loaded, not statically linked like
57-
// mysql/postgres, so it must be installed explicitly. EXTENSION_LOCKDOWN
58-
// only blocks *auto*-install; an explicit INSTALL of a known official
59-
// extension is allowed and is idempotent (a no-op once installed). This
60-
// keeps CI (fresh ~/.duckdb) working the same as a local machine.
56+
// Install explicitly because the engine disables automatic extension installation.
6157
await conn.run('INSTALL excel');
6258
await conn.run('LOAD excel');
6359
const available = readSheetNames(filePath);

‎src/duckdb/loaders/postgresql.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ export async function loadPostgreSQL(options: PostgreSQLSourceOptions, alias = A
116116
return {
117117
register: async (conn, namespace) => {
118118
try {
119+
const installedExtensions = await conn.runAndReadAll(
120+
`SELECT extension_name FROM duckdb_extensions()
121+
WHERE installed AND (extension_name = 'postgres' OR list_contains(aliases, 'postgres'))`
122+
);
123+
if (!installedExtensions.getRowObjectsJson().length) await conn.run('INSTALL postgres');
119124
await conn.run('LOAD postgres');
120125
// READ_ONLY keeps the analysis read-only against the source database
121126
await conn.run(

0 commit comments

Comments
 (0)