Skip to content

Commit 8e3da9c

Browse files
authored
Update docs (#40)
1 parent 54652ac commit 8e3da9c

File tree

5 files changed

+138
-6
lines changed

5 files changed

+138
-6
lines changed

docs/administration/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The admin database name is [configurable](../configuration/pgdog.toml/admin.md).
1818
| [`SHOW SERVERS`](servers.md) | Server connections made by PgDog to PostgreSQL. |
1919
| [`SHOW POOLS`](pools.md) | Connection pools used to multiplex clients and servers. |
2020
| [`SHOW CONFIG`](config.md) | Currently loaded values from `pgdog.toml`. |
21+
| `SHOW STATS` | Connection pools statistics. |
2122
| `SHOW PEERS` | List of PgDog processes running on the same network. Requires service discovery to be enabled. |
2223
| `RELOAD` | Reload configuration from disk. See [pgdog.toml](../configuration/pgdog.toml/general.md) and [users.toml](../configuration/users.toml/users.md) for which options can be changed at runtime. |
2324
| `RECONNECT` | Re-create all server connections using existing configuration. |

docs/configuration/pgdog.toml/general.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Default: **none**
7979

8080
### `tls_client_required`
8181

82-
Reject clients that connect without TLS. Consider setting this to `true` when using the `enabled_plain` mode of [passthrough_auth](#passthrough_auth).
82+
Reject clients that connect without TLS. Consider setting this to `true` when using the `enabled_plain` mode of [`passthrough_auth`](#passthrough_auth).
8383

8484
Default: **`false`** (disabled)
8585

@@ -234,9 +234,9 @@ Available options:
234234
- `exclude_primary`
235235
- `include_primary_if_replica_banned`
236236

237-
Include primary uses the primary database as well as the replicas to serve read queries. Exclude primary will send all read queries to replicas, leaving the primary to serve only writes.
237+
`include_primary` uses the primary database as well as the replicas to serve read queries. `exclude_primary` will send all read queries to replicas, leaving the primary to serve only writes.
238238

239-
Include primary if replica banned strategy will only send reads to the primary if one or more replicas have been banned. This is useful in case you want to use the primary as a failover for reads.
239+
`include_primary_if_replica_banned` strategy will only send reads to the primary if one or more replicas have been banned. This is useful in case you want to use the primary as a failover for reads.
240240

241241
Default: **`include_primary`**
242242

@@ -315,8 +315,8 @@ Enables support for prepared statements. Available options are:
315315
- `extended_anonymous`
316316
- `full`
317317

318-
Full enables support for rewriting prepared statements sent over the simple protocol. Extended handles prepared statements sent normally
319-
using the extended protocol. `full` attempts to rewrite prepared statements sent using the simple protocol. `extended_anonymous` caches and rewrites unnamed prepared statements, which is useful for some legacy client drivers.
318+
`full` enables support for rewriting prepared statements sent over the simple protocol. `extended` handles prepared statements sent normally
319+
using the extended protocol. `extended_anonymous` caches and rewrites unnamed prepared statements, which is useful for some legacy client drivers.
320320

321321
Default: **`extended`**
322322

@@ -383,3 +383,11 @@ Default: **`true`** (enabled)
383383
If enabled, log every time a user disconnects from PgDog.
384384

385385
Default: **`true`** (enabled)
386+
387+
## Statistics
388+
389+
### `stats_period`
390+
391+
How often to calculate averages shown in `SHOW STATS` [admin](../../administration/index.md) command and the [Prometheus](../../features/metrics.md) metrics.
392+
393+
Default: **`15_000`** (15s)

docs/configuration/pgdog.toml/network.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PgDog speaks the Postgres protocol which, underneath, uses TCP. Optimal TCP sett
88

99
```toml
1010
[tcp]
11-
keepalives = true
11+
keepalive = true
1212
time = 60_000
1313
interval = 60_000
1414
retries = 3
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
icon: material/set-split
3+
---
4+
5+
# Sharded schemas
6+
7+
Schema-based sharding places data from tables in different Postgres schemas on their own shards.
8+
9+
## Example
10+
11+
Each schema needs to have a shard mapping in the config, for example:
12+
13+
```toml
14+
[[sharded_schemas]]
15+
database = "prod"
16+
name = "customer_a"
17+
shard = 0
18+
19+
[[sharded_schemas]]
20+
database = "prod"
21+
name = "customer_b"
22+
shard = 1
23+
```
24+
25+
All queries that fully qualify the table names will be routed correctly, for example:
26+
27+
```postgresql
28+
SELECT * FROM customer_a.users -- Prefixed with the schema name.
29+
WHERE admin = true;
30+
```
31+
32+
## Default shard
33+
34+
For queries that don't specify a schema or for which a mapping doesn't exist, the default behavior is to send it to all shards. If this is not desirable, you can configure a default shard, like so:
35+
36+
```toml
37+
[[sharded_schemas]]
38+
database = "prod"
39+
shard = 0
40+
```
41+
42+
For example, the following queries will be sent to shard zero:
43+
44+
```postgresql
45+
-- No schema specified.
46+
SELECT * FROM pg_stat_activity;
47+
48+
-- Schema isn't mapped in the config.
49+
SELECT * FROM customer_c.users
50+
WHERE admin = true;
51+
```
52+
53+
## Manual routing
54+
55+
If you need to query a specific shard or can't specify the schema name in the query, you can add it to a comment, for example:
56+
57+
```postgresql
58+
/* pgdog_sharding_key: "customer_a" */
59+
SELECT * FROM pg_stat_activity;
60+
```
61+
62+
This will send the query to the shard mapped to the `customer_a` schema.

docs/features/sharding/sharding-functions.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,67 @@ PostgreSQL has dozens of data types. PgDog supports a subset of those for shardi
119119
| `VARCHAR` / `TEXT` | :material-check-circle-outline: | :material-check-circle-outline: | No |
120120
| `UUID` | :material-check-circle-outline: | :material-check-circle-outline: | No |
121121

122+
## Schema-based sharding
123+
124+
In addition to splitting the tables themselves, PgDog can shard Postgres databases by placing different schemas on different shards. This is useful for multi-tenant applications that have stricter separation between their users' data.
125+
126+
When enabled, PgDog will route queries that fully qualify tables based on their respective schema names.
127+
128+
### Schema-to-shard mapping
129+
130+
Schemas are mapped to their shards in [pgdog.toml], for example:
131+
132+
```toml
133+
[[sharded_schemas]]
134+
database = "prod"
135+
name = "customer_a"
136+
shard = 0
137+
138+
[[sharded_schemas]]
139+
database = "prod"
140+
name = "customer_b"
141+
shard = 1
142+
```
143+
144+
Queries that include the schema name in the tables they are referring to can be routed to the right shard. For example:
145+
146+
```postgresql
147+
SELECT * FROM customer_a.users WHERE email = $1;
148+
```
149+
150+
Since the `users` table is fully qualified as `customer_a.users`, the query will be routed to shard zero.
151+
152+
### DDL
153+
154+
Unlike other sharding functions, schema-based sharding will also route DDL (e.g., `CREATE TABLE`, `CREATE INDEX`, etc.) queries to their respective shard, as long as the entity name is fully qualified.
155+
156+
For example:
157+
158+
```postgresql
159+
CREATE TABLE customer_b.users (
160+
id BIGSERIAL PRIMARY KEY,
161+
email VARCHAR NOT NULL
162+
);
163+
164+
CREATE UNIQUE INDEX ON customer_b.users USING btree(email);
165+
```
166+
167+
Both of these DDL statements will be sent to shard one, because they explicitly refer to tables in schema `customer_b`, which is mapped to shard one.
168+
169+
### Default routing
170+
171+
If a schema isn't mapped to a shard number, PgDog will fallback to using other configured sharding functions. If none are set, the query will be sent to all shards.
172+
173+
To avoid this behavior and send all other queries to a particular shard, you can add a default schema mapping:
174+
175+
```toml
176+
[[sharded_schemas]]
177+
database = "prod"
178+
shard = 0
179+
```
180+
181+
This will send all queries that don't specify a schema or use a schema without a mapping to shard zero.
182+
122183
## Read more
123184

124185
- [COPY command](copy.md)

0 commit comments

Comments
 (0)