Database connections are not being closed when the server is stopped.
Error querying the database: db error: FATAL: sorry, too many clients already
blitz start0.9.0
I think the blitz server needs to add a listener for a kill signal. Then run db.disconnect() inside that. Note: this must be implemented in a way that it will not fail if the db module doesn't exist.
This should be for both blitz start and blitz start --production
thanks @flybayer for opening this issue!
the steps should be:
blitz startI hope the above makes sense.
Also, as per your suggestion I added the ?connection_limit=1 to my DB URL in .env, but I still get this issue.
May I help with this issue?
of course @ivandevp please feel free!
@ivandevp this might help: https://github.com/prisma/prisma/issues/1983
@abuuzayr nice!
Looks like the solution is this:
import { PrismaClient } from "@prisma/client"
let prisma
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient()
} else {
// Ensure the prisma instance is re-used during hot-reloading
global.prisma = global.prisma || new PrismaClient()
prisma = global.prisma
}
export default prisma
Hey guys! Sorry for the delay. I've been trying to reproduce but haven't found a way to do it. I followed the steps but can't see the error in the current canary version, when I create a project with Blitz 0.9.0 I got an error related to React.Suspense but not sure what is happening. Are you still facing the error in the current canary version? The problem and solution makes sense to me, but can't see it reflected in my dev environment.
I am also unable to reproduce. Tried 0.90 and 0.91.
@flybayer yup so far so good!
thanks @ivandevp @sbardian, perhaps the steps from running blitz new to getting the error are quite a lot, so it quite a long way to getting the error.
I just tried it with 0.9.2-canary.1 on a fresh project and I get the error.
Note: the default max_connections value for postgresql is 100.
npx [email protected] new testcd testschema.prisma and uncomment the Project modelblitz db migrateblitz generate all projectblitz startlocalhost:3000/projects (the index page does not make any query to the db or import the db)test=# select count(*) from pg_stat_activity;
count
-------
8
app/projects/pages/projects/index.tsxprojects which will depend on getProjects query> Running getProjects(null)
✔ getProjects returned [
{
"id": 1,
"name": "MyName"
}
]
instead of just
[ wait ] compiling ...
[ info ] bundled successfully, waiting for typecheck results...
[ wait ] compiling ...
[ info ] bundled successfully, waiting for typecheck results...
max_connections configuration in your Postgres server (defaults to 100). I got the error when the count was 105.test=# select count(*) from pg_stat_activity;
count
-------
105
(1 row)
Error in connector: Error querying the database: db error: FATAL: sorry, too many clients already
During active development, HMR/Fast Refresh is invoked on queries to the db multiple times, so I can get the error easily after a while.
@ivandevp @sbardian These are a fair amount of steps, but if you do wish to reproduce this error, do try them and let me know if your mileage varies.
These are the processes before the solution is deployed:
test=# select state,query from pg_stat_activity where usename = 'fawyna';
state | query
--------+-------------------------------------------------------------------------------------------------------------
idle | SELECT COUNT(*) FROM (SELECT "public"."Job"."id" FROM "public"."Job" WHERE 1=0 OFFSET $1) AS "sub" LIMIT $2
active | select state,query from pg_stat_activity where usename = 'fawyna';
(2 rows)
test=# select state,query from pg_stat_activity where usename = 'fawyna';
state | query
--------+-------------------------------------------------------------------------------------------------------------
idle | SELECT COUNT(*) FROM (SELECT "public"."Job"."id" FROM "public"."Job" WHERE 1=0 OFFSET $1) AS "sub" LIMIT $2
active | select state,query from pg_stat_activity where usename = 'fawyna';
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
(4 rows)
test=# select state,query from pg_stat_activity where usename = 'fawyna';
state | query
--------+-------------------------------------------------------------------------------------------------------------
idle | SELECT COUNT(*) FROM (SELECT "public"."Job"."id" FROM "public"."Job" WHERE 1=0 OFFSET $1) AS "sub" LIMIT $2
active | select state,query from pg_stat_activity where usename = 'fawyna';
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
(5 rows)
test=# select state,query from pg_stat_activity where usename = 'fawyna';
state | query
--------+-----------------------------------------------------------------------------------------------------------------------------------------
idle | SELECT COUNT(*) FROM (SELECT "public"."Job"."id" FROM "public"."Job" WHERE 1=0 OFFSET $1) AS "sub" LIMIT $2
active | select state,query from pg_stat_activity where usename = 'fawyna';
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
idle | COMMIT
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE "public"."Project"."id" = $1 LIMIT $2 OFFSET $3
(8 rows)
On every refresh, a new process is spawned.
After deploying the solution, the same process is used, but the query is changed:
test=# select state,query from pg_stat_activity where usename = 'fawyna';
state | query
--------+-------------------------------------------------------------------------------------------------------
active | select state,query from pg_stat_activity where usename = 'fawyna';
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE 1=1 OFFSET $1
(2 rows)
test=# select state,query from pg_stat_activity where usename = 'fawyna';
state | query
--------+-----------------------------------------------------------------------------------------------------------------------------------------
active | select state,query from pg_stat_activity where usename = 'fawyna';
idle | SELECT "public"."Project"."id", "public"."Project"."name" FROM "public"."Project" WHERE "public"."Project"."id" = $1 LIMIT $2 OFFSET $3
(2 rows)
@ivandevp do let me know if you want me to set up the PR, i'd be happy to!
Awesome @abuuzayr! Go ahead with the PR. I will try to get the error with the steps you mentioned but in the meantime I think is good to have the PR If you already got the solution.
Most helpful comment
@flybayer yup so far so good!
thanks @ivandevp @sbardian, perhaps the steps from running
blitz newto getting the error are quite a lot, so it quite a long way to getting the error.I just tried it with 0.9.2-canary.1 on a fresh project and I get the error.
Full reproduction steps
Note: the default
max_connectionsvalue for postgresql is 100.npx [email protected] new testcd testschema.prismaand uncomment the Project modelblitz db migrateblitz generate all projectblitz startlocalhost:3000/projects(the index page does not make any query to the db or import the db)app/projects/pages/projects/index.tsxprojectswhich will depend ongetProjectsqueryinstead of just
max_connectionsconfiguration in your Postgres server (defaults to 100). I got the error when the count was 105.During active development, HMR/Fast Refresh is invoked on queries to the db multiple times, so I can get the error easily after a while.
@ivandevp @sbardian These are a fair amount of steps, but if you do wish to reproduce this error, do try them and let me know if your mileage varies.
Testing the solution
These are the processes before the solution is deployed:
On every refresh, a new process is spawned.
After deploying the solution, the same process is used, but the query is changed:
@ivandevp do let me know if you want me to set up the PR, i'd be happy to!