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 1iYZt2-0005M4-FZ for pgsql-general@arkaria.postgresql.org; Sat, 23 Nov 2019 18:12:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iYZt1-0003eP-4o for pgsql-general@arkaria.postgresql.org; Sat, 23 Nov 2019 18:11:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iYZt0-0003eI-Qx for pgsql-general@lists.postgresql.org; Sat, 23 Nov 2019 18:11:58 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iYZsy-00045m-MC for pgsql-general@postgresql.org; Sat, 23 Nov 2019 18:11:58 +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 xANIBnTO006029; Sat, 23 Nov 2019 13:11:49 -0500 From: Tom Lane To: Adrian Klaver cc: aleksey ksenzov , pgsql-general Subject: Re: Constants in the foreighn key constraints In-reply-to: References: <85f6184b-cd37-0b7e-9bee-b0d781cead6f@aklaver.com> Comments: In-reply-to Adrian Klaver message dated "Sat, 23 Nov 2019 09:02:59 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6027.1574532709.1@sss.pgh.pa.us> Date: Sat, 23 Nov 2019 13:11:49 -0500 Message-ID: <6028.1574532709@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Adrian Klaver writes: > Please reply to list also. > Ccing list. > On 11/22/19 11:36 PM, aleksey ksenzov wrote: >> While I understand I can do everything with triggers/functions, for me >> it looks like a good idea to have possibility to use constants in >> constraints, so it would be very nice if postgres community could add >> this functionality in the nearest releases. It seems quite unlikely to me that we'd add such a thing. It'd be a weird wart on the foreign-key feature. Notable problems: * How would it interact with referential actions, notably ON UPDATE CASCADE, ON UPDATE/DELETE SET NULL, ON UPDATE/DELETE SET DEFAULT? I guess you could disallow those options for such a foreign key, but anytime you have a feature that's that non-orthogonal with existing ones, you have to ask yourself if you've designed it right. * Such FKs couldn't be displayed in the information_schema views, at least not without violating the letter and spirit of the SQL spec. We already have some cases of constraints that can't be shown in information_schema, but that's not the sort of wart I want more of. BTW, it seems to me that you can get the same behavior with existing features: make a regular multicolumn foreign key constraint, and then add a CHECK constraint restricting what value one of the referencing columns can have. Yeah, this requires useless storage of a column that will only ever have one value. I think that's an okay limitation for a niche use-case. It also generalizes more easily to cases where there's more than exactly one allowed value for a referencing column. regards, tom lane