question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma2 Application Potentially Creating DB Connections per API Request

See original GitHub issue

I have had a prisma app running in GCP that connects to a postgres instance also running in GCP for about a day.

All API calls started failing to the app with:

2020-01-24 21:15:52.056 CSTError querying the database: db error: FATAL: remaining connection slots are reserved for non-replication superuser connections
2020-01-24 21:15:52.056 CST at PhotonFetcher.request (/app/node_modules/@prisma/photon/index.js:62:23)
2020-01-24 21:15:52.056 CST at runMicrotasks (<anonymous>)
2020-01-24 21:15:52.056 CST at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-01-24 21:15:52.058 CSTGET500395 B44 msChrome 79 https://flag-api-vykaoik56q-uc.a.run.app/switches

Looks like the app is holding open connections. I have basically the boiler plate from the prisma init:

import { GraphQLServer } from 'graphql-yoga';
import { schema } from './schema';
import { createContext } from './context';
import getSwitchesWithAuthHeader from './controllers/switches';
import * as cors from 'cors';

const server = new GraphQLServer({ schema, context: createContext });
server.express.use(cors());
server.express.get('/switches', getSwitchesWithAuthHeader);

server.start(
  { port: 8080 },

  () =>
    console.log(
      `🚀 Server ready at: http://localhost:8080\n⭐️ See sample queries: http://pris.ly/e/ts/graphql#5-using-the-graphql-api`,
    ),
);

Versions:

    "@prisma/photon": "2.0.0-alpha.538",
    "basic-auth": "^2.0.1",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "graphql": "14.5.8",
    "graphql-yoga": "1.18.3",
    "nexus": "0.12.0-rc.5",

Restarting my database or the application fixes the issue temporarily.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
timsuchanekcommented, Feb 17, 2020

Thanks for reporting everyone! Can you please show me, how you instantiate the Prisma Client? You should normally just instantiate one client and reuse it for all requests.

1reaction
pimeyscommented, Jan 28, 2020

I’m trying to replicate this. Now just working with the rust core without the js layer. Having a local database with the following uri postgresql://user:password@localhost:5432/postgres?schema=sql_load_test&connection_limit=5.

Starting the Query Engine against the database and querying PostgreSQL about the number of open connections:

postgres=# SELECT sum(numbackends) FROM pg_stat_database;
-[ RECORD 1 ]
sum | 6

Which is my psql client and 5 connections for the pool. I try to keep this open, query every now and then and see if the async pool has a problem with leaked connections, which might be possible due to their nature, but might also be something else.

@timsuchanek suggested the JS client also might not close the QE cleanly keeping the connections open. We’ll put some effort now to replicate the issue here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection management (Guide) - Prisma
How to approach connection management with Prisma Client in serverless environments and long-running applications.
Read more >
How To Build a REST API with Prisma and PostgreSQL
In this tutorial, you will build a REST API for a small blogging application in TypeScript using Prisma and a PostgreSQL database.
Read more >
I want to use multiple database in prisma orm - Stack Overflow
You actually can with a workaround as specified here. Create two different schema.prisma in separate folders and initialise PrismaClient for ...
Read more >
What DBs does Redwood Support? - RedwoodJS Community
RedwoodJS uses Prisma2 as it's GraphQL provider. ... And if you want to use Redwood's GraphQL API to connect to AWS S3, send...
Read more >
Data access in Next.JS: APIs or direct database connection?
I am using PGBouncer in front of Postgres along with Prisma 2 + Next.js and I am using API Routes to access my...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found