Skip to content

Commit 8004051

Browse files
ReppCodesOxyjun
andauthored
Add some notes to hyperdrive docs (#19616)
* Note optimal postgres.js connection settings * Note that retrying internal errors is appropriate * Update src/content/docs/hyperdrive/get-started.mdx * Update src/content/docs/workers/tutorials/postgres/index.mdx * Update src/content/partials/hyperdrive/create-hyperdrive-config.mdx * Update src/content/partials/hyperdrive/use-postgresjs-to-make-query.mdx --------- Co-authored-by: Jun Lee <[email protected]>
1 parent 17fe459 commit 8004051

File tree

7 files changed

+60
-12
lines changed

7 files changed

+60
-12
lines changed

src/content/docs/hyperdrive/configuration/connect-to-postgres.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,4 @@ To identify active connections to your Postgres database server from Hyperdrive:
161161

162162
- Refer to the list of [supported database integrations](/workers/databases/connecting-to-databases/) to understand other ways to connect to existing databases.
163163
- Learn more about how to use the [Socket API](/workers/runtime-apis/tcp-sockets) in a Worker.
164-
- Understand the [protocols supported by Workers](/workers/reference/protocols/).
164+
- Understand the [protocols supported by Workers](/workers/reference/protocols/).

src/content/docs/hyperdrive/get-started.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,19 @@ export default {
195195
// Hyperdrive generates a unique connection string you can pass to
196196
// supported drivers, including node-postgres, Postgres.js, and the many
197197
// ORMs and query builders that use these drivers.
198-
const sql = postgres(env.HYPERDRIVE.connectionString);
198+
const sql = postgres(
199+
env.HYPERDRIVE.connectionString,
200+
{
201+
// Workers limit the number of concurrent external connections, so be sure to limit
202+
// the size of the local connection pool that postgres.js may establish.
203+
max: 5,
204+
205+
// If you are using array types in your Postgres schema, it is necessary to fetch
206+
// type information to correctly de/serialize them. However, if you are not using
207+
// those, disabling this will save you an extra round-trip every time you connect.
208+
fetch_types: false,
209+
},
210+
);
199211

200212
try {
201213
// Test query
@@ -245,4 +257,4 @@ By finishing this tutorial, you have created a Hyperdrive configuration, a Worke
245257
- How to [configure query caching](/hyperdrive/configuration/query-caching/).
246258
- [Troubleshooting common issues](/hyperdrive/observability/troubleshooting/) when connecting a database to Hyperdrive.
247259

248-
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the [Cloudflare Developers community on Discord](https://discord.cloudflare.com).
260+
If you have any feature requests or notice any bugs, share your feedback directly with the Cloudflare team by joining the [Cloudflare Developers community on Discord](https://discord.cloudflare.com).

src/content/docs/hyperdrive/observability/troubleshooting.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Hyperdrive may also encounter `ErrorResponse` wire protocol messages sent by you
3838

3939
| Error Message | Details | Recommended fixes |
4040
| ------------------------------------------------------ | ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
41-
| `Internal error.` | Something is broken on our side. | Check for an ongoing incident affecting Hyperdrive, and contact Cloudflare Support. |
41+
| `Internal error.` | Something is broken on our side. | Check for an ongoing incident affecting Hyperdrive, and contact Cloudflare Support. Retrying the query is appropriate, if it makes sense for your usage pattern. |
4242
| `Failed to acquire a connection from the pool.` | Hyperdrive timed out while waiting for a connection to your database, or cannot connect at all. | If you are seeing this error intermittently, your Hyperdrive pool is being exhausted because too many connections are being held open for too long by your worker. This can be caused by a myriad of different issues, but long-running queries/transactions are a common offender. |
4343
| `Server connection attempt failed: connection_refused` | Hyperdrive is unable to create new connections to your origin database. | A network firewall or access control list (ACL) is likely rejecting requests from Hyperdrive. Ensure you have allowed connections from the public Internet. Sometimes, this can be caused by your database host provider refusing incoming connections when you go over your connection limit. |
4444

@@ -50,4 +50,4 @@ Hyperdrive may also encounter `ErrorResponse` wire protocol messages sent by you
5050

5151
### Improve performance
5252

53-
Having query traffic written as transactions can limit performance. This is because in the case of a transaction, the connection must be held for the duration of the transaction, which limits connection multiplexing. If there are multiple queries per transaction, this can be particularly impactful on connection multiplexing. Where possible, we recommend not wrapping queries in transactions to allow the connections to be shared more aggressively.
53+
Having query traffic written as transactions can limit performance. This is because in the case of a transaction, the connection must be held for the duration of the transaction, which limits connection multiplexing. If there are multiple queries per transaction, this can be particularly impactful on connection multiplexing. Where possible, we recommend not wrapping queries in transactions to allow the connections to be shared more aggressively.

src/content/docs/pages/functions/bindings.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,4 +834,4 @@ When developing locally, add secrets by creating a `.dev.vars` file in the root
834834

835835
```
836836
SECRET_NAME=<SECRET_VALUE>
837-
```
837+
```

src/content/docs/workers/tutorials/postgres/index.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,19 @@ import postgres from "postgres";
195195

196196
export default {
197197
async fetch(request, env, ctx): Promise<Response> {
198-
const sql = postgres(env.DB_URL);
198+
const sql = postgres(
199+
env.DB_URL,
200+
{
201+
// Workers limit the number of concurrent external connections, so be sure to limit
202+
// the size of the local connection pool that postgres.js may establish.
203+
max: 5,
204+
205+
// If you are using array types in your Postgres schema, it is necessary to fetch
206+
// type information to correctly de/serialize them. However, if you are not using
207+
// those, disabling this will save you an extra round-trip every time you connect.
208+
fetch_types: false,
209+
},
210+
);
199211

200212
// Query the products table
201213
const result = await sql`SELECT * FROM products;`;
@@ -368,4 +380,4 @@ Your Worker application is now live and accessible at `<YOUR_WORKER>.<YOUR_SUBDO
368380

369381
To build more with databases and Workers, refer to [Tutorials](/workers/tutorials) and explore the [Databases documentation](/workers/databases).
370382

371-
If you have any questions, need assistance, or would like to share your project, join the Cloudflare Developer community on [Discord](https://discord.cloudflare.com) to connect with fellow developers and the Cloudflare team.
383+
If you have any questions, need assistance, or would like to share your project, join the Cloudflare Developer community on [Discord](https://discord.cloudflare.com) to connect with fellow developers and the Cloudflare team.

src/content/partials/hyperdrive/create-hyperdrive-config.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,19 @@ export default {
7272
// Hyperdrive generates a unique connection string you can pass to
7373
// supported drivers, including node-postgres, Postgres.js, and the many
7474
// ORMs and query builders that use these drivers.
75-
const sql = postgres(env.HYPERDRIVE.connectionString);
75+
const sql = postgres(
76+
env.HYPERDRIVE.connectionString,
77+
{
78+
// Workers limit the number of concurrent external connections, so be sure to limit
79+
// the size of the local connection pool that postgres.js may establish.
80+
max: 5,
81+
82+
// If you are using array types in your Postgres schema, it is necessary to fetch
83+
// type information to correctly de/serialize them. However, if you are not using
84+
// those, disabling this will save you an extra round-trip every time you connect.
85+
fetch_types: false,
86+
},
87+
);
7688

7789
try {
7890
// Test query
@@ -92,4 +104,4 @@ export default {
92104

93105
- Learn more about [How Hyperdrive Works](/hyperdrive/configuration/how-hyperdrive-works/).
94106
- Refer to the [troubleshooting guide](/hyperdrive/observability/troubleshooting/) to debug common issues.
95-
- Understand more about other [storage options](/workers/platform/storage-options/) available to Cloudflare Workers.
107+
- Understand more about other [storage options](/workers/platform/storage-options/) available to Cloudflare Workers.

src/content/partials/hyperdrive/use-postgresjs-to-make-query.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ export default {
2323
async fetch(request: Request, env: Env, ctx: ExecutionContext) {
2424
// NOTE: if `prepare: false` is passed when connecting, performance will
2525
// be slower but still correctly supported.
26-
const sql = postgres(env.HYPERDRIVE.connectionString);
26+
const sql = postgres(
27+
env.HYPERDRIVE.connectionString,
28+
{
29+
// Workers limit the number of concurrent external connections, so be sure to limit
30+
// the size of the local connection pool that postgres.js may establish.
31+
max: 5,
32+
33+
// If you are using array types in your Postgres schema, it is necessary to fetch
34+
// type information to correctly de/serialize them. However, if you are not using
35+
// those, disabling this will save you an extra round-trip every time you connect.
36+
fetch_types: false,
37+
},
38+
);
2739

2840
try {
2941
// A very simple test query
@@ -41,4 +53,4 @@ export default {
4153
}
4254
},
4355
} satisfies ExportedHandler<Env>;
44-
```
56+
```

0 commit comments

Comments
 (0)