Re: Unexpected behavior of DROP VIEW/TABLE IF EXISTS

From: Peter Moser <pitiz29a(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Unexpected behavior of DROP VIEW/TABLE IF EXISTS
Date: 2018-06-28 09:37:40
Message-ID: 172be039-6ee7-acfe-ad7c-2671eaba58a2@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 06/26/2018 07:06 PM, Tom Lane wrote:
> Also worth noting is that similar issues arise elsewhere, eg we now
> have "procedures" vs "functions" in a single namespace. Let's not have
> DROP TABLE acting in a way that's inconsistent with other parts of the
> system.

I think, that

DROP <type> <identifier> ...

could search within the type's namespace for the <type><identifier> in
combination, and only fail if it cannot be found.

I use those commands in a project with an Java ORM in place, that
automatically generates/updates a schema on each startup. It wrongly
generates a table X, where it should generate a view X. Hence, I do the
following inside an sql-script after startup:

DROP TABLE X IF EXISTS ...
DROP VIEW X IF EXISTS ...
CREATE VIEW X ...

It works on the first run, but not on a subsequent one, because the view
X already exists, hence DROP TABLE X fails.

If I switch the first two lines, it fails already during the first run,
because a table X exists...

DROP VIEW X IF EXISTS ...
DROP TABLE X IF EXISTS ...
CREATE VIEW X ...

It is only solvable with two different calls to the database, and error
handling on the application side.

Intuitively, I (and also others, that I asked) think that this command
should only fail, if a search for <type><identifier> in combination
succeeds and the DROP itself fails.

In general my use-case is, that I want to delete an X in a certain
namespace, where the type is not known in advance. I could query the
catalog to get that information and then build a procedure to "execute"
the right DROP command (as Pavel Stehule suggested), but that adds
complexity to the application code, where it shouldn't be necessary IMHO.

Best regards,
Peter

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Masahiko Sawada 2018-06-28 10:10:16 Re: Copy function for logical replication slots
Previous Message Yugo Nagata 2018-06-28 09:28:03 Fix error message when trying to alter statistics on included column