Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j2aFY-0005yl-7K for pgsql-general@arkaria.postgresql.org; Fri, 14 Feb 2020 12:39:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j2aFX-0000OR-2p for pgsql-general@arkaria.postgresql.org; Fri, 14 Feb 2020 12:39:15 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1j2aFW-0000OK-PK for pgsql-general@lists.postgresql.org; Fri, 14 Feb 2020 12:39:14 +0000 Received: from mail.arcict.com ([91.183.106.63] helo=arcict.com) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1j2aFT-0000Jw-HL for pgsql-general@postgresql.org; Fri, 14 Feb 2020 12:39:14 +0000 Received: from [192.163.1.153] ([192.163.1.153] verified) by arcict.com (CommuniGate Pro SMTP 6.2.12) with ESMTP id 4542788 for pgsql-general@postgresql.org; Fri, 14 Feb 2020 13:39:10 +0100 From: "Nick Renders" To: pgsql-general Subject: PL/pgSQL question about EXCEPTION clause & corrupt records Date: Fri, 14 Feb 2020 13:39:10 +0100 X-Mailer: MailMate (1.13.1r5676) Message-ID: <4DF3093D-C58F-4DC5-BE22-49F4D9630594@arcict.com> MIME-Version: 1.0 Content-Type: text/plain; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, We recently suffered a database crash which resulted in some corrupt records. I thought I would write a little PL script that would loop through all the data and report any inconsistencies. However, I can't get it to work properly. For instance, if I run the following statement in pgAdmin: SELECT * FROM f_gsxws_transaction WHERE gwta_number = 762513 I get the following message: ERROR: missing chunk number 0 for toast value 8289525 in pg_toast_5572299 So, as a test, I created a function that would just retrieve that one record: DECLARE rcontent f_gsxws_transaction%ROWTYPE; BEGIN SELECT * INTO rcontent FROM f_gsxws_transaction where gwta_number = 762513; RETURN rcontent; EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'Record 762513 is corrupt'; END; Now, when I run this function, I have noticed two things: 1) The function has no problem executing the SELECT statement. It is only when "rcontents" is returned, that the function fails. This is a problem, because the ultimate goal is to loop through all records and only return/alert something in case of an error. 2) The function never enters the EXCEPTION clause. Instead, when it hits the RETURN command, it breaks and shows the same message as in pgAdmin: missing chunk number 0 for toast value 8289525 in pg_toast_5572299. Is it possible to check for these kind of errors with a PL script? Or is there perhaps a better way to check for corrupt records in a database? Best regards, Nick Renders