Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The code block contains a sequence of standard SQL DDL and DML statements rather than PL/pgSQL procedural code. Using the sql language tag is more appropriate for syntax highlighting in this context.

Suggested change
```plpgsql
```sql

-- 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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.


{{< 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`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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`.
{{< note title="Note" >}}
Step 4 to lock the parent is optional if there are no reads or writes against the parent table while the partition is detached, and it requires enabling object locking (supported from YugabyteDB {{<release "2025.2">}} or later).
To enable the feature, set the YB-TServer flags `enable_object_locking_for_table_locks=true` and `ysql_yb_ddl_transaction_block_enabled=true`. Refer to Refer to [Enable table-level locks](../../../../../explore/transactions/explicit-locking/#enable-table-level-locks) for more details.
{{< /note >}}


{{< /note >}}

### Common errors and solutions

- `ERROR: duplicate key value violates unique constraint "uniqueerror_i_idx"`
Expand Down