Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpmkO-0001L6-5t for pgsql-general@arkaria.postgresql.org; Sat, 02 Feb 2019 04:17:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gpmkL-0006Pi-JL for pgsql-general@arkaria.postgresql.org; Sat, 02 Feb 2019 04:17:37 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpmkK-0006No-TU for pgsql-general@lists.postgresql.org; Sat, 02 Feb 2019 04:17:37 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gpmkD-0008NI-Qu for pgsql-general@postgresql.org; Sat, 02 Feb 2019 04:17:35 +0000 Received: by mail-pf1-x435.google.com with SMTP id c123so4239803pfb.0 for ; Fri, 01 Feb 2019 20:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=dhfBREq/tq74N/sD04pdXqIZvvfWwNgvhBg7Y0KYf+E=; b=aavgSWCO0TwIsJc45WkCyUBydD2Oj1e2D3qkO6c7YzT4h2oBdhW5iATlSOHzlIZp3b xUsZ0gA8j+otG4J6M9IyQLil1Hh+Qn4UtjfNOR2PWbGnJyuPfquKT49afdJrx23IEmKY Ovcrlewbcvn2shzb+KbPjI9sGrN/1fD/n6Hl40sf/3rCh+cvn7LT5wR60kZIit4DabME WTebi++XdSEGgG7UgcjjEZA+tNp4Ok1MhjQpW0qiJanZVBifCjARMDtC4ov+mPim3Vc5 v2WqmTsWfpIIVyiKC3Je+CQ8EhrXH+pRoJjItHrTcNtZyWyAHEcyvY8oDGX8lWBfnd2f QOVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=dhfBREq/tq74N/sD04pdXqIZvvfWwNgvhBg7Y0KYf+E=; b=Fw9cB9eInx6SILpXtFjJfPm+Q3WcDGHt/PGK3CWyrGrnsjQ3Lqz4XPfa3vLln/AYWk NoB+u1wgVU4r7I4qBQFrv+YkuXilLiSA3IggSCwR8juCYCMRpiJ+Z/II/O7wdHVIeSRp lH4Dk3YGsXpVzDBqUIS5VaGxGN0SHyE5Ok78gwNYrqCzrscUBZZ0mhkFPI+Zuw6PWxs7 pDubzoteKYg24z88kkDFNmRxDyvFJ7/SP2qvcR5QArnr8onRKcXa41pGZ7e0mPETj1FE +FVt31me9ZJpwEqTnLZVUJEyLm2m4Qqx619z/TH36pKVMMzSKknCgHnmEYrU9LN0D8hh Xa6Q== X-Gm-Message-State: AHQUAuazOxbCL2RizBq3D7TVoqeihzjerbcKyLLLwRuxrXugHQfBKRxC sOWn+V2PwL19OQDzqiLZXKU= X-Google-Smtp-Source: AHgI3IZn1MF3dGD0HnMqRXcJAnMAerfEeztg0H8hZCNCDGbia+A/Deo1M8ofxI0ynoRwqa436MFoOA== X-Received: by 2002:a63:b543:: with SMTP id u3mr4928573pgo.420.1549081048222; Fri, 01 Feb 2019 20:17:28 -0800 (PST) Received: from 192-168-1-103.tpgi.com.au ([61.69.131.212]) by smtp.gmail.com with ESMTPSA id q1sm13103812pgs.14.2019.02.01.20.17.26 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 01 Feb 2019 20:17:27 -0800 (PST) Message-ID: <83fcd2bb48fe0bdc523c95dc7eddeeece34fe421.camel@gmail.com> Subject: Re: FK Constraint with ON DELETE SET DEFAULT cascading as table owner From: rob stone To: Brad Leupen , "pgsql-general@postgresql.org" Date: Sat, 02 Feb 2019 15:17:23 +1100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.4-1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, On Fri, 2019-02-01 at 14:01 +0000, Brad Leupen wrote: > Hello, > > We are using RLS on Postgres 11 to implement multi tenancy in our > application. We have a tenant table whose id matches the tenant’s > user role. Each table has a tenant_id FKA that defaults to > “current_user”. All of our foreign key constraints are multipart > (tenant_id + row_id). So far this works great except when we try to > clean up FKA references on deletion. Here’s a script that > demonstrates the issue in an empty database: > > > CREATE ROLE tenant1; > > ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON > TABLES TO tenant1; > ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON > SEQUENCES TO tenant1; > > CREATE TABLE tenant ( > id TEXT NOT NULL DEFAULT user PRIMARY KEY > ); > > CREATE TABLE foo ( > tenant TEXT REFERENCES tenant NOT NULL DEFAULT user, > id SERIAL NOT NULL, > default_bar INT, > PRIMARY KEY (tenant, id) > ); > > CREATE TABLE bar ( > tenant TEXT REFERENCES tenant NOT NULL DEFAULT user, > id SERIAL NOT NULL, > foo_id INT, > PRIMARY KEY (tenant, id), > constraint foo FOREIGN KEY (tenant, foo_id) REFERENCES foo > (tenant, id) ON DELETE CASCADE > ); > > ALTER TABLE foo ADD CONSTRAINT default_bar FOREIGN KEY (tenant, > default_bar) REFERENCES bar (tenant, id) ON DELETE SET DEFAULT; > > ALTER TABLE foo ENABLE ROW LEVEL SECURITY; > ALTER TABLE bar ENABLE ROW LEVEL SECURITY; > > CREATE POLICY tenant_tenant on tenant USING (id = current_user) WITH > CHECK (id = current_user); > CREATE POLICY foo_tenant on foo USING (tenant = current_user) WITH > CHECK (tenant = current_user); > CREATE POLICY bar_tenant on bar USING (tenant = current_user) WITH > CHECK (tenant = current_user); > > SET ROLE tenant1; > > INSERT INTO tenant DEFAULT VALUES; > INSERT INTO foo DEFAULT VALUES; > INSERT INTO bar ( foo_id ) (SELECT id FROM foo ); > UPDATE foo SET default_bar = ( SELECT id FROM bar ); > DELETE FROM bar; > > This script winds up failing because the “user” default value on > foo.tenant evaluates to the table owner, not the active user role of > “tenant1”. Is this the expected behavior? The desired outcome, after > deleting from bar, would be for foo’s tenant to remain “tenant1” and > its default_bar value be set to null. Is there another, cleaner way > to achieve this? > > Thank you! > Brad I ran your script. Output:- postgres 11.1 => select * from tenant; id --------- tenant1 (1 row) postgres 11.1 => select * from foo; tenant | id | default_bar ---------+----+------------- tenant1 | 1 | 1 (1 row) postgres 11.1 => select * from bar; tenant | id | foo_id ---------+----+-------- tenant1 | 1 | 1 (1 row) postgres 11.1 => delete from bar; ERROR: insert or update on table "foo" violates foreign key constraint "foo_tenant_fkey" DETAIL: Key is not present in table "tenant". postgres 11.1 => Your foreign key constraint is defined as:- default_bar ==> FOREIGN KEY (tenant, default_bar) REFERENCES bar(tenant, id) ON DELETE SET DEFAULT If you don't specify a "default" it uses NULL. There is no tenant.id that is NULL. So, the foreign key validation fails. AFAICT, it is working as intended. Cheers, Robert