Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long queries with parameters #303

Closed
eugene1g opened this issue Apr 4, 2022 · 4 comments
Closed

Long queries with parameters #303

eugene1g opened this issue Apr 4, 2022 · 4 comments

Comments

@eugene1g
Copy link
Contributor

eugene1g commented Apr 4, 2022

I have a custom query builder to help users run complex queries against the database using parameters to apply various filters. This query builder can generate really long queries, but not long enough to be a problem for PG. However, running those long queries with postgres doesn't work right now when those queries use parameters.

My real code is too complex to be useful, but here's a simple contrived example -

// test.mjs
import postgres from "postgres";

const sql = postgres({
  // The issue happens when two queries are executed sequentially over the same connection
  // It also happens with any number of connections, but easier to demo with a single network connection to schedule the queries predictably
  max: 1,
});

const padding = ".".repeat(16_342);

const res = await Promise.all([
  // For the bug to surface, the query needs to be 16,359 chars or longer
  // We'll create a "large" query to go first over the wire to PG
  // If we reduce the padding even by one character, everything works as expected (try removing the whitespace in the closing ` */`
  sql.unsafe(`SELECT 0+$1 /*${padding} */`, [1]),
  // Any following queries will fail when it runs over the same network connection
  sql.unsafe(`SELECT 0+$1+$2+$3`, [1, 2, 3]),
]);

console.log(res);
process.exit(0);

The specific error I get is -

PostgresError: bind message supplies 1 parameters, but prepared statement "" requires 2
    at ErrorResponse (..../postgres/cjs/src/connection.js:775:26)
    at handle (..../node_modules/postgres/cjs/src/connection.js:484:6)
    at Socket.data (..../node_modules/postgres/cjs/src/connection.js:328:9)
    at Socket.emit (node:events:527:28)
    at Socket.emit (node:domain:475:12)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Socket.Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)

It vaguely feels like the problem would be in the network serialization logic - perhaps some Buffer is overflowing or not being cleared - but I haven't narrowed this down further.

@eugene1g
Copy link
Contributor Author

eugene1g commented Apr 5, 2022

My example above is contrived, and for a more realistic example we can use a common pattern of running DB logic in parallel like so -

// test2.mjs
import postgres from "postgres";

const sql = postgres({
  // The issue happens when two queries are executed sequentially over the same connection
  // It also happens with any number of connections, but easier to demo with a single network connection to schedule the queries predictably
  max: 1,
});

const res = await Promise.all([
  // run IO-heavy functions in parallel
  fetchInfo(1), 
  calcSum(1, 2, 3),
]);

async function fetchInfo(someValue) {
  // Calling .unsafe will throw, calling sql`` will just hang
  // const padding = ".".repeat(16_342);
  // const info = await sql.unsafe(`SELECT 0+$1 /*${padding} */`, [someValue]);
  const info =
    await sql`SELECT 0+${1} /*...............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................*/`;
  // do some post-processing of $info, then return it
  return info;
}

async function calcSum(val1, val2, val3) {
  // Calling .unsafe will throw, calling sql`` will just hang
  // const info = await sql.unsafe(`SELECT 0+$1+$2+$3`, [val1, val2, val3]);
  const info = await sql`SELECT 0+${val1}+${val2}+${val3}`;
  // do some post-processing of $info, then return it
  return info;
}

@porsager
Copy link
Owner

porsager commented Apr 5, 2022

@eugene1g Very nice find, and a great description!

This is happening due to drain implicitly dequeueing the next query even if a query is currently in the describe phase (waiting for response). That results in describe being sent for the second query before sending the parameters for the first.

I'll push a fix ASAP!

@eugene1g
Copy link
Contributor Author

eugene1g commented Apr 5, 2022

Amazing

@porsager
Copy link
Owner

porsager commented Apr 5, 2022

☺️ Thanks - now released in v3.0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants