Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YsE1E-0006DM-T4 for pgsql-committers@arkaria.postgresql.org; Tue, 12 May 2015 17:31:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YsE1D-0006Ms-IJ for pgsql-committers@arkaria.postgresql.org; Tue, 12 May 2015 17:30:59 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YsE1A-0006Mj-PC for pgsql-committers@postgresql.org; Tue, 12 May 2015 17:30:57 +0000 Received: from mail-wi0-x22f.google.com ([2a00:1450:400c:c05::22f]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1YsE17-0002dc-Kr for pgsql-committers@postgresql.org; Tue, 12 May 2015 17:30:55 +0000 Received: by widdi4 with SMTP id di4so164118750wid.0 for ; Tue, 12 May 2015 10:30:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=sO4QSrYmbhQL0OoaNqqdM/MXD9nTtbkcpuMduMZikJQ=; b=xnWtSIFJDxRboRfVRvOK+oKRAMLtBuJ+beSAuvC1/a84b9YVEhHuLu6UjL61Z9mZYM 1+iaa0TOXETHiDxD0+SfKJCeefhuE7f1dc1gXhSBtn2oXR1cXtgn58OWKMEK97CIuQea 292Lx8CfRPuTG6p60opolaL246ykDqvAgmSfP7MXyvDW64N0GZwVIaV61YStCOShE2K+ 20M/v4BFxv/z3pXX++06EtU8Leo2jtzlctHXwW4Pix/Clhj9hCJybtKu/3aeTfJLpajq mEhZCYqVf5BDA+BgYZRNAk13/792OfTIUyrvpC//+vYaOam8pMz9LjVKzKpByIIBr+vq TOHQ== MIME-Version: 1.0 X-Received: by 10.181.13.144 with SMTP id ey16mr32166295wid.38.1431451851487; Tue, 12 May 2015 10:30:51 -0700 (PDT) Received: by 10.28.174.18 with HTTP; Tue, 12 May 2015 10:30:51 -0700 (PDT) In-Reply-To: <55521D27.3010705@dunslane.net> References: <55520F33.401@iki.fi> <55521D27.3010705@dunslane.net> Date: Tue, 12 May 2015 23:00:51 +0530 Message-ID: Subject: Re: pgsql: Map basebackup tablespaces using a tablespace_map file From: Amit Kapila To: Andrew Dunstan Cc: hlinnaka , pgsql-committers@postgresql.org Content-Type: multipart/alternative; boundary=f46d043c804c1f01100515e5dbab X-Pg-Spam-Score: -2.4 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-committers Precedence: bulk Sender: pgsql-committers-owner@postgresql.org --f46d043c804c1f01100515e5dbab Content-Type: text/plain; charset=UTF-8 On Tue, May 12, 2015 at 9:02 PM, Andrew Dunstan wrote: > > On 05/12/2015 10:33 AM, Heikki Linnakangas wrote: > >> On 05/12/2015 04:42 PM, Andrew Dunstan wrote: >> >>> + >>> + /* >>> + * Remove the existing symlink if any and Create the >>> symlink >>> + * under PGDATA. We need to use rmtree instead of rmdir >>> as >>> + * the link location might contain directories or files >>> + * corresponding to the actual path. Some tar utilities >>> do >>> + * things that way while extracting symlinks. >>> + */ >>> + if (lstat(linkloc, &st) == 0 && S_ISDIR(st.st_mode)) >>> + { >>> + if (!rmtree(linkloc,true)) >>> + ereport(ERROR, >>> + (errcode_for_file_access(), >>> + errmsg("could not remove directory >>> \"%s\": %m", >>> + linkloc))); >>> + } >>> + else >>> + { >>> + if (unlink(linkloc) < 0 && errno != ENOENT) >>> + ereport(ERROR, >>> + (errcode_for_file_access(), >>> + errmsg("could not remove symbolic link >>> \"%s\": %m", >>> + linkloc))); >>> + } >>> + >>> >> >> That's scary. What tar utilitiy replaces the symlink with a non-empty >> directory? >> >> IIRC, it was star utility by using --copysymlinks options. It will actually copy the symlink data at appropriate location, but will not maintain symlink after extraction. I don't have a link handly for it, but can again search for it and send you if you want to have a look. > What if you call pg_start_backup() and take the backup with a utility that >> follows symlinks? I wouldn't recommend using such a tool, but with this >> patch, it will zap all the tablespaces. Before this, you at least got a >> working database and could read out all the data or fix the symlinks >> afterwards. >> >> Yeah, but I don't think user will do such a thing without being aware of the same and if he is aware, he will either restore the symlinks before starting the server or would atleast keep a copy of such a backup until he is able to restore the database completely. Do you think adding a note in docs makes sense? > Why is the RelationCacheInitFileRemove() call moved? >> > > I will let Amit comment on this. > > Because it assumes that tablespace directory pg_tblspc is all in place and it tries to remove the files by reading pg_tblspc directory as well. Now as we setup the symlinks in pg_tblspc after reading tablespace_map file, so we should remove relcache init file once the symlinks are setup in pg_tblspc directory. > >> XLogRecPtr >>> do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID >>> *starttli_p, >>> - char **labelfile) >>> + char **labelfile, DIR *tblspcdir, List **tablespaces, >>> + char **tblspcmapfile, bool infotbssize, >>> + bool needtblspcmapfile) >>> >> >> Why does this need to take tblspcdir as argument? > > It needs to be called from perform_base_backup() which already has access to tblspcdir. > Wouldn't it make more sense to open the directory inside >> do_pg_start_backup? >> > > > Good point. It does look cleaner to do that. > > If you prefer that way, I can look into rearranging the code. > > >> In the BASE_BACKUP command, is there any reason to not always include the >> tablespace map? > > The main reason is that it is not required to restore symlinks for plain format. Also I think it can cause a problem if user has used --tablespace-mapping option in plain format as the tablespace_map file doesn't contain information for same and recovery will try to restore symlinks for wrong path based on tablespace_map file. With Regards, Amit Kapila. EnterpriseDB: http://www.enterprisedb.com --f46d043c804c1f01100515e5dbab Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On T= ue, May 12, 2015 at 9:02 PM, Andrew Dunstan <andrew@dunslane.net&g= t; wrote:

On 05/12/2015 10:33 AM, Heikki Linnakangas wrote:
On 05/12/2015 04:42 PM, Andrew Dunstan wrote:
+
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/*
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * Remove the exist= ing symlink if any and Create the symlink
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * under PGDATA.=C2= =A0 We need to use rmtree instead of rmdir as
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * the link locatio= n might contain directories or files
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * corresponding to= the actual path. Some tar utilities do
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * things that way = while extracting symlinks.
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (lstat(linkloc, = &st) =3D=3D 0 && S_ISDIR(st.st_mode))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0{
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (!= rmtree(linkloc,true))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0ereport(ERROR,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(errcode_for_file_access(),
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 errmsg("could not remove direct= ory \"%s\": %m",
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0linkloc))= );
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0else
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0{
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (u= nlink(linkloc) < 0 && errno !=3D ENOENT)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0ereport(ERROR,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(errcode_for_file_access(),
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 errmsg("could not remove symbol= ic link \"%s\": %m",
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0linkloc))= );
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
+

That's scary. What tar utilitiy replaces the symlink with a non-empty d= irectory?


IIRC, it was star ut= ility by using --copysymlinks options.
It will actually copy the = symlink data at appropriate location,
but will not maintain symli= nk after extraction.
I don't have a link handly for it, but c= an again search for
it and send you if you want to have a look.
=C2=A0
What if you call pg_start_backup() and take the backup with a utility that = follows symlinks? I wouldn't recommend using such a tool, but with this= patch, it will zap all the tablespaces. Before this, you at least got a wo= rking database and could read out all the data or fix the symlinks afterwar= ds.



Yeah,= but I don't think user will do such a thing without
being=C2= =A0aware of the same and if he is aware, he will either
restore t= he=C2=A0symlinks before starting the server or would
atleast keep= a copy=C2=A0of such a backup until he is able to
restore the dat= abase completely.

Do you think adding = a note in docs makes sense?
=C2=A0
Why is the RelationCacheInitFileRemove() call moved?

I will let Amit comment on this.


Because it assumes that tablespac= e directory pg_tblspc is all in
place and it tries to remove the = files by reading pg_tblspc
directory as well.=C2=A0 Now as we set= up the symlinks in pg_tblspc
after reading tablespace_map file, s= o we should remove relcache init
file once the symlinks are setup= in pg_tblspc directory.
=C2=A0

=C2=A0XLogRecPtr
=C2=A0do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *st= arttli_p,
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 char **labe= lfile)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 char **labe= lfile, DIR *tblspcdir, List **tablespaces,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 char **tbls= pcmapfile, bool infotbssize,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 bool needtb= lspcmapfile)

Why does this need to take tblspcdir as argument?

It needs to be called from perform_base_backup() = which already
has access to tblspcdir.

=
=C2=A0
Wouldn't it make m= ore sense to open the directory inside do_pg_start_backup?

=C2=A0
Good point. It does look cleaner to do that.


If you prefer that way, I can loo= k into rearranging the
code.

=C2= =A0


In the BASE_BACKUP command, is there any reason to not always include the t= ablespace map?

The main= reason is that it is not required to restore symlinks
for = plain format.=C2=A0 Also I think it can cause a problem if user
has used --tablespace-mapping option in plain format as the
tablespace_map file doesn't contain information for same and reco= very
will try to restore symlinks for wrong path based on t= ablespace_map
file.



With Regards,
Amit Kapila.
EnterpriseDB:=C2= =A0http://www.en= terprisedb.com
--f46d043c804c1f01100515e5dbab--