# Preview PostgreSQL Table Renames Before Running ALTER TABLE

When a batch rename replaces text in table names, preview each old and new name before executing DDL. Two source names can map to the same destination. A destination may also belong to a relation you did not select. This read-only PostgreSQL check surfaces both cases.

## Build the rename list with SELECT

This example targets the `staging` schema and replaces every literal occurrence of `test` with `copy`. Start with the names you intend to rename, using the exact case shown in the object tree.

```sql
WITH selected(old_name) AS (
    VALUES ('test_orders'), ('test_payments'), ('test_test_labels')
), proposed AS (
    SELECT old_name, replace(old_name, 'test', 'copy') AS new_name
    FROM selected
), counted AS (
    SELECT *, count(*) OVER (PARTITION BY new_name) AS target_count
    FROM proposed
)
SELECT old_name, new_name,
       CASE
           WHEN old_name = new_name THEN 'unchanged'
           WHEN new_name = '' THEN 'empty name'
           WHEN octet_length(new_name) > current_setting('max_identifier_length')::int
               THEN 'name too long'
           WHEN target_count > 1 THEN 'duplicate target'
           WHEN EXISTS (
               SELECT 1
               FROM pg_catalog.pg_class AS c
               JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
               WHERE n.nspname = 'staging' AND c.relname = counted.new_name
           ) THEN 'target already exists'
           ELSE 'check source and dependencies'
       END AS review
FROM counted
ORDER BY old_name;
```

In this example, `test_test_labels` maps to `copy_copy_labels`. If `staging.copy_orders` already exists, the `test_orders` row is flagged. The query treats any occupied target as a collision. If your plan depends on freeing a name first, review the operation order separately. Check that every source exists, then look for application queries and scheduled jobs that refer to the old names. The catalog check is a point-in-time observation.

## Check the same mapping visually in SqlOtter

I work on SqlOtter. In the object tree, select the intended tables in the same database and schema, open **Rename Table**, then choose **Find and replace**. Enter `test` and `copy`, and compare **Original name** and **New name** for every row. The screen highlights the matching text in yellow and the replacement in green. Use the mapping to review the scope before clicking **OK**.

![Official SqlOtter screenshot of the batch rename preview](https://www.sqlotter.com/assets/product-screenshots/table-structure/batch-rename-tables.png)

The screenshot uses sample names and shows the preview before submitting. SqlOtter runs the selected renames in sequence. If a rename fails partway through, refresh the same schema and inspect which names changed before retrying. The SQL above only previews candidate names. It issues no ALTER TABLE statements.

[PostgreSQL identifier rules](https://www.postgresql.org/docs/current/sql-syntax-lexical.html) · [PostgreSQL pg_class](https://www.postgresql.org/docs/current/catalog-pg-class.html) · [SqlOtter rename guide](https://www.sqlotter.com/#/docs/table-structure/rename-tables)
