← Back to PostgreSQL, page 2

postgresql-0032 · Easy fresher

Why can CREATE OR REPLACE VIEW fail when a column is removed, and how is a view changed safely?

CREATE OR REPLACE VIEW can add new columns at the end but cannot remove, rename or retype existing ones. Changing those requires DROP VIEW and CREATE VIEW, and objects that depend on the view must be dropped and recreated too. Doing it in one transaction keeps the change atomic for readers.

Full answer

Tables and rows used here:

sql
CREATE TABLE staff (id int, name text, salary int, dept text);
INSERT INTO staff VALUES (1, 'Asha', 90000, 'eng'), (2, 'Ben', 60000, 'ops');
CREATE VIEW staff_public AS SELECT id, name, salary FROM staff;
CREATE VIEW eng_staff AS SELECT id, name FROM staff_public WHERE id = 1;
sql
CREATE OR REPLACE VIEW staff_public AS SELECT id, name FROM staff;

Result:

text
ERROR:  cannot drop columns from view
sql
CREATE OR REPLACE VIEW staff_public AS SELECT id, name, salary, dept FROM staff; SELECT * FROM staff_public ORDER BY id;

Output:

text
 id | name | salary | dept
----+------+--------+------
 1  | Asha | 90000  | eng
 2  | Ben  | 60000  | ops
(2 rows)
sql
DROP VIEW staff_public;

PostgreSQL returns:

text
ERROR:  cannot drop view staff_public because other objects depend on it
sql
BEGIN; DROP VIEW eng_staff; DROP VIEW staff_public; CREATE VIEW staff_public AS SELECT id, name, dept FROM staff; CREATE VIEW eng_staff AS SELECT id, name FROM staff_public WHERE dept = 'eng'; COMMIT; SELECT * FROM eng_staff;

Result on PostgreSQL 17:

text
 id | name
----+------
 1  | Asha
(1 row)

Removing salary with CREATE OR REPLACE fails with 'cannot drop columns from view'. Appending dept at the end is allowed. A plain DROP VIEW fails because eng_staff depends on staff_public; DROP ... CASCADE would remove the dependent view silently, which is risky. The final transaction drops both views and recreates them without salary, so no reader sees a moment where either is missing. Migration tools should script dependent views explicitly for this reason.

sql
CREATE TABLE staff (id int, name text, salary int, dept text);
INSERT INTO staff VALUES (1, 'Asha', 90000, 'eng'), (2, 'Ben', 60000, 'ops');
CREATE VIEW staff_public AS SELECT id, name, salary FROM staff;
CREATE VIEW eng_staff AS SELECT id, name FROM staff_public WHERE id = 1;

CREATE OR REPLACE VIEW staff_public AS SELECT id, name FROM staff;

CREATE OR REPLACE VIEW staff_public AS SELECT id, name, salary, dept FROM staff; SELECT * FROM staff_public ORDER BY id;

DROP VIEW staff_public;

BEGIN; DROP VIEW eng_staff; DROP VIEW staff_public; CREATE VIEW staff_public AS SELECT id, name, dept FROM staff; CREATE VIEW eng_staff AS SELECT id, name FROM staff_public WHERE dept = 'eng'; COMMIT; SELECT * FROM eng_staff;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • OR REPLACE can only append columns
  • Removing columns needs DROP and CREATE
  • Dependent views block DROP
  • CASCADE silently removes dependents
  • Recreate in one transaction

Common mistakes

  • Using DROP ... CASCADE without listing what it removes
  • Changing views outside a transaction

Follow-ups

  • How do you list objects that depend on a view?
  • When are views updatable?

Tips

  • Show the dependency error