Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YkcYY-0004QR-BV for pgsql-sql@arkaria.postgresql.org; Tue, 21 Apr 2015 18:05:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YkcYX-0006UY-Nv for pgsql-sql@arkaria.postgresql.org; Tue, 21 Apr 2015 18:05:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YkcYW-0006US-To for pgsql-sql@postgresql.org; Tue, 21 Apr 2015 18:05:57 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YkcYS-0006P1-H0 for pgsql-sql@postgresql.org; Tue, 21 Apr 2015 18:05:55 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id D6C8920841 for ; Tue, 21 Apr 2015 14:05:50 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute1.internal (MEProxy); Tue, 21 Apr 2015 14:05:50 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=MRSy6AwCYHfbeMu89Tc1+caCUDs=; b=ItEcDB SC+mcbZ9QMAsDbgtQw9LQlmlBy2rphc86ag9m/Gank+fjHSphwtb5mazxJfdK9M6 P692hWEtTC9RQiIIUWpW9vRpyvTRnpIHPTONBKXdogZlZI74CpwsQ1y0dwnrWB7/ byFBs90gzNwpP6HtuiqFxcfUPi0G9a7rpkCuw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=MRSy6AwCYHfbeMu 89Tc1+caCUDs=; b=jCt8GWJOD1GKKQrQrqHn7g+jdmXe0BNS1Wbsd2w9rkHOroX L07YqYb/Gmi6foPOJfQXCwuiUbygXobsYZeEkQxZ/FPg9ZiASskFzDUieaqqvjmP M4b5bDz3UXpIRegvv8TtEo8bTOfBUTknc/FofaO7sbwa6fhIvO2Gxp7Pt6II= X-Sasl-enc: MfjBCb5q/ek8KI+f5MWj5yLGL4xais+a9lCaz+EUsePB 1429639550 Received: from killi.site (unknown [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id 1D446C00017; Tue, 21 Apr 2015 14:05:50 -0400 (EDT) Message-ID: <5536917D.1020106@aklaver.com> Date: Tue, 21 Apr 2015 11:05:49 -0700 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: Shawn Gennaria , "David G. Johnston" CC: Alvaro Herrera , "pgsql-sql@postgresql.org" Subject: Re: How to determine offending column for insert exceptions References: <553665BC.7030400@aklaver.com> <55366EFD.3020908@aklaver.com> <20150421164635.GE4369@alvh.no-ip.org> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 04/21/2015 10:54 AM, Shawn Gennaria wrote: > David, > > Thanks for the insight. Indeed, I could not replicate Adrian's error > message by substituting his date example in my code. It just gives me a > generic 'cannot cast type date to integer' with no mention of a column > name. I think I better understand how the context affects the ability > to provide certain information in error messages. DO $$ DECLARE text_var1 text; text_var2 text; text_var3 text; BEGIN insert into int_test values (1, 'test', '2015-04-21'::date); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS text_var1 = MESSAGE_TEXT, text_var2 = PG_EXCEPTION_DETAIL, text_var3 = PG_EXCEPTION_HINT; RAISE NOTICE '%, %, %', text_var1, text_var2, text_var3; END$$; postgres@test=# \e NOTICE: column "test_col" is of type integer but expression is of type date, , You will need to rewrite or cast the expression. DO > > I'll attempt to solve my problem by querying pg_attribute for the > columns of the table I'm dealing with and then I'll just loop over them > all until I get a hit on the value returned in my error message. It > won't be pretty, but it's better than browsing thousands of columns in > CSVs trying to find these pitfalls. > > Thank you all for the assist! > > > On Tue, Apr 21, 2015 at 1:04 PM, David G. Johnston > > wrote: > > On Tue, Apr 21, 2015 at 9:48 AM, Shawn Gennaria > >wrote: > > Glad to hear it's here just in time. I am using 9.4, though, so > I wish I could figure out why it's returning NULL when I use > it. And the error message string doesn't contain any column > name to parse in my output. > > On Tue, Apr 21, 2015 at 12:46 PM, Alvaro Herrera > > wrote: > > Shawn Gennaria wrote: > > OK, I'm looking at > >www.postgresql.org/docs/9.4/interactive/plpgsql-control-structures.html#PLPGSQL-EXCEPTION-DIAGNOSTICS-VALUES > > > which I completely missed before. This sounds like my answer, but it's not > > returning anything when I try to extract the COLUMN_NAME. > > As far as I recall, COLUMN_NAME is new in 9.4. If you're > trying with an > earlier version, you can't get that info other than by > parsing the error > message string. > > > ​Adrian provided an example of trying to place a valid date into a > non-date column. The data itself was correct but the place it is > being stored to is invalid and can be reported explicitly. > > Shawn provided an example of trying to create an integer using an > invalid value. Type input errors are not column specific and so the > error - which is basically a parse error - does not provide column > information. > > There is likely some more experimenting here, and maybe room to > attached optional contextual markers to make better error messages, > but fundamentally these are two different kinds of errors which > cannot be generalized over to the extent of saying "errors should > provide column name information"...because what would you do for { > SELECT 'a'::int }? > > David J. > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql