From 4f987d5ea98676463ba532e45631f83a85724523 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Wed, 8 Mar 2023 22:45:14 +0100 Subject: [PATCH] Ensure queries are not pushed on connections with active cursors - fixes #411 --- src/connection.js | 1 + tests/index.js | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/connection.js b/src/connection.js index 4427f13d..b3d25e72 100644 --- a/src/connection.js +++ b/src/connection.js @@ -166,6 +166,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose build(q) return write(toBuffer(q)) && !q.describeFirst + && !q.cursorFn && sent.length < max_pipeline && (!q.options.onexecute || q.options.onexecute(connection)) } catch (error) { diff --git a/tests/index.js b/tests/index.js index 9c4ab427..f59c641b 100644 --- a/tests/index.js +++ b/tests/index.js @@ -2472,3 +2472,24 @@ t('Insert array with undefined transform', async() => { await sql`drop table test` ] }) + +t('concurrent cursors', async() => { + const xs = [] + + await Promise.all([...Array(7)].map((x, i) => [ + sql`select ${ i }::int as a, generate_series(1, 2) as x`.cursor(([x]) => xs.push(x.a + x.x)) + ]).flat()) + + return ['12233445566778', xs.join('')] +}) + +t('concurrent cursors multiple connections', async() => { + const sql = postgres({ ...options, max: 2 }) + const xs = [] + + await Promise.all([...Array(7)].map((x, i) => [ + sql`select ${ i }::int as a, generate_series(1, 2) as x`.cursor(([x]) => xs.push(x.a + x.x)) + ]).flat()) + + return ['12233445566778', xs.sort().join('')] +})