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/connection-pooler/index.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,12 @@ If your application uses `LISTEN`/`NOTIFY`, e.g., [DBOS](https://dbos.dev) or an
39
39
40
40
This allows applications to use `LISTEN`/`NOTIFY` in [transaction mode](pub_sub.md), just like any other Postgres feature.
41
41
42
+
### Connection recovery
43
+
44
+
Unlike other poolers, PgDog takes extra care not to close connections to Postgres unless absolutely necessary. It goes as far as to roll back abandoned transactions and drain abandoned queries. This protects the database against connection storms created by buggy applications.
45
+
46
+
You can read more about connection recovery methods [here](connection-recovery.md).
Copy file name to clipboardExpand all lines: docs/features/load-balancer/healthchecks.md
+16-9Lines changed: 16 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,8 @@ All databases load balanced by PgDog are regularly checked with health checks. A
9
9
If a replica database fails a health check, it's temporarily removed from the load balancer, preventing it from serving queries for a configurable period of time.
<p>Health checks prevent broken databases from serving queries.</p>
13
14
</center>
14
15
15
16
## How it works
@@ -23,8 +24,7 @@ PgDog performs two kinds of health checks to ensure applications don't accidenta
23
24
24
25
If a connection or database fails a health check, it is **temporarily removed** from the load balancer and cannot serve any more queries. This prevents applications from continuously hitting a broken database until it's restarted by an administrator.
25
26
26
-
!!! note "99.99% uptime"
27
-
This strategy is very effective at reducing error rates in busy applications. If you are operating a large number of databases, hardware failures are relatively common and an effective load balancer is required to maintain 99.99% database uptime.
27
+
This strategy is very effective at reducing error rates in busy applications. If you are operating a large number of databases, hardware failures are relatively common and an effective load balancer is required to maintain 99.99% database uptime.
28
28
29
29
### Connection health check
30
30
@@ -85,6 +85,13 @@ When PgDog is first started, it's possible that the database or the network is n
85
85
86
86
The **default** value for this setting is **5 seconds** (`5_000` milliseconds).
87
87
88
+
!!! note "Disabling health checks"
89
+
If you want to disable database health checks, you can set the `idle_healthcheck_delay` setting to a large number, e.g. 100 years, in milliseconds:
90
+
91
+
```toml
92
+
[general]
93
+
idle_healthcheck_delay = 3155760000000
94
+
```
88
95
89
96
### Primary database exception
90
97
@@ -108,10 +115,7 @@ The amount of time the database is banned from serving traffic is controlled wit
108
115
109
116
The **default** value is **5 minutes** (`300_000` milliseconds).
110
117
111
-
!!! note
112
-
A database will not be placed back into the load balancer until it passes a health check again.
113
-
114
-
Make sure that `idle_healthcheck_interval` is set to a lower value than `ban_timeout`, so health checks have time to run before you expect the database to resume serving traffic.
118
+
A database will not be placed back into the load balancer until it passes a health check again. Make sure that `idle_healthcheck_interval` is set to a lower value than `ban_timeout`, so health checks have time to run before you expect the database to resume serving traffic.
115
119
116
120
### False positives
117
121
@@ -170,7 +174,10 @@ This is configurable on startup only and will spin up an HTTP server on `http://
170
174
171
175
This health check looks at all configured connection pools, and if **at least one** is online, responds with `HTTP/1.1 200 OK`. If _all_ connection pools are down because of failed health checks, PgDog will respond with `HTTP/1.1 502 Bad Gateway`.
172
176
173
-
!!! note "Handling a lot of requests"
174
-
The HTTP health check uses existing internal state to answer requests and doesn't send queries to the connection pools. This makes it very quick and inexpensive, which ensures that massively distributed load balancers (like the AWS NLB) don't cause an unexpected influx of requests to the database.
177
+
#### Handling a lot of requests
178
+
179
+
The HTTP health check uses existing internal state to answer requests and doesn't send queries to the connection pools. This makes it very quick and inexpensive, which ensures that massively distributed load balancers (like the AWS NLB) don't cause an unexpected influx of requests to the database.
180
+
181
+
#### HTTPS
175
182
176
183
To make configuration easier, the health check endpoint doesn't support HTTPS, so make sure to configure your load balancer to use plain HTTP only.
Copy file name to clipboardExpand all lines: docs/features/load-balancer/index.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ icon: material/lan
9
9
10
10
# Load balancer overview
11
11
12
-
PgDog understands the PostgreSQL wire protocol and uses its SQL parser to understand queries. This allows it to split read queries from write queries and distribute traffic evenly between databases.
12
+
PgDog understands the PostgreSQL wire protocol and uses the native PostgreSQL parser to understand queries. This allows it to split read queries from write queries and distribute traffic evenly between databases.
13
13
14
14
Applications can connect to a single PgDog [endpoint](#single-endpoint), without having to manually manage multiple connection pools.
15
15
@@ -18,13 +18,15 @@ Applications can connect to a single PgDog [endpoint](#single-endpoint), without
18
18
When a query is received by PgDog, it will inspect it using the native Postgres SQL parser. If the query is a `SELECT` and the [configuration](../../configuration/pgdog.toml/databases.md) contains both primary and replica databases, PgDog will send it to one of the replicas. For all other queries, PgDog will send them to the primary.
Applications don't have to manually route queries between databases or maintain several connection pools internally.
25
26
26
-
!!! note "SQL compatibility"
27
-
PgDog's query parser is powered by the `pg_query` library, which extracts the Postgres native SQL parser directly from its source code. This makes it **100% compatible** with the PostgreSQL query language and allows PgDog to understand all valid PostgreSQL queries.
27
+
### SQL compatibility
28
+
29
+
PgDog's query parser is powered by the `pg_query` library, which extracts the Postgres native SQL parser directly from its source code. This makes it **100% compatible** with the PostgreSQL query language and allows PgDog to understand all valid PostgreSQL queries.
Copy file name to clipboardExpand all lines: docs/features/load-balancer/manual-routing.md
+28-10Lines changed: 28 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,17 +139,17 @@ SET LOCAL "pgdog"."role" TO "primary";
139
139
140
140
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.
141
141
142
-
!!! note "Statement ordering"
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.
142
+
#### Statement ordering
143
+
To make sure PgDog intercepts the routing hint early enough in the transaction flow, you need to send all hints _before_ executing DDL/DML statements.
144
144
145
-
The following flow, for example, _will not_ work:
145
+
The following flow, for example, _will not_ work:
146
146
147
-
```postgresql
148
-
BEGIN;
149
-
SELECT * FROM users WHERE id = $1;
150
-
SET LOCAL pgdog.role TO "primary"; -- The client is already connected to a server.
151
-
INSERT INTO users (id) VALUES ($1); -- If connected to a replica, this will fail.
152
-
```
147
+
```postgresql
148
+
BEGIN;
149
+
SELECT * FROM users WHERE id = $1;
150
+
SET LOCAL pgdog.role TO "primary"; -- The client is already connected to a server.
151
+
INSERT INTO users (id) VALUES ($1); -- If connected to a replica, this will fail.
152
+
```
153
153
154
154
155
155
@@ -171,6 +171,24 @@ If you've configured the desired database role (and/or shard) for each of your a
171
171
172
172
Once it's disabled, PgDog will rely solely on the `pgdog.role` and `pgdog.shard` parameters to make its routing decisions.
173
173
174
-
### Session state & `SET`
174
+
### Session state and SET
175
175
176
176
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](../connection-pooler/transaction-mode.md).
177
+
178
+
You can keep the parser enabled for handling `SET` commands only as follows:
179
+
180
+
=== "pgdog.toml"
181
+
```toml
182
+
[general]
183
+
query_parser = "session_state"
184
+
```
185
+
=== "Helm chart"
186
+
```yaml
187
+
queryParser: session_state
188
+
```
189
+
190
+
The internal implementation is using a very fast Regex to detect `SET` commands and will turn on the query parser for that statement only. The regex supports comments as well, so the following example will be detected:
191
+
192
+
```postgresql
193
+
/* api: users.create */ SET application_name TO 'sidekiq';
PgDog has built-in functionality for monitoring the state of Postgres replica databases. If configured, it can also automatically detect when a replica is promoted and redirect write queries to the new primary, and ban replicas from serving traffic if they have fallen far behind in the replication stream.
7
+
PgDog has built-in functionality for monitoring the state of Postgres replica databases. If configured, it can also automatically detect when a replica is promoted and redirect write queries to the new primary, or block replicas from serving traffic if they have fallen far behind in the replication stream.
8
8
9
9
## Replication
10
10
@@ -25,15 +25,15 @@ In addition to fetching raw metrics, PgDog can calculate the replication lag (al
25
25
| Primary LSN | Get the LSN from the primary using `pg_current_wal_lsn()`. |
26
26
| Replica LSN | Get the LSN from each replica using `pg_last_wal_replay_lsn()` or `pg_last_wal_receive_lsn()`. |
27
27
| LSN check | If the two LSNs are identical, replication lag is 0. |
28
-
| Calculate lag | If the two LSNs are different, replication lag is `now() - pg_last_xact_replay_timestamp()`. |
28
+
| Calculate lag | If the two LSNs are different, replication lag is `now() - pg_last_xact_replay_timestamp()` retrieved from the replica. |
29
29
30
30
This formula assumes that when the replica's LSN is behind the primary, the primary is still receiving write requests. While this is not always the case, it will show replication lag growing over time if the replication stream is falling behind or is broken.
31
31
32
32
!!! note "Formula accuracy"
33
33
It is possible to calculate the exact replication delay in bytes by subtracting a replica LSN from the primary LSN. While this provides an exact measurement,
34
34
that metric isn't very useful: it's hard to translate bytes into a measurement of how stale the data on the replica truly is.
35
35
36
-
Approximating the lag in milliseconds is more informative and will be reasonably accurate the majority of the time.
36
+
Approximating the lag in milliseconds is more informative and will be reasonably accurate most of the time.
37
37
38
38
### Configuration
39
39
@@ -66,10 +66,15 @@ Decreasing the value of `lsn_check_interval` will produce more accurate statisti
66
66
67
67
It's common for PgDog deployments to be serving upwards of 30,000-50,000 queries per second per pooler process, so you can run the LSN check query quite frequently without noticeable impact on system latency.
68
68
69
+
#### Saturated connection pool
70
+
71
+
If the connection pool is at capacity, either due to a database incident or an inefficient query, PgDog will create a standalone connection to fetch the LSN from each database in the configuration. This is to make sure that it can't miss
72
+
a [failover](#failover) event during a database incident.
73
+
69
74
### Replica lag ban
70
75
71
-
!!! note "Experimental feature"
72
-
This feature is new and experimental. Please report any issues you encounter.
76
+
!!! note "New feature"
77
+
This feature is new and experimental. Please report any issues you may encounter.
73
78
74
79
If a replica has fallen far behind the primary, it may start serving stale data to the application. This can cause hard to debug issues, so it's often best to remove this replica from the load balancer until it's able to catch up.
75
80
@@ -98,14 +103,15 @@ Unlike [health check-triggered](healthchecks.md) bans, replica lag ban is not cl
If the `pg_is_in_recovery()` function returns `true`, the database is configured as a standby. It can only serve read queries (e.g. `SELECT`) and is expected to be reasonably up-to-date with the primary database.
106
107
107
108
Replica databases can be promoted to serve write queries. If that happens, `pg_is_in_recovery()` will start returning `false`. You can read more about this in the [PostgreSQL documentation](https://www.postgresql.org/docs/18/functions-admin.html#FUNCTIONS-RECOVERY-CONTROL).
PgDog does not detect primary failure and **will not** call `pg_promote()`. It is expected that the databases are managed externally by another tool, like Patroni or AWS RDS, which handle replica promotion.
111
117
@@ -140,6 +146,10 @@ Failover is disabled by default. To enable it, change all configured databases i
140
146
141
147
On startup, PgDog will connect to each database, find out if they are in recovery, and automatically reload its configuration with the determined roles.
142
148
149
+
!!! info "Replication lag monitoring"
150
+
In order for PgDog to detect failover events, it needs to query the database for its [replication status](#configuration). Make sure to set
151
+
`lsn_check_delay` to a reasonable value (e.g., `0`) before enabling this feature.
152
+
143
153
### Split brain
144
154
145
155
If a replica is promoted while the existing primary is alive and serving queries, write queries can be routed to either database, causing data loss. This type of error is called "split brain", indicating that the database cluster no longer has an authoritative source of data it's managing.
0 commit comments