Hi, has anyone experienced the dreaded ```Error qu...
# orm-help
r
Hi, has anyone experienced the dreaded
Copy code
Error querying the database: db error: FATAL: sorry, too many clients already
error?
a
Hi @Reuben Porter! I'm sorry you are running into this, it's a frustrating error for sure. Could you provide me some more context so I can attempt to reproduce your error? Environment info, versions, under what circumstances you are seeing this error, etc.
r
Hi @Austin, thanks for the reply! So I am using the serverless framework and for local development serverless offline. I have a docker compose which starts two services: a serverless offline api and a postgres database. I have an integration test suite of around 200 tests, some of them clearing the db and reseeding after each test. The test run gets about half way through and then ends up failing with the error I posted above. I found another example in the docs to cut down on opening multiple connections, which seemed to help with that issue but then I am getting strange side effects, e.g. Prisma.raw is no longer working. Any ideas? I can send over some code etc if it helps.
Node v14 Prisma latest
postgres latest
@Ryan Do you have any experience with this error?
r
Are the tests running in parallel? Also are you running
prisma.$disconnect()
after each test?
r
I am using --runInBand
and I've tried with and without disconnecting after each test suite
makes no difference
@Ryan only just saw your reply sorry
If I use the 'singleton' client pattern then Prisma.raw no longer works - which I find extremely strange
r
--runInBand
shouldn’t cause this. What’s the singleton pattern?
r
To initialise the client
Copy code
import { PrismaClient } from '@prisma/client';

declare global {
    var prisma: PrismaClient | undefined;
}

export const prisma = global.prisma || new PrismaClient();

if (process.env.NODE_ENV !== 'production') global.prisma = prisma;
instead of
Copy code
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export default prisma
Using the 1st code snippet, seems to help with DB connections BUT Prisma.raw and Prisma.join no longer works!
e.g. this
Copy code
import { Prisma as db} from '@prisma/client';
...
AND dashboard.status IN (${db.join(filters.statuses)})
or
Copy code
import { Prisma as db} from '@prisma/client';
...
ORDER BY "${db.raw(filters.orderBy)}" ${db.raw(filters.dir)}
no longer works, whereas if I use
Copy code
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export default prisma
both of those raw queries above work
r
Works fine for me. Are you sure you’re importing it correctly and both are not named
db
?
join
and
raw
come from
Prisma
so that has no relation to the client you’re exporting.
r
Exactly, that's what you'd expect
however only when I make that client change does it no longer work
Copy code
osv3-test-api  | PrismaClientKnownRequestError:
osv3-test-api  | Invalid `prisma.queryRaw()` invocation:
osv3-test-api  |
osv3-test-api  |
osv3-test-api  |   Raw query failed. Code: `42601`. Message: `db error: ERROR: syntax error at or near "$8"`
from the query:
Copy code
ORDER BY "$7" $8
r
Could you share a small reproduction of this? I would like to test this.
r
Ok, I will try to setup a small repo
I don't suppose you're available for a call actually?
I can just show you via screenshare
understand if not ofc!
both imports btw
Copy code
import { Prisma as db } from '@prisma/client';
import { prisma } from 'lib/client';
deffo not a mistake
lib/client contains
Copy code
import { PrismaClient } from '@prisma/client';

declare global {
    var prisma: PrismaClient | undefined;
}

export const prisma = global.prisma || new PrismaClient();

if (process.env.NODE_ENV !== 'production') global.prisma = prisma;
r
A reproduction would be better as I could try this on my end and check what the actual issue is 🙂
r
is there a way I can log how many open connections there are?
r
You can check that from the database Dashboard that you’re using. Is it a local one or on the cloud?
r
I'm using a postgres db in docker
r
Copy code
SELECT sum(numbackends) FROM pg_stat_database;
This should give you the open connections.
r
thanks
but tbh after using the code above I no longer see the too many connections issue. It just means Prisma.raw and Prisma.join no longer work with prisma.$queryRaw, as baffling as that sounds!@
r
Yeah I would need to check that one.
r
how many connections would I expect to see using the example I provided above?
1 right
Ok so switching back to
Copy code
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export default prisma
fixes Prisma.raw and Prisma.join however look how many connections I end up with...
r
Ideally connections should be approximately as shown here.
185 seems too much. It makes me think that connections are not getting disconnected properly.
👀 1
r
Yeah so that;s why I switched to the other code snippet - breaks my raw queries though... ahhh this is such a tricky one
Yes I agree
So I tried adding this to my test suite to test if disconnect works as expected
Copy code
afterEach(async () => {
    await prisma.$disconnect();
});
but the connections sum just increases - so it's not disconnecting
👀 1
Ok so I tried importing sql template tag and using it directly from that and the raw query still fails!
Copy code
import { join, raw } from "sql-template-tag";
r
@Reuben Porter Was this resolved as well?
r
No this issue still persists but I'm just allowing a lot of connections for my test suite
r
Could you share a small reproduction if possible?
r
we have quite a lot of external dependencies so its just getting round to creating a reproduction which I can share
but yes I will create one as I think this may be a prisma related issue
and cna help you look into it?
r
Yeah sure, I can check test why the raw imports are not working.