Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ datasource db {

#### Viewing the connection pool size

The number of connections Prisma Client uses can be viewed using [logging](/orm/prisma-client/observability-and-logging/logging) and [metrics](/orm/prisma-client/observability-and-logging/metrics).
The number of connections Prisma Client uses can be viewed using [logging](/orm/prisma-client/observability-and-logging/logging) and built in APIs provided by the Prisma Postgres Driver.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it just the normal PG driver? Do you mean here the driver adapter, or the Prisma ?

It sounds like the former, that it's a driver just for our PPg, and perhaps that's not what you mean 🤔


Using the `info` [logging level](/orm/reference/prisma-client-reference#log-levels), you can log the number of connections in a connection pool that are opened when Prisma Client is instantiated.

Expand Down Expand Up @@ -127,78 +127,6 @@ Note that the output generated by `log: ['info']` can change in any release with

</Admonition>

If you need even more insights into the size of your connection pool and the amount of in-use and idle connection, you can use the [metrics](/orm/prisma-client/observability-and-logging/metrics) feature (which is currently in Preview).

Consider the following example:

<CodeWithResult expanded="{true}">
<cmd>

```ts
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
await Promise.all([prisma.user.findMany(), prisma.post.findMany()])

const metrics = await prisma.$metrics.json()
console.dir(metrics, { depth: Infinity })
}

main()
```

</cmd>
<cmdResult>

```json no-copy
{
"counters": [
// ...
{
"key": "prisma_pool_connections_open",
"labels": {},
"value": 2,
"description": "Number of currently open Pool Connections"
}
],
"gauges": [
// ...
{
"key": "prisma_pool_connections_busy",
"labels": {},
"value": 0,
"description": "Number of currently busy Pool Connections (executing a datasource query)"
},
{
"key": "prisma_pool_connections_idle",
"labels": {},
"value": 21,
"description": "Number of currently unused Pool Connections (waiting for the next datasource query to run)"
},
{
"key": "prisma_pool_connections_opened_total",
"labels": {},
"value": 2,
"description": "Total number of Pool Connections opened"
}
],
"histograms": [
/** ... **/
]
}
```

</cmdResult>
</CodeWithResult>

<Admonition type="info">

For more details on what is available in the metrics output, see the [About metrics](/orm/prisma-client/observability-and-logging/metrics#about-metrics) section.

</Admonition>

### Connection pool timeout

#### Default pool timeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,25 @@ const prisma = new PrismaClient().$extends({
The following example uses the `client` component to add two methods to Prisma Client:

- `$log` outputs a message.
- `$totalQueries` returns the number of queries executed by the current client instance. It uses the [metrics](/orm/prisma-client/observability-and-logging/metrics) feature to collect this information.
- `$totalQueries` returns the number of queries executed by the current client instance.

<Admonition type="info">

To use metrics in your project, you must enable the `metrics` feature flag in the `generator` block of your `schema.prisma` file. [Learn more](/orm/prisma-client/observability-and-logging/metrics#2-enable-the-feature-flag-in-the-prisma-schema-file).
```ts

</Admonition>

```ts
const total = 0
const prisma = new PrismaClient().$extends({
client: {
$log: (s: string) => console.log(s),
async $totalQueries() {
const index_prisma_client_queries_total = 0
// Prisma.getExtensionContext(this) in the following block
// returns the current client instance
const metricsCounters = await (
await Prisma.getExtensionContext(this).$metrics.json()
).counters

return metricsCounters[index_prisma_client_queries_total].value
},
async $totalQueries() { return total; },
},
query: {
$allModels: {
async $allOperations({ query, args }) {
total += 1;
return query(args);
},
},
},
})

async function main() {
Expand Down
Loading
Loading