Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jL7Pk-0002c3-N0 for pgsql-sql@arkaria.postgresql.org; Sun, 05 Apr 2020 15:42:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jL7Pj-0001Ac-D6 for pgsql-sql@arkaria.postgresql.org; Sun, 05 Apr 2020 15:42:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jL7Pj-0001AV-6Z for pgsql-sql@lists.postgresql.org; Sun, 05 Apr 2020 15:42:23 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jL7Pd-00019C-5M for pgsql-sql@lists.postgresql.org; Sun, 05 Apr 2020 15:42:22 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 035FgFVh011653; Sun, 5 Apr 2020 11:42:15 -0400 From: Tom Lane To: "David G. Johnston" cc: Ruwan Fernando , pgsql-sql Subject: Re: How do CHECK Constraint Function privileges work? In-reply-to: References: Comments: In-reply-to "David G. Johnston" message dated "Sun, 05 Apr 2020 07:37:26 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11651.1586101335.1@sss.pgh.pa.us> Date: Sun, 05 Apr 2020 11:42:15 -0400 Message-ID: <11652.1586101335@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "David G. Johnston" writes: > On Sun, Apr 5, 2020 at 1:22 AM Ruwan Fernando wrote: >> 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? > 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. No, that's not how it works, at least not for CHECK constraints --- those are executed and privilege-checked as the user running the DML command. (This might be the wrong thing, but that's what happens.) The important point about the OP's example is that privilege checks on schemas only happen at parse time, ie they are interpreted as "can you look up this object right now?". The only check made at execution time is whether the calling user has EXECUTE privilege on the function, working from the already-stored function OID --- so the schema is irrelevant at that point. Any stored expression such as a view or CHECK constraint will act that way. Not sure if this comports with what the SQL spec says, but that's how PG does it. regards, tom lane