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 1er24M-000530-Gw for pgsql-general@arkaria.postgresql.org; Wed, 28 Feb 2018 13:46:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1er24K-0002iI-P6 for pgsql-general@arkaria.postgresql.org; Wed, 28 Feb 2018 13:46:52 +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_SHA384:256) (Exim 4.89) (envelope-from ) id 1er24K-0002i9-Gq for pgsql-general@lists.postgresql.org; Wed, 28 Feb 2018 13:46:52 +0000 Received: from host3.dynacom.ondsl.gr ([62.103.35.211] helo=smadev.internal.net) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1er24D-0000jn-8Y for pgsql-general@lists.postgresql.org; Wed, 28 Feb 2018 13:46:52 +0000 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id w1SDkfCu040573 for ; Wed, 28 Feb 2018 15:46:42 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: Enforce primary key on every table during dev? To: pgsql-general@lists.postgresql.org References: From: Achilleas Mantzios Message-ID: Date: Wed, 28 Feb 2018 15:46:41 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; 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-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 28/02/2018 15:34, 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? cron job to check for tables without PK ? Although for a short period the offending table would be there live without a PK. But IMO every table, in addition to PK, should have also natural unique keys as much as possible. Better safe than sorry. > > Thanks, > Jeremy -- Achilleas Mantzios IT DEV Lead IT DEPT Dynacom Tankers Mgmt