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 1so1Uh-007igo-Ca for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Sep 2024 14:05:24 +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 1so1Uf-00DbJ2-La for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Sep 2024 14:05:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so1Ue-00DbIt-HY for pgsql-hackers@lists.postgresql.org; Tue, 10 Sep 2024 14:05:21 +0000 Received: from fhigh2-smtp.messagingengine.com ([103.168.172.153]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1so1Ua-000TZm-7G for pgsql-hackers@lists.postgresql.org; Tue, 10 Sep 2024 14:05:19 +0000 Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfhigh.phl.internal (Postfix) with ESMTP id 7431C1140094; Tue, 10 Sep 2024 10:05:14 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-08.internal (MEProxy); Tue, 10 Sep 2024 10:05:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1725977114; x= 1726063514; bh=qYLTbnJL/rC9yuYwg7Ma7Oo8NVdpEiAcEHUdAwzCHE0=; b=s LN4a/6HtUGVoDCgARa4NcJCdAo5s7fZlfXzf8+rZWHb+NWlS3iRzuGh4GOzxNDFc WP96NPWhrFrqslV/ksggpqjvjaanhAlrnBM4cIvlPlhkWoTG7OUjtIqp3x8t3E9M 0GmUxSvKxwBl16rD5dZrSq6x6KtRurVVHeJqL4L1pKN0UDv5Ve3ru8TXRGk6mO7T KaSFCMsHDFPHFNq3LBd0x45kzU0B1weRCIFk7ym5tyCLM64usP09r6fzy/7tw9kX Y9ElkFYrxnSHc2n6CncdAXn3jB/pURbVFTJm17ke/RNSpDooF07ML1eS4hT3TQZt MSPy+hZbe8gXVB5kbTbrA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudeiledgieefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkgggtugfgjgesthekredttddtjeen ucfhrhhomheptehlvhgrrhhoucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhh drnhhoqdhiphdrohhrgheqnecuggftrfgrthhtvghrnhepvdektdffudfftdffffehfffh jeejhffgieeuueekjeekfffgudffhfduffffueevnecuffhomhgrihhnpegvnhhtvghrph hrihhsvggusgdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgr ihhlfhhrohhmpegrlhhvhhgvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgpdhnsggprh gtphhtthhopedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehjihgrnhdruhhn ihhvvghrshgrlhhithihsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhh grtghkvghrsheslhhishhtshdrphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 10 Sep 2024 10:05:13 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1725977110; bh=xhr7N1djztZH553tbfav9Qs9c5tMGyAaOc4sMtTocXE=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=bEaSHjWIJpBy+NmjKn2YYXNDy6492RTMOg3sOiQ3yy62Yarw9vwRCZHRcHcSmZn28 MLhvrvTWbA9cJ4ACbjy4kmtDUdqThFigKIvYOKkO/QMcfAQVNR7VdorTrJdHlEDD9b YYE+avwbVvlvOoYZaphzhAEYmLtTy0a7quz7nBCMmqYWs9SyHtS/ngusX6Kf23GQ5S qe18X7XX56Pd5YW3e8m0mNsx4sNcrOavQ1E0wq2bXP0aMenwSYvJtgcYDgCj4NlrAm ww2cRwEuuODxxaHrx1fxo00Whagygly6GWwIzP3srYJseD5rC34OHMGxoeWdfez1m7 f4y9IQEqm6Kyw== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id BC379D3D; Tue, 10 Sep 2024 11:05:10 -0300 (-03) Date: Tue, 10 Sep 2024 16:05:10 +0200 From: Alvaro Herrera To: jian he Cc: Pg Hackers Subject: Re: not null constraints, again Message-ID: <202409101405.mkfg34imua4j@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-Sep-09, jian he wrote: > bold idea. print out the constraint name: violates not-null constraint \"%s\" > for the following code: > ereport(ERROR, > (errcode(ERRCODE_NOT_NULL_VIOLATION), > errmsg("null value in column \"%s\" of > relation \"%s\" violates not-null constraint", > NameStr(att->attname), > RelationGetRelationName(orig_rel)), > val_desc ? errdetail("Failing row contains > %s.", val_desc) : 0, > errtablecol(orig_rel, attrChk))); I gave this a quick run, but I'm not sure it actually improves things much. Here's one change from the regression tests. What do you think? INSERT INTO reloptions_test VALUES (1, NULL), (NULL, NULL); -ERROR: null value in column "i" of relation "reloptions_test" violates not-null constraint +ERROR: null value in column "i" of relation "reloptions_test" violates not-null constraint "reloptions_test_i_not_null" What do I get from having the constraint name? It's not like I'm going to drop the constraint and retry the insert. Here's the POC-quality patch for this. I changes a lot of regression tests, which I don't patch here. (But that's not the reason for me thinking that this isn't worth it.) diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index 29e186fa73d..d84137f4f43 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -1907,6 +1907,7 @@ ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo, * have been converted from the original input tuple after tuple routing. * 'resultRelInfo' is the final result relation, after tuple routing. */ +#include "catalog/pg_constraint.h" void ExecConstraints(ResultRelInfo *resultRelInfo, TupleTableSlot *slot, EState *estate) @@ -1932,6 +1933,7 @@ ExecConstraints(ResultRelInfo *resultRelInfo, char *val_desc; Relation orig_rel = rel; TupleDesc orig_tupdesc = RelationGetDescr(rel); + char *conname; /* * If the tuple has been routed, it's been converted to the @@ -1970,14 +1972,24 @@ ExecConstraints(ResultRelInfo *resultRelInfo, tupdesc, modifiedCols, 64); + { + HeapTuple tuple; + Form_pg_constraint conForm; + + tuple = findNotNullConstraintAttnum(RelationGetRelid(orig_rel), + att->attnum); + conForm = (Form_pg_constraint) GETSTRUCT(tuple); + conname = pstrdup(NameStr(conForm->conname)); + } ereport(ERROR, - (errcode(ERRCODE_NOT_NULL_VIOLATION), - errmsg("null value in column \"%s\" of relation \"%s\" violates not-null constraint", - NameStr(att->attname), - RelationGetRelationName(orig_rel)), - val_desc ? errdetail("Failing row contains %s.", val_desc) : 0, - errtablecol(orig_rel, attrChk))); + errcode(ERRCODE_NOT_NULL_VIOLATION), + errmsg("null value in column \"%s\" of relation \"%s\" violates not-null constraint \"%s\"", + NameStr(att->attname), + RelationGetRelationName(orig_rel), + conname), + val_desc ? errdetail("Failing row contains %s.", val_desc) : 0, + errtablecol(orig_rel, attrChk)); } } } -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/