Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YkcNs-0003jA-RG for pgsql-sql@arkaria.postgresql.org; Tue, 21 Apr 2015 17:54:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YkcNs-0004BG-A5 for pgsql-sql@arkaria.postgresql.org; Tue, 21 Apr 2015 17:54:56 +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 1YkcNq-00048y-FZ for pgsql-sql@postgresql.org; Tue, 21 Apr 2015 17:54:54 +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 1YkcNi-000690-W9 for pgsql-sql@postgresql.org; Tue, 21 Apr 2015 17:54:53 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 2D9A8208C5 for ; Tue, 21 Apr 2015 13:54:45 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute5.internal (MEProxy); Tue, 21 Apr 2015 13:54:45 -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=nuM6M933kvbNvACBT3l8RPsgR4Q=; b=ahyhUA jzxij8c9UpI22tm+3Y05Jdlw++vawUoENX7Crr87HNHfMUylPQyBEY7ZohBlPXOP ivR6H862BHbCMGasMAGYcVMkCkqTE21m2jOMZ9LUkpdatqOoM7L0m3MGXGXC1Cl+ 6WaiYhxB5uSCNoQCbzIQ8km4B8wLNVMDvPXoM= 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=nuM6M933kvbNvAC BT3l8RPsgR4Q=; b=EwtHiqJnukRW3ZlmWjvZQIapuDQpfSb3Y0pGQ8Sh3+fPsnR 5VUEzzHpGfu6xoEbEqhGsRk+PZHlJ9NL0BPNaOnEfOMJ2hDYHM3LRAh0TX191ve1 Ws9ePNaxsTs0Uo7JNgn7OBHFdsXSch0bEPKkS2pHvixIkJGWZVkk2euhAjGM= X-Sasl-enc: y1TBdn53Y5cvJSXN83deVsBllW2mntWZpOchl/9jhmxL 1429638884 Received: from killi.site (unknown [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id A97BB680125; Tue, 21 Apr 2015 13:54:44 -0400 (EDT) Message-ID: <55368EE3.9030208@aklaver.com> Date: Tue, 21 Apr 2015 10:54:43 -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 CC: pgsql-sql@postgresql.org Subject: Re: How to determine offending column for insert exceptions References: <553665BC.7030400@aklaver.com> <55366EFD.3020908@aklaver.com> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 09:37 AM, 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. In > desperation, I tried grabbing every value in that table, and it just > repeats the same info I already had via SQLSTATE and SQLERRM. > > Here's what my overall implementation looks like: > > DECLARE > col_name text; > sql_state text; > ... > BEGIN > FOR rec IN ( > SELECT 1 file per row: info about each of my csv files to > dynamically build the tables, copy and insert data > ) LOOP > ... > QRY_INSERT := 'INSERT INTO rec.final_table SELECT rec.inserts FROM > rec.temp_table'; -- rec.inserts is text formed like 'a::int, b::int,...' > BEGIN > EXECUTE QRY_INSERT; > EXCEPTION > WHEN OTHERS THEN > GET STACKED DIAGNOSTICS col_name = COLUMN_NAME, sql_state = > RETURNED_SQLSTATE ...etc... > RAISE INFO '%, %, ......', col_name, sql_state, ......; > END; > END LOOP; > END; > > The only values I get back are: > RETURNED_SQLSTATE = 22003 > MESSAGE_TEXT = 'value "2156947514" is out of range for type integer > PG_EXCEPTION_CONTEXT = SQL statement > > The rest are null. I'm confused why your error message was more > informative. > > I tried leaving everything else out of the exception and just using a > bare RAISE like you said, but that just put the same exact error message > out to my Messages tab in pgAdmin-- no mention of any columns. > > Yeah I saw the below and did not pay enough attention to the actual error you posted. "The majority of the data can fit into integer fields, but occasionally I hit some entries that need to be text or bigint or floats." So as David said this is a parser error, though: postgres@test=# \d int_test Table "public.int_test" Column | Type | Modifiers ---------+---------+----------- int_fld | integer | Table "public.source_tbl" Column | Type | Modifiers --------+-------------------+----------- v_fld | character varying | postgres@test=# insert into int_test values ('2156947514'::int); ERROR: value "2156947514" is out of range for type integer LINE 1: insert into int_test values ('2156947514'::int); postgres@test=# insert into int_test select v_fld::int from source_tbl; ERROR: value "2156947514" is out of range for type integer postgres@test=# select '2156947514'::int; ERROR: value "2156947514" is out of range for type integer LINE 1: select '2156947514'::int; Only part of the error in the SELECT is pushed up to the INSERT error. The only thing I can think to do is pre-test the data in the CSV or the temp table for type, instead of letting the parser do it. One program I can point you at, assuming you are comfortable with Python, is ddl-generator: https://github.com/catherinedevlin/ddl-generator I have used it on smaller datasets(number of columns) then you are working on, so I can't say how it will scale to your case. -- 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