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 1j2cLG-0001Vu-OA for pgsql-general@arkaria.postgresql.org; Fri, 14 Feb 2020 14:53:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j2cLE-00020B-Qa for pgsql-general@arkaria.postgresql.org; Fri, 14 Feb 2020 14:53:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j2cLE-000203-DQ for pgsql-general@lists.postgresql.org; Fri, 14 Feb 2020 14:53:16 +0000 Received: from hermes.gatewaynet.com ([193.92.121.98]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j2cL8-0006ez-5i for pgsql-general@lists.postgresql.org; Fri, 14 Feb 2020 14:53:15 +0000 Received: from smadev.internal.net (pc131.internal.net [10.9.200.131]) by hermes.gatewaynet.com (8.15.2/8.15.2/Debian-8) with ESMTPS id 01EEr0hS023585 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 14 Feb 2020 16:53:05 +0200 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id 01EEr0OX083783 for ; Fri, 14 Feb 2020 16:53:00 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: PL/pgSQL question about EXCEPTION clause & corrupt records To: pgsql-general@lists.postgresql.org References: <4DF3093D-C58F-4DC5-BE22-49F4D9630594@arcict.com> From: Achilleas Mantzios Message-ID: Date: Fri, 14 Feb 2020 16:53:00 +0200 User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:60.0) Gecko/20100101 Thunderbird/60.3.2 MIME-Version: 1.0 In-Reply-To: <4DF3093D-C58F-4DC5-BE22-49F4D9630594@arcict.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Virus-Scanned: clamav-milter 0.101.4 at hermes X-Virus-Status: Clean X-Spam-Status: No, score=-27.9 required=5.0 tests=ALL_TRUSTED,DYN_HAM_COORD, DYN_HAM_IT,DYN_HAM_NAUTICAL,DYN_HAM_SIGNAT1,TW_GW autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on hermes.gatewaynet.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 14/2/20 2:39 μ.μ., Nick Renders wrote: > > 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. Does the table have any PKs or UKs? do something like FOR vid IN SELECT FROM f_gsxws_transaction where gwta_number = 762513 ORDER BY LOOP     RAISE NOTICE 'examining row with = %',vid;     select * into rcontent FROM f_gsxws_transaction where = vid;     RAISE NOTICE 'content of row = % , is % ',vid,rcontent; END LOOP; > > > 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 > > -- Achilleas Mantzios IT DEV Lead IT DEPT Dynacom Tankers Mgmt