PL/pgSQL question about EXCEPTION clause & corrupt records

From: "Nick Renders" <postgres(at)arcict(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: PL/pgSQL question about EXCEPTION clause & corrupt records
Date: 2020-02-14 12:39:10
Message-ID: 4DF3093D-C58F-4DC5-BE22-49F4D9630594@arcict.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Achilleas Mantzios 2020-02-14 14:53:00 Re: PL/pgSQL question about EXCEPTION clause & corrupt records
Previous Message Adrian Klaver 2020-02-14 05:03:48 Re: Cannot connect to postgresql-11 from another machine after boot