Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nclTz-0003z7-27 for pgsql-general@arkaria.postgresql.org; Fri, 08 Apr 2022 10:04:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nclTx-0007Nf-Sa for pgsql-general@arkaria.postgresql.org; Fri, 08 Apr 2022 10:04:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nclQp-0008Mw-J4 for pgsql-general@lists.postgresql.org; Fri, 08 Apr 2022 10:01:31 +0000 Received: from mail-oi1-x22e.google.com ([2607:f8b0:4864:20::22e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nclQk-0000X3-Jr for pgsql-general@lists.postgresql.org; Fri, 08 Apr 2022 10:01:30 +0000 Received: by mail-oi1-x22e.google.com with SMTP id 12so8280005oix.12 for ; Fri, 08 Apr 2022 03:01:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to; bh=Up/jCA3sZgmL9a84lUJ6rVS+ACQdY2zuYZnGIr4/SrI=; b=bJigwt/1RseiGPd44N6JYkknluPNDDfxAZ+DXQwxYR5e9C+haDI/dVoA9EA36rOZ3b aqMXabZU4UbFSYTth5YFV4+qNKw1Xn4XMUujjRIgq34eP7mZHkG6BUTx2YNwz8qgXuvX SHLXCRk9PtcYpgIT7xOMq+QxgU0xM/JVu4AGwsgJ6+fUvMGMPnZ/hxelcZRwkmuTqWZ6 PP9RyjnkwPd8/+YpJ9IGxtRgbLlufeWEc3QPBzwT1lB76tifZ+37XQsgx/P5GFP3hq8h YcODij1cmv1rHm9o6bcYenA41rjkxy8OjmeO0D0yx01KR8SwsBChVXNOHbXzUqOYvbDC OC6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to; bh=Up/jCA3sZgmL9a84lUJ6rVS+ACQdY2zuYZnGIr4/SrI=; b=u7ZcUpXGejFgB8MARoEaSLE5EeTJwKOBgNCb0c55F8BZL2aaOP8sq9vxHz5Mx7SuIQ IiC+3/GyGlllCijY9VVvtdG7aAt0EDC1ujyRNBkCD2DknXV1hmsTOas4l4xQK8IaQrgq F7cjeWRaeYB/qpEH6CVIfOTWemdf811PQ2qtEmhtdj4BZHNIMBH183RvhyeCluYfj5gV v6SSwMje8VV4F5qkVdedH/82ip4epdtPnhFUGsdH+YCHM/0Cw3MVQViKN5qaGXtO1U3L uZN064Xm/6T5DqhU0WM9m95sZWU9fb2itlypIQUGyaCA6/Ff3QcSnROxFwiWCXGqoIE4 KdMg== X-Gm-Message-State: AOAM530W9ihtVgUxyN8rAoj4QW0ORd7gkZLXp4L+LV3jj5ijl45J86ZI vlOSo+LnyHSgnTBAwjoYL2RT4IInsxs= X-Google-Smtp-Source: ABdhPJzxWd8iNMipi11/hi5f3rg/CYDmr4ceQv+nN/k5sTOs4QQJZ7vs6rP58Y1Yy9CDYZo+HwVKWA== X-Received: by 2002:a05:6808:1302:b0:2da:84f6:9eef with SMTP id y2-20020a056808130200b002da84f69eefmr7403873oiv.72.1649412085594; Fri, 08 Apr 2022 03:01:25 -0700 (PDT) Received: from [192.168.88.10] (ip68-11-68-85.no.no.cox.net. [68.11.68.85]) by smtp.googlemail.com with ESMTPSA id j4-20020a9d7384000000b005b23499b66dsm9071641otk.23.2022.04.08.03.01.24 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 08 Apr 2022 03:01:25 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------686QEWcmAr007TDuiCsvZcGU" Message-ID: <1c582b7b-e6df-e7ff-26b2-1571378858e8@gmail.com> Date: Fri, 8 Apr 2022 05:01:24 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: Could not read block 0 in file Content-Language: en-US To: pgsql-general@lists.postgresql.org References: <5D839689-C2C9-437E-AD6B-CD744AC3F489@icloud.com> From: Ron In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------686QEWcmAr007TDuiCsvZcGU Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 4/8/22 04:54, Magnus Hagander wrote: > > > On Fri, Apr 8, 2022 at 11:06 AM wrote: > > Hi, > > While we are looking for a suitable backup to recover from, I hope > this community may have some other advice on forward steps in case we > cannot restore. > > RCA: Unexpected shutdown due to critical power failure > > Current Issue: The file base/16509/17869 is zero bytes in size. > > Additional Information: > Platform: Windows Server > PostGres Version: 10.16 (64-bit) > > The database does start, and is otherwise functioning and working > aside from a particular application feature that relies on the lookup > of the values in the table that was held in the currently zero-bytes > data file. > > The non-functioning table (ApprovalStageDefinition) is a relatively > simple table with 5 rows of static data. The contents can easily be > recovered with a query such as the following for each of the 5 records: > insert into ApprovalStageDefinition values (1, 'Stage One', 'Stage One'); > > The error message when running this query is: > ERROR:  could not read block 0 in file "base/16509/17869": read only 0 > of 8192 bytes > > The file does exist on the file system, with zero bytes, as do the > associated fsm and vm files. > > PostGres does allow us to describe the table: >  \d ApprovalStageDefinition; >          Table "public.approvalstagedefinition" >     Column       |  Type  | Collation | Nullable | Default > -------------------+--------+-----------+----------+--------- >  stageid           | bigint |           | not null | >  stagename         | citext |           | not null | >  internalstagename | citext |           | not null | > Indexes: >   "approvalstagedef_pk" PRIMARY KEY, btree (stageid) >   "approvalstagedefinition_uk1" UNIQUE CONSTRAINT, btree (stagename) >   "approvalstagedefinition_uk2" UNIQUE CONSTRAINT, btree > (internalstagename) > Check constraints: >   "approvalstagedefinition_internalstagename_c" CHECK > (length(internalstagename::text) <= 100) >   "approvalstagedefinition_stagename_c" CHECK (length(stagename::text) > <= 100) > Referenced by: >   TABLE "approvaldetails" CONSTRAINT "approvaldetails_fk5" FOREIGN KEY > (stageid) REFERENCES approvalstagedefinition(stageid) ON DELETE CASCADE >   TABLE "currentapprovalstage" CONSTRAINT "currentapprovalst_fk1" > FOREIGN KEY (stageid) REFERENCES approvalstagedefinition(stageid) ON > DELETE CASCADE >   TABLE "serviceapprovermapping" CONSTRAINT > "serviceapprovermapping_fk4" FOREIGN KEY (stageid) REFERENCES > approvalstagedefinition(stageid) ON DELETE CASCADE > > Desired Solution: > A way to recreate the data file based on the existing schema so that > we can then insert the required records. > > Challenges/Apprehensions: > I am a PostGres novice, and reluctant to try dropping the table and > recreating it based on the existing schema as I don’t know what else > it may break, especially with regards to foreign keys and references. > > Any constructive advice would be appreciated. > > > As the file is zero bytes, there is no data to recover at the PostgreSQL > level unless you have backups. Your filesystem lost the content of it, so > any recovery must be done at the file system or storage level.  PostgreSQL > can do nothing about a zero bytes file. > > The only real option here is to restore from backup, unless you have some > operating system/storage expert at hand who can recover the file from the > filesystem for you. Could OP drop the constraints, drop the table and then recreate the table, indices and constraints? > > You can drop and recreate the table, but since your filesystem has already > lost data what's to say it hasn't corrupted other parts of the database as > well? And as you note, since this is underlying storage corruption > PostgreSQL will not be able to do anything about foreign keys etc. You > will have to verify all those manually. If you do trust the rest of the > system (with some reason), drop the three foreign keys, drop and recreate > the table, and then re-add the foreign keys. But having had this type of > fundamental disk corruption, I personally wouldn't trust the rest of the > contents. > > If you end up not actually having any backups, I'd suggest: > 1. Drop the table > 2. pg_dump what's there > 3. Re-initialize a new database from initdb (I would also say create a > completely new filesystem underneath it since that's where the corruption > is, if that's easily done) > 4. Restore the pg_dump. At this point it will throw errors on any foreign > keys that are "off", and you will have to clean that up manually. > > You should also make sure to apply the latest patches for your PostgreSQL > bringing it to version 10.20. But there are AFAIK no bugs in any of those > minors that would cause this type of corruption, so not being properly > updated is not the root cause of your issue. -- Angular momentum makes the world go 'round. --------------686QEWcmAr007TDuiCsvZcGU Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit On 4/8/22 04:54, Magnus Hagander wrote:


On Fri, Apr 8, 2022 at 11:06 AM <friend.have_00@icloud.com> wrote:
Hi,

While we are looking for a suitable backup to recover from, I hope this community may have some other advice on forward steps in case we cannot restore.

RCA: Unexpected shutdown due to critical power failure

Current Issue: The file base/16509/17869 is zero bytes in size.

Additional Information:
Platform: Windows Server
PostGres Version: 10.16 (64-bit)

The database does start, and is otherwise functioning and working aside from a particular application feature that relies on the lookup of the values in the table that was held in the currently zero-bytes data file.

The non-functioning table (ApprovalStageDefinition) is a relatively simple table with 5 rows of static data. The contents can easily be recovered with a query such as the following for each of the 5 records:
insert into ApprovalStageDefinition values (1, 'Stage One', 'Stage One');

The error message when running this query is:
ERROR:  could not read block 0 in file "base/16509/17869": read only 0 of 8192 bytes

The file does exist on the file system, with zero bytes, as do the associated fsm and vm files.

PostGres does allow us to describe the table:
 \d ApprovalStageDefinition;
           Table "public.approvalstagedefinition"
      Column       |  Type  | Collation | Nullable | Default
-------------------+--------+-----------+----------+---------
 stageid           | bigint |           | not null |
 stagename         | citext |           | not null |
 internalstagename | citext |           | not null |
Indexes:
    "approvalstagedef_pk" PRIMARY KEY, btree (stageid)
    "approvalstagedefinition_uk1" UNIQUE CONSTRAINT, btree (stagename)
    "approvalstagedefinition_uk2" UNIQUE CONSTRAINT, btree (internalstagename)
Check constraints:
    "approvalstagedefinition_internalstagename_c" CHECK (length(internalstagename::text) <= 100)
    "approvalstagedefinition_stagename_c" CHECK (length(stagename::text) <= 100)
Referenced by:
    TABLE "approvaldetails" CONSTRAINT "approvaldetails_fk5" FOREIGN KEY (stageid) REFERENCES approvalstagedefinition(stageid) ON DELETE CASCADE
    TABLE "currentapprovalstage" CONSTRAINT "currentapprovalst_fk1" FOREIGN KEY (stageid) REFERENCES approvalstagedefinition(stageid) ON DELETE CASCADE
    TABLE "serviceapprovermapping" CONSTRAINT "serviceapprovermapping_fk4" FOREIGN KEY (stageid) REFERENCES approvalstagedefinition(stageid) ON DELETE CASCADE

Desired Solution:
A way to recreate the data file based on the existing schema so that we can then insert the required records.

Challenges/Apprehensions:
I am a PostGres novice, and reluctant to try dropping the table and recreating it based on the existing schema as I don’t know what else it may break, especially with regards to foreign keys and references.

Any constructive advice would be appreciated.


As the file is zero bytes, there is no data to recover at the PostgreSQL level unless you have backups. Your filesystem lost the content of it, so any recovery must be done at the file system or storage level.  PostgreSQL can do nothing about a zero bytes file.

The only real option here is to restore from backup, unless you have some operating system/storage expert at hand who can recover the file from the filesystem for you.

Could OP drop the constraints, drop the table and then recreate the table, indices and constraints?


You can drop and recreate the table, but since your filesystem has already lost data what's to say it hasn't corrupted other parts of the database as well? And as you note, since this is underlying storage corruption PostgreSQL will not be able to do anything about foreign keys etc. You will have to verify all those manually. If you do trust the rest of the system (with some reason), drop the three foreign keys, drop and recreate the table, and then re-add the foreign keys. But having had this type of fundamental disk corruption, I personally wouldn't trust the rest of the contents.

If you end up not actually having any backups, I'd suggest:
1. Drop the table
2. pg_dump what's there
3. Re-initialize a new database from initdb (I would also say create a completely new filesystem underneath it since that's where the corruption is, if that's easily done)
4. Restore the pg_dump. At this point it will throw errors on any foreign keys that are "off", and you will have to clean that up manually.

You should also make sure to apply the latest patches for your PostgreSQL bringing it to version 10.20. But there are AFAIK no bugs in any of those minors that would cause this type of corruption, so not being properly updated is not the root cause of your issue.

--
Angular momentum makes the world go 'round.
--------------686QEWcmAr007TDuiCsvZcGU--