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.

RangeError: Maximum call stack size exceeded

See original GitHub issue

Summary When calling getClient() I keep having issues with this error before I can even execute anything. I don’t really understand what client.query.apply(client, arguments) is supposed to be doing so I am not sure how to fix it. I didn’t see anything about it in the documents. I guess the next step is to look through the node-postgres code here.

Has anyone seen an error like this before?

Code

const { Pool } = require('pg');
const pool = new Pool({
    user: process.env.DB_USER,
    host: process.env.DB_HOST,
    database: process.env.DB_NAME,
    password: process.env.DB_PASS,
    port: process.env.DB_PORT
});

module.exports = {
  query: (text, params, callback) => { // Single SQL Query
    const start = Date.now()
    return pool.query(text, params, (err, res) => {
      const duration = Date.now() - start
      if(err) {
          callback(err, res)
      } else {
        console.log('executed query', { text, duration, rows: res.rowCount })
        console.log('params', {params})
        console.log('response', {res})
        callback(err, res)
      }
    })
  },
  getClient: (callback) => { // Multiple SQL Queries
    pool.connect((err, client, done) => {
      const query = client.query.bind(client)
      // monkey patch the query method to keep track of the last query executed
      client.query = () => {
        client.lastQuery = arguments
        client.query.apply(client, arguments)
      }
      // set a timeout of 5 seconds, after which we will log this client's last query
      const timeout = setTimeout(() => {
        console.error('A client has been checked out for more than 5 seconds!')
        console.error(`The last executed query on this client was: ${client.lastQuery}`)
      }, 5000)
      const release = (err) => {
        // call the actual 'done' method, returning this client to the pool
        done(err)
        // clear our timeout
        clearTimeout(timeout)
        // set the query method back to its old un-monkey-patched version
        client.query = query
      }
      callback(err, client, release)
    })
  }
}

Error

node-backend | /usr/src/service/db/index.js:33
node-backend |         client.query.apply(client, arguments)
node-backend |                      ^
node-backend | 
node-backend | RangeError: Maximum call stack size exceeded
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend |     at Client.client.query (/usr/src/service/db/index.js:33:22)
node-backend | npm ERR! code ELIFECYCLE
node-backend | npm ERR! errno 1
node-backend | npm ERR! server@0.0.0 server: `node server.js`
node-backend | npm ERR! Exit status 1
node-backend | npm ERR!
node-backend | npm ERR! Failed at the server@0.0.0 server script.
node-backend | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sehropecommented, Oct 22, 2019

@gemurdock The idea is to centralize all the interactions with this pg module in a single part of your application so that if you need to customize something later it can be done in one place. For example you could add before / after logging to print the SQL, errors, or timing information to the console for debugging.

Without the centralization you’d need to make that change in possibly many different call sites throughout your application.

The code in example is indeed incorrect. There’s a couple things on there that should be updated. What it’s trying to do is to print an alert if a connection is checked out for more that 5-seconds with the rationale being that it’s probably a coding error of a connection not being properly returned to the pool.

1reaction
charmandercommented, Oct 22, 2019

Yes, the documentation is wrong there. (It also tries to use arguments in an arrow function, and makes bind wrappers on top of other bind wrappers.) Something like this is closer:

const query = client.query

client.query = (...args) => {
  client.lastQuery = args
  return query.apply(this, args)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Maximum call stack size exceeded error
It means that somewhere in your code, you are calling a function which in turn calls another function and so forth, until you...
Read more >
JavaScript Error: Maximum Call Stack Size Exceeded
If you see the “Maximum Call Stack Size Exceeded” error, there's likely a problem with a recursive function within your JavaScript code.
Read more >
Uncaught RangeError: Maximum call ... - Net-Informations.Com
This error is almost always means you have a problem with recursion in JavaScript code, as there isn't any other way in JavaScript...
Read more >
RangeError: Maximum call stack size exceeded - Educative.io
The most common source for this error is infinite recursion. You must have a recursive function in your code whose base case is...
Read more >
RangeError: Maximum call stack size exceeded in JavaScript
The "RangeError: Maximum call stack size exceeded" error occurs when a function is called so many times that the invocations exceed the call...
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