Skip to main content

Preview PostgreSQL Table Renames Before Running ALTER TABLE

Updated 3 min readMarkdown
Part of seriesSQL Table Filtering & Schema Inspection
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.

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

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 · PostgreSQL pg_class · SqlOtter rename guide

About the author

  • SqlOtter , The world's most powerful database management tool

No comments yet. Be the first to comment.