Skip to content

Commit 386cc87

Browse files
committed
Spellchecking lb docs
1 parent fb5d82f commit 386cc87

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

docs/features/load-balancer/manual-routing.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Query comments are supported in all types of queries, including prepared stateme
2323

2424
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.
2525

26-
The Postgres protocol doesn't have any restrictions on parameter names or values, and PgDog has access to them at connection creation.
26+
The Postgres protocol doesn't have any restrictions on parameter names or values, and PgDog can intercept and handle them at any time.
2727

2828
The following two parameters allow you to control which database is used for all queries on a client connection:
2929

@@ -34,16 +34,20 @@ The following two parameters allow you to control which database is used for all
3434

3535
The `pgdog.role` parameter accepts the following values:
3636

37-
| Parameter value | Behavior |
38-
|-|-|
39-
| `primary` | All queries are sent to the primary database. |
40-
| `replica` | All queries are load balanced between replica databases, and possibly the primary if [`read_write_split`](../../configuration/pgdog.toml/general.md#read_write_split) is set to `include_primary` (default). |
37+
| Parameter value | Behavior | Example |
38+
|-|-|-|
39+
| `primary` | All queries are sent to the primary database. | `SET pgdog.role TO "primary"` |
40+
| `replica` | All queries are load balanced between replica databases, and possibly the primary if [`read_write_split`](../../configuration/pgdog.toml/general.md#read_write_split) is set to `include_primary` (default). | `SET pgdog.role TO "replica"` |
41+
42+
The `pgdog.shard` parameter accepts a shard number for any database specified in [`pgdog.toml`](../../configuration/pgdog.toml/databases.md), for example:
4143

42-
The `pgdog.shard` parameter accepts a shard number for any database specified in [`pgdog.toml`](../../configuration/pgdog.toml/databases.md).
44+
```postgresql
45+
SET pgdog.shard TO 1;
46+
```
4347

4448
### Setting the parameters
4549

46-
Configuring parameters at connection creation is PostgreSQL driver-specific. Some of the common drivers and frameworks are shown below.
50+
Configuring parameters can be done at connection creation, or by using the `SET` command. Below are examples of some of the common PostgreSQL drivers and web frameworks.
4751

4852
#### Database URL
4953

@@ -114,19 +118,19 @@ Depending on the environment, the parameters may need to be URL-encoded, e.g., `
114118

115119
### Using `SET`
116120

117-
The PostgreSQL protocol supports configuring connection parameters using the `SET` statement. This also works for configuring both `pgdog.role` and `pgdog.shard`.
121+
The PostgreSQL protocol supports changing connection parameters using the `SET` statement. By extension, this also works for changing `pgdog.role` and `pgdog.shard` settings.
118122

119-
For example, to make sure all subsequent queries to be sent to the primary, you can execute the following statement:
123+
For example, to make sure all subsequent queries are sent to the primary, you can execute the following statement:
120124

121125
```postgresql
122126
SET pgdog.role TO "primary";
123127
```
124128

125-
The parameter is persisted on the connection until it's closed or the parameter is changed with another `SET` statement.
129+
The parameter is persisted on the connection until it's closed or the value is changed with another `SET` statement. Before routing a query, the load balancer will check the value of this parameter, so setting it early on during connection creation ensures all transactions are executed on the right database.
126130

127131
#### Inside transactions
128132

129-
If you want to provide a transaction routing hint without affecting the rest of the connection, you can use `SET LOCAL` instead:
133+
It's possible to set routing hints for the lifetime of a single transaction, by using the `SET LOCAL` command. This ensures the routing hint is used for one transaction only and doesn't affect the rest of the queries:
130134

131135
```postgresql
132136
BEGIN;
@@ -136,7 +140,7 @@ SET LOCAL pgdog.role TO "primary";
136140
In this example, all transaction statements (including the `BEGIN` statement) will be sent to the primary database. Whether the transaction is committed or reverted, the value of `pgdog.role` will be reset to its previous value.
137141

138142
!!! note "Statement ordering"
139-
To make sure PgDog intercepts the routing hint early enough in the transaction flow, make sure to send all hints _before_ executing actual queries.
143+
To make sure PgDog intercepts the routing hint early enough in the transaction flow, you need to send all hints _before_ executing actual queries.
140144

141145
The following flow, for example, _will not_ work:
142146

@@ -153,7 +157,7 @@ In this example, all transaction statements (including the `BEGIN` statement) wi
153157

154158
In certain situations, the overhead of parsing queries may be too high, e.g., when your application can't use prepared statements.
155159

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):
160+
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):
157161

158162
```toml
159163
[general]

docs/features/load-balancer/transactions.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ icon: material/swap-horizontal
66

77
PgDog's load balancer is [transaction-aware](../transaction-mode.md) and will ensure that all statements inside a transaction are sent to the same PostgreSQL connection on just one database.
88

9-
To make sure all queries inside a transaction succeed, PgDog will route all manually started transactions to the **primary** database.
9+
To make sure all queries inside a transaction succeed, PgDog will route all manually started transactions to the primary database.
1010

1111
## How it works
1212

@@ -18,57 +18,62 @@ INSERT INTO users (email, created_at) VALUES ($1, NOW()) RETURNING *;
1818
COMMIT;
1919
```
2020

21-
PgDog processes queries immediately upon receiving them, and since transactions can contain multiple statements, it isn't possible to determine whether one of the statements won't write to the database.
21+
PgDog executes queries immediately upon receiving them, and since transactions can contain multiple statements, it isn't possible to determine in advance what the statements will do.
2222

23-
Therefore, it is more reliable to send the entire transaction to the primary database.
23+
Therefore, it is more reliable to send the entire transaction to the primary, which can handle all types of queries.
2424

2525
### Read-only transactions
2626

27-
The PostgreSQL query language allows you to declare a transaction as read-only, which prevents it from writing data to the database. PgDog can take advantage of this property and will send such transactions to a replica database.
27+
The PostgreSQL query language allows you to declare a transaction as read-only. This property prevents it from writing data, even if a database can accept writes.
2828

29-
Read-only transactions are started with the `BEGIN READ ONLY` command, for example:
29+
PgDog takes advantage of this property and will send such transactions to a replica. Read-only transactions are started with the `BEGIN READ ONLY` command, for example:
3030

3131
```postgresql
3232
BEGIN READ ONLY;
3333
SELECT * FROM users WHERE id = $1;
3434
COMMIT;
3535
```
3636

37-
Read-only transactions are useful when queries need a consistent view of the database. Some Postgres database drivers allow this option to be set in the code, for example:
37+
In addition to forcing all statements to a replica, read-only transactions are useful when queries need a consistent view of the database. Most Postgres client drivers allow this option to be set in the code, for example:
3838

39-
=== "pgx (go)"
39+
=== "pgx (Go)"
4040
```go
4141
tx, err := conn.BeginTx(ctx, pgx.TxOptions{
4242
AccessMode: pgx.ReadOnly,
4343
})
4444
```
45-
=== "Sequelize (node)"
45+
=== "Sequelize (Node)"
4646
```javascript
4747
const tx = await sequelize.transaction({
4848
readOnly: true,
4949
});
5050
```
51-
=== "SQLAlchemy (python)"
52-
Add `postgresql_readonly=True` to [execution options](https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.Engine.execution_options), like so:
51+
=== "SQLAlchemy (Python)"
5352
```python
5453
engine = create_engine("postgresql://user:pw@pgdog:6432/prod")
5554
.execution_options(postgresql_readonly=True)
5655
```
5756

5857
### Replication lag
5958

60-
While transactions are used to atomically change multiple tables, they can also be used to manually route `SELECT` queries to the primary database. For example:
59+
Since PgDog sends all manual transactions to the primary, they can also be used to send `SELECT` queries to the primary as well.
60+
61+
For example:
6162

6263
```postgresql
6364
BEGIN;
6465
SELECT * FROM users WHERE id = $1;
6566
COMMIT;
6667
```
6768

68-
This is useful when the data in the table(s) has been recently updated and you want to avoid errors caused by replication lag. This often manifests as "record not-found"-style errors, for example:
69+
This avoids having to write additional code to handle replication lag, which is useful when the data in the table(s) has been recently updated and you want to avoid fetching stale or nonexistent rows.
6970

70-
```
71-
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=9999):
72-
```
7371

74-
While sending read queries to the primary adds load, it is often necessary in real-time systems that are not equipped to handle replication delays.
72+
!!! note "Example"
73+
If you're using Rails/ActiveRecord, these types of errors sometimes manifest like this:
74+
75+
```
76+
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=9999):
77+
```
78+
79+
While sending read queries to the primary adds additional load, it is often necessary in real-time systems that are not equipped to handle replication delays.

0 commit comments

Comments
 (0)