question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query streams break with `query_timeout`

See original GitHub issue

Bug:

If a query_timeout is reached when using a submit-able query object that doesn’t have a callback method on it (such as all streaming query types, and probably most user-crafted submit-ables), then the Client will throw a queryCallback is not a function error. General repro would be:

const pg = require('pg')
const QueryStream = require('pg-query-stream')
const db = new pg.Client({ ..., query_timeout: 1000 })
db.connect(err => {
  if (err) throw new Error('Connection error')
  const qs = new QueryStream('SELECT pg_sleep(2)')
  db.query(qs)
    .on('data', d => console.log('data', d))
    .on('end', d => console.log('end', d))
    .on('error', d => (qs.close(), console.log('error', d)))
})

Analysis:

Although the node-postgres Query object has a callback method for handling bulk results, other streaming objects, such as pg-cursor and pg-query-stream, do not have such a method. Looking at the code in client.js, we see: https://github.com/brianc/node-postgres/blob/master/lib/client.js#L431-L457

If the Query object has a submit method on it, and no callback method, and if no callback is passed in as the second (values) argument to Client#query, then query.callback will be undefined. Subsequently if a readTimeout is specified (either as a query_timeout on the Query object, or as part of the database configuration), then the read timeout assumes that query.callback is defined and attempts to call it, resulting in a TypeError: queryCallback is not a function.

Fix:

Since the callback attribute appears to be an implementation detail of the node-postgres Query object, and the documentation for Client#query does not indicate that this method should be present, or that a second parameter should be passed, it’s unclear whether the fix is to make callback a required attribute on all Query-like objects, or if it’s to fail gracefully if such a callback does not exist.

Furthermore, it’s debatable whether a query_timeout is even appropriate on non-bulk query types (timeout to results ready? timeout until entire result set has been retrieved?), and whether that timeout code should move into the typical bulk Query object itself, dodging the issue entirely.

Because that’s unclear, I’m leaving it to you to decide. Happy to submit a pull-request with a fix if you can tell me where/what you think the appropriate fix is.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:19
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
vitaly-tcommented, Jul 5, 2022

@brianc The issue still exists in the latest version. Could you have a look at it, please?

1reaction
avivshafircommented, Jul 5, 2022

any update?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the "webSphereDefaultQueryTimeout" property ... - IBM
A break such as Ctrl-c (or setQueryTimeout) can be sent as part of the normal data stream (inband), or as a separate asynchronous...
Read more >
Troubleshoot query time-out errors - SQL Server
This article describes how to troubleshoot the time-out errors when you run slow-running queries.
Read more >
Query Timeout - Informatica Documentation
Query Timeout. You can time out a search that takes a longer time to query the database. To time out the searches, perform...
Read more >
com.datastax.oss.driver.api.core.DriverTimeoutException
timeout . Also I didn't understand why it is 500ms, I've set init-query-timeout=10000 milliseconds by reference its default value ...
Read more >
KSQL Syntax Reference | Confluent Platform 5.2.0
CREATE STREAM/TABLE AS SELECT (from existing streams/tables); SELECT (non-persistent query). Use the following syntax to declare nested data:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found