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

Pool query never returns #2069

Closed
danbulant opened this issue Jan 12, 2020 · 37 comments
Closed

Pool query never returns #2069

danbulant opened this issue Jan 12, 2020 · 37 comments

Comments

@danbulant
Copy link

danbulant commented Jan 12, 2020

When I do Pool.query (Pool is instance of pg.Pool and successfully connected to DB), it never returns.
I tried both promise syntax (async/await and then/catch) and callback syntax, none of them worked. The script just never ends.

Using connection with the same details with psql works.

Node v12.14.0
npm 6.13.4
pg 7.17.1
PostgreSQL 12.1

EDIT: forgot to specify this happens when there's no row returned.
EDIT2: Select DATE() works while from table doesn't

@sehrope
Copy link
Contributor

sehrope commented Jan 14, 2020

Can you connect to the same database via something else like psql? If it hangs forever it could be a network issues dropping packets in between your computer and the remote DB. That's not specific to this driver.

If you can connect via some other means, then show a minimal example of the code that's not working.

@danbulant
Copy link
Author

Hello @sehrope .
As I already stated, using psql with the same details works.

It is not a connection issue, as it's connected to localhost.

@sehrope
Copy link
Contributor

sehrope commented Jan 14, 2020

Show your actual code or a minimal example of JavaScript code that reproduces the issue.

@danbulant
Copy link
Author

Just this query:
var res = await conn.query("SELECT * FROM public.posts p, public.users u WHERE p.user = u.id");

Never returns, never throws anything. No error in console, the app just hangs...

@sehrope
Copy link
Contributor

sehrope commented Jan 14, 2020

Where is conn coming from? Show the entire block that's acquiring the connection, and how it's being created.

@danbulant
Copy link
Author

const { Pool, Client } = require('pg')
const fs = require("fs");
conn = new Pool(JSON.parse(fs.readFileSync(__dirname + "/../sqlDetails.json")))
var res = await conn.query("SELECT * FROM public.posts p, public.users u WHERE p.user = u.id");
console.log(res)

with this code, the node just hangs and never outputs anything

@danbulant
Copy link
Author

However, when I simplify the query to just "SELECT DATE()", it works

@danbulant
Copy link
Author

the query SELECT * FROM public.posts p, public.users u WHERE p.user = u.id also works in psql

@sehrope
Copy link
Contributor

sehrope commented Jan 14, 2020

That query would fetch every single row from post which could and return it as a single array. If it's a lot of rows (like millions...) that could take a really long time and possibly never finish due to memory allocation / GC issues.

If that's not the issue then try splitting things up to see where it's hanging. Also maybe break out the top level await to further isolate things:

const { Pool, Client } = require('pg')
const fs = require("fs");

const pool = new Pool(JSON.parse(fs.readFileSync(__dirname + "/../sqlDetails.json")));

async function testConnect() {
    console.log('Before connect');
    const client = await pool.connect();
    console.log('Connected!');
    client.release();
}

async function testSmallQuery() {
    console.log('Before small query');
    const sql = "SELECT 1 AS x";
    const result = await pool.query(sql);
    console.log('Small query returned %s rows', result.rows.length);
}

async function testBigQuery() {
    console.log('Before big query');
    const sql = "SELECT * FROM public.posts p, public.users u WHERE p.user = u.id";
    const result = await pool.query(sql);
    console.log('Big query returned %s rows', result.rows.length);
}

async function main() {
    await testConnect();
    await testSmallQuery();
    await testBigQuery();
}

main()
    .then (() => {
        console.error('Done');
        process.exit(0);
    })
    .catch((err) => {
        console.error('Error: %s', err);
        console.error('Error: %s', err.stack);
        process.exit(1);
    });

main();

@danbulant
Copy link
Author

Ok I'll test it tomorrow, thanks.

As per the big data - that isn't the case, the DB is brand new and has just one post in it (and one user), so it should return just one row right now.

@danbulant
Copy link
Author

So, as I expected, the code hangs in "Before big query", even when I added "LIMIT 1" at the end.

@danbulant
Copy link
Author

danbulant commented Jan 15, 2020

The query code itself works. Could it possibly be that there are duplicate fields and/or number of fields?

image

Also SELECT * FROM information_schema.tables works as well. Why just this query doesn't work?

Could it be types? When I do SELECT u.name, u.username, p.content, p.uuid, p.type, p.date FROM public.posts p, public.users u WHERE p.user = u.id LIMIT 1 (which works in psql), it hangs as well.
The types are (not in order):

  • character variable
  • text
  • uuid
  • datetime without timezone
  • integer

@charmander
Copy link
Collaborator

charmander commented Jan 15, 2020

I can’t reproduce this.

$ container() {
    docker run --rm -it \
        --runtime=runsc \
        --security-opt=no-new-privileges \
        --user="$UID:$(id -g)" \
        --init \
        "$@"
}

$ docker network create --internal pg-2069-net
e66e2556d6a4eb04aca0d33b13bff31ec686b0ffbd69fe7e91cf2bb21bafb7b0

$ container --network=pg-2069-net --name=pg-2069-postgres --user= --detach postgres:12
3664b467bee0056c7550569d387889a09915d2be21b9a149fe0fb65e9d97d1a6

$ container --name=pg-2069-node --rm=false --workdir=/home/node node:12.14.0-alpine sh
~ $ cat > package.json
{ "private": true }
~ $ npm install pg
npm notice created a lockfile as package-lock.json. You should commit this file.
+ pg@7.17.1
added 17 packages from 9 contributors and audited 17 packages in 2.283s
found 0 vulnerabilities

~ $ npm ls
/home/node
`-- pg@7.17.1
  +-- buffer-writer@2.0.0
  +-- packet-reader@1.0.0
  +-- pg-connection-string@0.1.3
  +-- pg-packet-stream@1.1.0
  +-- pg-pool@2.0.9
  +-- pg-types@2.2.0
  | +-- pg-int8@1.0.1
  | +-- postgres-array@2.0.0
  | +-- postgres-bytea@1.0.0
  | +-- postgres-date@1.0.4
  | `-- postgres-interval@1.2.0
  |   `-- xtend@4.0.2
  +-- pgpass@1.0.2
  | `-- split@1.0.1
  |   `-- through@2.3.8
  `-- semver@4.3.2

~ $ npm --version
6.13.4
~ $ %

$ docker container inspect pg-2069-postgres --format '{{(index .NetworkSettings.Networks "pg-2069-net").IPAddress}}'
172.21.0.2

$ docker commit pg-2069-node
sha256:04ac3514864cb83b2380822242d1fd4c5714391d3f8f18cd722d036f313564c1

$ container --network=pg-2069-net 04ac3514864c node --experimental-repl-await
Welcome to Node.js v12.14.0.
Type ".help" for more information.
> const { Pool } = require('pg');
undefined
> const pool = new Pool({ host: '172.21.0.2', user: 'postgres' });
undefined
> void await pool.query(`CREATE TABLE users (id integer NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, name text NOT NULL, username text NOT NULL); INSERT INTO users (name, username) VALUES ('John Doe', 'john doe')`);
undefined
> void await pool.query(`CREATE TABLE posts (id integer NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, "user" integer NOT NULL REFERENCES users (id), content text NOT NULL, uuid uuid NOT NULL, type integer NOT NULL, date timestamp without time zone NOT NULL); INSERT INTO posts ("user", content, uuid, type, date) VALUES (1, 'Testing post', '87be7d22-7a6e-463f-afb3-b8e5349c9d3a', 0, '2020-01-12 18:28:48'), (1, 'Testing post', 'dc7e636c-5aeb-4c81-aef2-e1477a35f341', 0, '2020-01-12 18:28:48')`);
undefined
> (await pool.query(`SELECT u.name, u.username, p.content, p.uuid, p.type, p.date FROM public.posts p, public.users u WHERE p.user = u.id LIMIT 1`)).rows
[
  {
    name: 'John Doe',
    username: 'john doe',
    content: 'Testing post',
    uuid: '87be7d22-7a6e-463f-afb3-b8e5349c9d3a',
    type: 0,
    date: 2020-01-12T18:28:48.000Z
  }
]
> (await pool.query(`SHOW server_version`)).rows[0]
{ server_version: '12.1 (Debian 12.1-1.pgdg100+1)' }
cleanup
$ docker attach pg-2069-postgres
^C2020-01-15 23:18:54.712 UTC [2] LOG:  received fast shutdown request
2020-01-15 23:18:54.720 UTC [2] LOG:  aborting any active transactions
2020-01-15 23:18:54.778 UTC [2] LOG:  background worker "logical replication launcher" (PID 58) exited with exit code 1
2020-01-15 23:18:54.784 UTC [53] LOG:  shutting down
2020-01-15 23:18:54.911 UTC [2] LOG:  database system is shut down

$ docker rm pg-2069-node
pg-2069-node

$ docker rmi 04ac3514864c
Deleted: sha256:04ac3514864cb83b2380822242d1fd4c5714391d3f8f18cd722d036f313564c1
Deleted: sha256:9cee742410306e8e6e1deda09daa494e8939fc757119a06008800b18003b4fe7

$ docker network rm pg-2069-net
pg-2069-net

Can you show the contents of sqlDetails.json and the versions of all packages involved (npm ls)? Also, to narrow down whether it does have something to do with types, try removing fields one by one from your last query.

@danbulant
Copy link
Author

sqlDetails.json:

{
    "user": "Pushr",
    "password": "Pg554;",
    "host": "localhost",
    "database": "pushr"
}
NPM LS
+-- ansi-colors@4.1.1
+-- ascii-table@0.0.9
+-- express@4.17.1
| +-- accepts@1.3.7
| | +-- mime-types@2.1.25
| | | `-- mime-db@1.42.0
| | `-- negotiator@0.6.2
| +-- array-flatten@1.1.1
| +-- body-parser@1.19.0
| | +-- bytes@3.1.0
| | +-- content-type@1.0.4 deduped
| | +-- debug@2.6.9 deduped
| | +-- depd@1.1.2 deduped
| | +-- http-errors@1.7.2
| | | +-- depd@1.1.2 deduped
| | | +-- inherits@2.0.3
| | | +-- setprototypeof@1.1.1 deduped
| | | +-- statuses@1.5.0 deduped
| | | `-- toidentifier@1.0.0
| | +-- iconv-lite@0.4.24
| | | `-- safer-buffer@2.1.2
| | +-- on-finished@2.3.0 deduped
| | +-- qs@6.7.0 deduped
| | +-- raw-body@2.4.0
| | | +-- bytes@3.1.0 deduped
| | | +-- http-errors@1.7.2 deduped
| | | +-- iconv-lite@0.4.24 deduped
| | | `-- unpipe@1.0.0 deduped
| | `-- type-is@1.6.18 deduped
| +-- content-disposition@0.5.3
| | `-- safe-buffer@5.1.2 deduped
| +-- content-type@1.0.4
| +-- cookie@0.4.0
| +-- cookie-signature@1.0.6
| +-- debug@2.6.9
| | `-- ms@2.0.0
| +-- depd@1.1.2
| +-- encodeurl@1.0.2
| +-- escape-html@1.0.3
| +-- etag@1.8.1
| +-- finalhandler@1.1.2
| | +-- debug@2.6.9 deduped
| | +-- encodeurl@1.0.2 deduped
| | +-- escape-html@1.0.3 deduped
| | +-- on-finished@2.3.0 deduped
| | +-- parseurl@1.3.3 deduped
| | +-- statuses@1.5.0 deduped
| | `-- unpipe@1.0.0
| +-- fresh@0.5.2
| +-- merge-descriptors@1.0.1
| +-- methods@1.1.2
| +-- on-finished@2.3.0
| | `-- ee-first@1.1.1
| +-- parseurl@1.3.3
| +-- path-to-regexp@0.1.7
| +-- proxy-addr@2.0.5
| | +-- forwarded@0.1.2
| | `-- ipaddr.js@1.9.0
| +-- qs@6.7.0
| +-- range-parser@1.2.1
| +-- safe-buffer@5.1.2
| +-- send@0.17.1
| | +-- debug@2.6.9 deduped
| | +-- depd@1.1.2 deduped
| | +-- destroy@1.0.4
| | +-- encodeurl@1.0.2 deduped
| | +-- escape-html@1.0.3 deduped
| | +-- etag@1.8.1 deduped
| | +-- fresh@0.5.2 deduped
| | +-- http-errors@1.7.2 deduped
| | +-- mime@1.6.0
| | +-- ms@2.1.1
| | +-- on-finished@2.3.0 deduped
| | +-- range-parser@1.2.1 deduped
| | `-- statuses@1.5.0 deduped
| +-- serve-static@1.14.1
| | +-- encodeurl@1.0.2 deduped
| | +-- escape-html@1.0.3 deduped
| | +-- parseurl@1.3.3 deduped
| | `-- send@0.17.1 deduped
| +-- setprototypeof@1.1.1
| +-- statuses@1.5.0
| +-- type-is@1.6.18
| | +-- media-typer@0.3.0
| | `-- mime-types@2.1.25 deduped
| +-- utils-merge@1.0.1
| `-- vary@1.1.2
+-- express-ws@4.0.0
| `-- ws@5.2.2
|   `-- async-limiter@1.0.1
`-- pg@7.17.1
  +-- buffer-writer@2.0.0
  +-- packet-reader@1.0.0
  +-- pg-connection-string@0.1.3
  +-- pg-packet-stream@1.1.0
  +-- pg-pool@2.0.9
  +-- pg-types@2.2.0
  | +-- pg-int8@1.0.1
  | +-- postgres-array@2.0.0
  | +-- postgres-bytea@1.0.0
  | +-- postgres-date@1.0.4
  | `-- postgres-interval@1.2.0
  |   `-- xtend@4.0.2
  +-- pgpass@1.0.2
  | `-- split@1.0.1
  |   `-- through@2.3.8
  `-- semver@4.3.2

Even doing simple SELECT name FROM public.users LIMIT 1 hangs. Name is varchar(32).

@nieverbe
Copy link

Any new on this issue? Seeing the same problem

@brianc
Copy link
Owner

brianc commented Feb 25, 2020

Any new on this issue? Seeing the same problem

This is almost certainly an issue w/ your setup....but I'm willing to investigate it if you give me a self-contained way to reproduce the problem. By that I mean a single gist or code sample I can copy/paste into a blank file, run node the-new-file.js and have the problem triggered. Send me that & I'll take a look!

@danbulant
Copy link
Author

The conection itself works - I see in pgAdmin there's a new connection, but the query just never finished.

I moved to MySQL because of this, so I won't update much.

Even the simple code @sehrope send hang up when using non-system select (means selecting actual data from table). Same query worked in pgAdmin.

@charmander
Copy link
Collaborator

charmander commented Feb 28, 2020

@danbulant @nieverbe Some information that might help find the problem:

  • Which operating systems are you using?

  • How is PostgreSQL installed on them?

  • If you add:

    client.connection.on('message', console.log)

    what gets logged?

  • Is Node hanging, or is it waiting? (Check CPU usage or see if a setInterval(() => console.log(1), 1000) stops.)

  • If you’re familiar with a packet capture tool (e.g. Wireshark), what does it show?

  • Does it work on a different device? Against a remote database?

  • Does it work with TLS enabled?

@nieverbe Which options are you passing to the pg.Pool or pg.Client constructor?

@NomadXD
Copy link

NomadXD commented Apr 25, 2020

I am trying to use postgresql inside a docker container and same thing happens. No errors, exceptions or anything. It just stops when i call database from nodejs.

@brianc
Copy link
Owner

brianc commented Apr 25, 2020

@NomadXD gonna need some info on what version of node, postgres, node-postgres, operating system, code example, etc.

Like I mentioned above it's most likely an issue w/ your configuration or the way your code is written...this lib is heavily tested and heavily deployed across basically every node/pg/os version there is w/o this being a common issue...generally this ends up being "user error" though I'd still like to understand what it is as often "user error" is due to poor or missing documentation or bad defaults which I can totally help fix!

@NomadXD
Copy link

NomadXD commented Apr 25, 2020

@brianc Thanks a lot for your quick response.

So I am running an application with 6 containers using docker-compose. When I run the application on my local machine it works without any error. But today I tried to deploy it in aws ec2 ubuntu 18.04. Project builds and when I start the server everything works fine.

When I run an end point without database call, it works fine. But when there is a database call, it hangs without giving any error or result. Tried adding console logs and found that the code does not execute after pool.query(). Tried to get some exceptions with try catch but doesnt work either.

docker-compose.yml

version: '3'
services:
  postgres:
    container_name: postgres
    image: mdillon/postgis
    environment:
      POSTGRES_MULTIPLE_DATABASES: user_data,gov_auth
      POSTGRES_USER: commhawk
      POSTGRES_PASSWORD: password
    volumes:
        - postgres:/data/postgres
        - ./database:/docker-entrypoint-initdb.d
    ports:
      - "5433:5432"
    restart: unless-stopped

  rethinkdb:
    container_name: rethinkdb
    image: rethinkdb:latest
    ports:
      - "3004:8080"
      - "29015:29015"
      - "28015:28015"
    volumes: 
      - rethinkdb:/data

  user_data_service:
    build: './user_data_service'
    container_name: uds
    ports: 
      - "3002:3000"
    depends_on: 
      - postgres
    environment:
      DATABASE_URL: postgres://commhawk:password@postgres:5432/user_data
    # volumes: 
    #   - ./user_data_service:/src
      # - container_node_modules:/src/node_modules
  
  gov_authority_service:
    build: './gov_authority_service'
    container_name: gov_auth
    ports:
      - "3001:3000"
    depends_on: 
      - postgres
    environment:
      DATABASE_URL: postgres://commhawk:password@postgres:5432/gov_auth
    # volumes:
    #   - ./gov_authority_service:/src 



  socket_service:
    build: './socket_service'
    container_name: socket
    ports: 
      - "3003:3000"
    depends_on: 
      - rethinkdb
    # volumes: 
    #   - ./socket_service:/src


  api_gateway:
    image: express-gateway:latest
    ports:
      - "8080:8080"
    depends_on:
      - user_data_service
      - socket_service
      - gov_authority_service
    volumes: 
      - ./api_gateway/gateway.config.yml:/var/lib/eg/gateway.config.yml 
    

volumes:
  postgres:
  rethinkdb:
  # container_node_modules:

In my Dockerfile I am using node:latest . Sample Dockerfile here.

FROM node:latest

COPY . /src

WORKDIR /src

RUN npm install --save-dev nodemon

RUN npm install --production

EXPOSE 3000

CMD ["npm","run","start"]

And package.json

{
  "name": "gov_authority_service",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js",
    "dev": "nodemon -L server.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^2.0.3"
  },
  "dependencies": {
    "bcrypt": "^4.0.1",
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "node-fetch": "^2.6.0",
    "pg": "^8.0.2",
    "uuid": "^7.0.3"
  }
}


Can provide github project also if u need.

I don't have to install postgresql in my ubuntu server right ?

@NomadXD
Copy link

NomadXD commented Apr 25, 2020

@brianc Here is my github link https://github.com/NomadXD/commhawk.git.
There is a brach called deploy/aws. You can directly build it using docker-compose build and run using docker-compose up.

Then try the {yourhost}/api/gov/signup end point with the following json as a POST request.

{
  "type":4,
  "addressLine1":"Main street Lane",
  "addressLine2":"Horana",
  "city":"Horana",
  "district":"Kalutara",
  "province": "WP",
  "location":{"lng":80.062324,"lat":6.714721},
  "email":"hoarana@health.gov.lk",
  "telephoneNumber":"0342266888",
  "fax":"0342266888",
  "password":"password",
  "firestation":{
  "fireTrucks":2,
  "fireFighters":10
  }
  
  
}

@NomadXD
Copy link

NomadXD commented Apr 25, 2020

@brianc

How I create pool

const pool = new Pool({
  user: "commhawk",
  host: "postgres",
  database: "gov_auth",
  password: "password",
  port: "5432"
});

@charmander
Copy link
Collaborator

@NomadXD Try upgrading to pg 8.0.3. 8.0.2 isn’t compatible with Node 14.

@NomadXD
Copy link

NomadXD commented Apr 26, 2020

@charmander @brianc Thanks a lot. I was trying to fix this yesterday for like 10 hours but couldnt get it working. Now it works like a charm !!! The reason for my local version to run normally is that the local version is running node 13+ pg 8.0.2 (built it last month or so). But when yesterday , I built it on server, node:latest has pulled node 14 from docker hub which is incompatible with pg 8.0.2 as u mentioned. So I downgraded node 14 to node 13 on the server. Now works fine. Thanks a lot for helping me out.

And also I think it is more helpful and convenient if you can add checking versions for the library. I mean if the versions are incompatible return something like "pg 8.0.2 is incompatible with your current node version ". Or maybe include a chart with matching versions in the docs. Because otherwise it's like nothing happens when users face this situation. Cheers !!!!!

@charmander
Copy link
Collaborator

pg 8.0.2 didn’t know it wouldn’t be compatible with Node 14, and now it can’t be changed.

@lanre-mark
Copy link

lanre-mark commented Apr 30, 2020

Thanks @NomadXD @charmander @brianc I have been troubleshooting same issue since the last 2 days and this conversation log came to my rescue.

However, @NomadXD's suggestion will be helpful going forward.

And also I think it is more helpful and convenient if you can add checking versions for the library. I mean if the versions are incompatible return something like "pg 8.0.2 is incompatible with your current node version "

@brianc
Copy link
Owner

brianc commented Apr 30, 2020

yeah i do keep the engines field up to date in package.json here. Problem is I didn't know before node 14 came out that it would change something in a non-backwards-compatible way and break this module. If I could go back and edit the older versions & mark in their engines field they aren't compatible w/ node 14 that'd save everyone some trouble but node module versions are immutable once published...which is ultimately for the best but does cause occasional things like this.

@brianc brianc closed this as completed Apr 30, 2020
@charmander
Copy link
Collaborator

@brianc It was someone else’s bug :D

@charmander charmander reopened this Apr 30, 2020
@brianc
Copy link
Owner

brianc commented Apr 30, 2020

It was someone else’s bug

ah woops yup missed that my bad!

@NomadXD
Copy link

NomadXD commented Apr 30, 2020

@brianc Totally understand and appreciate your efforts in trying to keep this library updated to the latest. Since we can't handle this version mismatch issue at code level, how about creating a table with pg version in one column and node version in the other column and including that in the documentation. At least people will give a tiny consideration to check the versions, when an issue like this happens. I literally never though about versions until @charmander mentioned about it haha

@mymro
Copy link

mymro commented Jul 22, 2020

I just tried debugging the exact same issue. Downgrading to node 13 solved it. It would be great to have some form of documentation or feedback when running the code. Just having a promise that never resolves or rejects is really hard to debug.

@charmander
Copy link
Collaborator

@mymro You should upgrade to the latest pg (8.3.0) instead of downgrading to an EOL Node.

@milad1367
Copy link

milad1367 commented Jan 3, 2021

@NomadXD Try upgrading to pg 8.0.3. 8.0.2 isn’t compatible with Node 14.

I was trying for 5 hours
finally fixed
I want to left :)

@henryhu712
Copy link

I have fought the issue for 5 hours also. Updating pg to 8.2.0 to fix it, node 14.15.4.

@chr15m
Copy link

chr15m commented Aug 18, 2021

Thanks for this thread! I lost a day of work, but I'm glad you all investigated it carefully and documented the fix here. Thank you! 🙏

@brianc perhaps you could change the issue title to add [solved] to help people find it?

@charmander
Copy link
Collaborator

@chr15m In general, we just close an issue to mark it solved. However, in this case, the solution that helped you isn’t the original issue anyway. Not sure whether @danbulant closed it because they found a solution, but if they did, they didn’t say what it was.

rdubigny added a commit to numerique-gouv/moncomptepro that referenced this issue Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests