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 1er8xA-0003On-Fj for pgsql-general@arkaria.postgresql.org; Wed, 28 Feb 2018 21:07:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1er8x9-0002Ol-6r for pgsql-general@arkaria.postgresql.org; Wed, 28 Feb 2018 21:07:55 +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 1er8x8-0002OQ-Re for pgsql-general@lists.postgresql.org; Wed, 28 Feb 2018 21:07:54 +0000 Received: from mail-pg0-x22e.google.com ([2607:f8b0:400e:c05::22e]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1er8x5-00025r-JW for pgsql-general@postgresql.org; Wed, 28 Feb 2018 21:07:54 +0000 Received: by mail-pg0-x22e.google.com with SMTP id i133so1417710pgc.12 for ; Wed, 28 Feb 2018 13:07:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=fxBwzT99cIPwwS6EkIgNQaaQhHXndmW9T29jx88qUq8=; b=ERTFGVRBzz/jOGD1i+zPva0pxPTaMscGxhjzu2qGVdBVdeDbkGlFUUE2IpbfdKQwUI r6SedqqAiXIrDZGTvU0WUr3zbpkpB1i5itL0x0flRPm3/lqGGiHxhctRWzz1LnR/jIMC b10oHbEPyjA6KcxsBn+Mu7Gy0qLvp4w6JEMdPWoGcrPNLdSjwPgjhMGti1IYIvDbN4aJ 7aIyXtBzEJOIzm2sTYelPhXbAsOwytDC3sCPHdfguUVjaTYEpGjpFQdiAsDWWYASb1Pm wgQ4WioVf7DmZ3RHYSWz3bn2AV9vQj4K3AoXPKqlJaOfLaeFD4T/c2+rqBW89lZNuzpf pbcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=fxBwzT99cIPwwS6EkIgNQaaQhHXndmW9T29jx88qUq8=; b=kpL9ktOOiNbc61XSCO/5bKyaTA7K06oUo4GgCF24KWyrcg7xk1cw1S6C30I+lZkGed f4g61g39uiFHMxfdFK8inyeW1/OVaT2RdnUBWmZu/u3OfAD2of90GyYMSliguodNdF9s sWa4KQ+vhcc54bDk4WjoGYTcsoH1Y9a89B0i/W8jtOuh+f7hgBuI9AvTDzju9FqoywGd bkJaoFi7Lkc/nOZwoiRxjKPV42HIaP987DTtqlDNzS2e5hPiG4j/6Qs1fUs7IjrvifBQ yfbDrAP8R8YN1giENplnVl0xLUvLj1d2mnS+4CGIuBblcbZ0SgLW7gtUEHroNkdQDUdP LeSA== X-Gm-Message-State: APf1xPDgDDhNCw6Z2ZCIZp63xiotTXFQhzy0DLgPFeTUGXZYAQkfXX+Y UeSlduA7XFd1S0McKrQy4/1b6/pG X-Google-Smtp-Source: AG47ELunYiCvTH3E6DK5lnRECjHh6dfDhru6TTWFwEb9ZsgzoPj86PnEtdbGhYtntU3w40MlbyO90w== X-Received: by 10.167.128.143 with SMTP id v15mr7385289pff.36.1519852067576; Wed, 28 Feb 2018 13:07:47 -0800 (PST) Received: from tim-desktop (2001-44b8-31f2-bb00-59ab-05d0-28be-c8cb.static.ipv6.internode.on.net. [2001:44b8:31f2:bb00:59ab:5d0:28be:c8cb]) by smtp.gmail.com with ESMTPSA id e125sm3932950pgc.76.2018.02.28.13.07.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 28 Feb 2018 13:07:47 -0800 (PST) References: User-agent: mu4e 0.9.18; emacs 25.3.1 From: Tim Cross To: Jeremy Finzel Cc: Postgres General Subject: Re: Enforce primary key on every table during dev? In-reply-to: Date: Thu, 01 Mar 2018 08:07:43 +1100 Message-ID: <87vaegq0v4.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. Tim -- Tim Cross