I have created a view which is SELECT a, b FROM table_a; Then I renamed in the table_a the column a to c. What's interesting SELECT * FROM view after the column renaming still works, but column a doesn't exists anymore in the table_a. Why the view wasn't invalidated? The query SELECT a, b FROM table_X returns now error, what is reasonable: "ERROR: column a does not exist"
CREATE TABLE table_a(a int, b int);
CREATE VIEW test_view
AS
SELECT a, b
FROM table_a;
SELECT *
FROM test_view;
ALTER TABLE table_a
RENAME COLUMN a TO c;
-- it works
SELECT *
FROM test_view;
-- it doesn't work
SELECT a, b
FROM table_a;
Take a look at the ddl for the view. Before renaming the column you will see the definition as you actually wrote:
CREATE OR REPLACE VIEW test_view
AS SELECT a,
b
FROM table_a;
However, the view ddl after renaming the table column will be:
CREATE OR REPLACE VIEW public.test_view
AS SELECT c AS a,
b
FROM table_a;
Notice that the column selected has also been updated, but the prior name is maintained as the alias for the column. Thus viewing the result of querying the view will see the same columns names.
NOTE: There will be some slight differences depending on how you retrieve the view definition (the above from DBeaver Version 23.3.3). You can also resort to the function pg_get_viewdef(...).