Skip to content
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

Document PgBouncer mode #443

Closed
janpio opened this issue May 25, 2020 · 2 comments
Closed

Document PgBouncer mode #443

janpio opened this issue May 25, 2020 · 2 comments
Assignees
Labels
docs Documentation creation, updates or corrections

Comments

@janpio
Copy link
Contributor

janpio commented May 25, 2020

See prisma/prisma#2520 for what exactly is implemented.

Additional information bits from previous documentation issue prisma/prisma-client-js#503 (which is now incorrect, thus this extract here):

1. pgBouncer must run in transaction mode.

Other modes like session do not make any sense:

Earlier notes from @pimeys on pgBouncer (with modifications by @janpio) that we might want to incorporate into the documentation in some way:


Pgbouncer is a lightweight connection pooler for PostgreSQL. Instead of running
inside the application code, pgbouncer is a separate service offering support
for postgres line protocol and handling the pooling logic outside of
applications using the database.

Typically postgres supports only a certain amount of concurrent connections and
this limit can be reached quite fast when the service usage goes up.
This means the developer cannot just launch more instances of the service due to
the already running instances are reseving all the connections.

Pgbouncer can be run in three different modes, them being session,
transaction and statement mode.

Session mode gives one connection per client, and is useful mainly to define
limits per team in an organization. For common users this mode doesn't allow any
advantages over just connecting to postgres directly.

Statement mode shares a connection with every query coming to the system. It
prevents certain useful features, such as transactions or prepared statements
and is not really useful with Prisma.

Transaction mode offers a connection for every transaction. With this mode and
certain other settings enabled, the prisma query is able to operate normally.
Needed settings for transaction mode:

  • Prisma needs to be started in a mode that triggers a transaction in every
    case, even when just reading data. This allows us to use prepared statements
    in Prisma.
  • We need to clean up possibly already present prepared statements in the connection. To do that we run the command DEALLOCATE ALL before trying to prepare and execute the real query.

Downsides

  • Slight performance impact due to extra roundtrips for DEALLOCATE ALL, BEGIN and COMMIT every single query.

2. Migration Engine and Introspection Engine will not work with pgBouncer (in transaction mode)

image
image

use the real database directly instead of the pgBouncer credentials to work around this

3. If you are using pgBouncer, but do not set the query param you will get an error message similar to this:

ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState("26000"), message: "prepared statement \"s0\" does not exist", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("prepare.c"), line: Some(505), routine: Some("FetchPreparedStatement") }) }) })

2 and 3 best should be confirmed before documenting.

@janpio janpio transferred this issue from prisma/prisma Jun 12, 2020
@mhwelander mhwelander added the docs Documentation creation, updates or corrections label Jun 16, 2020
@janpio janpio added web/candidate Candidate for next Web Sprint docs/candidate and removed web/candidate Candidate for next Web Sprint labels Aug 22, 2020
@danba340
Copy link

What do you mean by 'but do not set the query param'?
Would be great if you could clarify 👍

@janpio
Copy link
Contributor Author

janpio commented Sep 28, 2020

To enable PgBoucner mode in Prisma you need to add a query parameter &pgbouncer=true to the connection string of your database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation creation, updates or corrections
Projects
None yet
Development

No branches or pull requests

5 participants