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 1gUZ2O-0000BT-Ki for pgsql-general@arkaria.postgresql.org; Wed, 05 Dec 2018 15:24:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUZ2N-0002ZZ-A3 for pgsql-general@arkaria.postgresql.org; Wed, 05 Dec 2018 15:24:31 +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 1gUZ11-0005db-0p for pgsql-general@lists.postgresql.org; Wed, 05 Dec 2018 15:23:07 +0000 Received: from hermes.gatewaynet.com ([193.92.121.98]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUZ0w-0007SN-9L for pgsql-general@lists.postgresql.org; Wed, 05 Dec 2018 15:23:05 +0000 Received: from smadev.internal.net (pc131.internal.net [10.9.200.131]) by hermes.gatewaynet.com (8.14.3/8.14.3/Debian-9.4) with ESMTP id wB5FMpEh030482 for ; Wed, 5 Dec 2018 17:22:56 +0200 Received: from smadev.internal.net (smadev [10.9.200.131]) by smadev.internal.net (8.15.2/8.15.2) with ESMTP id wB5FMprJ020133 for ; Wed, 5 Dec 2018 17:22:51 +0200 (EET) (envelope-from achill@matrix.gatewaynet.com) Subject: Re: Database corruption in cascaded replica, "pg_xact/003A" doesn't exist, reading as zeroes" To: pgsql-general@lists.postgresql.org References: From: Achilleas Mantzios Message-ID: <98f9b849-183c-4ef2-8cf9-d1efdd9d752c@matrix.gatewaynet.com> Date: Wed, 5 Dec 2018 17:22:51 +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: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Virus-Scanned: clamav-milter 0.98.7 at hermes X-Virus-Status: Clean X-Spam-Status: No, score=-22.2 required=5.0 tests=ALL_TRUSTED,DYN_HAM_COORD, DYN_HAM_IT,DYN_HAM_NAUTICAL,DYN_HAM_SIGNAT1,DYN_SPAM_SUBJ,DYN_SPAM_WATCHES8, SARE_SPEC_REPLICA_OBFU autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on hermes.gatewaynet.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/12/18 4:10 μ.μ., Dejan Petrovic wrote: > I believe this is a result of my "broken" procedure for setting up a cascaded replica. I would love to know where the issue is. > > This is a report of a database corruption which was detected after promoting a replica server to a master server. We suspect the actual corruption occurred during replication of a two-level cascaded > replica, however it was not detected until after it was promoted to master server when pg_dump and pg_restore failed. After re-checking postgresql logs on the corrupted node, we found this log line, > which was the only indicator of a possible issue, but was overlooked: "2018-07-25 13:14:42 UTCLOG: file "pg_xact/003A" doesn't exist, reading as zeroes". > > After extensive investigation, we detected three different cases of data corruption: > 1.)Issue with pg_toast - happens during pg_dump or when row with corrupted data is selected > "pg_dump: Dumping the contents of table failed: PQgetResult() failed. > pg_dump: Error message from server: ERROR: missing chunk number 2 for toast value 86123528 in pg_toast_41291242" > > 2.) Issue with uncommitted transactions, occurs when running VACUUM or pg_dump/pg_restore: > "ERROR: uncommitted xmin 60817551 from before xid cutoff 61487222 needs to be frozen > SQL state: XX001" > > 3.) 2 duplicated rows in different tables, violated PK unique constraint, occurs during pg_restore > > This is how we fixed the three issues: > 1.) Using bisection and SELECT, we found the row/column with corrupted data. We nulled the corrupt column and deleted the row (it was not critical, had no FKs) > 2.) We ran VACUUM on all tables to check where this issue occured and updated all rows between reported xmin and xid cutoff with same data - this generated a new xmin on the broken rows, which fixed > the issue. > 3.) We removed the duplicated rows in a transaction, disabled all triggers before and enabled them right after > 4.) Our final step is to do a full dump and restore on master DB so that the DB is rebuilt > > This is the procedure we used to replicate the cascaded replica (master -> replica -> replica) > > Notes: > Machines are running on Centos 7, Postgresql 10.2 > DB-1 = master > DB-2 = replica of DB-1 > DB-3 = replica of DB-2 > > --------------> DB-3 (new cascaded replica) > systemctl disable postgresql-10 > systemctl stop postgresql-10 > --------------> DB-1 (master) > psql -U postgres > select pg_start_backup('clone',true); > <------------- DB-3 (cascaded replica) > rsync -azv -e --delete DB-2:/isl/pgsql10/data /isl/pgsql10/ --exclude pg_wal --exclude postgresql.pid > assert rsync finished > vi recovery.conf >     standby_mode = 'on' >     primary_conninfo = 'host=DB-2' >     recovery_target_timeline = 'latest' In addition to what Stephen wrote you, from : https://www.postgresql.org/docs/10/continuous-archiving.html the low level backup method is relying on : "1. Ensure that WAL archiving is enabled and working." Have you setup this correctly? Also there should at least be a restore_command inside your recovery.conf . Otherwise how do you know that DB-2 has the WALs you need? Whereas with your WALs archived it is guaranteed that the backup is consistent. But anyway, its better to either use pg_basebackup or invest in some higher level tool like Stephen said. > -------------> DB-1 > select pg_stop_backup(); > <------------- DB-3 > rsync -azv -e --delete DB-2:/isl/pgsql10/data/pg_wal /isl/pgsql10/data/ > systemctl start postgresql-10 > systemctl enable postgresql-10 > tail log file > > Any comments regarding the cascaded replication procedure or database corruption detection or resolution are welcome. > > Best regards, > > Dejan Petrovic > ISL Online > > -- Achilleas Mantzios IT DEV Lead IT DEPT Dynacom Tankers Mgmt