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
{{ message }}
Repository navigation
Commit 7e56adf
Browse filesBrowse the repository at this point in the historyBrowse files
fix: attach the window a live writer is filling, under one lock (#65)
* fix: attach the window a live writer is filling, under one lock
DEFAULT reconciliation moved the window's rows out in one transaction and
retried the ATTACH in another. On a table that is being written to the writer
refills the window in the gap, PostgreSQL refuses the attach again, and after
DEFAULT_CONFLICT_MAX_RETRIES the rows are moved back -- so the current month,
the one every live table is writing into, could never be attached, and each
attempt cost a full scan of DEFAULT under ACCESS EXCLUSIVE.
The move and the attach now share a transaction: reconcile_and_attach takes
EXCLUSIVE on the parent and ACCESS EXCLUSIVE on the partition and the DEFAULT
sibling, moves what is left of the window, and attaches. Nothing can reach
DEFAULT in between, and no insert is mid-routing when the partition set
changes -- a writer waits at the parent and is then routed into the new
partition, statement unchanged. The bulk of the window still moves first under
the lighter SHARE ROW EXCLUSIVE, so the exclusive window is a tail and a scan.
A window that still cannot be attached is now a default_holds_rows issue
rather than the driver's exception: apply() records it and the run goes on,
and partition_data returns a move issue with complete=False instead of raising
out of the drain.
Closes#64
* test: cover the locked move-and-attach and the branches around it
Unit tests for reconcile_and_attach's lock order and statement order in
both mirrors, for a tail move an incoming foreign key refuses inside it,
and for an attach target swapped during the bulk reconcile; the docs the
new lock level changes follow.
* docs: the legacy table still owns the serial sequence when the drain ends
Following the guide's last step verbatim on a BIGSERIAL table fails: the
sequence is owned by the column it was declared on, so it belongs to the
renamed legacy table while every partition's default draws from it, and
DROP TABLE is refused. Measured on 17, with what CASCADE does instead.
* docs: what a queued insert does when the partition set changes under it
The measurement behind the move-and-attach's EXCLUSIVE lock on the parent,
where the semantics page keeps the rest of the ATTACH lock findings.
* docs: say which lock the move-and-attach takes, not that ATTACH takes it
* test: let the attach race a writer that is already writing
The block was entered on a task that may not have had the loop yet, so on
a slow runner the attach could finish before the first insert went out.
---------
Co-authored-by: Alexey Shalaev <shalaevad.alexey@gmail.com>
Copy file name to clipboardExpand all lines: docs/concepts/execution.md
+39-8Lines changed: 39 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,12 @@ statements. There is no long transaction wrapping a run, because `DETACH … CON
12
12
cannot run inside a transaction block at all, and because a run that creates three
13
13
partitions and fails on the fourth should keep the three.
14
14
15
+
Two steps are exceptions, and both for the same reason: a lock has to span more than one
16
+
statement or the thing it protects can change underneath. A drop takes its lock, revalidates,
17
+
drains and drops in one transaction; the last step of [DEFAULT reconciliation](#default-reconciliation)
18
+
moves the window's remaining rows and attaches in one transaction. `ATTACH`, unlike
19
+
`DETACH … CONCURRENTLY`, may run inside a transaction block.
20
+
15
21
The consequence for callers: pass the service an **engine**, never a session you are
16
22
using elsewhere.
17
23
@@ -50,13 +56,36 @@ belong to the new window, PostgreSQL refuses the attach (`23514`). The executor:
50
56
51
57
1. moves those rows from DEFAULT into the new partition, naming columns on both sides
52
58
(`ATTACH` matches by name, so physical column order may differ) and leaving rows with a
53
-
NULL trailing key where PostgreSQL routes them;
54
-
2. retries the attach.
55
-
56
-
If the attach still fails, the rows are returned to DEFAULT rather than left in a table
57
-
no query can see. For a nested branch the moved rows are routed onward into its leaves.
58
-
A DEFAULT sibling holding rows for a hash or list member is reported
59
-
(`default_holds_rows`) rather than moved: only a RANGE window can be selected by its key.
59
+
NULL trailing key where PostgreSQL routes them. This is one statement under
60
+
`SHARE ROW EXCLUSIVE` on DEFAULT: writers wait, readers do not;
61
+
2. moves whatever arrived in the meantime and attaches, **in one transaction**, under
62
+
`EXCLUSIVE` on the parent and `ACCESS EXCLUSIVE` on the partition and on DEFAULT — taken
63
+
in that order, and taken before the move.
64
+
65
+
Step 2 is what makes the window a live writer is inserting into attachable at all. Two
66
+
transactions cannot do it: the move commits, DEFAULT is free again, and the next insert for
67
+
the window lands in it before `ATTACH` scans — under any steady write rate that never
68
+
converges, and every attempt is a full scan of DEFAULT under `ACCESS EXCLUSIVE`. Sharing one
69
+
lock closes the gap. Step 1 is what keeps that exclusive window short: the bulk of the month
70
+
moves before the heavy lock is taken, so what step 2 covers is a tail and a scan.
71
+
72
+
The parent's lock is one level above what `ATTACH` itself takes, and it is there for the
73
+
writer. An INSERT picks its partition from the set it saw when it took `ROW EXCLUSIVE` on
74
+
the parent: one that got that far and then queued on DEFAULT's lock would come out of the
75
+
wait still aimed at DEFAULT and be rejected by the constraint the attach just narrowed — a
76
+
lost write, and what a plain `ATTACH` does to a live writer. Blocking at the parent instead
77
+
means no insert is ever mid-routing while the partition set changes: the writer waits,
78
+
re-plans, and lands in the new partition, its statement unchanged. `EXCLUSIVE` does not
79
+
conflict with `ACCESS SHARE`, so readers of the other partitions carry on.
80
+
81
+
If the attach still fails, the rows step 1 moved are returned to DEFAULT rather than left in
82
+
a table no query can see — step 2's own move rolls back with the attach, and needs no
83
+
compensation. A window that still cannot be attached is reported as `default_holds_rows`, an
84
+
issue like any other: the run goes on, and `partition_data` returns it rather than raising.
85
+
For a nested branch the moved rows are routed onward into its leaves. A DEFAULT sibling
86
+
holding rows for a hash or list member is reported rather than moved: only a RANGE window can
87
+
be selected by its key. A **foreign** DEFAULT partition cannot be locked at all, so there the
88
+
race stays and the report is all there is.
60
89
61
90
## Detach
62
91
@@ -103,6 +132,8 @@ repository).
103
132
|---|---|
104
133
|`CREATE TABLE … (LIKE parent)`|`ACCESS SHARE` on the parent |
105
134
|`ATTACH PARTITION`|`SHARE UPDATE EXCLUSIVE` on the parent, `ACCESS EXCLUSIVE` on the child and on a DEFAULT sibling; `SHARE ROW EXCLUSIVE` on tables referencing the parent through a foreign key |
135
+
| the reconciling row move ([DEFAULT reconciliation](#default-reconciliation), step 1) |`SHARE ROW EXCLUSIVE` on the DEFAULT partition and on the child: writers of DEFAULT wait, readers do not |
136
+
| the move-and-attach ([DEFAULT reconciliation](#default-reconciliation), step 2) |`EXCLUSIVE` on the parent (one level above `ATTACH`'s own, so no writer is mid-routing), then `ATTACH`'s `ACCESS EXCLUSIVE` on the partition and the DEFAULT sibling — all taken before the move |
106
137
|`DETACH PARTITION` (plain) |`ACCESS EXCLUSIVE` on parent, partition, and every table referencing the parent |
107
138
|`DETACH PARTITION … CONCURRENTLY`|`SHARE UPDATE EXCLUSIVE` on the parent; `ACCESS EXCLUSIVE` on the partition and, in its second transaction, on referencing tables |
108
139
|`DROP TABLE` of a detached table |`ACCESS EXCLUSIVE` on that table only |
@@ -117,7 +148,7 @@ it takes on `op.capabilities`.
117
148
118
149
| What happened | Effect on the run |
119
150
|---|---|
120
-
| a topology conflict at execution time — a DEFAULT sibling holding rows for a hash bucket, a name taken by a relation with other bounds, a detach PostgreSQL refuses because rows are still referenced | recorded in `result.issues`; the run goes on |
151
+
| a topology conflict at execution time — a DEFAULT partition holding rows the attach could not take, a name taken by a relation with other bounds, a detach PostgreSQL refuses because rows are still referenced | recorded in `result.issues`; the run goes on |
121
152
| a `PlanStaleError` — the relation is not the one the plan saw | recorded as an issue with `continue_on_error`, otherwise raised |
122
153
| any other error — a connection drop, a permission denied, a `before_*` hook raising | aborts the run, unless `continue_on_error`, in which case it is recorded and the next operation runs |
0 commit comments