Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rflFk-00256H-0P for pgsql-admin@arkaria.postgresql.org; Thu, 29 Feb 2024 18:35:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rflFi-0079qx-00 for pgsql-admin@arkaria.postgresql.org; Thu, 29 Feb 2024 18:35:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rflFh-0079qn-LZ for pgsql-admin@lists.postgresql.org; Thu, 29 Feb 2024 18:35:30 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rflFf-002894-1q for pgsql-admin@postgresql.org; Thu, 29 Feb 2024 18:35:29 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 41TIZP1F2720843; Thu, 29 Feb 2024 13:35:25 -0500 From: Tom Lane To: "David G. Johnston" cc: Debraj Manna , pgsql-admin Subject: Re: Adding custom constraints on a postgres database In-reply-to: References: Comments: In-reply-to "David G. Johnston" message dated "Thu, 29 Feb 2024 11:26:27 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2720841.1709231725.1@sss.pgh.pa.us> Date: Thu, 29 Feb 2024 13:35:25 -0500 Message-ID: <2720842.1709231725@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Thu, Feb 29, 2024, 11:10 Debraj Manna wrote: >> Can someone let me know if there is a way I can add some custom >> rules/constraints on a Postgres database? >> >> For example, all tables in a database should have a primary key specified. >> If someone is trying to violate the rules specified then the SQL command >> should fail. > https://www.postgresql.org/docs/current/event-trigger-definition.html When/where are you expecting to enforce this? If you prohibit the common pattern of CREATE TABLE followed sometime later by ALTER TABLE ADD PRIMARY KEY, you will soon regret that. I think you might be better served by writing a lint-like thing that can be run periodically to look through the system catalogs for policy violations. Rejecting SQL commands on-the-fly will be expensive, as well as painful to work with. regards, tom lane