-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[docs] Add instructions to recreate a partition index #31362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jaki
wants to merge
2
commits into
yugabyte:master
Choose a base branch
from
jaki:docs-recreate-partition-index
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -372,6 +372,53 @@ DROP INDEX uniqueerror_i_idx; | |||||||||||||||
| DROP INDEX | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| ### Recreate a partition index | ||||||||||||||||
|
|
||||||||||||||||
| Suppose you have a partitioned table where the parent has a unique constraint. | ||||||||||||||||
| To recreate indexes, you should avoid directly creating an index on the partitioned table (parent table) because that cannot use `CONCURRENTLY`. | ||||||||||||||||
| Follow these steps to recreate a partition index online: | ||||||||||||||||
|
|
||||||||||||||||
| ```plpgsql | ||||||||||||||||
| -- Set up example | ||||||||||||||||
| CREATE TABLE parent (i int UNIQUE) PARTITION BY RANGE (i); | ||||||||||||||||
| CREATE TABLE child0 PARTITION OF parent FOR VALUES FROM (0) TO (100000); | ||||||||||||||||
| CREATE TABLE child1 PARTITION OF parent FOR VALUES FROM (100000) TO (200000); | ||||||||||||||||
| CREATE TABLE child2 PARTITION OF parent FOR VALUES FROM (200000) TO (300000); | ||||||||||||||||
| INSERT INTO parent VALUES (generate_series(0, 299999)); | ||||||||||||||||
|
|
||||||||||||||||
| -- 1. Build standalone unique index (slow, but non-blocking) | ||||||||||||||||
| CREATE UNIQUE INDEX CONCURRENTLY child0_i_unique ON child0 (i); | ||||||||||||||||
| -- 2. Add CHECK constraint matching partition bounds (fast: no scan) | ||||||||||||||||
| ALTER TABLE child0 ADD CONSTRAINT child0_partition_check | ||||||||||||||||
| CHECK (i IS NOT NULL AND i >= 0 AND i < 100000) NOT VALID; | ||||||||||||||||
| -- 3. Validate the CHECK (slow, but non-blocking on parent) | ||||||||||||||||
| ALTER TABLE child0 VALIDATE CONSTRAINT child0_partition_check; | ||||||||||||||||
| -- 4. Lock the parent to prevent queries from missing the partition's data | ||||||||||||||||
| -- while it is detached. | ||||||||||||||||
| BEGIN; | ||||||||||||||||
| LOCK TABLE parent IN ACCESS EXCLUSIVE MODE; | ||||||||||||||||
| -- 5. Detach (fast) | ||||||||||||||||
| ALTER TABLE parent DETACH PARTITION child0; | ||||||||||||||||
| -- 6. Drop inherited constraint (fast: drops old backing index) | ||||||||||||||||
| ALTER TABLE child0 DROP CONSTRAINT child0_i_key; | ||||||||||||||||
| -- 7. Promote standalone index to a constraint (fast: no rebuild) | ||||||||||||||||
| ALTER TABLE child0 ADD CONSTRAINT child0_i_key UNIQUE USING INDEX child0_i_unique; | ||||||||||||||||
| -- 8. Reattach (fast: CHECK and UNIQUE already satisfy parent) | ||||||||||||||||
| ALTER TABLE parent ATTACH PARTITION child0 FOR VALUES FROM (0) TO (100000); | ||||||||||||||||
| COMMIT; | ||||||||||||||||
| -- 9. Drop temporary CHECK constraint | ||||||||||||||||
| ALTER TABLE child0 DROP CONSTRAINT child0_partition_check; | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| Repeat steps 1–9 for `child1`, `child2`, and any other partitions as needed. | ||||||||||||||||
| Step 4 to lock the parent is optional if there will be no reads or writes against the parent table while the partition is detached. | ||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| {{< note title="Note" >}} | ||||||||||||||||
|
|
||||||||||||||||
| Step 4 requires object locking support (available since 2025.2): set YB-TServer flags `enable_object_locking_for_table_locks=true` and `ysql_yb_ddl_transaction_block_enabled=true`. | ||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| {{< /note >}} | ||||||||||||||||
|
|
||||||||||||||||
| ### Common errors and solutions | ||||||||||||||||
|
|
||||||||||||||||
| - `ERROR: duplicate key value violates unique constraint "uniqueerror_i_idx"` | ||||||||||||||||
|
|
||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code block contains a sequence of standard SQL DDL and DML statements rather than PL/pgSQL procedural code. Using the
sqllanguage tag is more appropriate for syntax highlighting in this context.