Skip to content

Commit b773f6f

Browse files
authored
feat: explain the configuration for copy_data retries (#66)
1 parent 36ed373 commit b773f6f

8 files changed

Lines changed: 65 additions & 11 deletions

File tree

docs/features/sharding/resharding/.pages

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ nav:
33
- 'databases.md'
44
- 'schema.md'
55
- 'replica-identity.md'
6-
- 'hash.md'
6+
- 'move.md'
77
- 'cutover.md'

docs/features/sharding/resharding/cutover.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ icon: material/set-right
88
This is a new and experimental feature. Please make sure to test it before deploying to production
99
and report any issues you find.
1010

11-
Traffic cutover involves moving application traffic (read and write queries) to the destination database. This happens when the source and destination databases are in sync: all source data is copied, and resharded with [logical replication](hash.md).
11+
Traffic cutover involves moving application traffic (read and write queries) to the destination database. This happens when the source and destination databases are in sync: all source data is copied, and resharded with [logical replication](move.md).
1212

1313
## Performing the cutover
1414

docs/features/sharding/resharding/databases.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PgDog's strategy for resharding Postgres databases is to create a new, independe
88

99
## Requirements
1010

11-
New databases should be **empty**: don't migrate your [table definitions](schema.md) or [data](hash.md). These will be taken care of automatically by PgDog.
11+
New databases should be **empty**: don't migrate your [table definitions](schema.md) or [data](move.md). These will be taken care of automatically by PgDog.
1212

1313
### Database users
1414

docs/features/sharding/resharding/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The resharding process is composed of four independent operations. The first one
2424
|-|-|
2525
| [Create new cluster](databases.md) | Create a new set of empty databases that will be used for storing data in the new, sharded cluster. |
2626
| [Schema synchronization](schema.md) | Replicate table and index definitions to the new shards, making sure the new cluster has the same schema as the old one. |
27-
| [Move & reshard data](hash.md) | Copy data using logical replication, while redistributing rows in-flight between new shards. |
27+
| [Move & reshard data](move.md) | Copy data using logical replication, while redistributing rows in-flight between new shards. |
2828
| [Cutover traffic](cutover.md) | Make the new cluster service both reads and writes from the application, without taking downtime. |
2929

3030
While each step can be executed separately by the operator, PgDog provides an [admin database](../../../administration/index.md) command to perform online resharding and traffic cutover steps in a completely automated fashion:
@@ -50,5 +50,5 @@ The `<source>` and `<destination>` parameters accept the name of the source and
5050

5151
{{ next_steps_links([
5252
("Schema sync", "schema.md", "Synchronize table, index and other schema entities between the source and destination databases."),
53-
("Move data", "hash.md", "Redistribute data between shards using the configured sharding function. This happens without downtime and keeps the shards up-to-date with the source database until traffic cutover."),
53+
("Move data", "move.md", "Redistribute data between shards using the configured sharding function. This happens without downtime and keeps the shards up-to-date with the source database until traffic cutover."),
5454
]) }}
Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ PgDog will distribute the table copy load evenly between all replicas in the con
121121
To prevent the resharding process from impacting production queries, you can create a separate set of replicas just for resharding.
122122

123123
Managed clouds (e.g., AWS RDS) make this especially easy, but require a warm-up period to fetch all the data from the backup snapshot, before they can read data at full speed of their storage volumes.
124-
124+
125125
To make sure dedicated replicas are not used for read queries in production, you can configure PgDog to use them for resharding only:
126-
126+
127127
```toml
128128
[[databases]]
129129
name = "prod"
@@ -215,6 +215,59 @@ FROM pg_replication_slots;
215215
```
216216
The replication delay between the two database clusters is measured in bytes. When that number reaches zero, the two databases are byte-for-byte identical, and traffic can be [cut over](cutover.md) to the destination database.
217217

218+
## Troubleshooting
219+
220+
### Shard failure during copy
221+
222+
If a shard goes down mid-copy, PgDog retries that table with exponential backoff. By default: up to **5 attempts**, starting at **1 second** and doubling each time.
223+
224+
| Situation | Behavior |
225+
|-|-|
226+
| Destination shard down | New connection opened on the next attempt. |
227+
| Source shard down | `TEMPORARY` replication slot re-created from scratch. It's cleaned up automatically when the connection closes before starting new attempt. |
228+
229+
To change the defaults, configure [`resharding_copy_retry_max_attempts`](../../../configuration/pgdog.toml/general.md#resharding_copy_retry_max_attempts) and [`resharding_copy_retry_min_delay`](../../../configuration/pgdog.toml/general.md#resharding_copy_retry_min_delay) in `pgdog.toml`:
230+
231+
```toml
232+
[general]
233+
resharding_copy_retry_max_attempts = 5 # per-table retry attempts
234+
resharding_copy_retry_min_delay = 1000 # base backoff in ms; doubles each attempt, max 32×
235+
```
236+
237+
### Primary key violations when retrying after copy failure
238+
239+
In rare cases, if the connection drops after `COPY` commits but before PgDog records the table as done, some rows may remain in the destination. The next retry will hit primary key violations.
240+
241+
242+
PgDog catches this and tells you exactly what to run:
243+
244+
```
245+
data sync for "public"."orders" failed with rows remaining in destination;
246+
truncate manually before retrying: TRUNCATE "public"."orders_new";
247+
```
248+
249+
Run the `TRUNCATE` on the destination (you can copy the command above, but make sure to run it strictly on the destination), then re-run `COPY_DATA`.
250+
251+
### Binary format mismatch
252+
253+
Different major Postgres versions can produce incompatible binary `COPY` data. PgDog surfaces this as a `BinaryFormatMismatch` error. Switch to text:
254+
255+
```toml
256+
[general]
257+
resharding_copy_format = "text"
258+
```
259+
260+
See [Integer primary keys](#integer-primary-keys) for the other common reason to use text format.
261+
262+
### Replication slot not cleaned up after stop or crash
263+
264+
If `COPY_DATA` is stopped via `STOP_TASK` or PgDog exits unexpectedly, the **permanent** replication slot will remain on the source. Postgres will not recycle WAL until it is dropped. Clean it up manually:
265+
266+
```postgresql
267+
SELECT pg_drop_replication_slot('slot_name');
268+
```
269+
270+
The slot name appears in the `COPY_DATA` startup log and in `SHOW REPLICATION_SLOTS`.
218271
## Next steps
219272

220273
{{ next_steps_links([

docs/features/sharding/resharding/schema.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The schema synchronization process is composed of 4 distinct steps, all of which
1212
| Phase | Description |
1313
|-|-|
1414
| [Pre-data](#pre-data-phase) | Create identical tables on all shards along with the primary key constraint and index (when present). Secondary indexes are _not_ created yet. |
15-
| [Post-data](#post-data-phase) | Create secondary indexes on all tables and shards. This is done after [moving data](hash.md), as a separate step, because it's considerably faster to create indexes on whole tables than while inserting individual rows. |
15+
| [Post-data](#post-data-phase) | Create secondary indexes on all tables and shards. This is done after [moving data](move.md), as a separate step, because it's considerably faster to create indexes on whole tables than while inserting individual rows. |
1616
| [Cutover](#cutover) | This step is executed during traffic cutover, while application queries are blocked from executing on the database. |
1717
| Post-cutover | This step makes sure the rollback database cluster can handle reverse logical replication. |
1818

@@ -110,7 +110,7 @@ This will make sure all tables and schemas in your database are copied and resha
110110

111111
## Post-data phase
112112

113-
The post-data phase is performed after the [data copy](hash.md) is complete and tables have been synchronized with logical replication. Its job is to create all secondary indexes (e.g., `CREATE INDEX`).
113+
The post-data phase is performed after the [data copy](move.md) is complete and tables have been synchronized with logical replication. Its job is to create all secondary indexes (e.g., `CREATE INDEX`).
114114

115115
This step is performed after copying data because it makes the copy process considerably faster: Postgres doesn't need to update several indexes while writing rows into the tables.
116116

@@ -189,5 +189,5 @@ pg_dump_path = "/path/to/pg_dump"
189189
## Next steps
190190

191191
{{ next_steps_links([
192-
("Move data", "hash.md", "Redistribute data between shards using the configured sharding function. This happens without downtime and keeps the shards up-to-date with the source database until traffic cutover."),
192+
("Move data", "move.md", "Redistribute data between shards using the configured sharding function. This happens without downtime and keeps the shards up-to-date with the source database until traffic cutover."),
193193
]) }}

docs/roadmap.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Support for sorting rows in [cross-shard](features/sharding/cross-shard-queries/
8989

9090
| Feature | Status | Notes |
9191
|-|-|-|
92-
| [Data sync](features/sharding/resharding/hash.md) | :material-wrench: | Sync table data with logical replication. |
92+
| [Data sync](features/sharding/resharding/move.md) | :material-wrench: | Sync table data with logical replication. |
9393
| [Schema sync](features/sharding/resharding/schema.md) | :material-wrench: | Sync table, index and constraint definitions. |
9494
| Online rebalancing | :material-calendar-check: | Not automated yet, requires manual orchestration. |
9595

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ plugins:
9494
'features/sharding/migrations.md': 'features/sharding/schema_management/migrations.md'
9595
'features/sharding/primary-keys.md': 'features/sharding/sequences.md'
9696
'features/sharding/schema_management/primary_keys.md': 'features/sharding/sequences.md'
97+
'features/sharding/resharding/hash.md': 'features/sharding/resharding/move.md'
9798
'features/sharding/cross-shard/index.md': 'features/sharding/cross-shard-queries/index.md'
9899

99100
extra:

0 commit comments

Comments
 (0)