Last active
March 13, 2021 00:44
-
-
Save monsha/e15cc0ee5277a01df4be67722d4b99c4 to your computer and use it in GitHub Desktop.
How to find corrupted records in postgresql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Find the corrupted rows | |
DO $$ | |
declare | |
curid INT := 0; | |
vcontent TEXT; | |
badid INT; | |
begin | |
FOR badid IN SELECT id FROM events LOOP | |
curid = curid + 1; | |
if curid % 100000 = 0 then | |
raise notice '% rows inspected', curid; | |
end if; | |
begin | |
SELECT message | |
INTO vcontent | |
FROM events where id = badid; | |
vcontent := substr(vcontent,100,2000); | |
exception | |
when others then | |
raise notice 'data for message % is corrupt', badid; | |
continue; | |
end; | |
end loop; | |
end; | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment