pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Achilleas Mantzios <achill@matrix.gatewaynet.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: PL/pgSQL question about EXCEPTION clause & corrupt records
Date: Fri, 14 Feb 2020 16:53:00 +0200
Message-ID: <e6e294b0-ef53-8315-e757-88241621b902@matrix.gatewaynet.com> (raw)
In-Reply-To: <4DF3093D-C58F-4DC5-BE22-49F4D9630594@arcict.com>
References: <4DF3093D-C58F-4DC5-BE22-49F4D9630594@arcict.com>

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 <somepkid> FROM f_gsxws_transaction where gwta_number = 762513 ORDER BY <somepkid> LOOP
     RAISE NOTICE 'examining row with <somepkid>= %',vid;
     select * into rcontent FROM f_gsxws_transaction where <somepkid> = vid;
     RAISE NOTICE 'content of row <somepkid>= % , 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






view thread (8+ messages)  latest in thread

Message-ID: <e6e294b0-ef53-8315-e757-88241621b902@matrix.gatewaynet.com>
Permalink:  ../e6e294b0-ef53-8315-e757-88241621b902@matrix.gatewaynet.com/
Also on:    postgresql.org/message-id/e6e294b0-ef53-8315-e757-88241621b902@matrix.gatewaynet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: achill@matrix.gatewaynet.com
  Subject: Re: PL/pgSQL question about EXCEPTION clause & corrupt records
  In-Reply-To: <e6e294b0-ef53-8315-e757-88241621b902@matrix.gatewaynet.com>
  References: <4DF3093D-C58F-4DC5-BE22-49F4D9630594@arcict.com> <e6e294b0-ef53-8315-e757-88241621b902@matrix.gatewaynet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox