Search code examples
javascriptnode.jspostgresqlconnection-poolingnode-pg-pool

Node.js pg Pool slow on second execute


I've run into a strange issue that I can't seem to get right.

I'm using pg pooling in a Node.js app and it runs really quickly on the first execute and then on the second one it is dramatically slower (30/50secs).

I've read through the documentation on the GitHub page https://github.com/brianc/node-postgres/wiki/Example

Which seems to require the use of a global variable in order to not create multiple pools which I've tried to do as follows.

I have my main server file bellow

var express = require("express");
var path = require("path");
var app = express();
var port = 3000;

app.use("/public", express.static(process.cwd() + "/public"));

app.set("views", process.cwd() + "/public/views");
app.set("view engine", "ejs");

var login_route = require("./public/logic/login/login_route.js");
app.use(login_route);

app.listen(port, function() {

   var message = "Server Started: Port " + port;
   console.log(message);

});

Which is using a route file which does the database query

var express = require("express");
var router = express.Router();

var pg = require("pg");

var credentials = {
   host: "127.0.0.1",
   port: "5432",
   database: "altaltalt",
   user: "postgres",
   password: "postgres",
   max: 100,
   idleTimeoutMillis: 30000
};

var pool = new pg.Pool(credentials);

// this bit of code is executed
// when a button is clicked
router.get("/exec", function(req, res) {

   pool.connect (
      function(error, client, done) {

         if(error) {
            console.log("connection failed");
         }

         client.query(

            "select * from dummytable",
             function(error, result) {

               done();

               if(error) {
                  console.log(error);
               }

               alert(result.rows);

             }

         );

      }
   );

});

module.exports = router;

When I make a get request to that route it executes really quickly once the button is pressed but any time it is pressed after that is when it becomes terribly slow.

Am I missing something in the code? Is there an additional step to handle multiple requests?


Solution

  • alert() doesn't exist in Node. Also, when you accept an HTTP request, you should send back a response. Lastly, if you want to run just one query, you can use pool.query().

    All together:

    router.get('/exec', function(req, res) {
      pool.query('select * from dummytable', function(error, result) {
        if (error) {
          console.log('query failed');
          return res.sendStatus(500);
        }
        res.send(result.rows);
      });
    });
    

    And using pool.connect():

    router.get('/exec', function(req, res) {
      pool.connect(function(error, client, done) {
        if (error) {
          console.log('connection failed');
          return res.sendStatus(500);
        }
    
        client.query('select * from dummytable', function(error, result) {
          done(error);
          if (error) {
            console.log('query failed');
            return res.sendStatus(500);
          }
          res.send(result.rows);
        });
      });
    });