Skip to content

Commit ae2a621

Browse files
committed
document schema cache
1 parent 288861c commit ae2a621

4 files changed

Lines changed: 66 additions & 4 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
icon: material/cached
3+
---
4+
5+
# Schema cache
6+
7+
To be able to automatically detect sharded and omnisharded tables, PgDog fetches the Postgres schema from all shards on startup. The schema is placed into an in-memory cache and contains Postgres schema names, tables and their columns, including data types.
8+
9+
## How it works
10+
11+
The schema cache allows PgDog to detect the column order in certain queries and extract sharding keys automatically. For example, consider the following table and query:
12+
13+
```postgresql
14+
CREATE TABLE users (
15+
id BIGINT PRIMARY KEY,
16+
email VARCHAR NOT NULL UNIQUE
17+
);
18+
```
19+
20+
When an `INSERT` query without explicit column names is executed, PgDog will be able to extract the sharding key using the schema cache:
21+
22+
```postgresql
23+
INSERT INTO users VALUES ($1, $2);
24+
-- $1 is id
25+
-- $2 is email
26+
```
27+
28+
### Omnisharded check
29+
30+
In addition to detecting sharding keys, PgDog uses the schema cache to determine if a table is sharded or omnisharded. If a sharded table is configured using a column name only in [`pgdog.toml`](../../../configuration/pgdog.toml/sharded_tables.md), inspecting its schema is the only way to find if it has the sharding key, for example:
31+
32+
```toml
33+
[[sharded_tables]]
34+
database = "prod"
35+
column = "user_id"
36+
```
37+
38+
When a query comes in that doesn't specify this column, PgDog needs to decide if the table is sharded or omnisharded:
39+
40+
```postgresql
41+
SELECT * FROM users WHERE created_at > NOW() - INTERVAL '1 hour';
42+
```
43+
44+
PgDog will inspect its in-memory schema cache for the `users` table (taking into consideration the `search_path` setting), and if the table contains the `user_id` column, treat it as a [cross-shard `SELECT`](../cross-shard-queries/select.md). Otherwise, it'll assume the table is [omnisharded](../omnishards.md) and send the query to one of the shards only.
45+
46+
### Migrations
47+
48+
If you change your database schema, e.g. add a new table or alter an existing one, make sure to reload the schema cache, so PgDog is able to route queries correctly. To reload the schema cache, just run the [`RELOAD`](../../../administration/index.md) command via the admin database:
49+
50+
```
51+
RELOAD;
52+
```
53+
54+
The cache is reloaded every time PgDog re-creates its connection pools, so executing the `RECONNECT` command or restarting PgDog will have the same effect.

docs/features/sharding/schema_management/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ icon: material/database
55

66
Schemas in sharded databases require additional tooling and maintenance to work correctly. Most aspects of this are handled by PgDog automatically and documented below.
77

8+
## Schema cache
9+
10+
If your database is sharded, PgDog will automatically load the schema from all shards on startup and use it to detect sharded and omnisharded tables.
11+
12+
[**→ Schema cache**](cache.md)
13+
814
### Schema manager
915

1016
Tooling and automation for managing tables, indexes, primary keys, and other schema entities.

docs/features/sharding/schema_management/manager.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
icon: material/knob
33
---
4+
45
# Schema manager
56

67
PgDog provides several tools for sharded schema management. Currently, most of the functionality is implemented in Postgres with pl/PgSQL functions. To take advantage of it, you need to install these functions into your sharded database.

docs/features/sharding/supported-queries.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ SELECT * FROM users WHERE id IN ($1, $2, $3);
3333
The sharding key can be present anywhere in the query, including a join, a subquery, or a CTE. For example:
3434

3535
```postgresql
36-
--- Join.
36+
-- Join.
3737
SELECT * FROM users
3838
INNER JOIN orders ON users.id = $1 AND orders.user_id = users.id;
3939
40-
--- Subquery.
40+
-- Subquery.
4141
SELECT * FROM users WHERE id IN (
4242
SELECT user_id FROM orders WHERE user_id = $1
4343
);
4444
45-
--- CTE.
45+
-- CTE.
4646
WITH my_user AS (
4747
SELECT * FROM users WHERE id = $1
4848
) SELECT * FROM my_user;
@@ -64,10 +64,11 @@ DELETE FROM users WHERE id IN ($1, $2, $3);
6464

6565
### `INSERT`
6666

67-
`INSERT` queries need to specify the column names in order for PgDog to be able to extract the sharding key from the tuple:
67+
`INSERT` queries can take two forms: specifying column names explicitly or implicitly. PgDog is able to detect the sharding key in both cases (using its [schema cache](schema_management/cache.md)) and route the query correctly:
6868

6969
```postgresql
7070
INSERT INTO users (id, email) VALUES ($1, $2);
71+
INSERT INTO users VALUES ($1, $2);
7172
```
7273

7374
Statements with multiple tuples can be [rewritten automatically](cross-shard-queries/insert.md#multiple-tuples) to write each row to its corresponding shard.

0 commit comments

Comments
 (0)