Re: CREATE OR REPLACE MATERIALIZED VIEW

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Isaac Morland <isaac(dot)morland(at)gmail(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Paul A Jungwirth <pj(at)illuminatedcomputing(dot)com>, Erik Wienhold <ewie(at)ewie(dot)name>, Said Assemlal <sassemlal(at)neurorx(dot)com>, pgsql-hackers(at)postgresql(dot)org, Haibo Yan <haibo(dot)yan(at)hotmail(dot)com>
Subject: Re: CREATE OR REPLACE MATERIALIZED VIEW
Date: 2026-08-14 19:01:19
Message-ID: 3778322.1786734079@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Isaac Morland <isaac(dot)morland(at)gmail(dot)com> writes:
> This sounds like CREATE OR REPLACE to me, even if there is something to
> explain in the documentation about what happens to the existing data
> (assuming it is currently populated). I don't think of CREATE OR REPLACE as
> completely re-creating an object anyhow. Certainly it can't arbitrarily
> replace a function (can't change return type) nor a view (can't remove or
> change type of a column). It's OK if not all possible changes are supported
> by the "OR REPLACE" part of the command.

To my mind, the formal requirement for CREATE OR REPLACE should be
"if the command succeeds, the resulting object properties are
identical to what they'd be if we were creating it fresh" -- basically
a form of idempotency. It's okay to fail when there are reasons why
we can't or shouldn't make that so.

In particular, ISTM that if we invent CREATE OR REPLACE MATERIALIZED
VIEW, then the view content should be either computed afresh or left
empty (depending on WITH NO DATA); it should never leave stale data.
But I think Robert is correct that an ALTER command that does keep
the old data is often going to be what's wanted.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Sami Imseih 2026-08-14 19:08:20 Re: pgstat: Flush some statistics within running transactions, take 2
Previous Message Daniel Gustafsson 2026-08-14 18:57:36 Re: Prevent premature startup of pg_upgrade targets