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 1erPdw-0005ho-Om for pgsql-general@arkaria.postgresql.org; Thu, 01 Mar 2018 14:57:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1erPdv-0005av-2U for pgsql-general@arkaria.postgresql.org; Thu, 01 Mar 2018 14:57:11 +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 1er9IX-0007tE-Gt for pgsql-general@lists.postgresql.org; Wed, 28 Feb 2018 21:30:01 +0000 Received: from smtp.emerald.synacor.com ([208.47.184.7] helo=smtp01.emerald.cmh.synacor.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1er9IS-0002g1-KH for pgsql-general@postgresql.org; Wed, 28 Feb 2018 21:30:00 +0000 X_CMAE_Category: , , X-CNFS-Analysis: v=2.2 cv=FtdHxCjq c=1 sm=1 tr=0 a=sOJG561dOovE9JBqKNxcJA==:117 a=KGjhK52YXX0A:10 a=FKkrIqjQGGEA:10 a=Rjg61QVRfgoA:10 a=IkcTkHD0fZMA:10 a=Op4juWPpsa0A:10 a=qzyW-89uDWQA:10 a=nvhMdYX1sOkA:10 a=2wibhlOU01UA:10 a=pGLkceISAAAA:8 a=dv8OE1nOAAAA:8 a=KsQWOMYMAAAA:8 a=WKHuofOLQDKc-70GvAoA:9 a=QEXdDO2ut3YA:10 a=AqK8oIbFhZhCFxCkA2Fx:22 a=cH4uAexGb9owj9GhT78d:22 X-CM-Score: 0 X-Scanned-by: Cloudmark Authority Engine X-Authed-Username: YnRvYmVyQGJyb2Fkc3RyaXBlLm5ldA== X_CMAE_Category: , , X-CNFS-Analysis: X-CM-Score: X-Scanned-by: Cloudmark Authority Engine Authentication-Results: smtp01.emerald.cmh.synacor.com smtp.mail=btober@broadstripe.net; spf=neutral; sender-id=neutral Authentication-Results: smtp01.emerald.cmh.synacor.com header.from=btober@broadstripe.net; sender-id=neutral Received-SPF: neutral (smtp01.emerald.cmh.synacor.com: 10.33.73.65 is neither permitted nor denied by domain of broadstripe.net) Received: from [10.33.73.65] ([10.33.73.65:51608] helo=md03.emerald.cmh.synacor.com) by smtp.etc.emerald.synacor.com (envelope-from ) (ecelerity 3.6.1.42806 r(Platform:3.6.1.1)) with ESMTP id 60/BE-03937-05F179A5; Wed, 28 Feb 2018 16:29:52 -0500 Date: Wed, 28 Feb 2018 16:29:52 -0500 (EST) From: "btober@computer.org" To: Tim Cross Cc: Jeremy Finzel , Postgres General Message-ID: <1677317814.82065227.1519853392657.JavaMail.zimbra@broadstripe.net> In-Reply-To: <87vaegq0v4.fsf@gmail.com> References: <87vaegq0v4.fsf@gmail.com> Subject: Re: Enforce primary key on every table during dev? MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [8.44.100.11] X-Mailer: Zimbra 8.0.7_GA_6021 (ZimbraWebClient - FF52 (Win)/8.0.7_GA_6021) Thread-Topic: Enforce primary key on every table during dev? Thread-Index: zjPxgT2Cpd19R8mKl4g0H9PqCtEXNw== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ----- Original Message ----- > From: "Tim Cross" > Sent: Wednesday, February 28, 2018 4:07:43 PM > > Jeremy Finzel writes: > > > 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. > > > > I think you would be better off having an automated report which alerts > you to tables lacking a primary key and deal with that policy through > other means. Using triggers in this way often leads to unexpected > behaviour and difficult to identify bugs. The policy is a management > policy and probably should be dealt with via management channels rather > than technical ones. Besides, the likely outcome will be your developers > will just adopt the practice of adding a serial column to every table, > which in itself doesn't really add any value. I concur with other respondents that suggest this is more of a policy issue. In fact, you yourself identify it right there in the first sentence as a policy issue! One tool that changed my life (as a PostgreSQL enthusiast) forever is David Wheeler's pgTAP (http://pgtap.org/) tool. It includes a suite of functionality to assess the database schema via automated testing. Part of a rigorous development environment might include using this tool so that any application/database changes be driven by tests, and then your code review process would assure that the appropriate tests are added to the pgTAP script to confirm that changes meet a policy standard such as what you are demanding. I can't imagine doing PostgreSQL development without it now. Same guy also produced a related tool called Sqitch (http://sqitch.org/) for data base change management. Use these tools together, so that before a developer is allowed to check in a feature branch, your teams' code review process maintains rigorous oversight of modifications. -- B