-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
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. |
Hello @sehrope . It is not a connection issue, as it's connected to localhost. |
Show your actual code or a minimal example of JavaScript code that reproduces the issue. |
Just this query: Never returns, never throws anything. No error in console, the app just hangs... |
Where is |
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 |
However, when I simplify the query to just |
the query |
That query would fetch every single row from If that's not the issue then try splitting things up to see where it's hanging. Also maybe break out the top level 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(); |
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. |
So, as I expected, the code hangs in "Before big query", even when I added "LIMIT 1" at the end. |
The query code itself works. Could it possibly be that there are duplicate fields and/or number of fields? Also Could it be types? When I do
|
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: {
"user": "Pushr",
"password": "Pg554;",
"host": "localhost",
"database": "pushr"
}
Even doing simple |
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 |
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. |
@danbulant @nieverbe Some information that might help find the problem:
@nieverbe Which options are you passing to the |
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. |
@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! |
@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
In my Dockerfile I am using node:latest . Sample Dockerfile here.
And package.json
Can provide github project also if u need. I don't have to install postgresql in my ubuntu server right ? |
@brianc Here is my github link https://github.com/NomadXD/commhawk.git. Then try the {yourhost}/api/gov/signup end point with the following json as a POST request.
|
How I create pool
|
@NomadXD Try upgrading to pg 8.0.3. 8.0.2 isn’t compatible with Node 14. |
@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 !!!!! |
pg 8.0.2 didn’t know it wouldn’t be compatible with Node 14, and now it can’t be changed. |
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.
|
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 It was someone else’s bug :D |
ah woops yup missed that my bad! |
@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 |
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. |
@mymro You should upgrade to the latest pg (8.3.0) instead of downgrading to an EOL Node. |
I was trying for 5 hours |
I have fought the issue for 5 hours also. Updating pg to 8.2.0 to fix it, node 14.15.4. |
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? |
@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. |
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
The text was updated successfully, but these errors were encountered: