Repository navigation
Conversation
…ence Adopting the table's foreign key at the ATTACH drops that key's triggers on the referenced table under ACCESS EXCLUSIVE, so a long reader there queued the ATTACH while it held the table and the DEFAULT partition, and blocked every new reader of the referenced table until the lock budget ran out.
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
🤖 Local review of
GitHub reports passing CI on PostgreSQL 14–18. I reviewed the diff and PostgreSQL source without running the repository tests locally. Builderbot result retrieval was unavailable (DNS lookup failed); this is a local review, not a confirmed Builderbot result. |
|
🤖 Review of 9e50bc3: no blocking findings, 4 non-blocking The change does what the description says. BlockingNone. Non-blocking1. The doc comment on 2. No test shows the server really conflicts with a reader on the referenced table. Test case: a reader on the referenced table makes the ATTACH hit its lock budget (not run; written against the fixtures in this file)// A reader holds accounts and the step does not wait for it. The ATTACH
// adopts the table's foreign key by dropping that key's triggers on accounts,
// which needs ACCESS EXCLUSIVE there, so it ends on its lock budget and the
// table stays standalone.
func TestAttachPartitionOnlineAReaderOfTheReferencedTableBlocksTheAttach(t *testing.T) {
withAttachProofServer(t, func(t *testing.T, newFixture func(t *testing.T) attachProofFixture) {
f := newFixture(t)
f.exec(t, eventsWithAccountsParent)
f.exec(t, events2027AccountsFK)
target := f.attachTarget(t)
release, _ := f.holdLock(t, "accounts", "ACCESS SHARE")
defer release()
_, err := attachPartitionOnline(t.Context(), f.pool, target,
Budget{LockTimeout: 200 * time.Millisecond, StatementTimeout: 5 * time.Second}, 0, nil, nil)
var budgetErr *BudgetError
require.ErrorAs(t, err, &budgetErr)
assert.Empty(t, f.partitionBound(t, "events_2027"))
})
}3. The partitioned referenced table, the only place Test case: a reader on a partition of the referenced table is waited out (not run; needs attachWaitsOutAHolderOn to take a hold table and a reported table)// A reader holds a partition of accounts, which the parent's foreign key
// references. The ATTACH drops the foreign key's triggers on every partition
// of accounts under ACCESS EXCLUSIVE, so the step waits that reader out and
// progress names accounts.
func TestAttachPartitionOnlineWaitsOutAReaderOnAPartitionOfTheReferencedTable(t *testing.T) {
withAttachProofServer(t, func(t *testing.T, newFixture func(t *testing.T) attachProofFixture) {
f := newFixture(t)
f.exec(t, `
CREATE TABLE %[1]s.accounts (
id bigint PRIMARY KEY
) PARTITION BY RANGE (id);
CREATE TABLE %[1]s.accounts_low PARTITION OF %[1]s.accounts FOR VALUES FROM (0) TO (100);
INSERT INTO %[1]s.accounts VALUES (1), (2), (3);
CREATE TABLE %[1]s.events (
id bigint NOT NULL,
day date NOT NULL,
account_id bigint REFERENCES %[1]s.accounts (id)
) PARTITION BY RANGE (day);
CREATE TABLE %[1]s.events_default PARTITION OF %[1]s.events DEFAULT;
CREATE TABLE %[1]s.events_2027 (
account_id bigint,
id bigint NOT NULL,
day date NOT NULL
);
INSERT INTO %[1]s.events_2027 VALUES (2, 2, '2027-03-01')`)
f.exec(t, events2027AccountsFK)
f.attachWaitsOutAHolderOnPartition(t, f.attachTarget(t), "accounts_low", "accounts", "ACCESS SHARE")
})
}4. A NOT ENFORCED foreign key on the parent is waited on for readers although the ATTACH adopts nothing for it. This review was generated by Claude Code (claude-sonnet-5-5). |
|
🤖 What I verified on 9e50bc3 The lock claim. Adopting a table's existing foreign key makes the server drop that key's own action triggers on the referenced table, and dropping a trigger opens its table Cases checked in the diff and the code around it
Why it is complete. Every table the This review was generated by Claude Code (claude-sonnet-5-5). |
aparajon
left a comment
There was a problem hiding this comment.
🤖 Stamping with comments: 0 blocking, 4 non-blocking (see the review comments above).
This review was generated by Claude Code (claude-sonnet-5-5).
The online attach now waits for long readers, not only writers, of every table the parent's foreign keys reference before it runs the
ATTACH.Why
When pg-sprite attaches a partition online, it prepares the new table first and then runs
ATTACH PARTITIONin one brief transaction. Before that transaction asks for its locks, it waits, outside PostgreSQL's lock queue, for anyone already holding a lock that would conflict with them. For a table the parent's foreign keys reference, such ascustomersfororders.customer_id, it waited only for writers, on the belief that theATTACHtakes a lock there that readers can share. In fact, when theATTACHreuses the foreign key the run already built on the new table, which the online attach always does, it takes an exclusive lock oncustomers. A long report query oncustomerstherefore made theATTACHqueue while it already held the new table and theDEFAULTpartition. In that queue it blocked every new reader ofcustomersuntil the lock budget ran out.What
ATTACHtakes at mostSHARE ROW EXCLUSIVEon them.ACCESS EXCLUSIVEon the referenced table. When the referenced table is partitioned, it is taken on every partition, and on the root too from 15. Creating the foreign key instead takes onlySHARE ROW EXCLUSIVE.ACCESS SHAREon the referenced table. On PostgreSQL 14, 16, and 18 it checks that progress reports the wait for that reader and that the attach finishes once the reader ends. It fails if the referenced table goes back to the writer-only wait. The two existing waiting tests now share a helper that takes the lock mode.Before / after
Take
orders, withFOREIGN KEY (customer_id) REFERENCES customers, aDEFAULTpartition, andorders_2027already prepared by the run. A dashboard query has been readingcustomersfor two minutes.A reader that outlasts the holder wait is treated as a long writer is today: the
ATTACHthen asks for its locks under the lock budget.