Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bLSHD-0000mR-Oh for pgsql-bugs@arkaria.postgresql.org; Fri, 08 Jul 2016 09:40:52 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bLSHD-0007zW-C1 for pgsql-bugs@arkaria.postgresql.org; Fri, 08 Jul 2016 09:40:51 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bLSHC-0007yz-9A for pgsql-bugs@postgresql.org; Fri, 08 Jul 2016 09:40:50 +0000 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bLSH9-0002VT-3r for pgsql-bugs@postgresql.org; Fri, 08 Jul 2016 09:40:49 +0000 Received: by mail-wm0-x230.google.com with SMTP id n127so9433176wme.1 for ; Fri, 08 Jul 2016 02:40:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant.it; s=google; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to; bh=GdcJnPhoF1GXLg5p0de+ns6aclyawU60DjIB5Ae94IY=; b=jsrj/NDwybDF6BUIzsxsIFYVvICkXExHhSnTSBuSQEzg9OJPCeuoXWx1JOOnx4CK7r D/h1RtCPKKA2AeNP1H9PqPmMwEeuhPkOq/e0anmur4xKyVkk7h/znwFYxFmZx0CapbhR htZYXwgiL9vyDv85TEokGiPn1no/xLBtnMRoY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to; bh=GdcJnPhoF1GXLg5p0de+ns6aclyawU60DjIB5Ae94IY=; b=KcFOWO8YdrdtENLaJ3XMgiPUKd3V4YR7IABYcHc2oXMIYbfFsgSCW6c86IZB+61neo Qg00FTKH7tunuq0b/A8bgoIZwu5kzQIKyhSO1TbZ2CvUcJoQC7fs601aLgYfL6AwUcLU PWcTLaSPd8YSUI2Zp9HerHqLOftzHKn1K5btwSsy8C4RV4POBzK1odQSWZAoF9XcdvaG vktjVtni/K4An3VDPR00QJGQvZPW1PSPXeoG0BGTncavtDYuL1nPmZzTCyCD1T5sEpHu P/74pGCXWB5NR66kLIJ8s6F5t7ehGuxX+VMVDeqIijTQEgkAJ8P2lJ2fopop1tbbsewB 9bgA== X-Gm-Message-State: ALyK8tIR3kMsWAINx+1ixvQMWC4pTasEbSJd3Ib4JgJDdzzOyLwL4aMwXXzvB/19pjH2nIzD X-Received: by 10.28.107.23 with SMTP id g23mr2532292wmc.101.1467970845024; Fri, 08 Jul 2016 02:40:45 -0700 (PDT) Received: from titanium.local (net-37-182-231-222.cust.vodafonedsl.it. [37.182.231.222]) by smtp.googlemail.com with ESMTPSA id bh7sm6202782wjb.22.2016.07.08.02.40.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Jul 2016 02:40:44 -0700 (PDT) Subject: Re: BUG #14230: Wrong timeline returned by pg_stop_backup on a standby To: Michael Paquier References: <20160706150739.1426.84965@wrigleys.postgresql.org> Cc: francesco.canovai@2ndquadrant.it, PostgreSQL mailing lists , PostgreSQL-development , Magnus Hagander From: Marco Nenciarini Message-ID: Date: Fri, 8 Jul 2016 11:40:39 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="G9WVEWuSGulhUnRjpxXgiX1dC1qmnxJeu" X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --G9WVEWuSGulhUnRjpxXgiX1dC1qmnxJeu Content-Type: multipart/mixed; boundary="GLnaAHwXVc6fBA9HFC29KSiarleRt5Xpf" From: Marco Nenciarini To: Michael Paquier Cc: francesco.canovai@2ndquadrant.it, PostgreSQL mailing lists , PostgreSQL-development , Magnus Hagander Message-ID: Subject: Re: [BUGS] BUG #14230: Wrong timeline returned by pg_stop_backup on a standby References: <20160706150739.1426.84965@wrigleys.postgresql.org> In-Reply-To: --GLnaAHwXVc6fBA9HFC29KSiarleRt5Xpf Content-Type: multipart/mixed; boundary="------------AAC94E87080C54522FFC851F" This is a multi-part message in MIME format. --------------AAC94E87080C54522FFC851F Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 07/07/16 08:38, Michael Paquier wrote: > On Thu, Jul 7, 2016 at 12:57 AM, Marco Nenciarini > wrote: >> After further analysis, the issue is that we retrieve the starttli fro= m >> the ControlFile structure, but it was using ThisTimeLineID when writin= g >> the backup label. >> >> I've attached a very simple patch that fixes it. >=20 > ThisTimeLineID is always set at 0 on purpose on a standby, so we > cannot rely on it (well it is set temporarily when recycling old > segments). At recovery when parsing the backup_label file there is no > actual use of the start segment name, so that's only a cosmetic > change. But surely it would be better to get that fixed, because > that's useful for debugging. >=20 > While looking at your patch, I thought that it would have been > tempting to use GetXLogReplayRecPtr() to get the timeline ID when in > recovery, but what we really want to know here is the timeline of the > last REDO pointer, which is starttli, and that's more consistent with > the fact that we use startpoint when writing the backup_label file. In > short, +1 for this fix. >=20 > I am adding that in the list of open items, adding Magnus in CC whose > commit for non-exclusive backups is at the origin of this defect. >=20 While we were testing the patch we noticed another behavior that is not strictly a bug, but can confuse backup tools: To quickly produce some WAL files we were executing a series of pg_switch_xlog+CHECKPOINT, and we noticed that doing a backup from a standby after that results in a startpoint higher than the stoppoint. Let me show it on a brand new master/replica cluster (master is port 5496, replica is 6496). The script is attached. ------------------------------------------------------------------- You are now connected to database "postgres" as user "postgres" via socket in "/tmp" at port "5496". SELECT pg_is_in_recovery(); -[ RECORD 1 ]-----+-- pg_is_in_recovery | f CHECKPOINT; CHECKPOINT SELECT pg_switch_xlog(); -[ RECORD 1 ]--+---------- pg_switch_xlog | 0/30000E8 CHECKPOINT; CHECKPOINT SELECT pg_switch_xlog(); -[ RECORD 1 ]--+---------- pg_switch_xlog | 0/40000E8 You are now connected to database "postgres" as user "postgres" via socket in "/tmp" at port "6496". SELECT pg_is_in_recovery(); -[ RECORD 1 ]-----+-- pg_is_in_recovery | t SELECT pg_start_backup('tst backup',TRUE,FALSE); -[ RECORD 1 ]---+---------- pg_start_backup | 0/4000028 SELECT * FROM pg_stop_backup(FALSE); -[ RECORD 1 ]------------------------------------------------------------= - lsn | 0/20000F8 labelfile | START WAL LOCATION: 0/4000028 (file 000000000000000000000004= )+ | CHECKPOINT LOCATION: 0/4000060 = + | BACKUP METHOD: streamed = + | BACKUP FROM: standby = + | START TIME: 2016-07-08 10:46:55 CEST = + | LABEL: tst backup = + | spcmapfile | SELECT * FROM pg_control_checkpoint(); -[ RECORD 1 ]--------+------------------------- checkpoint_location | 0/4000060 prior_location | 0/2000060 redo_location | 0/4000028 redo_wal_file | 000000010000000000000004 timeline_id | 1 prev_timeline_id | 1 full_page_writes | t next_xid | 0:865 next_oid | 12670 next_multixact_id | 1 next_multi_offset | 0 oldest_xid | 858 oldest_xid_dbid | 1 oldest_active_xid | 865 oldest_multi_xid | 1 oldest_multi_dbid | 1 oldest_commit_ts_xid | 865 newest_commit_ts_xid | 865 checkpoint_time | 2016-07-08 10:46:55+02 SELECT * FROM pg_control_recovery(); -[ RECORD 1 ]-----------------+---------- min_recovery_end_location | 0/20000F8 min_recovery_end_timeline | 1 backup_start_location | 0/0 backup_end_location | 0/0 end_of_backup_record_required | f ------------------------------------------------------------------- In particular, the pg_start_backup LSN is 0/4000028 and the pg_stop_backup LSN is 0/20000F8. The same issue is present when you do a backup using pg_basebackup: ------------------------------------------------------------------- transaction log start point: 0/8000028 on timeline 1 pg_basebackup: starting background WAL receiver 22244/22244 kB (100%), 1/1 tablespace transaction log end point: 0/20000F8 pg_basebackup: waiting for background process to finish streaming ... pg_basebackup: base backup completed ------------------------------------------------------------------- The resulting backup is working perfectly, because Postgres has no use for pg_stop_backup LSN, but this can confuse any tool that uses the stop LSN to figure out which WAL files are needed by the backup (in this case the only file needed is the one containing the start checkpoint). After some discussion with =C3=81lvaro, my proposal is to avoid that by returning the stoppoint as the maximum between the startpoint and the min_recovery_end_location, in case of backup from the standby. The patch is once again a very simple one line diff. I have attached both patches to this email, as in my opinion they should go together, because the subject is the same: avoid giving misleading information to backup tools. Regards, Marco --=20 Marco Nenciarini - 2ndQuadrant Italy PostgreSQL Training, Services and Support marco.nenciarini@2ndQuadrant.it | www.2ndQuadrant.it --------------AAC94E87080C54522FFC851F Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; name="test.sql" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="test.sql" XHgKXGNvbm5lY3QgLSAtIC0gNTQ5NgpTRUxFQ1QgcGdfaXNfaW5fcmVjb3ZlcnkoKTsKQ0hF Q0tQT0lOVDsKU0VMRUNUIHBnX3N3aXRjaF94bG9nKCk7CkNIRUNLUE9JTlQ7ClNFTEVDVCBw Z19zd2l0Y2hfeGxvZygpOwpcY29ubmVjdCAtIC0gLSA2NDk2ClNFTEVDVCBwZ19pc19pbl9y ZWNvdmVyeSgpOwpTRUxFQ1QgcGdfc3RhcnRfYmFja3VwKCd0c3QgYmFja3VwJyxUUlVFLEZB TFNFKTsKU0VMRUNUICogRlJPTSBwZ19zdG9wX2JhY2t1cChGQUxTRSk7ClNFTEVDVCAqIEZS T00gcGdfY29udHJvbF9jaGVja3BvaW50KCk7ClNFTEVDVCAqIEZST00gcGdfY29udHJvbF9y ZWNvdmVyeSgpOwo= --------------AAC94E87080C54522FFC851F Content-Type: text/x-patch; name="standby_backup_stop_point.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="standby_backup_stop_point.patch" diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/trans= am/xlog.c index e4645a3..6ee5712 100644 *** a/src/backend/access/transam/xlog.c --- b/src/backend/access/transam/xlog.c *************** do_pg_stop_backup(char *labelfile, bool=20 *** 10444,10450 **** =20 =20 LWLockAcquire(ControlFileLock, LW_SHARED); ! stoppoint =3D ControlFile->minRecoveryPoint; stoptli =3D ControlFile->minRecoveryPointTLI; LWLockRelease(ControlFileLock); =20 --- 10444,10450 ---- =20 =20 LWLockAcquire(ControlFileLock, LW_SHARED); ! stoppoint =3D Max(startpoint, ControlFile->minRecoveryPoint); stoptli =3D ControlFile->minRecoveryPointTLI; LWLockRelease(ControlFileLock); =20 --------------AAC94E87080C54522FFC851F Content-Type: text/x-patch; name="standby_backup_timeline.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="standby_backup_timeline.patch" diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/trans= am/xlog.c index 6ee5712..48d320a 100644 *** a/src/backend/access/transam/xlog.c --- b/src/backend/access/transam/xlog.c *************** do_pg_start_backup(const char *backupids *** 9974,9980 **** } while (!gotUniqueStartpoint); =20 XLByteToSeg(startpoint, _logSegNo); ! XLogFileName(xlogfilename, ThisTimeLineID, _logSegNo); =20 /* * Construct tablespace_map file --- 9974,9980 ---- } while (!gotUniqueStartpoint); =20 XLByteToSeg(startpoint, _logSegNo); ! XLogFileName(xlogfilename, starttli, _logSegNo); =20 /* * Construct tablespace_map file --------------AAC94E87080C54522FFC851F-- --GLnaAHwXVc6fBA9HFC29KSiarleRt5Xpf-- --G9WVEWuSGulhUnRjpxXgiX1dC1qmnxJeu Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQIcBAEBCAAGBQJXf3UaAAoJEFifA/AbpVA4j+YP/Rl2xAhVlqaPp0EliChVCRNK /fkHQJ4ibLG9p4JJOCDWSZayPMoJE5P0K5tJ9V833C2VozzvVNLqbt4O8On0t2J1 FVPoHsursv6aKA2kO83gwJEfPXcRgzy+gEQFSGnIb0dDaZpxusZDvkyg/uq9Hzrz x6EUMZqGXt5BHm3H4I4Jhej7aMs3uypwUQhcUXed3okEPQBlrR59MPcPt0IumWTR MezgBvGHeGu+uhBby/atPry/rNejXBoGp1DLATVupg5Ni5MlJiD71Fs+kAyIMqoR xWbvBPCA0xjQi4EwNJODgVhC/V5gIMGVG0HyyDB5TJd1leJHFUmdR3e0AJQBCPrQ zllQ44alvZHtL4eMnHY/ByA0jJOXA/sf0SL2KhS340Etxycz//+yiSPoPTvs1wxm kV/Hwg+8uKOiz1S7p5IiqBadBj5P4ibbcnPdV865PHT279GPZ/Rp6XqURQzSsNC7 VCoibA9nOkAzuThGnGiTYJ7fdcdQkHuL7EOtp8ZA/+D7///eiT1ybfESrA0LKZ3f 2pOUrVA23llIs1+w35eTx+DJZSvCvVRn6AxfhwUy23JPwopF1gX+U9lDsJypNwjJ dOOge/D808pn+JEmVv0LHDg116qr+EDAWj4Co+dNHYoi9b9IqakStES25pnccv9M LYDJi2lk5m16S6N+z5n+ =iSjR -----END PGP SIGNATURE----- --G9WVEWuSGulhUnRjpxXgiX1dC1qmnxJeu--