Re: Allow CREATE OR REPLACE VIEW to rename the columns

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Allow CREATE OR REPLACE VIEW to rename the columns
Date: 2019-10-31 04:42:49
Message-ID: 18080.1572496969@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Fujii Masao <masao(dot)fujii(at)gmail(dot)com> writes:
> Currently CREATE OR REPLACE VIEW command fails if the column names
> are changed.

That is, I believe, intentional. It's an effective aid to catching
mistakes in view redefinitions, such as misaligning the new set of
columns relative to the old. That's particularly important given
that we allow you to add columns during CREATE OR REPLACE VIEW.
Consider the oversimplified case where you start with

CREATE VIEW v AS SELECT 1 AS x, 2 AS y;

and you want to add a column z, and you get sloppy and write

CREATE OR REPLACE VIEW v AS SELECT 1 AS x, 3 AS z, 2 AS y;

If we did not throw an error on this, references that formerly
pointed to column y would now point to z (as that's still attnum 2),
which is highly unlikely to be what you wanted.

The right way to handle a column rename in a view is to do a separate
ALTER VIEW RENAME COLUMN, making it totally clear what you intend to
happen. (Right now, we make you spell that "ALTER TABLE RENAME COLUMN",
but it'd be reasonable to add that syntax to ALTER VIEW too.) I don't
think this functionality should be folded into redefinition of the content
of the view. It'd add more opportunity for mistakes than anything else.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2019-10-31 04:45:07 Re: [BUG] Partition creation fails after dropping a column and adding a partial index
Previous Message Tatsuo Ishii 2019-10-31 04:41:02 Re: Allow cluster_name in log_line_prefix