pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Aidan Samuel <aidan.samuel@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: CREATE OR REPLACE MATERIALIZED VIEW
Date: Wed, 09 Jan 2019 20:13:47 -0500
Message-ID: <32666.1547082827@sss.pgh.pa.us> (raw)
In-Reply-To: <CAAWhf=PHch2H3ekYnbafuwqWqwyRok8WVPaDxKosZE4GQ2pq5w@mail.gmail.com>
References: <CAAWhf=PHch2H3ekYnbafuwqWqwyRok8WVPaDxKosZE4GQ2pq5w@mail.gmail.com>

Aidan Samuel <aidan.samuel@gmail.com> writes:
> How come table creation doesn't allow [ OR REPLACE ], and view creation
> doesn't allow [ IF NOT EXISTS ]? Is this just how the SQL spec defines
> things?

I think all that stuff is our own fault rather than something you can
find in the SQL spec.

For indivisible database objects such as functions, IMV the "or replace"
semantics (COR) is better than the "if not exists" semantics (CINE).
With COR, if the command succeeds then you know what properties the
object has: the ones you just gave.  With CINE, you don't really know
anything at all except that there's an object by that name.  So we've
generally preferred to implement COR if practical.

For tables, though, the tradeoffs seem pretty different.  In particular,
COR semantics would more or less imply throwing away the table contents,
and probably dropping existing foreign-key connections too, since none of
that could be inferred from the given command.  It's a little scary to
do that implicitly.  So we insist that if you really want to lose data,
you say DROP TABLE explicitly, and then you can create a fresh empty
table.

(Personally I think CINE is seldom anything but a foot-gun, and would
rather we didn't have it at all; but some folks insisted.)

> I also noticed that the syntax for creating VIEWS and creating MATERIALISED
> VIEWS is handled separately [1][2].

Again, those aren't really the same thing: a matview is a lot more like a
table than it is like a view, so we don't treat it the same.  I think
if you compare the text for the CREATE VIEW and CREATE MATVIEW man pages,
you'll agree that a merged version would be pretty confusing.  All the
options are different.

Admittedly, this is all judgment calls and experience, but that's
why it's like that.

			regards, tom lane




view thread (3+ messages)  latest in thread

Message-ID: <32666.1547082827@sss.pgh.pa.us>
Permalink:  ../32666.1547082827@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/32666.1547082827@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, aidan.samuel@gmail.com
  Subject: Re: CREATE OR REPLACE MATERIALIZED VIEW
  In-Reply-To: <32666.1547082827@sss.pgh.pa.us>
  References: <CAAWhf=PHch2H3ekYnbafuwqWqwyRok8WVPaDxKosZE4GQ2pq5w@mail.gmail.com> <32666.1547082827@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox