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 1mpFWB-0005rR-Ux for pgsql-docs@arkaria.postgresql.org; Mon, 22 Nov 2021 20:02: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 1mpFWA-0003DX-MF for pgsql-docs@arkaria.postgresql.org; Mon, 22 Nov 2021 20:02:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mpFWA-0003DO-B3 for pgsql-docs@lists.postgresql.org; Mon, 22 Nov 2021 20:02:22 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mpFW7-0001aL-96 for pgsql-docs@lists.postgresql.org; Mon, 22 Nov 2021 20:02:21 +0000 Received: by mail-wm1-x32d.google.com with SMTP id j140-20020a1c2392000000b003399ae48f58so136705wmj.5 for ; Mon, 22 Nov 2021 12:02:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=oCABHjqy1ErWIoPKm8gAZfSYGHN9Vms0SsTVtFrcSQ0=; b=uS38sTQXAdQbxWiq77QZ4Xfd5GfbOb9nKMc4Gim/SpGCQghSpDbwVjocO49mD1ghtl Mbm1aa4CSuxKhz6UMv2dD0k4mLwZzQ+F+huJLoGfeLxzh9ZqN59ktbUyNdXfvcJ5h85u jArx28/Fc9foBO4550pG4SdYrKdowk+YZSH8swZeJjQVCF34hJG7JpRWxtAH1pkIYFYW xZn+qmbxOF/ZqGht97Ax0/Vc/xLso2/cuS3aW2JzZ/DLuLU+FCoe/V1C+hPCS69CXq8E x26UXwU0yO3wRgOIeYenf7Z/2wexEU9W0MozlA56T7hRpjxTYvvZBNRvA4/9n6QA6AYE bhmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=oCABHjqy1ErWIoPKm8gAZfSYGHN9Vms0SsTVtFrcSQ0=; b=pYTWLH+SfxI5qGilgxvyUpT9l8qZM37WOYRvp40nHo8ElOs7B09aIaY3yudPWbVONV vjYJcgJ4TkRWwopc7J9AYOW0+p9wxJSGMRCvxIANaSH5Vvaf6D4SWwoy+bjOH60D6I0U 3G5bA/xTSDf7jNEIEKJkS/LkD89nGPX1dO5CxXNl2bqOpufLCjdM7DNpy+dBqsFDcYDE XfpZuR1BCeY/fq9Qge+8D7+kX3dzKnXp6EGnk+KmoJWqNeSVF47q411lxcinkGWq8jfO gjZ71c+YEXInxHsRi2fZMBx/bKa7ZG5V5gBr4w3ZWqgp/GvXTT7dP/ehFtKWlI68c2I7 1BeQ== X-Gm-Message-State: AOAM533zGID+fus1HQLh3UZSCyMaj6acfkHV/HoOsTC4jhHeRpHmdKxT Lajj3eeyAGI6B5P2YIOnBK28cwiZyRIjaQ== X-Google-Smtp-Source: ABdhPJyB0Kk5P/TcRKeZKMe4c2he9V+/7HJzH+Hbq4YKXGbXfp5HzgeMZnlEwrSdr1K8GDfxAOFvNQ== X-Received: by 2002:a05:600c:3494:: with SMTP id a20mr33002028wmq.195.1637611337824; Mon, 22 Nov 2021 12:02:17 -0800 (PST) Received: from localhost.localdomain (dynamic-edcf9wxfo1mlm4hl9-pd01.res.v6.highway.a1.net. [2001:871:5e:9b1c:2bb7:dfb8:1674:1c0d]) by smtp.gmail.com with ESMTPSA id s63sm11101436wme.22.2021.11.22.12.02.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Nov 2021 12:02:17 -0800 (PST) Message-ID: Subject: Re: Triggers on underlying tables of updatable views From: Laurenz Albe To: Max Ziermann , pgsql-docs@lists.postgresql.org Date: Mon, 22 Nov 2021 21:02:16 +0100 In-Reply-To: <3e14b477-40ce-4b6a-047a-9fea3843427c@htw-dresden.de> References: <163758281947.26190.10251137266467665332@wrigleys.postgresql.org> <2f54dbb7d59aacacbc4654b0bc56faaebe993e09.camel@cybertec.at> <3e14b477-40ce-4b6a-047a-9fea3843427c@htw-dresden.de> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-2.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2021-11-22 at 19:54 +0100, Max Ziermann wrote: > Am 22.11.21 um 16:41 schrieb Laurenz Albe: > > On Mon, 2021-11-22 at 12:06 +0000, PG Doc comments form wrote: > > > "Note that the user performing the insert, update or delete on the view must > > > have the corresponding insert, update or delete privilege on the view. In > > > addition the view's owner must have the relevant privileges on the > > > underlying base relations, but the user performing the update does not need > > > any permissions on the underlying base relations (see Section 41.5)." > > > > > > Could it be made more clear that triggers on a underlying table of an > > > updatable view are still executed with the permissions of the user > > > performing an insert/update/delete on the view? > > > > But that is not the case: that trigger will be executed with the permissions > > of the owner of the underlying table. > > Maybe I am missing an obvious point, but I don't think that's the case. > SQL example: > > > CREATE ROLE view_access; > CREATE TABLE data (a integer primary key); > CREATE VIEW data_view AS SELECT * FROM data; > GRANT SELECT, INSERT ON data_view TO view_access; > > CREATE FUNCTION trig() RETURNS trigger AS $$ > BEGIN > RAISE NOTICE 'trig() executed as user=%', current_user; > PERFORM COUNT(*) FROM data; > RETURN new; > END > $$ LANGUAGE plpgsql; > CREATE TRIGGER trig AFTER INSERT ON data FOR EACH ROW EXECUTE FUNCTION > trig(); > > > -- Executed by owner of view, data and trigger: (as expected) all ok > INSERT INTO data_view VALUES (1); > > SET ROLE TO view_access; > -- Executed by view_access: fails > INSERT INTO data_view VALUES (2); > > > For the second INSERT, the trigger is called by the user performing the > insert instead of the user owning the table or the view. I think I know the source of the confusion. "current_user" will always return the current user, even if you are running under the security context of a different user. So the function will run with the permissions of the owner of "data", but "current_user" will return "view_access". Yours, Laurenz Albe