Repository navigation
Schema change: add_primary_key - #8
Merged
Merged
Conversation
For a table with no primary key: the columns are made NOT NULL first through the validated CHECK, a unique index is built without blocking writes, and it is attached with ADD CONSTRAINT ... PRIMARY KEY USING INDEX, which then only changes the catalog. A table the plan creates gets a plain ADD PRIMARY KEY. Refused: a table that already has a key, a partitioned table, unknown or repeated columns, a name that is taken. Later steps in the same plan, such as a foreign key to the new key, see it.
…set up their own tables The scenario runs the whole runbook (NOT NULL path, unique index, USING INDEX), checks the key, the index, that a duplicate is refused and that the helper CHECK is gone; the lock levels and the predicted end state are checked like every other scenario. A scenario can now carry SQL that runs before it is compiled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
A new plan operation,
add_primary_key(table, optional constraint name, columns), for a table that has no primary key (the health report already lists these).ADD PRIMARY KEYdoes not have to read the table under its lock).CREATE UNIQUE INDEX CONCURRENTLY, then attached withADD CONSTRAINT … PRIMARY KEY USING INDEX, which only changes the catalog. On a table the plan itself creates it is a plainADD PRIMARY KEY.How it was checked
npm run check:openai).scripts/schema-change-pg.ts(scenarios can now carry their own setup SQL) runs the whole runbook on a table without a key: the key exists on a NOT NULL column, its index is the primary key's, a duplicate is refused and the helper CHECK is gone. Like every scenario it also has the lock level of each step compared withpg_locksand the predicted end state compared with the catalog.npm run test:schema-pg:versionspasses on PostgreSQL 12, 13, 14, 15, 16, 17 and 18.Reviewer notes
drop_constraintrefuses it), a key on a partitioned table, enums, identity on an existing column, a safe column rename.