Re: Using a VIEW as a temporary mechanism for renaming a table

From: Berend Tober <btober(at)computer(dot)org>
To: Ben Buckman <ben(at)shyp(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Using a VIEW as a temporary mechanism for renaming a table
Date: 2016-06-08 22:55:14
Message-ID: 5758A252.3060804@computer.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ben Buckman wrote:
> Hello,
> I would like to rename a table with ~35k rows (on pgsql 9.4), let's say
> from `oldthings` to `newthings`.
> Our application is actively reading from and writing to this table, and
> the code will break if the table name suddenly changes at runtime. So I
> can't simply run an `ALTER TABLE oldthings RENAME TO newthings`, unless
> we take downtime, which we'd prefer not to do. (I'd also prefer to avoid
> a data migration from one table to another, which would require
> dual-writes or some other way to handle data written during the transition.)
>
> It seems that a reasonable approach to do this without downtime, would
> be to use a temporary VIEW. We can `CREATE VIEW newthings AS SELECT *
> FROM oldthings;`. Views in pg9.4 that are backed by a single table
> support writes. So my plan is like this:
>
> 1. Create the view, essentially as an alias to the table.
> 2. In the code, change all references from the old name to the new name.
> The code would "think" it's using a renamed table, but would really be
> using a view.
> (At this point, I expect that all basic CRUD operations on the view
> should behave as if they were on the table, and that the added
> performance impact would be negligible.)
> 3. In a transaction, drop the view and rename the table, so `newthings`
> is now the original table and `oldthings` no longer exists. (In my
> testing, this operation took <10ms.)
> (When this is done, the view will have only existed and been used by
> the application for a few minutes.)
>
> What are people's thoughts on this approach? Is there a flaw or
> potential danger that I should be aware of? Is there a simpler approach
> I should consider instead?

I would totally do it this way ... and after creating the view, I'd
probably leave it as the normal interface. In fact, I've adopted a
practice of utilizing views as the user interface generally and not
exposing the actual tables at all.

As you may realize, but I'll point out for completeness, that for more
complicated situations (i.e, when the view is not just representing a
single table as your current case), if the view represents a multi-table
join, you can use triggers to intercept DML on the view and implement
logic to interact with the multiple underlying tables for inserts and
updates.

Additionally, if you don't want to modify the application, consider
creating the view, using the same original table name but in a separate
schema and setting the search_path so the the view is found before the
table. Then you can rename the table, simultaneously redefining the view
to point the the new table.

-- B

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Patrick B 2016-06-08 23:24:57 Automate copy - Postgres 9.2
Previous Message Adrian Klaver 2016-06-08 21:38:51 Re: Re-sync slave server