Skip site navigation (1) Skip section navigation (2)

modifying views

From: "Robert Haas" <robertmhaas(at)gmail(dot)com>
To: "PG Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: modifying views
Date: 2008-07-30 02:12:22
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-hackers
I've been thinking a little more about modifying views:

AFAICS, out of all of the things that ALTER TABLE is capable of doing,
there are only four that make any sense in the context of views:

(1) add a column
(2) drop a column
(3) change the type of an existing column
(4) renaming a column

The tricky part of course is that the underlying query has to change
along with the type definition.  It's fairly obvious what the
semantics of dropping a column should be, but the other cases are
trickier.  New columns could possibly be returned as NULL (pending a
subsequent CREATE OR REPLACE VIEW), but changing the type of a column
just seems crazy as a standalone operation.  The bigger problem though
is that I'm pretty sure that ALTER TABLE <viewname> ADD COLUMN <name>
<type> is not a command that anyone is eager to be typing.  What we
really want to be able to do is a CREATE OR REPLACE VIEW command and
have the system worry about what columns needed to be added, dropped,
or retyped, and giving us an error if we've tried to (for example)
remove a column that some other view depends on.  (Apart from CREATE
OR REPLACE VIEW, the only operation that looks independently useful to

Unfortunately, it looks to me like a fully general implementation of
this feature would be Really Hard, because a CREATE OR REPLACE VIEW
command, beyond attempting to add, drop, or retype columns, could also
attempt to reorder them.  A cursory inspection of the situation
suggests this would require modifying the attnum values in
pg_attribute, which looks like a nightmare.  (Anyone want to argue
otherwise?  Tom?  I'd love to be wrong about this...)  However, it
might be possible to allow some or all of the following: (1) dropping
columns from anywhere, (2) adding new columns at the end, and (3)
changing the type of existing columns.  (I think (1) and (2) are
clearly more useful than (3).)




pgsql-hackers by date

Next:From: David FetterDate: 2008-07-30 04:21:39
Subject: Re: WITH RECUSIVE patches 0723
Previous:From: David E. WheelerDate: 2008-07-29 23:53:23
Subject: Re: Type Categories for User-Defined Types

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group