Re: Privilege required for IF EXISTS event if the object already exists

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Shay Rojansky <roji(at)roji(dot)org>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Privilege required for IF EXISTS event if the object already exists
Date: 2021-12-15 16:10:41
Message-ID: CAKFQuwbT9vOCK3=gXrdewVuR-G3T5Q5EJVLY-iOMRVJN9+mrow@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Dec 15, 2021 at 5:35 AM Shay Rojansky <roji(at)roji(dot)org> wrote:

> Hi all,
>
> I've received numerous complaints about CREATE SCHEMA IF NOT EXISTS
> failing when the user lacks CREATE privileges on the database - even if the
> schema already exists. A typical scenario would be a multi-tenant
> schema-per-tenant setup, where the schema and tenant user are created
> beforehand, but then some database layer or ORM wants to ensure that the
> schema is there so the above is issued.
>
> Would it be reasonable to have the above no error if the schema already
> exists?
>

I would say it is reasonable in theory. But I cannot think of an actual
scenario that would benefit from such a change. Your stated use case is
rejected since you explicitly do not want tenants to be able to create
schemas - so the simple act of issuing "CREATE SCHEMA" is disallowed.

That would make it similar to the following (which I'm switching to in the
> Entity Framework Core ORM):
>
> DO $$
> BEGIN
> IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'foo') THEN
> CREATE SCHEMA "foo";
> END IF;
> END $$;
>
>
Because tenants are not allowed to CREATE SCHEMA you should replace "CREATE
SCHEMA" in the body of that DO block with "RAISE ERROR 'Schema foo required
but not present!';" Or, just tell them to create objects in the presumed
present schema and let them see the "schema not found" error that would
occur in rare case the schema didn't exist.

David J.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2021-12-15 16:42:55 Re: logical decoding and replication of sequences
Previous Message Brar Piening 2021-12-15 15:59:19 Re: Add id's to various elements in protocol.sgml