Skip to content

Commit 35cb10c

Browse files
authored
refactor(Postgres Node): Backport connection pooling to postgres v1 (#12484)
1 parent c97bd48 commit 35cb10c

File tree

12 files changed

+49
-69
lines changed

12 files changed

+49
-69
lines changed

Diff for: packages/@n8n/nodes-langchain/nodes/memory/MemoryPostgresChat/MemoryPostgresChat.node.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
22
import { PostgresChatMessageHistory } from '@langchain/community/stores/message/postgres';
33
import { BufferMemory, BufferWindowMemory } from 'langchain/memory';
4+
import { configurePostgres } from 'n8n-nodes-base/dist/nodes/Postgres/transport';
45
import type { PostgresNodeCredentials } from 'n8n-nodes-base/dist/nodes/Postgres/v2/helpers/interfaces';
56
import { postgresConnectionTest } from 'n8n-nodes-base/dist/nodes/Postgres/v2/methods/credentialTest';
6-
import { configurePostgres } from 'n8n-nodes-base/dist/nodes/Postgres/v2/transport';
77
import type {
88
ISupplyDataFunctions,
99
INodeType,

Diff for: packages/@n8n/nodes-langchain/nodes/vector_store/VectorStorePGVector/VectorStorePGVector.node.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
type PGVectorStoreArgs,
55
} from '@langchain/community/vectorstores/pgvector';
66
import type { EmbeddingsInterface } from '@langchain/core/embeddings';
7+
import { configurePostgres } from 'n8n-nodes-base/dist/nodes/Postgres/transport';
78
import type { PostgresNodeCredentials } from 'n8n-nodes-base/dist/nodes/Postgres/v2/helpers/interfaces';
8-
import { configurePostgres } from 'n8n-nodes-base/dist/nodes/Postgres/v2/transport';
99
import type { INodeProperties } from 'n8n-workflow';
1010
import type pg from 'pg';
1111

Diff for: packages/nodes-base/credentials/Postgres.credentials.ts

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export class Postgres implements ICredentialType {
3737
},
3838
default: '',
3939
},
40+
{
41+
displayName: 'Maximum Number of Connections',
42+
name: 'maxConnections',
43+
type: 'number',
44+
default: 100,
45+
description:
46+
'Make sure this value times the number of workers you have is lower than the maximum number of connections your postgres instance allows.',
47+
},
4048
{
4149
displayName: 'Ignore SSL Issues (Insecure)',
4250
name: 'allowUnauthorizedCerts',

Diff for: packages/nodes-base/nodes/Postgres/PostgresTrigger.functions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import type {
77
INodeListSearchItems,
88
} from 'n8n-workflow';
99

10+
import { configurePostgres } from './transport';
1011
import type { PgpDatabase, PostgresNodeCredentials } from './v2/helpers/interfaces';
11-
import { configurePostgres } from './v2/transport';
1212

1313
export function prepareNames(id: string, mode: string, additionalFields: IDataObject) {
1414
let suffix = id.replace(/-/g, '_');

Diff for: packages/nodes-base/nodes/Postgres/v2/transport/index.ts renamed to packages/nodes-base/nodes/Postgres/transport/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
PgpConnectionParameters,
1717
PostgresNodeCredentials,
1818
PostgresNodeOptions,
19-
} from '../helpers/interfaces';
19+
} from '../v2/helpers/interfaces';
2020

2121
const getPostgresConfig = (
2222
credentials: PostgresNodeCredentials,
@@ -29,6 +29,7 @@ const getPostgresConfig = (
2929
user: credentials.user,
3030
password: credentials.password,
3131
keepAlive: true,
32+
max: credentials.maxConnections,
3233
};
3334

3435
if (options.connectionTimeout) {

Diff for: packages/nodes-base/nodes/Postgres/v1/PostgresV1.node.ts

+23-52
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type {
22
ICredentialsDecrypted,
33
ICredentialTestFunctions,
4-
IDataObject,
54
IExecuteFunctions,
65
INodeCredentialTestResult,
76
INodeExecutionData,
@@ -10,11 +9,12 @@ import type {
109
INodeTypeDescription,
1110
} from 'n8n-workflow';
1211
import { NodeConnectionType, NodeOperationError } from 'n8n-workflow';
13-
import pgPromise from 'pg-promise';
1412

1513
import { oldVersionNotice } from '@utils/descriptions';
1614

1715
import { pgInsertV2, pgQueryV2, pgUpdate, wrapData } from './genericFunctions';
16+
import { configurePostgres } from '../transport';
17+
import type { PgpConnection, PostgresNodeCredentials } from '../v2/helpers/interfaces';
1818

1919
const versionDescription: INodeTypeDescription = {
2020
displayName: 'Postgres',
@@ -298,33 +298,27 @@ export class PostgresV1 implements INodeType {
298298
this: ICredentialTestFunctions,
299299
credential: ICredentialsDecrypted,
300300
): Promise<INodeCredentialTestResult> {
301-
const credentials = credential.data as IDataObject;
302-
try {
303-
const pgp = pgPromise();
304-
const config: IDataObject = {
305-
host: credentials.host as string,
306-
port: credentials.port as number,
307-
database: credentials.database as string,
308-
user: credentials.user as string,
309-
password: credentials.password as string,
310-
};
301+
const credentials = credential.data as PostgresNodeCredentials;
311302

312-
if (credentials.allowUnauthorizedCerts === true) {
313-
config.ssl = {
314-
rejectUnauthorized: false,
315-
};
316-
} else {
317-
config.ssl = !['disable', undefined].includes(credentials.ssl as string | undefined);
318-
config.sslmode = (credentials.ssl as string) || 'disable';
319-
}
303+
let connection: PgpConnection | undefined;
320304

321-
const db = pgp(config);
322-
await db.connect();
305+
try {
306+
const { db } = await configurePostgres.call(this, credentials, {});
307+
308+
// Acquires a new connection that can be used to to run multiple
309+
// queries on the same connection and must be released again
310+
// manually.
311+
connection = await db.connect();
323312
} catch (error) {
324313
return {
325314
status: 'Error',
326315
message: error.message,
327316
};
317+
} finally {
318+
if (connection) {
319+
// release connection
320+
await connection.done();
321+
}
328322
}
329323
return {
330324
status: 'OK',
@@ -335,42 +329,19 @@ export class PostgresV1 implements INodeType {
335329
};
336330

337331
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
338-
const credentials = await this.getCredentials('postgres');
332+
const credentials = await this.getCredentials<PostgresNodeCredentials>('postgres');
339333
const largeNumbersOutput = this.getNodeParameter(
340334
'additionalFields.largeNumbersOutput',
341335
0,
342336
'',
343337
) as string;
344338

345-
const pgp = pgPromise();
346-
347-
if (largeNumbersOutput === 'numbers') {
348-
pgp.pg.types.setTypeParser(20, (value: string) => {
349-
return parseInt(value, 10);
350-
});
351-
pgp.pg.types.setTypeParser(1700, (value: string) => {
352-
return parseFloat(value);
353-
});
354-
}
355-
356-
const config: IDataObject = {
357-
host: credentials.host as string,
358-
port: credentials.port as number,
359-
database: credentials.database as string,
360-
user: credentials.user as string,
361-
password: credentials.password as string,
362-
};
363-
364-
if (credentials.allowUnauthorizedCerts === true) {
365-
config.ssl = {
366-
rejectUnauthorized: false,
367-
};
368-
} else {
369-
config.ssl = !['disable', undefined].includes(credentials.ssl as string | undefined);
370-
config.sslmode = (credentials.ssl as string) || 'disable';
371-
}
372-
373-
const db = pgp(config);
339+
const { db, pgp } = await configurePostgres.call(this, credentials, {
340+
largeNumbersOutput:
341+
largeNumbersOutput === 'numbers' || largeNumbersOutput === 'text'
342+
? largeNumbersOutput
343+
: undefined,
344+
});
374345

375346
let returnItems: INodeExecutionData[] = [];
376347

Diff for: packages/nodes-base/nodes/Postgres/v2/actions/router.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { NodeExecutionOutput, NodeOperationError } from 'n8n-workflow';
33

44
import * as database from './database/Database.resource';
55
import type { PostgresType } from './node.type';
6+
import { configurePostgres } from '../../transport';
67
import type { PostgresNodeCredentials, PostgresNodeOptions } from '../helpers/interfaces';
78
import { configureQueryRunner } from '../helpers/utils';
8-
import { configurePostgres } from '../transport';
99

1010
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
1111
let returnData: INodeExecutionData[] = [];

Diff for: packages/nodes-base/nodes/Postgres/v2/helpers/interfaces.ts

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type EnumInfo = {
2828
export type PgpClient = pgPromise.IMain<{}, pg.IClient>;
2929
export type PgpDatabase = pgPromise.IDatabase<{}, pg.IClient>;
3030
export type PgpConnectionParameters = pg.IConnectionParameters<pg.IClient>;
31+
export type PgpConnection = pgPromise.IConnected<{}, pg.IClient>;
3132
export type ConnectionsData = { db: PgpDatabase; pgp: PgpClient };
3233

3334
export type QueriesRunner = (
@@ -57,6 +58,7 @@ export type PostgresNodeCredentials = {
5758
database: string;
5859
user: string;
5960
password: string;
61+
maxConnections: number;
6062
allowUnauthorizedCerts?: boolean;
6163
ssl?: 'disable' | 'allow' | 'require' | 'verify' | 'verify-full';
6264
} & (

Diff for: packages/nodes-base/nodes/Postgres/v2/methods/credentialTest.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@ import type {
44
INodeCredentialTestResult,
55
} from 'n8n-workflow';
66

7-
import type { PgpClient, PostgresNodeCredentials } from '../helpers/interfaces';
8-
import { configurePostgres } from '../transport';
7+
import { configurePostgres } from '../../transport';
8+
import type { PgpConnection, PostgresNodeCredentials } from '../helpers/interfaces';
99

1010
export async function postgresConnectionTest(
1111
this: ICredentialTestFunctions,
1212
credential: ICredentialsDecrypted,
1313
): Promise<INodeCredentialTestResult> {
1414
const credentials = credential.data as PostgresNodeCredentials;
1515

16-
let pgpClientCreated: PgpClient | undefined;
16+
let connection: PgpConnection | undefined;
1717

1818
try {
19-
const { db, pgp } = await configurePostgres.call(this, credentials, {});
19+
const { db } = await configurePostgres.call(this, credentials, {});
2020

21-
pgpClientCreated = pgp;
22-
23-
await db.connect();
21+
connection = await db.connect();
2422
} catch (error) {
2523
let message = error.message as string;
2624

@@ -41,8 +39,8 @@ export async function postgresConnectionTest(
4139
message,
4240
};
4341
} finally {
44-
if (pgpClientCreated) {
45-
pgpClientCreated.end();
42+
if (connection) {
43+
await connection.done();
4644
}
4745
}
4846
return {

Diff for: packages/nodes-base/nodes/Postgres/v2/methods/listSearch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ILoadOptionsFunctions, INodeListSearchResult } from 'n8n-workflow';
22

3+
import { configurePostgres } from '../../transport';
34
import type { PostgresNodeCredentials } from '../helpers/interfaces';
4-
import { configurePostgres } from '../transport';
55

66
export async function schemaSearch(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
77
const credentials = await this.getCredentials<PostgresNodeCredentials>('postgres');

Diff for: packages/nodes-base/nodes/Postgres/v2/methods/loadOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
22

3+
import { configurePostgres } from '../../transport';
34
import type { PostgresNodeCredentials } from '../helpers/interfaces';
45
import { getTableSchema } from '../helpers/utils';
5-
import { configurePostgres } from '../transport';
66

77
export async function getColumns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
88
const credentials = await this.getCredentials<PostgresNodeCredentials>('postgres');

Diff for: packages/nodes-base/nodes/Postgres/v2/methods/resourceMapping.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ILoadOptionsFunctions, ResourceMapperFields, FieldType } from 'n8n-workflow';
22

3+
import { configurePostgres } from '../../transport';
34
import type { PostgresNodeCredentials } from '../helpers/interfaces';
45
import { getEnumValues, getEnums, getTableSchema, uniqueColumns } from '../helpers/utils';
5-
import { configurePostgres } from '../transport';
66

77
const fieldTypeMapping: Partial<Record<FieldType, string[]>> = {
88
string: ['text', 'varchar', 'character varying', 'character', 'char'],

0 commit comments

Comments
 (0)