Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1er2FD-0005tP-2D for pgsql-general@arkaria.postgresql.org; Wed, 28 Feb 2018 13:58:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1er2FA-0000FZ-TJ for pgsql-general@arkaria.postgresql.org; Wed, 28 Feb 2018 13:58:04 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1er2F9-0000C7-Sj for pgsql-general@lists.postgresql.org; Wed, 28 Feb 2018 13:58:04 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1er2F6-0001cs-H2 for pgsql-general@postgresql.org; Wed, 28 Feb 2018 13:58:02 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 41F1121CAF; Wed, 28 Feb 2018 08:57:58 -0500 (EST) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Wed, 28 Feb 2018 08:57:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=5nFEv7agKIbO5lbqNITPnJ74ZFISd gxadiVppBK+khI=; b=b25K8r93Yeau1pYlQVcLpwcXjQUcSc+M0fGXOA5XF5gJS 0gwAFy8qe7HcmyVg9QmX972pc4iMRiXLCBQdkaSqRvo/TiJsV+u6Ip9SGn8prd6L 0qw/0H5KTyiHY6gE6OEhtGGgMeLsRhAPIo6chwOEeMDkhp8RNv1QiOjkYQYmBQZw lM6h7bYHr+qBDbbYd6vXUD3PFiHIJCAOv/9fdvIdMNsx2UrbB/hWPDYHRpEL5Iz6 giyfzSfKaNc86Eyn+skVNEOkImzq9p2cC5meHW6h68apiMzcokNAvBMq1zaclN1n HLY+Eqi6OMCTGmF+mqsvZm2zAMMTQn00KJosnmPUA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=5nFEv7 agKIbO5lbqNITPnJ74ZFISdgxadiVppBK+khI=; b=mwGj7G/II+qUMyxf774xgi R6bfh2c223xyzPk9ujpTPMGO8yRLWu/tFYVyuOkEn32ZnISREGVu22dTeCQacYmK EBIX7ixApJz8DQWHj/7nvLK+/8fcBFLCu0VrXWCCIAVPPgokB65XquvnM/UpPjBV SZd+8ktlk1I5zbA0loZ8Qxbig42xLXSedk6JlycVCqJkUoDdnUzrZEkd0lCu16rw kPTRMMQ1KoQzRg7M5zSoFsxfd0uwwhJ7O7qYqM68a11TGUxPjeR8FoEeVDIKJrvI uWCL4x+cF5qVr0d9AfuJ4gxtymML0FfUSfC9g8hgjC9T63V2tzlueKUHqXc9wsxg == X-ME-Sender: Received: from [192.168.1.2] (71-35-123-124.tukw.qwest.net [71.35.123.124]) by mail.messagingengine.com (Postfix) with ESMTPA id B2AD924505; Wed, 28 Feb 2018 08:57:57 -0500 (EST) Subject: Re: Enforce primary key on every table during dev? To: John McKown , PostgreSQL General References: From: Adrian Klaver Message-ID: Date: Wed, 28 Feb 2018 05:57:56 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02/28/2018 05:52 AM, John McKown wrote: > On Wed, Feb 28, 2018 at 7:34 AM, Jeremy Finzel >wrote: > > We want to enforce a policy, partly just to protect those who might > forget, for every table in a particular schema to have a primary > key.  This can't be done with event triggers as far as I can see, > because it is quite legitimate to do: > > BEGIN; > CREATE TABLE foo (id int); > ALTER TABLE foo ADD PRIMARY KEY (id); > COMMIT; > > It would be nice to have some kind of "deferrable event trigger" or > some way to enforce that no transaction commits which added a table > without a primary key. > > Any ideas? > > Thanks, > Jeremy > > > > ​What stops somebody from doing: > > CREATE TABLE foo (filler text primary key default null, realcol1 int, > realcol2 text); > > And then just never bother to ever insert anything into the column > FILLER? It fulfills your stated requirement​ of every table having a Then you would get this: test=# CREATE TABLE foo (filler text primary key default null, realcol1 int, realcol2 text); CREATE TABLE test=# insert into foo (realcol1, realcol2) values (1, 'test'); ERROR: null value in column "filler" violates not-null constraint DETAIL: Failing row contains (null, 1, test). > primary key. Of course, you could amend the policy to say a "non-NULL > primary key". > > > > -- > I have a theory that it's impossible to prove anything, but I can't > prove it. > > Maranatha! <>< > John McKown -- Adrian Klaver adrian.klaver@aklaver.com