Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG][Supabase][Postgres client] Exhausted connection pool #928

Closed
atanaskanchev opened this issue Jul 23, 2023 · 5 comments
Closed

[BUG][Supabase][Postgres client] Exhausted connection pool #928

atanaskanchev opened this issue Jul 23, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@atanaskanchev
Copy link

What version of drizzle-orm are you using?

0.27.2

What version of drizzle-kit are you using?

0.19.10

Describe the Bug

PostgresError: remaining connection slots are reserved for non-replication superuser connections
    at ErrorResponse (webpack-internal:///(rsc)/../../node_modules/postgres/src/connection.js:528:70)
    at handle (webpack-internal:///(rsc)/../../node_modules/postgres/src/connection.js:335:728)
    at Socket.data (webpack-internal:///(rsc)/../../node_modules/postgres/src/connection.js:223:17)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

Expected behavior

No response

Environment & setup

Client setup

import { drizzle, type PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'

import { schema } from '@lf/services-drizzle-orm-schema'

if (!process.env.DATABASE_URL) {
  throw new Error('DATABASE_URL is missing')
}

const connectionString = process.env.DATABASE_URL

const queryClient = postgres(connectionString)

export const db: PostgresJsDatabase<typeof schema> = drizzle(queryClient, { schema, logger: true })```
@atanaskanchev atanaskanchev added the bug Something isn't working label Jul 23, 2023
@Lenghak
Copy link

Lenghak commented Aug 15, 2023

Hello, @atanaskanchev! I was wondering if you had already solved this issue since I myself have been struggling with it for quite some time, and it is getting more frequent right now. I would love to hear from you if you have any workarounds for this.

@NovaBG03
Copy link

NovaBG03 commented Sep 6, 2023

I'm experiencing the same problem

@mirsahebali
Copy link

same here too

@hawkup
Copy link

hawkup commented Sep 28, 2023

In my case, this issue arises due to hot reloading during development. With each hot reload, a new Drizzle instance is created, consuming a new connection.
To resolve it, I created a global Drizzle instance to prevent it from being recreated with every hot reload.

Here's my code, which I adapted from the Prisma version.

import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"

declare module global {
  let postgresSqlClient: ReturnType<typeof postgres> | undefined
}

let postgresSqlClient

const databaseUrl = 'postgres://postgres:adminadmin@0.0.0.0:5432/db'

if (process.env.NODE_ENV !== "production") {
  if (!global.postgresSqlClient) {
    global.postgresSqlClient = postgres(databaseUrl)
  }
  postgresSqlClient = global.postgresSqlClient
} else {
  postgresSqlClient = postgres(databaseUrl)
}

export const db = drizzle(postgresSqlClient)

Reference

@Angelelz
Copy link
Collaborator

This is know issue if you're using HMR. The solution is given in the comment above.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants