Skip to content

Commit f85b851

Browse files
feat(Postgres Node): Batching warning for executeQuery operation insert query (#14287)
1 parent c5e2d2d commit f85b851

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

packages/nodes-base/nodes/Postgres/test/v2/utils.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IDataObject, INode } from 'n8n-workflow';
1+
import type { IDataObject, IExecuteFunctions, INode } from 'n8n-workflow';
22
import { NodeOperationError } from 'n8n-workflow';
33
import pgPromise from 'pg-promise';
44

@@ -18,6 +18,7 @@ import {
1818
convertValuesToJsonWithPgp,
1919
hasJsonDataTypeInSchema,
2020
evaluateExpression,
21+
addExecutionHints,
2122
} from '../../v2/helpers/utils';
2223

2324
const node: INode = {
@@ -535,3 +536,38 @@ describe('Test PostgresV2, convertArraysToPostgresFormat', () => {
535536
});
536537
});
537538
});
539+
540+
describe('Test PostgresV2, addExecutionHints', () => {
541+
it('should add batching insert hint to executeQuery operation', () => {
542+
const context = {
543+
getNodeParameter: (parameterName: string) => {
544+
if (parameterName === 'options.queryBatching') {
545+
return 'single';
546+
}
547+
if (parameterName === 'query') {
548+
return 'INSERT INTO my_test_table VALUES (`{{ $json.name }}`)';
549+
}
550+
},
551+
addExecutionHints: jest.fn(),
552+
} as unknown as IExecuteFunctions;
553+
554+
addExecutionHints(context, [{ json: {} }, { json: {} }], 'executeQuery', false);
555+
expect(context.addExecutionHints).toHaveBeenCalledWith({
556+
message:
557+
"Inserts were batched for performance. If you need to preserve item matching, consider changing 'Query batching' to 'Independent' in the options.",
558+
location: 'outputPane',
559+
});
560+
});
561+
it('should add run per item hint to select operation', () => {
562+
const context = {
563+
addExecutionHints: jest.fn(),
564+
} as unknown as IExecuteFunctions;
565+
566+
addExecutionHints(context, [{ json: {} }, { json: {} }], 'select', false);
567+
expect(context.addExecutionHints).toHaveBeenCalledWith({
568+
location: 'outputPane',
569+
message:
570+
"This node ran 2 times, once for each input item. To run for the first item only, enable 'execute once' in the node settings",
571+
});
572+
});
573+
});

packages/nodes-base/nodes/Postgres/v2/actions/common.descriptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const optionsCollection: INodeProperties = {
5252
description: 'A single query for all incoming items',
5353
},
5454
{
55-
name: 'Independently',
55+
name: 'Independent',
5656
value: 'independently',
5757
description: 'Execute one query per incoming item of the run',
5858
},

packages/nodes-base/nodes/Postgres/v2/actions/router.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as database from './database/Database.resource';
55
import type { PostgresType } from './node.type';
66
import { configurePostgres } from '../../transport';
77
import type { PostgresNodeCredentials, PostgresNodeOptions } from '../helpers/interfaces';
8-
import { configureQueryRunner } from '../helpers/utils';
8+
import { addExecutionHints, configureQueryRunner } from '../helpers/utils';
99

1010
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
1111
let returnData: INodeExecutionData[] = [];
@@ -53,12 +53,7 @@ export async function router(this: IExecuteFunctions): Promise<INodeExecutionDat
5353
);
5454
}
5555

56-
if (operation === 'select' && items.length > 1 && !node.executeOnce) {
57-
this.addExecutionHints({
58-
message: `This node ran ${items.length} times, once for each input item. To run for the first item only, enable 'execute once' in the node settings`,
59-
location: 'outputPane',
60-
});
61-
}
56+
addExecutionHints(this, items, operation, node.executeOnce);
6257

6358
return [returnData];
6459
}

packages/nodes-base/nodes/Postgres/v2/helpers/utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,3 +616,30 @@ export const convertArraysToPostgresFormat = (
616616
}
617617
}
618618
};
619+
620+
export function addExecutionHints(
621+
context: IExecuteFunctions,
622+
items: INodeExecutionData[],
623+
operation: string,
624+
executeOnce: boolean | undefined,
625+
) {
626+
if (operation === 'select' && items.length > 1 && !executeOnce) {
627+
context.addExecutionHints({
628+
message: `This node ran ${items.length} times, once for each input item. To run for the first item only, enable 'execute once' in the node settings`,
629+
location: 'outputPane',
630+
});
631+
}
632+
633+
if (
634+
operation === 'executeQuery' &&
635+
items.length > 1 &&
636+
(context.getNodeParameter('options.queryBatching', 0, 'single') as string) === 'single' &&
637+
(context.getNodeParameter('query', 0, '') as string).toLowerCase().startsWith('insert')
638+
) {
639+
context.addExecutionHints({
640+
message:
641+
"Inserts were batched for performance. If you need to preserve item matching, consider changing 'Query batching' to 'Independent' in the options.",
642+
location: 'outputPane',
643+
});
644+
}
645+
}

0 commit comments

Comments
 (0)