Skip to content

Commit 4d88e1e

Browse files
authored
feat: document schema manager functions (#64)
1 parent 299f82d commit 4d88e1e

11 files changed

Lines changed: 88 additions & 103 deletions

File tree

docs/architecture/comparison.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PgDog aims to be the de facto PostgreSQL proxy and pooler. Below is a feature co
3131
|-|-|-|
3232
| [Manual routing](../features/sharding/manual-routing.md) | Only using comments (regex), doesn't work with prepared statements | :material-check-circle-outline: |
3333
| [Automatic routing](../features/sharding/query-routing.md) | No | :material-check-circle-outline: |
34-
| [Primary key generation](../features/sharding/schema_management/primary_keys.md) | No | :material-check-circle-outline: |
34+
| [Primary key generation](../features/sharding/sequences.md) | No | :material-check-circle-outline: |
3535
| [Cross-shard queries](../features/sharding/cross-shard-queries/index.md) | No | Partial support |
3636
| [COPY](../features/sharding/cross-shard-queries/copy.md) | No | :material-check-circle-outline: |
3737
| [Postgres-compatible sharding functions](../features/sharding/sharding-functions.md) | No | Same functions as declarative partitioning |

docs/enterprise_edition/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,3 @@ PgDog Enterprise is new and in active development. A lot of the features we want
8686
| QoS | Quality of service guarantees, incl. throttling on a per-user/database/query level. |
8787
| AWS RDS integration | Deploy PgDog on top of AWS RDS, without the hassle of Kubernetes or manual configuration. |
8888
| Automatic resharding | Detect hot shards and re-shard data without operator intervention. |
89-
| [Durable two-phase](cross-shard-writes.md) | Rollback / commit abandoned two-phase transactions. |

docs/enterprise_edition/insights/query_plans.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ icon: material/chart-timeline
66
For any [running query](active_queries.md) exceeding a configurable time threshold, PgDog will ask Postgres for a query plan. The query plans are stored in their own view, accessible via two methods:
77

88
1. [`SHOW QUERY_PLANS`](#admin-database) admin command
9-
2. [Activity](active_queries.md#dashboard) view in the dashboard
9+
2. [Activity](active_queries.md#web-ui) view in the dashboard
1010

1111
## How it works
1212

@@ -63,4 +63,4 @@ query_plan_max_age = 15_000
6363

6464
### Dashboard
6565

66-
The query plans are automatically attached to running queries and sent to the Dashboard via a dedicated connection. They can be viewed in real-time in the [Activity](active_queries.md#dashboard) tab.
66+
The query plans are automatically attached to running queries and sent to the Dashboard via a dedicated connection. They can be viewed in real-time in the [Activity](active_queries.md#web-ui) tab.

docs/features/sharding/cross-shard-queries/insert.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ VALUES
9494
RETURNING *;
9595
```
9696

97-
However, if you prefer to use sequences instead, you can rely on [database-generated](../schema_management/primary_keys.md) primary keys.
97+
However, if you prefer to use sequences instead, you can rely on [database-generated](../sequences.md) primary keys.
9898

99-
Statements that don't include the primary key in the `INSERT` tuple will be sent to one of the shards, using the same round robin algorithm used for [omnisharded](#omnisharded-tables) tables. The shard will then generate the primary key value using PgDog's [sharded sequences](../schema_management/primary_keys.md#pgdognext_id_seq).
99+
Statements that don't include the primary key in the `INSERT` tuple will be sent to one of the shards, using the same round robin algorithm used for [omnisharded](#omnisharded-tables) tables. The shard will then generate the primary key value using PgDog's [sharded sequences](../schema_management/functions.md#pgdognext_id_seq).
100100

101101
For example, assuming the table `users` is sharded on the primary key `id`, omitting it from the `INSERT` statement will send it to only one of the shards:
102102

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
nav:
2+
- 'manager.md'
3+
- 'functions.md'
4+
- 'index.md'
5+
- 'cache.md'
6+
- 'migrations.md'
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
icon: material/function-variant
3+
---
4+
5+
# Schema manager functions
6+
7+
The [schema manager](index.md) uses PL/pgSQL functions to generate shard-aware identifiers and perform other actions inside the database to make sharding work. These functions are documented below.
8+
9+
## Functions
10+
11+
### `pgdog.next_id_seq`
12+
13+
The `pgdog.next_id_seq` function generates a unique, shard-aware `BIGINT` number that can be used as a primary key. It accepts the following arguments:
14+
15+
| Argument | Data type | Description |
16+
|-|-|-|
17+
| `sequence_name` | `regclass` | The sequence used as basis for generating integers. |
18+
| `table_name` | `regclass` | The partitioned by hash table which is required for `satisfies_hash_partition` to extract the hash data type. |
19+
20+
If not specified, `table_name` will default to `'pgdog.validator_bigint'::regclass`, so this function can be used with any Postgres sequence. For [sharded sequences](../sequences.md), a special table is created in the `pgdog_internal` schema for each sharded sequence, to avoid lock contention on a single Postgres catalog entity.
21+
22+
#### Sequence cache
23+
24+
When looking for the next valid number, `next_id_seq` will consume several values from the sequence in a row. By default, each call to `nextval` requires a write to the WAL, which could be a bit slower than optimal. To mitigate this, we automatically increase the sequence's cache size to 100, which is usually enough to generate the next value entirely in memory.
25+
26+
### `pgdog.next_uuid_auto`
27+
28+
The `pgdog.next_uuid_auto` function generates a unique, shard-aware `UUID` value which can be used as a primary key. It accepts no arguments and uses `pgdog.validator_uuid` as basis for calling `satisfies_hash_partition`.
29+
30+
##### Example
31+
32+
=== "Function"
33+
```postgresql
34+
SELECT pgdog.next_uuid_auto();
35+
```
36+
=== "Output"
37+
```
38+
next_uuid_auto
39+
--------------------------------------
40+
f54c49c1-47f6-4ca1-a108-782286e447c3
41+
```
42+
43+
UUID generation is not a big problem for sharded databases, since clients can generate and provide UUIDs as part of a query. PgDog still supports generating shard-aware UUIDs in the database, so this function can be configured as a default instead of `gen_random_uuid()`, for example:
44+
45+
```postgresql
46+
CREATE TABLE measurements (
47+
id UUID PRIMARY KEY DEFAULT pgdog.next_uuid_auto(),
48+
value REAL NOT NULL,
49+
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
50+
);
51+
```
52+
53+
### `pgdog.install_sharded_sequence`
54+
55+
The `pgdog.install_sharded_sequence` function replaces the `DEFAULT` value for a table column with a call to [`pgdog.next_id_seq`](#pgdognext_id_seq). It accepts the following arguments:
56+
57+
| Argument | Data type | Description |
58+
|-|-|-|
59+
| `schema_name` | `text` | The name of the schema where the table resides. This is commonly `public` but can be any other schema. |
60+
| `table_name` | `text` | The name of the table that contains the primary key column. |
61+
| `column_name` | `text` | The name of the primary key column. |
62+
| `lock_timeout` | `text` | Maximum amount of time this function call will be allowed to block other queries from accessing this table while it mutates the schema definition. This is set to `1s` by default. |
63+
64+
Under the hood, this function will create two entities:
65+
66+
1. A regular Postgres sequence (using `CREATE SEQUENCE`)
67+
2. A copy of the table specified in `table_name` in the `pgdog_internal` schema and set that as the `table_name` argument for `pgdog.next_id_seq`
68+
69+
One table and sequence is created per column, so it's possible to install multiple sharded sequences into the same table. Creating separate tables for each sharded sequence prevents lock contention on Postgres catalog entities while generating values.

docs/features/sharding/schema_management/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ DDL statements to all shards concurrently, ensuring table and index definitions
2929
Primary keys are typically generated automatically by Postgres. We provide pl/PgSQL functions
3030
to make this work in sharded databases as well.
3131

32-
[**Primary keys**](primary_keys.md)
32+
[**Sharded sequences**](../sequences.md)

docs/features/sharding/schema_management/manager.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ The `--database` parameter expects the name of the sharded database in [`pgdog.t
1919
| Entity type | Name | Description |
2020
|-|-|-|
2121
| Schema | `pgdog` | The schema which contains functions and tables used for sharding and synchronization. |
22+
| Schema | `pgdog_internal` | The schema which contains copies of sharded tables and their [sharded sequences](../sequences.md). |
2223
| Table | `pgdog.config` | Table with configuration options specific to each shard, e.g., shard number, total number of shards, etc. |
23-
| Function | [`pgdog.next_id_seq`](primary_keys.md#pgdognext_id_seq) | Globally unique ID generation for `BIGINT` columns. |
24-
| Function | [`pgdog.next_uuid_auto`](primary_keys.md#uuids) | Globally unique UUID generation for `UUID` columns. |
24+
| Function | [`pgdog.next_id_seq`](functions.md#pgdognext_id_seq) | Globally unique, shard-aware, ID generation for `BIGINT` columns. Uses [sharded sequences](../sequences.md). |
25+
| Function | [`pgdog.next_uuid_auto`](functions.md#pgdognext_uuid_auto) | Globally unique, shard-aware UUID generation for `UUID` columns. |
26+
| Function | [`pgdog.install_sharded_sequence`](functions.md#pgdoginstall_sharded_sequence) | PL/pgSQL function used to setup a [sharded sequence](../sequences.md) for a primary key. |
2527

2628
!!! note
2729
PgDog gets all necessary information about shards from its configuration. Unless the configuration files are in `$PWD`, you should pass them as arguments, for example:

docs/features/sharding/schema_management/primary_keys.md

Lines changed: 0 additions & 92 deletions
This file was deleted.

docs/roadmap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Manage [table schema(s)](features/sharding/schema_management/index.md) and ensur
9999

100100
| Feature | Status | Notes |
101101
|-|-|-|
102-
| [Primary keys](features/sharding/schema_management/primary_keys.md) | :material-calendar-check: | `BIGINT` and `UUID` partially supported for hash-based sharding only. [#386](https://github.com/pgdogdev/pgdog/issues/386). Other data types require cross-shard unique index support. |
102+
| [Primary keys](features/sharding/sequences.md) | :material-calendar-check: | `BIGINT` and `UUID` supported for hash-based sharding only. [#386](https://github.com/pgdogdev/pgdog/issues/386). Other data types require cross-shard unique index support. |
103103
| Unique indexes | :material-calendar-check: | Enforce uniqueness constraints across an unsharded column(s). [#439](https://github.com/pgdogdev/pgdog/issues/439). |
104104
| `CHECK` constraints | :material-close: | They are generally arbitrary SQL checks and need to be executed prior to row updates. |
105105
| Schema validator | :material-calendar-check: | Check that all shards have identical tables, indexes, etc. |

0 commit comments

Comments
 (0)