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/administration/index.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,7 @@ The admin database name is [configurable](../configuration/pgdog.toml/admin.md).
18
18
|[`SHOW SERVERS`](servers.md)| Server connections made by PgDog to PostgreSQL. |
19
19
|[`SHOW POOLS`](pools.md)| Connection pools used to multiplex clients and servers. |
20
20
|[`SHOW CONFIG`](config.md)| Currently loaded values from `pgdog.toml`. |
21
+
|`SHOW STATS`| Connection pools statistics. |
21
22
|`SHOW PEERS`| List of PgDog processes running on the same network. Requires service discovery to be enabled. |
22
23
|`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. |
23
24
|`RECONNECT`| Re-create all server connections using existing configuration. |
Copy file name to clipboardExpand all lines: docs/configuration/pgdog.toml/general.md
+13-5Lines changed: 13 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -79,7 +79,7 @@ Default: **none**
79
79
80
80
### `tls_client_required`
81
81
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).
83
83
84
84
Default: **`false`** (disabled)
85
85
@@ -234,9 +234,9 @@ Available options:
234
234
-`exclude_primary`
235
235
-`include_primary_if_replica_banned`
236
236
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.
238
238
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.
240
240
241
241
Default: **`include_primary`**
242
242
@@ -315,8 +315,8 @@ Enables support for prepared statements. Available options are:
315
315
-`extended_anonymous`
316
316
-`full`
317
317
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.
If enabled, log every time a user disconnects from PgDog.
384
384
385
385
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.
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.
Copy file name to clipboardExpand all lines: docs/features/sharding/sharding-functions.md
+61Lines changed: 61 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,6 +119,67 @@ PostgreSQL has dozens of data types. PgDog supports a subset of those for shardi
119
119
|`VARCHAR` / `TEXT`| :material-check-circle-outline: | :material-check-circle-outline: | No |
120
120
|`UUID`| :material-check-circle-outline: | :material-check-circle-outline: | No |
121
121
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.
0 commit comments