You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features/load-balancer/manual-routing.md
+18-21Lines changed: 18 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,11 +21,11 @@ Query comments are supported in all types of queries, including prepared stateme
21
21
22
22
## Parameters
23
23
24
-
Startup parameters are connection-specific settings that are typically set on connection creation to configure database behavior. For example, this is how ORMs and web frameworks control settings like `application_name`, `work_mem`, `statement_timeout` and many others.
24
+
Parameters are connection-specific settings that can be set on connection creation to configure database behavior. For example, this is how ORMs and web frameworks control settings like `application_name`, `statement_timeout` and many others.
25
25
26
-
The Postgres protocol doesn't have any restrictions on parameter names or values, and PgDog can choose to forward those settings to Postgres (or not).
26
+
The Postgres protocol doesn't have any restrictions on parameter names or values, and PgDog has access to them at connection creation.
27
27
28
-
PgDog has two parameters that control which database is used for all queries on a client connection:
28
+
The following two parameters allow you to control which database is used for all queries on a client connection:
29
29
30
30
| Parameter | Description |
31
31
|-|-|
@@ -43,7 +43,7 @@ The `pgdog.shard` parameter accepts a shard number for any database specified in
43
43
44
44
### Setting the parameters
45
45
46
-
Setting the parameter at connection creation is PostgreSQL driver-specific. Some of the common drivers and frameworks are shown below.
46
+
Configuring parameters at connection creation is PostgreSQL driver-specific. Some of the common drivers and frameworks are shown below.
47
47
48
48
#### Database URL
49
49
@@ -57,7 +57,7 @@ Depending on the environment, the parameters may need to be URL-encoded, e.g., `
57
57
58
58
=== "asyncpg"
59
59
60
-
[asyncpg](https://pypi.org/project/asyncpg/) is a popular PostgreSQL driver for asynchronous Python applications. It allows you to set connection parameters when creating a connection:
60
+
[asyncpg](https://pypi.org/project/asyncpg/) is a popular PostgreSQL driver for asynchronous Python applications. It allows you to set connection parameters on connection setup:
61
61
62
62
```python
63
63
conn = await asyncpg.connect(
@@ -74,16 +74,13 @@ Depending on the environment, the parameters may need to be URL-encoded, e.g., `
74
74
75
75
=== "SQLAlchemy"
76
76
77
-
[SQLAlchemy](https://www.sqlalchemy.org/) is a popular Python ORM, which supports any number of PostgreSQL connection drivers. For example, if you're using `asyncpg`, you can set connection parameters as follows:
77
+
[SQLAlchemy](https://www.sqlalchemy.org/) is a Python ORM, which supports any number of PostgreSQL connection drivers. For example, if you're using `asyncpg`, you can set connection parameters as follows:
@@ -103,31 +100,33 @@ Depending on the environment, the parameters may need to be URL-encoded, e.g., `
103
100
options: "-c pgdog.role=replica -c pgdog.shard=0"
104
101
```
105
102
106
-
These options are passed to the [`pg`](https://github.com/ged/ruby-pg) driver, so if you're using it directly, you can create connections manually like so:
103
+
These options are passed to the [`pg`](https://github.com/ged/ruby-pg) driver. If you're using it directly, you can create connections like so:
107
104
108
105
```ruby
109
106
require "pg"
110
107
111
108
conn = PG.connect(
112
109
host: "10.0.0.0",
113
-
# user, password, etc.
110
+
# [...]
114
111
options: "-c pgdog.role=primary -c pgdog.shard=1"
115
112
)
116
113
```
117
114
118
115
### Using `SET`
119
116
120
-
The PostgreSQL protocol supports setting connection parameters using the `SET` statement. This also works for configuring both `pgdog.role` and `pgdog.shard` parameters.
117
+
The PostgreSQL protocol supports configuring connection parameters using the `SET` statement. This also works for configuring both `pgdog.role` and `pgdog.shard`.
121
118
122
-
For example, if you want all subsequent queries to be sent to the primary, you can execute the following statement:
119
+
For example, to make sure all subsequent queries to be sent to the primary, you can execute the following statement:
123
120
124
121
```postgresql
125
122
SET pgdog.role TO "primary";
126
123
```
127
124
125
+
The parameter is persisted on the connection until it's closed or the parameter is changed with another `SET` statement.
126
+
128
127
#### Inside transactions
129
128
130
-
If you want to provide a transaction routing hint without affecting the rest of the connection, you can use `SET LOCAL`:
129
+
If you want to provide a transaction routing hint without affecting the rest of the connection, you can use `SET LOCAL` instead:
131
130
132
131
```postgresql
133
132
BEGIN;
@@ -154,17 +153,15 @@ In this example, all transaction statements (including the `BEGIN` statement) wi
154
153
155
154
In certain situations, the overhead of parsing queries may be too high, e.g., when your application can't use prepared statements.
156
155
157
-
If you've configured the desired database role (and/or shard) for each of your application connections, you can safely disable the query parser in [pgdog.toml](../../configuration/pgdog.toml/general.md#query_parser):
156
+
If you've configured the desired database role (and/or shard) for each of your application connections, you can disable the query parser in [pgdog.toml](../../configuration/pgdog.toml/general.md#query_parser):
158
157
159
158
```toml
160
159
[general]
161
160
query_parser = "off"
162
161
```
163
162
164
-
Once the parser is disabled, PgDog will rely solely on the `pgdog.role` and `pgdog.shard` parameters to make its routing decisions.
165
-
166
-
### Session state
163
+
Once it's disabled, PgDog will rely solely on the `pgdog.role` and `pgdog.shard` parameters to make its routing decisions.
167
164
168
-
The query parser is used to intercept and interpret `SET` commands, which set session variables at runtime.
165
+
### Session state & `SET`
169
166
170
-
If the parser is disabled and your application uses `SET` commands to configure the connection at startup, PgDog will not be able to guarantee that all connections have the correct session settings in [transaction mode](../transaction-mode.md).
167
+
The query parser is used to intercept and interpret `SET` commands. If the parser is disabled and your application uses `SET` commands to configure the connection, PgDog will not be able to guarantee that all connections have the correct session settings in [transaction mode](../transaction-mode.md).
0 commit comments