Re: How do CHECK Constraint Function privileges work?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Ruwan Fernando <rutechs(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: How do CHECK Constraint Function privileges work?
Date: 2020-04-05 14:37:26
Message-ID: CAKFQuwZ-BV+fN-N2Ba8X+UWAO=1+8B_+dUZty7oGSNoyJ6iZFg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sun, Apr 5, 2020 at 1:22 AM Ruwan Fernando <rutechs(at)gmail(dot)com> wrote:

> I have created a test harness in a fresh DB where a role has access to 2
> custom schemas, and have created a constraint check function in the
> app_private schema, while creating a table on the app_public schema.
>
> CREATE SCHEMA app_public;
> CREATE SCHEMA app_private;
>
> GRANT USAGE ON SCHEMA app_public TO "grant_test_role";
>
> CREATE OR REPLACE FUNCTION app_private.constraint_max_length(name_ TEXT)
> RETURNS BOOLEAN
> AS $$
> BEGIN
> -- do some checks here
> RETURN TRUE;
> END;
> $$ LANGUAGE plpgsql;
>
> CREATE TABLE app_public.test_tab (
> id INT NOT NULL PRIMARY KEY,
> name TEXT NOT NULL,
>
> CONSTRAINT name_length_check CHECK
> (app_private.constraint_max_length(name));
> );
>

Assuming you are connected as a superuser role here; you are not setting an
explicit owner.

>
> BEGIN;
> SET LOCAL ROLE TO "grant_test_role";
> INSERT INTO app_public.test_tab (id, name) VALUES (1, 'Very Long Name');
> COMMIT;
>
> My expectation was the INSERT would give me an exception due to
> "grant_test_role" not having permissions on the "app_private" schema, but
> it does not. Why does the CHECK constraint function executes fine in this
> instance?
>
> I feel I'm missing some knowledge on how PostgreSQL internals work when
> checking privileges for CHECK constraint expressions, and I didn't find
> anything mentioned about this in documentation.
>

While I cannot locate the relevant documentation right now, privileges for
triggers and constraints attached to a table are made against the owner of
the table, not the user performing the action.

David J.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2020-04-05 15:42:15 Re: How do CHECK Constraint Function privileges work?
Previous Message Ruwan Fernando 2020-04-05 08:22:03 How do CHECK Constraint Function privileges work?