Renaming

ALTER TABLE ... RENAME TO

Lock mode ACCESS EXCLUSIVE
Risk HIGH
Blocks reads, writes, other DDL
pgfence rule rename-table
ALTER TABLE orders RENAME TO orders_v2;

What this does

Metadata-only and near-instant, but the risk is not the lock duration, it is that every query, view, and foreign key that references the old name breaks the moment the rename commits, with no transition period.

Safe rewrite

Rename the table, then create a view with the old name for backwards compatibility during the transition

-- 1. Rename the table:
ALTER TABLE orders RENAME TO orders_v2;
-- 2. Create a view with the old name for backwards compatibility:
CREATE VIEW orders AS SELECT * FROM orders_v2;
-- This simple view is auto-updatable: SELECT, INSERT, UPDATE, DELETE all work through it.
-- 3. Migrate all application references to the new name.
-- 4. Drop the view after all references are updated.

Read more

More in Renaming