Thread

Commits

  1. Improve recently-added test reliability

  2. Fix new recovery test for log_error_verbosity=verbose case

  3. Fix test instability

  4. Fix replay of create database records on standby

  5. Allow "in place" tablespaces.

  6. Fix get_dirent_type() for Windows junction points.

  7. Revert "Fix replay of create database records on standby"

  8. Add end-to-end testing of pg_basebackup's tar-format output.

  9. Make DROP DATABASE command generate less WAL records.

  10. Consolidate methods for translating a Perl path to a Windows path.

  1. standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-04-17T07:56:30Z

    Hello postgres hackers,
    
    Recently my colleagues and I encountered an issue: a standby can
    not recover after an unclean shutdown and it's related to tablespace.
    The issue is that the standby re-replay some xlog that needs tablespace
    directories (e.g. create a database with tablespace),
    but the tablespace directories has already been removed in the
    previous replay.
    
    In details, the standby normally finishes replaying for the below
    operations, but due to unclean shutdown, the redo lsn
    is not updated in pg_control and is still kept a value before the 'create
    db with tabspace' xlog, however since the tablespace
    directories were removed so it reports error when repay the database create
    wal.
    
    create db with tablespace
    drop database
    drop tablespace.
    
    Here is the log on the standby.
    2019-04-17 14:52:14.926 CST [23029] LOG:  starting PostgreSQL 12devel on
    x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat
    4.8.5-4), 64-bit
    2019-04-17 14:52:14.927 CST [23029] LOG:  listening on IPv4 address
    "192.168.35.130", port 5432
    2019-04-17 14:52:14.929 CST [23029] LOG:  listening on Unix socket
    "/tmp/.s.PGSQL.5432"
    2019-04-17 14:52:14.943 CST [23030] LOG:  database system was interrupted
    while in recovery at log time 2019-04-17 14:48:27 CST
    2019-04-17 14:52:14.943 CST [23030] HINT:  If this has occurred more than
    once some data might be corrupted and you might need to choose an earlier
    recovery target.
    2019-04-17 14:52:14.949 CST [23030] LOG:  entering standby mode
    
    2019-04-17 14:52:14.950 CST [23030] LOG:  redo starts at 0/30105B8
    
    2019-04-17 14:52:14.951 CST [23030] FATAL:  could not create directory
    "pg_tblspc/65546/PG_12_201904072/65547": No such file or directory
    2019-04-17 14:52:14.951 CST [23030] CONTEXT:  WAL redo at 0/3011650 for
    Database/CREATE: copy dir 1663/1 to 65546/65547
    2019-04-17 14:52:14.951 CST [23029] LOG:  startup process (PID 23030)
    exited with exit code 1
    2019-04-17 14:52:14.951 CST [23029] LOG:  terminating any other active
    server processes
    2019-04-17 14:52:14.953 CST [23029] LOG:  database system is shut down
    
    
    Steps to reprodce:
    
    1. setup a master and standby.
    2. On both side, run: mkdir /tmp/some_isolation2_pg_basebackup_tablespace
    
    3. Run SQLs:
    drop tablespace if exists some_isolation2_pg_basebackup_tablespace;
    create tablespace some_isolation2_pg_basebackup_tablespace location
    '/tmp/some_isolation2_pg_basebackup_tablespace';
    
    3. Clean shutdown and restart both postgres instances.
    
    4. Run the following SQLs:
    
    drop database if exists some_database_with_tablespace;
    create database some_database_with_tablespace tablespace
    some_isolation2_pg_basebackup_tablespace;
    drop database some_database_with_tablespace;
    drop tablespace some_isolation2_pg_basebackup_tablespace;
    \! pkill -9 postgres; ssh host70 pkill -9 postgres
    
    Note immediate shutdown via pg_ctl should also be able to reproduce and the
    above steps probably does not 100% reproduce.
    
    I created an initial patch for this issue (see the attachment). The idea is
    re-creating those directories recursively. The above issue exists
    in dbase_redo(),
    but TablespaceCreateDbspace (for relation file create redo) is probably
    buggy also so I modified that function also. Even there is no bug
    in that function, it seems that using simple pg_mkdir_p() is cleaner. Note
    reading TablespaceCreateDbspace(), I found it seems that this issue
    has already be thought though insufficient but frankly this solution
    (directory recreation) seems to be not perfect given actually this should
    have been the responsibility of tablespace creation (also tablespace
    creation does more like symlink creation, etc). Also, I'm not sure whether
    we need to use invalid page mechanism (see xlogutils.c).
    
    Another solution is that, actually, we create a checkpoint when
    createdb/movedb/dropdb/droptablespace, maybe we should enforce to create
    restartpoint on standby for such special kind of checkpoint wal - that
    means we need to set a flag in checkpoing wal and let checkpoint redo
    code to create restartpoint if that flag is set. This solution seems to be
    safer.
    
    Thanks,
    Paul
    
  2. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    P <apraveen@pivotal.io> — 2019-04-19T04:38:04Z

    On Wed, Apr 17, 2019 at 1:27 PM Paul Guo <pguo@pivotal.io> wrote:
    >
    > create db with tablespace
    > drop database
    > drop tablespace.
    
    Essentially, that sequence of operations causes crash recovery to fail
    if the "drop tablespace" transaction was committed before crashing.
    This is a bug in crash recovery in general and should be reproducible
    without configuring a standby.  Is that right?
    
    Your patch creates missing directories in the destination.  Don't we
    need to create the tablespace symlink under pg_tblspc/?  I would
    prefer extending the invalid page mechanism to deal with this, as
    suggested by Ashwin off-list.  It will allow us to avoid creating
    directories and files only to remove them shortly afterwards when the
    drop database and drop tablespace records are replayed.
    
    Asim
    
    
    
    
  3. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-04-22T04:36:43Z

    Please see my replies inline. Thanks.
    
    On Fri, Apr 19, 2019 at 12:38 PM Asim R P <apraveen@pivotal.io> wrote:
    
    > On Wed, Apr 17, 2019 at 1:27 PM Paul Guo <pguo@pivotal.io> wrote:
    > >
    > > create db with tablespace
    > > drop database
    > > drop tablespace.
    >
    > Essentially, that sequence of operations causes crash recovery to fail
    > if the "drop tablespace" transaction was committed before crashing.
    > This is a bug in crash recovery in general and should be reproducible
    > without configuring a standby.  Is that right?
    >
    
    No. In general, checkpoint is done for drop_db/create_db/drop_tablespace on
    master.
    That makes the file/directory update-to-date if I understand the related
    code correctly.
    For standby, checkpoint redo does not ensure that.
    
    
    >
    > Your patch creates missing directories in the destination.  Don't we
    > need to create the tablespace symlink under pg_tblspc/?  I would
    >
    
     'create db with tablespace' redo log does not include the tablespace real
    directory information.
    Yes, we could add in it into the xlog, but that seems to be an overdesign.
    
    
    > prefer extending the invalid page mechanism to deal with this, as
    > suggested by Ashwin off-list.  It will allow us to avoid creating
    
    directories and files only to remove them shortly afterwards when the
    > drop database and drop tablespace records are replayed.
    >
    >
    'invalid page' mechanism seems to be more proper for missing pages of a
    file. For
    missing directories, we could, of course, hack to use that (e.g. reading
    any page of
    a relfile in that database) to make sure the tablespace create code
    (without symlink)
    safer (It assumes those directories will be deleted soon).
    
    More feedback about all of the previous discussed solutions is welcome.
    
  4. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> — 2019-04-22T07:15:13Z

    Hello.
    
    At Mon, 22 Apr 2019 12:36:43 +0800, Paul Guo <pguo@pivotal.io> wrote in <CAEET0ZGpUrMGUzfyzVF9FuSq+zb=QovYa2cvyRnDOTvZ5vXxTw@mail.gmail.com>
    > Please see my replies inline. Thanks.
    > 
    > On Fri, Apr 19, 2019 at 12:38 PM Asim R P <apraveen@pivotal.io> wrote:
    > 
    > > On Wed, Apr 17, 2019 at 1:27 PM Paul Guo <pguo@pivotal.io> wrote:
    > > >
    > > > create db with tablespace
    > > > drop database
    > > > drop tablespace.
    > >
    > > Essentially, that sequence of operations causes crash recovery to fail
    > > if the "drop tablespace" transaction was committed before crashing.
    > > This is a bug in crash recovery in general and should be reproducible
    > > without configuring a standby.  Is that right?
    > >
    > 
    > No. In general, checkpoint is done for drop_db/create_db/drop_tablespace on
    > master.
    > That makes the file/directory update-to-date if I understand the related
    > code correctly.
    > For standby, checkpoint redo does not ensure that.
    
    That's right partly. As you must have seen, fast shutdown forces
    restartpoint for the last checkpoint and it prevents the problem
    from happening. Anyway it seems to be a problem.
    
    > > Your patch creates missing directories in the destination.  Don't we
    > > need to create the tablespace symlink under pg_tblspc/?  I would
    > >
    > 
    >  'create db with tablespace' redo log does not include the tablespace real
    > directory information.
    > Yes, we could add in it into the xlog, but that seems to be an overdesign.
    
    But I don't think creating directory that is to be removed just
    after is a wanted solution. The directory most likely to be be
    removed just after.
    
    > > prefer extending the invalid page mechanism to deal with this, as
    > > suggested by Ashwin off-list.  It will allow us to avoid creating
    > 
    > directories and files only to remove them shortly afterwards when the
    > > drop database and drop tablespace records are replayed.
    > >
    > >
    > 'invalid page' mechanism seems to be more proper for missing pages of a
    > file. For
    > missing directories, we could, of course, hack to use that (e.g. reading
    > any page of
    > a relfile in that database) to make sure the tablespace create code
    > (without symlink)
    > safer (It assumes those directories will be deleted soon).
    > 
    > More feedback about all of the previous discussed solutions is welcome.
    
    It doesn't seem to me that the invalid page mechanism is
    applicable in straightforward way, because it doesn't consider
    simple file copy.
    
    Drop failure is ignored any time. I suppose we can ignore the
    error to continue recovering as far as recovery have not reached
    consistency. The attached would work *at least* your case, but I
    haven't checked this covers all places where need the same
    treatment.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  5. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> — 2019-04-22T07:40:27Z

    Oops! The comment in the previous patch is wrong.
    
    At Mon, 22 Apr 2019 16:15:13 +0900 (Tokyo Standard Time), Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> wrote in <20190422.161513.258021727.horiguchi.kyotaro@lab.ntt.co.jp>
    > At Mon, 22 Apr 2019 12:36:43 +0800, Paul Guo <pguo@pivotal.io> wrote in <CAEET0ZGpUrMGUzfyzVF9FuSq+zb=QovYa2cvyRnDOTvZ5vXxTw@mail.gmail.com>
    > > Please see my replies inline. Thanks.
    > > 
    > > On Fri, Apr 19, 2019 at 12:38 PM Asim R P <apraveen@pivotal.io> wrote:
    > > 
    > > > On Wed, Apr 17, 2019 at 1:27 PM Paul Guo <pguo@pivotal.io> wrote:
    > > > >
    > > > > create db with tablespace
    > > > > drop database
    > > > > drop tablespace.
    > > >
    > > > Essentially, that sequence of operations causes crash recovery to fail
    > > > if the "drop tablespace" transaction was committed before crashing.
    > > > This is a bug in crash recovery in general and should be reproducible
    > > > without configuring a standby.  Is that right?
    > > >
    > > 
    > > No. In general, checkpoint is done for drop_db/create_db/drop_tablespace on
    > > master.
    > > That makes the file/directory update-to-date if I understand the related
    > > code correctly.
    > > For standby, checkpoint redo does not ensure that.
    > 
    > That's right partly. As you must have seen, fast shutdown forces
    > restartpoint for the last checkpoint and it prevents the problem
    > from happening. Anyway it seems to be a problem.
    > 
    > > > Your patch creates missing directories in the destination.  Don't we
    > > > need to create the tablespace symlink under pg_tblspc/?  I would
    > > >
    > > 
    > >  'create db with tablespace' redo log does not include the tablespace real
    > > directory information.
    > > Yes, we could add in it into the xlog, but that seems to be an overdesign.
    > 
    > But I don't think creating directory that is to be removed just
    > after is a wanted solution. The directory most likely to be be
    > removed just after.
    > 
    > > > prefer extending the invalid page mechanism to deal with this, as
    > > > suggested by Ashwin off-list.  It will allow us to avoid creating
    > > 
    > > directories and files only to remove them shortly afterwards when the
    > > > drop database and drop tablespace records are replayed.
    > > >
    > > >
    > > 'invalid page' mechanism seems to be more proper for missing pages of a
    > > file. For
    > > missing directories, we could, of course, hack to use that (e.g. reading
    > > any page of
    > > a relfile in that database) to make sure the tablespace create code
    > > (without symlink)
    > > safer (It assumes those directories will be deleted soon).
    > > 
    > > More feedback about all of the previous discussed solutions is welcome.
    > 
    > It doesn't seem to me that the invalid page mechanism is
    > applicable in straightforward way, because it doesn't consider
    > simple file copy.
    > 
    > Drop failure is ignored any time. I suppose we can ignore the
    > error to continue recovering as far as recovery have not reached
    > consistency. The attached would work *at least* your case, but I
    > haven't checked this covers all places where need the same
    > treatment.
    
    The comment for the new function XLogMakePGDirectory is wrong:
    
    + * There is a possibility that WAL replay causes a creation of the same
    + * directory left by the previous crash. Issuing ERROR prevents the caller
    + * from continuing recovery.
    
    The correct one is:
    
    + * There is a possibility that WAL replay causes an error by creation of a
    + * directory under a directory removed before the previous crash. Issuing
    + * ERROR prevents the caller from continuing recovery.
    
    It is fixed in the attached.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
  6. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> — 2019-04-22T12:19:33Z

    At Mon, 22 Apr 2019 16:40:27 +0900 (Tokyo Standard Time), Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> wrote in <20190422.164027.33866403.horiguchi.kyotaro@lab.ntt.co.jp>
    > At Mon, 22 Apr 2019 16:15:13 +0900 (Tokyo Standard Time), Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> wrote in <20190422.161513.258021727.horiguchi.kyotaro@lab.ntt.co.jp>
    > > At Mon, 22 Apr 2019 12:36:43 +0800, Paul Guo <pguo@pivotal.io> wrote in <CAEET0ZGpUrMGUzfyzVF9FuSq+zb=QovYa2cvyRnDOTvZ5vXxTw@mail.gmail.com>
    > > > Please see my replies inline. Thanks.
    > > > 
    > > > On Fri, Apr 19, 2019 at 12:38 PM Asim R P <apraveen@pivotal.io> wrote:
    > > > 
    > > > > On Wed, Apr 17, 2019 at 1:27 PM Paul Guo <pguo@pivotal.io> wrote:
    > > > > >
    > > > > > create db with tablespace
    > > > > > drop database
    > > > > > drop tablespace.
    > > > >
    > > > > Essentially, that sequence of operations causes crash recovery to fail
    > > > > if the "drop tablespace" transaction was committed before crashing.
    > > > > This is a bug in crash recovery in general and should be reproducible
    > > > > without configuring a standby.  Is that right?
    > > > >
    > > > 
    > > > No. In general, checkpoint is done for drop_db/create_db/drop_tablespace on
    > > > master.
    > > > That makes the file/directory update-to-date if I understand the related
    > > > code correctly.
    > > > For standby, checkpoint redo does not ensure that.
    
    The attached exercises this sequence, needing some changes in
    PostgresNode.pm and RecursiveCopy.pm to allow tablespaces.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  7. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Michael Paquier <michael@paquier.xyz> — 2019-04-23T02:34:38Z

    On Mon, Apr 22, 2019 at 09:19:33PM +0900, Kyotaro HORIGUCHI wrote:
    > The attached exercises this sequence, needing some changes in
    > PostgresNode.pm and RecursiveCopy.pm to allow tablespaces.
    
    +    # Check for symlink -- needed only on source dir
    +    # (note: this will fall through quietly if file is already gone)
    +    if (-l $srcpath)
    +    {
    +        croak "Cannot operate on symlink \"$srcpath\""
    +          if ($srcpath !~ /\/(pg_tblspc\/[0-9]+)$/);
    +
    +        # We have mapped tablespaces. Copy them individually
    +        my $linkname = $1;
    +        my $tmpdir = TestLib::tempdir;
    +        my $dstrealdir = TestLib::real_dir($tmpdir);
    +        my $srcrealdir = readlink($srcpath);
    +
    +        opendir(my $dh, $srcrealdir);
    +        while (readdir $dh)
    +        {
    +            next if (/^\.\.?$/);
    +            my $spath = "$srcrealdir/$_";
    +            my $dpath = "$dstrealdir/$_";
    +
    +            copypath($spath, $dpath);
    +        }
    +        closedir $dh;
    +
    +        symlink $dstrealdir, $destpath;
    +        return 1;
    +    }
    
    The same stuff is proposed here:
    https://www.postgresql.org/message-id/CAGRcZQUxd9YOfifOKXOfJ+Fp3JdpoeKCzt+zH_PRMNaaDaExdQ@mail.gmail.com
    
    So there is a lot of demand for making the recursive copy more skilled
    at handling symlinks for tablespace tests, and I'd like to propose to
    do something among those lines for the tests on HEAD, presumably for
    v12 and not v13 as we are talking about a bug fix here?  I am not sure
    yet which one of the proposals is better than the other though.
    --
    Michael
    
  8. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> — 2019-04-23T05:02:34Z

    At Tue, 23 Apr 2019 11:34:38 +0900, Michael Paquier <michael@paquier.xyz> wrote in <20190423023438.GH2712@paquier.xyz>
    > On Mon, Apr 22, 2019 at 09:19:33PM +0900, Kyotaro HORIGUCHI wrote:
    > > The attached exercises this sequence, needing some changes in
    > > PostgresNode.pm and RecursiveCopy.pm to allow tablespaces.
    > 
    > +    # Check for symlink -- needed only on source dir
    > +    # (note: this will fall through quietly if file is already gone)
    > +    if (-l $srcpath)
    > +    {
    > +        croak "Cannot operate on symlink \"$srcpath\""
    > +          if ($srcpath !~ /\/(pg_tblspc\/[0-9]+)$/);
    > +
    > +        # We have mapped tablespaces. Copy them individually
    > +        my $linkname = $1;
    > +        my $tmpdir = TestLib::tempdir;
    > +        my $dstrealdir = TestLib::real_dir($tmpdir);
    > +        my $srcrealdir = readlink($srcpath);
    > +
    > +        opendir(my $dh, $srcrealdir);
    > +        while (readdir $dh)
    > +        {
    > +            next if (/^\.\.?$/);
    > +            my $spath = "$srcrealdir/$_";
    > +            my $dpath = "$dstrealdir/$_";
    > +
    > +            copypath($spath, $dpath);
    > +        }
    > +        closedir $dh;
    > +
    > +        symlink $dstrealdir, $destpath;
    > +        return 1;
    > +    }
    > 
    > The same stuff is proposed here:
    > https://www.postgresql.org/message-id/CAGRcZQUxd9YOfifOKXOfJ+Fp3JdpoeKCzt+zH_PRMNaaDaExdQ@mail.gmail.com
    > 
    > So there is a lot of demand for making the recursive copy more skilled
    > at handling symlinks for tablespace tests, and I'd like to propose to
    > do something among those lines for the tests on HEAD, presumably for
    > v12 and not v13 as we are talking about a bug fix here?  I am not sure
    > yet which one of the proposals is better than the other though.
    
    TBH I like that (my one cieted above) not so much. However, I
    prefer to have v12 if this is a bug and to be fixed in
    v12. Otherwise we won't add a test for this later:p
    
    Anyway I'll visit there. Thanks. 
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
    
  9. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-04-23T05:31:58Z

    Hi Kyotaro, ignoring the MakePGDirectory() failure will fix this database
    create redo error, but I suspect some other kind of redo, which depends on
    the files under the directory (they are not copied since the directory is
    not created) and also cannot be covered by the invalid page mechanism,
    could fail. Thanks.
    
    On Mon, Apr 22, 2019 at 3:40 PM Kyotaro HORIGUCHI <
    horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    
    > Oops! The comment in the previous patch is wrong.
    >
    > At Mon, 22 Apr 2019 16:15:13 +0900 (Tokyo Standard Time), Kyotaro
    > HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> wrote in <
    > 20190422.161513.258021727.horiguchi.kyotaro@lab.ntt.co.jp>
    > > At Mon, 22 Apr 2019 12:36:43 +0800, Paul Guo <pguo@pivotal.io> wrote in
    > <CAEET0ZGpUrMGUzfyzVF9FuSq+zb=QovYa2cvyRnDOTvZ5vXxTw@mail.gmail.com>
    > > > Please see my replies inline. Thanks.
    > > >
    > > > On Fri, Apr 19, 2019 at 12:38 PM Asim R P <apraveen@pivotal.io> wrote:
    > > >
    > > > > On Wed, Apr 17, 2019 at 1:27 PM Paul Guo <pguo@pivotal.io> wrote:
    > > > > >
    > > > > > create db with tablespace
    > > > > > drop database
    > > > > > drop tablespace.
    > > > >
    > > > > Essentially, that sequence of operations causes crash recovery to
    > fail
    > > > > if the "drop tablespace" transaction was committed before crashing.
    > > > > This is a bug in crash recovery in general and should be reproducible
    > > > > without configuring a standby.  Is that right?
    > > > >
    > > >
    > > > No. In general, checkpoint is done for
    > drop_db/create_db/drop_tablespace on
    > > > master.
    > > > That makes the file/directory update-to-date if I understand the
    > related
    > > > code correctly.
    > > > For standby, checkpoint redo does not ensure that.
    > >
    > > That's right partly. As you must have seen, fast shutdown forces
    > > restartpoint for the last checkpoint and it prevents the problem
    > > from happening. Anyway it seems to be a problem.
    > >
    > > > > Your patch creates missing directories in the destination.  Don't we
    > > > > need to create the tablespace symlink under pg_tblspc/?  I would
    > > > >
    > > >
    > > >  'create db with tablespace' redo log does not include the tablespace
    > real
    > > > directory information.
    > > > Yes, we could add in it into the xlog, but that seems to be an
    > overdesign.
    > >
    > > But I don't think creating directory that is to be removed just
    > > after is a wanted solution. The directory most likely to be be
    > > removed just after.
    > >
    > > > > prefer extending the invalid page mechanism to deal with this, as
    > > > > suggested by Ashwin off-list.  It will allow us to avoid creating
    > > >
    > > > directories and files only to remove them shortly afterwards when the
    > > > > drop database and drop tablespace records are replayed.
    > > > >
    > > > >
    > > > 'invalid page' mechanism seems to be more proper for missing pages of a
    > > > file. For
    > > > missing directories, we could, of course, hack to use that (e.g.
    > reading
    > > > any page of
    > > > a relfile in that database) to make sure the tablespace create code
    > > > (without symlink)
    > > > safer (It assumes those directories will be deleted soon).
    > > >
    > > > More feedback about all of the previous discussed solutions is welcome.
    > >
    > > It doesn't seem to me that the invalid page mechanism is
    > > applicable in straightforward way, because it doesn't consider
    > > simple file copy.
    > >
    > > Drop failure is ignored any time. I suppose we can ignore the
    > > error to continue recovering as far as recovery have not reached
    > > consistency. The attached would work *at least* your case, but I
    > > haven't checked this covers all places where need the same
    > > treatment.
    >
    > The comment for the new function XLogMakePGDirectory is wrong:
    >
    > + * There is a possibility that WAL replay causes a creation of the same
    > + * directory left by the previous crash. Issuing ERROR prevents the caller
    > + * from continuing recovery.
    >
    > The correct one is:
    >
    > + * There is a possibility that WAL replay causes an error by creation of a
    > + * directory under a directory removed before the previous crash. Issuing
    > + * ERROR prevents the caller from continuing recovery.
    >
    > It is fixed in the attached.
    >
    > regards.
    >
    > --
    > Kyotaro Horiguchi
    > NTT Open Source Software Center
    >
    >
    
  10. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> — 2019-04-23T07:39:49Z

    Hello.
    
    At Tue, 23 Apr 2019 13:31:58 +0800, Paul Guo <pguo@pivotal.io> wrote in <CAEET0ZEcwz57z2yfWRds43b3TfQPPDSWmbjGmD43xRxLT41NDg@mail.gmail.com>
    > Hi Kyotaro, ignoring the MakePGDirectory() failure will fix this database
    > create redo error, but I suspect some other kind of redo, which depends on
    > the files under the directory (they are not copied since the directory is
    > not created) and also cannot be covered by the invalid page mechanism,
    > could fail. Thanks.
    
    If recovery starts from just after tablespace creation, that's
    simple. The Symlink to the removed tablespace is already removed
    in the case. Hence server innocently create files directly under
    pg_tblspc, not in the tablespace. Finally all files that were
    supposed to be created in the removed tablespace are removed
    later in recovery.
    
    If recovery starts from recalling page in a file that have been
    in the tablespace, XLogReadBufferExtended creates one (perhaps
    directly in pg_tblspc as described above) and the files are
    removed later in recoery the same way to above. This case doen't
    cause FATAL/PANIC during recovery even in master.
    
    XLogReadBufferExtended@xlogutils.c
    | * Create the target file if it doesn't already exist.  This lets us cope
    | * if the replay sequence contains writes to a relation that is later
    | * deleted.  (The original coding of this routine would instead suppress
    | * the writes, but that seems like it risks losing valuable data if the
    | * filesystem loses an inode during a crash.  Better to write the data
    | * until we are actually told to delete the file.)
    
    So buffered access cannot be a problem for the reason above. The
    remaining possible issue is non-buffered access to files in
    removed tablespaces. This is what I mentioned upthread:
    
    me> but I haven't checked this covers all places where need the same
    me> treatment.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
    
  11. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> — 2019-04-24T08:13:54Z

    Mmm. I posted to wrong thread. Sorry.
    
    At Tue, 23 Apr 2019 16:39:49 +0900 (Tokyo Standard Time), Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> wrote in <20190423.163949.36763221.horiguchi.kyotaro@lab.ntt.co.jp>
    > At Tue, 23 Apr 2019 13:31:58 +0800, Paul Guo <pguo@pivotal.io> wrote in <CAEET0ZEcwz57z2yfWRds43b3TfQPPDSWmbjGmD43xRxLT41NDg@mail.gmail.com>
    > > Hi Kyotaro, ignoring the MakePGDirectory() failure will fix this database
    > > create redo error, but I suspect some other kind of redo, which depends on
    > > the files under the directory (they are not copied since the directory is
    > > not created) and also cannot be covered by the invalid page mechanism,
    > > could fail. Thanks.
    > 
    > If recovery starts from just after tablespace creation, that's
    > simple. The Symlink to the removed tablespace is already removed
    > in the case. Hence server innocently create files directly under
    > pg_tblspc, not in the tablespace. Finally all files that were
    > supposed to be created in the removed tablespace are removed
    > later in recovery.
    > 
    > If recovery starts from recalling page in a file that have been
    > in the tablespace, XLogReadBufferExtended creates one (perhaps
    > directly in pg_tblspc as described above) and the files are
    > removed later in recoery the same way to above. This case doen't
    > cause FATAL/PANIC during recovery even in master.
    > 
    > XLogReadBufferExtended@xlogutils.c
    > | * Create the target file if it doesn't already exist.  This lets us cope
    > | * if the replay sequence contains writes to a relation that is later
    > | * deleted.  (The original coding of this routine would instead suppress
    > | * the writes, but that seems like it risks losing valuable data if the
    > | * filesystem loses an inode during a crash.  Better to write the data
    > | * until we are actually told to delete the file.)
    > 
    > So buffered access cannot be a problem for the reason above. The
    > remaining possible issue is non-buffered access to files in
    > removed tablespaces. This is what I mentioned upthread:
    > 
    > me> but I haven't checked this covers all places where need the same
    > me> treatment.
    
    RM_DBASE_ID is fixed by the patch.
    
    XLOG/XACT/CLOG/MULTIXACT/RELMAP/STANDBY/COMMIT_TS/REPLORIGIN/LOGICALMSG:
      - are not relevant.
    
    HEAP/HEAP2/BTREE/HASH/GIN/GIST/SEQ/SPGIST/BRIN/GENERIC:
      - Resources works on buffer is not affected.
    
    SMGR:
      - Both CREATE and TRUNCATE seems fine.
    
    TBLSPC:
      - We don't nest tablespace directories. No Problem.
    
    I don't find a similar case.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
    
  12. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-04-28T07:33:13Z

    On Wed, Apr 24, 2019 at 4:14 PM Kyotaro HORIGUCHI <
    horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    
    > Mmm. I posted to wrong thread. Sorry.
    >
    > At Tue, 23 Apr 2019 16:39:49 +0900 (Tokyo Standard Time), Kyotaro
    > HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> wrote in <
    > 20190423.163949.36763221.horiguchi.kyotaro@lab.ntt.co.jp>
    > > At Tue, 23 Apr 2019 13:31:58 +0800, Paul Guo <pguo@pivotal.io> wrote in
    > <CAEET0ZEcwz57z2yfWRds43b3TfQPPDSWmbjGmD43xRxLT41NDg@mail.gmail.com>
    > > > Hi Kyotaro, ignoring the MakePGDirectory() failure will fix this
    > database
    > > > create redo error, but I suspect some other kind of redo, which
    > depends on
    > > > the files under the directory (they are not copied since the directory
    > is
    > > > not created) and also cannot be covered by the invalid page mechanism,
    > > > could fail. Thanks.
    > >
    > > If recovery starts from just after tablespace creation, that's
    > > simple. The Symlink to the removed tablespace is already removed
    > > in the case. Hence server innocently create files directly under
    > > pg_tblspc, not in the tablespace. Finally all files that were
    > > supposed to be created in the removed tablespace are removed
    > > later in recovery.
    > >
    > > If recovery starts from recalling page in a file that have been
    > > in the tablespace, XLogReadBufferExtended creates one (perhaps
    > > directly in pg_tblspc as described above) and the files are
    > > removed later in recoery the same way to above. This case doen't
    > > cause FATAL/PANIC during recovery even in master.
    > >
    > > XLogReadBufferExtended@xlogutils.c
    > > | * Create the target file if it doesn't already exist.  This lets us
    > cope
    > > | * if the replay sequence contains writes to a relation that is later
    > > | * deleted.  (The original coding of this routine would instead suppress
    > > | * the writes, but that seems like it risks losing valuable data if the
    > > | * filesystem loses an inode during a crash.  Better to write the data
    > > | * until we are actually told to delete the file.)
    > >
    > > So buffered access cannot be a problem for the reason above. The
    > > remaining possible issue is non-buffered access to files in
    > > removed tablespaces. This is what I mentioned upthread:
    > >
    > > me> but I haven't checked this covers all places where need the same
    > > me> treatment.
    >
    > RM_DBASE_ID is fixed by the patch.
    >
    > XLOG/XACT/CLOG/MULTIXACT/RELMAP/STANDBY/COMMIT_TS/REPLORIGIN/LOGICALMSG:
    >   - are not relevant.
    >
    > HEAP/HEAP2/BTREE/HASH/GIN/GIST/SEQ/SPGIST/BRIN/GENERIC:
    >   - Resources works on buffer is not affected.
    >
    > SMGR:
    >   - Both CREATE and TRUNCATE seems fine.
    >
    > TBLSPC:
    >   - We don't nest tablespace directories. No Problem.
    >
    > I don't find a similar case.
    
    
    I took some time in digging into the related code. It seems that ignoring
    if the dst directory cannot be created directly
    should be fine since smgr redo code creates tablespace path finally by
    calling TablespaceCreateDbspace().
    What's more, I found some more issues.
    
    1) The below error message is actually misleading.
    
    2019-04-17 14:52:14.951 CST [23030] FATAL:  could not create directory
    "pg_tblspc/65546/PG_12_201904072/65547": No such file or directory
    2019-04-17 14:52:14.951 CST [23030] CONTEXT:  WAL redo at 0/3011650 for
    Database/CREATE: copy dir 1663/1 to 65546/65547
    
    That should be due to dbase_desc(). It could be simply fixed following the
    code logic in GetDatabasePath().
    
    2) It seems that src directory could be missing then
    dbase_redo()->copydir() could error out. For example,
    
    \!rm -rf /tmp/tbspace1
    \!mkdir /tmp/tbspace1
    \!rm -rf /tmp/tbspace2
    \!mkdir /tmp/tbspace2
    create tablespace tbs1 location '/tmp/tbspace1';
    create tablespace tbs2 location '/tmp/tbspace2';
    create database db1 tablespace tbs1;
    alter database db1 set tablespace tbs2;
    drop tablespace tbs1;
    
    Let's say, the standby finishes all replay but redo lsn on pg_control is
    still the point at 'alter database', and then
    kill postgres, then in theory when startup, dbase_redo()->copydir() will
    ERROR since 'drop tablespace tbs1'
    has removed the directories (and symlink) of tbs1. Below simple code change
    could fix that.
    
    diff --git a/src/backend/commands/dbcommands.c
    b/src/backend/commands/dbcommands.c
    index 9707afabd9..7d755c759e 100644
    --- a/src/backend/commands/dbcommands.c
    +++ b/src/backend/commands/dbcommands.c
    @@ -2114,6 +2114,15 @@ dbase_redo(XLogReaderState *record)
             */
            FlushDatabaseBuffers(xlrec->src_db_id);
    
    +       /*
    +        * It is possible that the source directory is missing if
    +        * we are re-replaying the xlog while subsequent xlogs
    +        * drop the tablespace in previous replaying. For this
    +        * we just skip.
    +        */
    +       if (!(stat(src_path, &st) == 0 && S_ISDIR(st.st_mode)))
    +           return;
    +
            /*
             * Copy this subdirectory to the new location
             *
    
    If we want to fix the issue by ignoring the dst path create failure, I do
    not think we should do
    that in copydir() since copydir() seems to be a common function. I'm not
    sure whether it is
    used by some extensions or not. If no maybe we should move the dst patch
    create logic
    out of copydir().
    
    Also I'd suggest we should use pg_mkdir_p() in TablespaceCreateDbspace() to
    replace
    the code block includes a lot of get_parent_directory(), MakePGDirectory(),
    etc even it
    is not fixing a bug since pg_mkdir_p() code change seems to be more
    graceful and simpler.
    
    Whatever ignore mkdir failure or mkdir_p, I found that these steps seem to
    be error-prone
    along with postgre evolving since they are hard to test and also we are not
    easy to think out
    various potential bad cases. Is it possible that we should do real
    checkpoint (flush & update
    redo lsn) when seeing checkpoint xlogs for these operations? This will slow
    down standby
    but master also does this and also these operations are not usual,
    espeically it seems that it
    does not slow down wal receiving usually?
    
  13. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-04-30T06:33:47Z

    I updated the original patch to
    
    1) skip copydir() if either src path or dst parent path is missing in
    dbase_redo(). Both missing cases seem to be possible. For the src path
    missing case, mkdir_p() is meaningless. It seems that moving the directory
    existence check step to dbase_redo() has less impact on other code.
    
    2) Fixed dbase_desc(). Now the xlog output looks correct.
    
    rmgr: Database    len (rec/tot):     42/    42, tx:        486, lsn:
    0/016386A8, prev 0/01638630, desc: CREATE copy dir base/1 to
    pg_tblspc/16384/PG_12_201904281/16386
    
    rmgr: Database    len (rec/tot):     34/    34, tx:        487, lsn:
    0/01638EB8, prev 0/01638E40, desc: DROP dir
    pg_tblspc/16384/PG_12_201904281/16386
    
    I'm not familiar with the TAP test details previously. I learned a lot
    about how to test such case from Kyotaro's patch series.👍
    
    On Sun, Apr 28, 2019 at 3:33 PM Paul Guo <pguo@pivotal.io> wrote:
    
    >
    > On Wed, Apr 24, 2019 at 4:14 PM Kyotaro HORIGUCHI <
    > horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    >
    >> Mmm. I posted to wrong thread. Sorry.
    >>
    >> At Tue, 23 Apr 2019 16:39:49 +0900 (Tokyo Standard Time), Kyotaro
    >> HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> wrote in <
    >> 20190423.163949.36763221.horiguchi.kyotaro@lab.ntt.co.jp>
    >> > At Tue, 23 Apr 2019 13:31:58 +0800, Paul Guo <pguo@pivotal.io> wrote
    >> in <CAEET0ZEcwz57z2yfWRds43b3TfQPPDSWmbjGmD43xRxLT41NDg@mail.gmail.com>
    >> > > Hi Kyotaro, ignoring the MakePGDirectory() failure will fix this
    >> database
    >> > > create redo error, but I suspect some other kind of redo, which
    >> depends on
    >> > > the files under the directory (they are not copied since the
    >> directory is
    >> > > not created) and also cannot be covered by the invalid page mechanism,
    >> > > could fail. Thanks.
    >> >
    >> > If recovery starts from just after tablespace creation, that's
    >> > simple. The Symlink to the removed tablespace is already removed
    >> > in the case. Hence server innocently create files directly under
    >> > pg_tblspc, not in the tablespace. Finally all files that were
    >> > supposed to be created in the removed tablespace are removed
    >> > later in recovery.
    >> >
    >> > If recovery starts from recalling page in a file that have been
    >> > in the tablespace, XLogReadBufferExtended creates one (perhaps
    >> > directly in pg_tblspc as described above) and the files are
    >> > removed later in recoery the same way to above. This case doen't
    >> > cause FATAL/PANIC during recovery even in master.
    >> >
    >> > XLogReadBufferExtended@xlogutils.c
    >> > | * Create the target file if it doesn't already exist.  This lets us
    >> cope
    >> > | * if the replay sequence contains writes to a relation that is later
    >> > | * deleted.  (The original coding of this routine would instead
    >> suppress
    >> > | * the writes, but that seems like it risks losing valuable data if the
    >> > | * filesystem loses an inode during a crash.  Better to write the data
    >> > | * until we are actually told to delete the file.)
    >> >
    >> > So buffered access cannot be a problem for the reason above. The
    >> > remaining possible issue is non-buffered access to files in
    >> > removed tablespaces. This is what I mentioned upthread:
    >> >
    >> > me> but I haven't checked this covers all places where need the same
    >> > me> treatment.
    >>
    >> RM_DBASE_ID is fixed by the patch.
    >>
    >> XLOG/XACT/CLOG/MULTIXACT/RELMAP/STANDBY/COMMIT_TS/REPLORIGIN/LOGICALMSG:
    >>   - are not relevant.
    >>
    >> HEAP/HEAP2/BTREE/HASH/GIN/GIST/SEQ/SPGIST/BRIN/GENERIC:
    >>   - Resources works on buffer is not affected.
    >>
    >> SMGR:
    >>   - Both CREATE and TRUNCATE seems fine.
    >>
    >> TBLSPC:
    >>   - We don't nest tablespace directories. No Problem.
    >>
    >> I don't find a similar case.
    >
    >
    > I took some time in digging into the related code. It seems that ignoring
    > if the dst directory cannot be created directly
    > should be fine since smgr redo code creates tablespace path finally by
    > calling TablespaceCreateDbspace().
    > What's more, I found some more issues.
    >
    > 1) The below error message is actually misleading.
    >
    > 2019-04-17 14:52:14.951 CST [23030] FATAL:  could not create directory
    > "pg_tblspc/65546/PG_12_201904072/65547": No such file or directory
    > 2019-04-17 14:52:14.951 CST [23030] CONTEXT:  WAL redo at 0/3011650 for
    > Database/CREATE: copy dir 1663/1 to 65546/65547
    >
    > That should be due to dbase_desc(). It could be simply fixed following the
    > code logic in GetDatabasePath().
    >
    > 2) It seems that src directory could be missing then
    > dbase_redo()->copydir() could error out. For example,
    >
    > \!rm -rf /tmp/tbspace1
    > \!mkdir /tmp/tbspace1
    > \!rm -rf /tmp/tbspace2
    > \!mkdir /tmp/tbspace2
    > create tablespace tbs1 location '/tmp/tbspace1';
    > create tablespace tbs2 location '/tmp/tbspace2';
    > create database db1 tablespace tbs1;
    > alter database db1 set tablespace tbs2;
    > drop tablespace tbs1;
    >
    > Let's say, the standby finishes all replay but redo lsn on pg_control is
    > still the point at 'alter database', and then
    > kill postgres, then in theory when startup, dbase_redo()->copydir() will
    > ERROR since 'drop tablespace tbs1'
    > has removed the directories (and symlink) of tbs1. Below simple code
    > change could fix that.
    >
    > diff --git a/src/backend/commands/dbcommands.c
    > b/src/backend/commands/dbcommands.c
    > index 9707afabd9..7d755c759e 100644
    > --- a/src/backend/commands/dbcommands.c
    > +++ b/src/backend/commands/dbcommands.c
    > @@ -2114,6 +2114,15 @@ dbase_redo(XLogReaderState *record)
    >          */
    >         FlushDatabaseBuffers(xlrec->src_db_id);
    >
    > +       /*
    > +        * It is possible that the source directory is missing if
    > +        * we are re-replaying the xlog while subsequent xlogs
    > +        * drop the tablespace in previous replaying. For this
    > +        * we just skip.
    > +        */
    > +       if (!(stat(src_path, &st) == 0 && S_ISDIR(st.st_mode)))
    > +           return;
    > +
    >         /*
    >          * Copy this subdirectory to the new location
    >          *
    >
    > If we want to fix the issue by ignoring the dst path create failure, I do
    > not think we should do
    > that in copydir() since copydir() seems to be a common function. I'm not
    > sure whether it is
    > used by some extensions or not. If no maybe we should move the dst patch
    > create logic
    > out of copydir().
    >
    > Also I'd suggest we should use pg_mkdir_p() in TablespaceCreateDbspace()
    > to replace
    > the code block includes a lot of
    > get_parent_directory(), MakePGDirectory(), etc even it
    > is not fixing a bug since pg_mkdir_p() code change seems to be more
    > graceful and simpler.
    >
    > Whatever ignore mkdir failure or mkdir_p, I found that these steps seem to
    > be error-prone
    > along with postgre evolving since they are hard to test and also we are
    > not easy to think out
    > various potential bad cases. Is it possible that we should do real
    > checkpoint (flush & update
    > redo lsn) when seeing checkpoint xlogs for these operations? This will
    > slow down standby
    > but master also does this and also these operations are not usual,
    > espeically it seems that it
    > does not slow down wal receiving usually?
    >
    >
    >
    >
    
  14. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> — 2019-05-07T06:47:11Z

    Hi.
    
    At Tue, 30 Apr 2019 14:33:47 +0800, Paul Guo <pguo@pivotal.io> wrote in <CAEET0ZGhmDKrq7JJu2rLLqcJBR8pA4OYrKsirZ5Ft8-deG1e8A@mail.gmail.com>
    > I updated the original patch to
    
    It's reasonable not to touch copydir.
    
    > 1) skip copydir() if either src path or dst parent path is missing in
    > dbase_redo(). Both missing cases seem to be possible. For the src path
    > missing case, mkdir_p() is meaningless. It seems that moving the directory
    > existence check step to dbase_redo() has less impact on other code.
    
    Nice catch.
    
    
    +      if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    +      {
    
    This patch is allowing missing source and destination directory
    even in consistent state. I don't think it is safe.
    
    
    
    +        ereport(WARNING,
    +            (errmsg("directory \"%s\" for copydir() does not exists."
    +                "It is possibly expected. Skip copydir().",
    +                parent_path)));
    
    This message seems unfriendly to users, or it seems like an elog
    message. How about something like this. The same can be said for
    the source directory.
    
    | WARNING:  skipped creating database directory: "%s"
    | DETAIL:  The tabelspace %u may have been removed just before crash.
    
    # I'm not confident in this at all:(
    
    > 2) Fixed dbase_desc(). Now the xlog output looks correct.
    > 
    > rmgr: Database    len (rec/tot):     42/    42, tx:        486, lsn:
    > 0/016386A8, prev 0/01638630, desc: CREATE copy dir base/1 to
    > pg_tblspc/16384/PG_12_201904281/16386
    > 
    > rmgr: Database    len (rec/tot):     34/    34, tx:        487, lsn:
    > 0/01638EB8, prev 0/01638E40, desc: DROP dir
    > pg_tblspc/16384/PG_12_201904281/16386
    
    WAL records don't convey such information. The previous
    description seems right to me.
    
    > I'm not familiar with the TAP test details previously. I learned a lot
    > about how to test such case from Kyotaro's patch series.👍
    
    Yeah, good to hear.
    
    > On Sun, Apr 28, 2019 at 3:33 PM Paul Guo <pguo@pivotal.io> wrote:
    > > If we want to fix the issue by ignoring the dst path create failure, I do
    > > not think we should do
    > > that in copydir() since copydir() seems to be a common function. I'm not
    > > sure whether it is
    > > used by some extensions or not. If no maybe we should move the dst patch
    > > create logic
    > > out of copydir().
    
    Agreed to this.
    
    > > Also I'd suggest we should use pg_mkdir_p() in TablespaceCreateDbspace()
    > > to replace
    > > the code block includes a lot of
    > > get_parent_directory(), MakePGDirectory(), etc even it
    > > is not fixing a bug since pg_mkdir_p() code change seems to be more
    > > graceful and simpler.
    
    But I don't agree to this. pg_mkdir_p goes above two-parents up,
    which would be unwanted here.
    
    > > Whatever ignore mkdir failure or mkdir_p, I found that these steps seem to
    > > be error-prone
    > > along with postgre evolving since they are hard to test and also we are
    > > not easy to think out
    > > various potential bad cases. Is it possible that we should do real
    > > checkpoint (flush & update
    > > redo lsn) when seeing checkpoint xlogs for these operations? This will
    > > slow down standby
    > > but master also does this and also these operations are not usual,
    > > espeically it seems that it
    > > does not slow down wal receiving usually?
    
    That dramatically slows recovery (not replication) if databases
    are created and deleted frequently. That wouldn't be acceptable.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  15. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-05-13T09:37:50Z

    Thanks for the reply.
    
    On Tue, May 7, 2019 at 2:47 PM Kyotaro HORIGUCHI <
    horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    
    >
    > +      if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    > +      {
    >
    > This patch is allowing missing source and destination directory
    > even in consistent state. I don't think it is safe.
    >
    
    I do not understand this. Can you elaborate?
    
    
    >
    >
    >
    > +        ereport(WARNING,
    > +            (errmsg("directory \"%s\" for copydir() does not exists."
    > +                "It is possibly expected. Skip copydir().",
    > +                parent_path)));
    >
    > This message seems unfriendly to users, or it seems like an elog
    > message. How about something like this. The same can be said for
    > the source directory.
    >
    > | WARNING:  skipped creating database directory: "%s"
    > | DETAIL:  The tabelspace %u may have been removed just before crash.
    >
    
    Yeah. Looks better.
    
    
    >
    > # I'm not confident in this at all:(
    >
    > > 2) Fixed dbase_desc(). Now the xlog output looks correct.
    > >
    > > rmgr: Database    len (rec/tot):     42/    42, tx:        486, lsn:
    > > 0/016386A8, prev 0/01638630, desc: CREATE copy dir base/1 to
    > > pg_tblspc/16384/PG_12_201904281/16386
    > >
    > > rmgr: Database    len (rec/tot):     34/    34, tx:        487, lsn:
    > > 0/01638EB8, prev 0/01638E40, desc: DROP dir
    > > pg_tblspc/16384/PG_12_201904281/16386
    >
    > WAL records don't convey such information. The previous
    > description seems right to me.
    >
    
    2019-04-17 14:52:14.951 CST [23030] CONTEXT:  WAL redo at 0/3011650 for
    Database/CREATE: copy dir 1663/1 to 65546/65547
    The directories are definitely wrong and misleading.
    
    
    > > > Also I'd suggest we should use pg_mkdir_p() in
    > TablespaceCreateDbspace()
    > > > to replace
    > > > the code block includes a lot of
    > > > get_parent_directory(), MakePGDirectory(), etc even it
    > > > is not fixing a bug since pg_mkdir_p() code change seems to be more
    > > > graceful and simpler.
    >
    > But I don't agree to this. pg_mkdir_p goes above two-parents up,
    > which would be unwanted here.
    >
    > I do not understand this also. pg_mkdir_p() is similar to 'mkdir -p'.
    This change just makes the code concise. Though in theory the change is not
    needed.
    
    
    > > > Whatever ignore mkdir failure or mkdir_p, I found that these steps
    > seem to
    > > > be error-prone
    > > > along with postgre evolving since they are hard to test and also we are
    > > > not easy to think out
    > > > various potential bad cases. Is it possible that we should do real
    > > > checkpoint (flush & update
    > > > redo lsn) when seeing checkpoint xlogs for these operations? This will
    > > > slow down standby
    > > > but master also does this and also these operations are not usual,
    > > > espeically it seems that it
    > > > does not slow down wal receiving usually?
    >
    > That dramatically slows recovery (not replication) if databases
    > are created and deleted frequently. That wouldn't be acceptable.
    >
    
    This behavior is rare and seems to have the same impact on master & standby
    from checkpoint/restartpoint.
    We do not worry about master so we should not worry about standby also.
    
  16. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp> — 2019-05-14T03:06:13Z

    Hello.
    
    At Mon, 13 May 2019 17:37:50 +0800, Paul Guo <pguo@pivotal.io> wrote in <CAEET0ZF9yN4DaXyuFLzOcAYyxuFF1Ms_OQWeA+Rwv3GhA5Q-SA@mail.gmail.com>
    > Thanks for the reply.
    > 
    > On Tue, May 7, 2019 at 2:47 PM Kyotaro HORIGUCHI <
    > horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    > 
    > >
    > > +      if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    > > +      {
    > >
    > > This patch is allowing missing source and destination directory
    > > even in consistent state. I don't think it is safe.
    > >
    > 
    > I do not understand this. Can you elaborate?
    
    Suppose we were recoverying based on a backup at LSN1 targeting
    to LSN3 then it crashed at LSN2, where LSN1 < LSN2 <= LSN3. LSN2
    is called as "consistency point", before where the database is
    not consistent. It's because we are applying WAL recored older
    than those that were already applied in the second trial. The
    same can be said for crash recovery, where LSN1 is the latest
    checkpoint ('s redo LSN) and LSN2=LSN3 is the crashed LSN.
    
    Creation of an existing directory or dropping of a non-existent
    directory are apparently inconsistent or "broken" so we should
    stop recovery when seeing such WAL records while database is in
    consistent state.
    
    > > +        ereport(WARNING,
    > > +            (errmsg("directory \"%s\" for copydir() does not exists."
    > > +                "It is possibly expected. Skip copydir().",
    > > +                parent_path)));
    > >
    > > This message seems unfriendly to users, or it seems like an elog
    > > message. How about something like this. The same can be said for
    > > the source directory.
    > >
    > > | WARNING:  skipped creating database directory: "%s"
    > > | DETAIL:  The tabelspace %u may have been removed just before crash.
    > >
    > 
    > Yeah. Looks better.
    > 
    > 
    > >
    > > # I'm not confident in this at all:(
    > >
    > > > 2) Fixed dbase_desc(). Now the xlog output looks correct.
    > > >
    > > > rmgr: Database    len (rec/tot):     42/    42, tx:        486, lsn:
    > > > 0/016386A8, prev 0/01638630, desc: CREATE copy dir base/1 to
    > > > pg_tblspc/16384/PG_12_201904281/16386
    > > >
    > > > rmgr: Database    len (rec/tot):     34/    34, tx:        487, lsn:
    > > > 0/01638EB8, prev 0/01638E40, desc: DROP dir
    > > > pg_tblspc/16384/PG_12_201904281/16386
    > >
    > > WAL records don't convey such information. The previous
    > > description seems right to me.
    > >
    > 
    > 2019-04-17 14:52:14.951 CST [23030] CONTEXT:  WAL redo at 0/3011650 for
    > Database/CREATE: copy dir 1663/1 to 65546/65547
    > The directories are definitely wrong and misleading.
    
    The original description is right in the light of how the server
    recognizes. The record exactly says that "copy dir 1663/1 to
    65546/65547" and the latter path is converted in filesystem layer
    via a symlink.
    
    
    > > > > Also I'd suggest we should use pg_mkdir_p() in
    > > TablespaceCreateDbspace()
    > > > > to replace
    > > > > the code block includes a lot of
    > > > > get_parent_directory(), MakePGDirectory(), etc even it
    > > > > is not fixing a bug since pg_mkdir_p() code change seems to be more
    > > > > graceful and simpler.
    > >
    > > But I don't agree to this. pg_mkdir_p goes above two-parents up,
    > > which would be unwanted here.
    > >
    > > I do not understand this also. pg_mkdir_p() is similar to 'mkdir -p'.
    > This change just makes the code concise. Though in theory the change is not
    > needed.
    
    We don't want to create tablespace direcotory after concurrent
    DROPing, as the comment just above is saying:
    
    |  * Acquire TablespaceCreateLock to ensure that no DROP TABLESPACE
    |  * or TablespaceCreateDbspace is running concurrently.
    
    If the concurrent DROP TABLESPACE destroyed the grand parent
    directory, we mustn't create it again.
    
    > > > > Whatever ignore mkdir failure or mkdir_p, I found that these steps
    > > seem to
    > > > > be error-prone
    > > > > along with postgre evolving since they are hard to test and also we are
    > > > > not easy to think out
    > > > > various potential bad cases. Is it possible that we should do real
    > > > > checkpoint (flush & update
    > > > > redo lsn) when seeing checkpoint xlogs for these operations? This will
    > > > > slow down standby
    > > > > but master also does this and also these operations are not usual,
    > > > > espeically it seems that it
    > > > > does not slow down wal receiving usually?
    > >
    > > That dramatically slows recovery (not replication) if databases
    > > are created and deleted frequently. That wouldn't be acceptable.
    > >
    > 
    > This behavior is rare and seems to have the same impact on master & standby
    > from checkpoint/restartpoint.
    > We do not worry about master so we should not worry about standby also.
    
    I didn't mention replication. I said that that slows recovery,
    which is not governed by master's speed.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
    
  17. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-05-27T13:39:03Z

    On Tue, May 14, 2019 at 11:06 AM Kyotaro HORIGUCHI <
    horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    
    > Hello.
    >
    > At Mon, 13 May 2019 17:37:50 +0800, Paul Guo <pguo@pivotal.io> wrote in <
    > CAEET0ZF9yN4DaXyuFLzOcAYyxuFF1Ms_OQWeA+Rwv3GhA5Q-SA@mail.gmail.com>
    > > Thanks for the reply.
    > >
    > > On Tue, May 7, 2019 at 2:47 PM Kyotaro HORIGUCHI <
    > > horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    > >
    > > >
    > > > +      if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    > > > +      {
    > > >
    > > > This patch is allowing missing source and destination directory
    > > > even in consistent state. I don't think it is safe.
    > > >
    > >
    > > I do not understand this. Can you elaborate?
    >
    > Suppose we were recoverying based on a backup at LSN1 targeting
    > to LSN3 then it crashed at LSN2, where LSN1 < LSN2 <= LSN3. LSN2
    > is called as "consistency point", before where the database is
    > not consistent. It's because we are applying WAL recored older
    > than those that were already applied in the second trial. The
    > same can be said for crash recovery, where LSN1 is the latest
    > checkpoint ('s redo LSN) and LSN2=LSN3 is the crashed LSN.
    >
    > Creation of an existing directory or dropping of a non-existent
    > directory are apparently inconsistent or "broken" so we should
    > stop recovery when seeing such WAL records while database is in
    > consistent state.
    >
    
    This seems to be hard to detect. I thought using invalid_page mechanism
    long ago,
    but it seems to be hard to fully detect a dropped tablespace.
    
    > > > 2) Fixed dbase_desc(). Now the xlog output looks correct.
    > > > >
    > > > > rmgr: Database    len (rec/tot):     42/    42, tx:        486, lsn:
    > > > > 0/016386A8, prev 0/01638630, desc: CREATE copy dir base/1 to
    > > > > pg_tblspc/16384/PG_12_201904281/16386
    > > > >
    > > > > rmgr: Database    len (rec/tot):     34/    34, tx:        487, lsn:
    > > > > 0/01638EB8, prev 0/01638E40, desc: DROP dir
    > > > > pg_tblspc/16384/PG_12_201904281/16386
    > > >
    > > > WAL records don't convey such information. The previous
    > > > description seems right to me.
    > > >
    > >
    > > 2019-04-17 14:52:14.951 CST [23030] CONTEXT:  WAL redo at 0/3011650 for
    > > Database/CREATE: copy dir 1663/1 to 65546/65547
    > > The directories are definitely wrong and misleading.
    >
    > The original description is right in the light of how the server
    > recognizes. The record exactly says that "copy dir 1663/1 to
    > 65546/65547" and the latter path is converted in filesystem layer
    > via a symlink.
    >
    
    In either $PG_DATA/pg_tblspc or symlinked real tablespace directory,
    there is an additional directory like PG_12_201905221 between
    tablespace oid and database oid. See the directory layout as below,
    so the directory info in xlog dump output was not correct.
    
    $ ls -lh data/pg_tblspc/
    
    
    total 0
    
    
    lrwxrwxrwx. 1 gpadmin gpadmin 6 May 27 17:23 16384 -> /tmp/2
    
    
    $ ls -lh /tmp/2
    
    
    total 0
    
    
    drwx------. 3 gpadmin gpadmin 18 May 27 17:24 PG_12_201905221
    
    >
    >
    > > > > > Also I'd suggest we should use pg_mkdir_p() in
    > > > TablespaceCreateDbspace()
    > > > > > to replace
    > > > > > the code block includes a lot of
    > > > > > get_parent_directory(), MakePGDirectory(), etc even it
    > > > > > is not fixing a bug since pg_mkdir_p() code change seems to be more
    > > > > > graceful and simpler.
    > > >
    > > > But I don't agree to this. pg_mkdir_p goes above two-parents up,
    > > > which would be unwanted here.
    > > >
    > > > I do not understand this also. pg_mkdir_p() is similar to 'mkdir -p'.
    > > This change just makes the code concise. Though in theory the change is
    > not
    > > needed.
    >
    > We don't want to create tablespace direcotory after concurrent
    > DROPing, as the comment just above is saying:
    >
    > |  * Acquire TablespaceCreateLock to ensure that no DROP TABLESPACE
    > |  * or TablespaceCreateDbspace is running concurrently.
    >
    > If the concurrent DROP TABLESPACE destroyed the grand parent
    > directory, we mustn't create it again.
    >
    
    Yes, this is a good reason to keep the original code. Thanks.
    
    By the way, based on your previous test patch I added another test which
    could easily detect
    the missing src directory case.
    
  18. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-06-19T07:21:54Z

    On Mon, May 27, 2019 at 9:39 PM Paul Guo <pguo@pivotal.io> wrote:
    
    >
    >
    > On Tue, May 14, 2019 at 11:06 AM Kyotaro HORIGUCHI <
    > horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    >
    >> Hello.
    >>
    >> At Mon, 13 May 2019 17:37:50 +0800, Paul Guo <pguo@pivotal.io> wrote in <
    >> CAEET0ZF9yN4DaXyuFLzOcAYyxuFF1Ms_OQWeA+Rwv3GhA5Q-SA@mail.gmail.com>
    >> > Thanks for the reply.
    >> >
    >> > On Tue, May 7, 2019 at 2:47 PM Kyotaro HORIGUCHI <
    >> > horiguchi.kyotaro@lab.ntt.co.jp> wrote:
    >> >
    >> > >
    >> > > +      if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    >> > > +      {
    >> > >
    >> > > This patch is allowing missing source and destination directory
    >> > > even in consistent state. I don't think it is safe.
    >> > >
    >> >
    >> > I do not understand this. Can you elaborate?
    >>
    >> Suppose we were recoverying based on a backup at LSN1 targeting
    >> to LSN3 then it crashed at LSN2, where LSN1 < LSN2 <= LSN3. LSN2
    >> is called as "consistency point", before where the database is
    >> not consistent. It's because we are applying WAL recored older
    >> than those that were already applied in the second trial. The
    >> same can be said for crash recovery, where LSN1 is the latest
    >> checkpoint ('s redo LSN) and LSN2=LSN3 is the crashed LSN.
    >>
    >> Creation of an existing directory or dropping of a non-existent
    >> directory are apparently inconsistent or "broken" so we should
    >> stop recovery when seeing such WAL records while database is in
    >> consistent state.
    >>
    >
    > This seems to be hard to detect. I thought using invalid_page mechanism
    > long ago,
    > but it seems to be hard to fully detect a dropped tablespace.
    >
    > > > > 2) Fixed dbase_desc(). Now the xlog output looks correct.
    >> > > >
    >> > > > rmgr: Database    len (rec/tot):     42/    42, tx:        486, lsn:
    >> > > > 0/016386A8, prev 0/01638630, desc: CREATE copy dir base/1 to
    >> > > > pg_tblspc/16384/PG_12_201904281/16386
    >> > > >
    >> > > > rmgr: Database    len (rec/tot):     34/    34, tx:        487, lsn:
    >> > > > 0/01638EB8, prev 0/01638E40, desc: DROP dir
    >> > > > pg_tblspc/16384/PG_12_201904281/16386
    >> > >
    >> > > WAL records don't convey such information. The previous
    >> > > description seems right to me.
    >> > >
    >> >
    >> > 2019-04-17 14:52:14.951 CST [23030] CONTEXT:  WAL redo at 0/3011650 for
    >> > Database/CREATE: copy dir 1663/1 to 65546/65547
    >> > The directories are definitely wrong and misleading.
    >>
    >> The original description is right in the light of how the server
    >> recognizes. The record exactly says that "copy dir 1663/1 to
    >> 65546/65547" and the latter path is converted in filesystem layer
    >> via a symlink.
    >>
    >
    > In either $PG_DATA/pg_tblspc or symlinked real tablespace directory,
    > there is an additional directory like PG_12_201905221 between
    > tablespace oid and database oid. See the directory layout as below,
    > so the directory info in xlog dump output was not correct.
    >
    > $ ls -lh data/pg_tblspc/
    >
    >
    > total 0
    >
    >
    > lrwxrwxrwx. 1 gpadmin gpadmin 6 May 27 17:23 16384 -> /tmp/2
    >
    >
    > $ ls -lh /tmp/2
    >
    >
    > total 0
    >
    >
    > drwx------. 3 gpadmin gpadmin 18 May 27 17:24 PG_12_201905221
    >
    >>
    >>
    >> > > > > Also I'd suggest we should use pg_mkdir_p() in
    >> > > TablespaceCreateDbspace()
    >> > > > > to replace
    >> > > > > the code block includes a lot of
    >> > > > > get_parent_directory(), MakePGDirectory(), etc even it
    >> > > > > is not fixing a bug since pg_mkdir_p() code change seems to be
    >> more
    >> > > > > graceful and simpler.
    >> > >
    >> > > But I don't agree to this. pg_mkdir_p goes above two-parents up,
    >> > > which would be unwanted here.
    >> > >
    >> > > I do not understand this also. pg_mkdir_p() is similar to 'mkdir -p'.
    >> > This change just makes the code concise. Though in theory the change is
    >> not
    >> > needed.
    >>
    >> We don't want to create tablespace direcotory after concurrent
    >> DROPing, as the comment just above is saying:
    >>
    >> |  * Acquire TablespaceCreateLock to ensure that no DROP TABLESPACE
    >> |  * or TablespaceCreateDbspace is running concurrently.
    >>
    >> If the concurrent DROP TABLESPACE destroyed the grand parent
    >> directory, we mustn't create it again.
    >>
    >
    > Yes, this is a good reason to keep the original code. Thanks.
    >
    > By the way, based on your previous test patch I added another test which
    > could easily detect
    > the missing src directory case.
    >
    >
    
    I updated the patch to v3. In this version, we skip the error if copydir
    fails due to missing src/dst directory,
    but to make sure the ignoring is legal, I add a simple log/forget mechanism
    (Using List) similar to the xlog invalid page
    checking mechanism. Two tap tests are included. One is actually from a
    previous patch by Kyotaro in this
    email thread and another is added by me. In addition, dbase_desc() is fixed
    to make the message accurate.
    
    Thanks.
    
  19. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2019-07-08T03:15:35Z

    On Wed, Jun 19, 2019 at 7:22 PM Paul Guo <pguo@pivotal.io> wrote:
    > I updated the patch to v3. In this version, we skip the error if copydir fails due to missing src/dst directory,
    > but to make sure the ignoring is legal, I add a simple log/forget mechanism (Using List) similar to the xlog invalid page
    > checking mechanism. Two tap tests are included. One is actually from a previous patch by Kyotaro in this
    > email thread and another is added by me. In addition, dbase_desc() is fixed to make the message accurate.
    
    Hello Paul,
    
    FYI t/011_crash_recovery.pl is failing consistently on Travis CI with
    this patch applied:
    
    https://travis-ci.org/postgresql-cfbot/postgresql/builds/555368907
    
    -- 
    Thomas Munro
    https://enterprisedb.com
    
    
    
    
  20. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-07-15T10:52:01Z

    On Mon, Jul 8, 2019 at 11:16 AM Thomas Munro <thomas.munro@gmail.com> wrote:
    
    > On Wed, Jun 19, 2019 at 7:22 PM Paul Guo <pguo@pivotal.io> wrote:
    > > I updated the patch to v3. In this version, we skip the error if copydir
    > fails due to missing src/dst directory,
    > > but to make sure the ignoring is legal, I add a simple log/forget
    > mechanism (Using List) similar to the xlog invalid page
    > > checking mechanism. Two tap tests are included. One is actually from a
    > previous patch by Kyotaro in this
    > > email thread and another is added by me. In addition, dbase_desc() is
    > fixed to make the message accurate.
    >
    > Hello Paul,
    >
    > FYI t/011_crash_recovery.pl is failing consistently on Travis CI with
    > this patch applied:
    >
    >
    > https://urldefense.proofpoint.com/v2/url?u=https-3A__travis-2Dci.org_postgresql-2Dcfbot_postgresql_builds_555368907&d=DwIBaQ&c=lnl9vOaLMzsy2niBC8-h_K-7QJuNJEsFrzdndhuJ3Sw&r=Usi0ex6Ch92MsB5QQDgYFw&m=ABylo8AVfubiiYVbCBSgmNnHEMJhMqGXx5c0hkug7Vw&s=5h4m_JhrZwZqsRsu1CHCD3W2eBl14mT8jWLFsj2-bJ4&e=
    >
    >
    >
    This failure is because the previous v3 patch does not align with a recent
    patch
    
    commit 660a2b19038b2f6b9f6bcb2c3297a47d5e3557a8
    
    Author: Noah Misch <noah@leadboat.com>
    
    Date:   Fri Jun 21 20:34:23 2019 -0700
    
        Consolidate methods for translating a Perl path to a Windows path.
    
    
    My patch uses TestLib::real_dir which is now replaced
    with TestLib::perl2host in the above commit.
    
    I've updated the patch to v4 to make my code align. Now the test passes in
    my local environment.
    
    Please see the attached v4 patch.
    
    Thanks.
    
  21. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2019-08-01T22:37:46Z

    On Mon, Jul 15, 2019 at 10:52 PM Paul Guo <pguo@pivotal.io> wrote:
    > Please see the attached v4 patch.
    
    While moving this to the next CF, I noticed that this needs updating
    for the new pg_list.h API.
    
    -- 
    Thomas Munro
    https://enterprisedb.com
    
    
    
    
  22. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-08-22T13:13:20Z

    Thanks. I updated the patch to v5. It passes install-check testing and
    recovery testing.
    
    On Fri, Aug 2, 2019 at 6:38 AM Thomas Munro <thomas.munro@gmail.com> wrote:
    
    > On Mon, Jul 15, 2019 at 10:52 PM Paul Guo <pguo@pivotal.io> wrote:
    > > Please see the attached v4 patch.
    >
    > While moving this to the next CF, I noticed that this needs updating
    > for the new pg_list.h API.
    >
    > --
    > Thomas Munro
    >
    > https://urldefense.proofpoint.com/v2/url?u=https-3A__enterprisedb.com&d=DwIBaQ&c=lnl9vOaLMzsy2niBC8-h_K-7QJuNJEsFrzdndhuJ3Sw&r=Usi0ex6Ch92MsB5QQDgYFw&m=1zhC6VaaS7Ximav7vaUXMUt6EGjrVZpNZut32ug7LDI&s=jSDXnTPIW4WNZCCZ_HIbu7gZ3apEBx36DCeNeNuhLpY&e=
    >
    
  23. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Anastasia Lubennikova <a.lubennikova@postgrespro.ru> — 2019-08-22T16:13:05Z

    22.08.2019 16:13, Paul Guo wrote:
    > Thanks. I updated the patch to v5. It passes install-check testing and 
    > recovery testing.
    Hi,
    Thank you for working on this fix.
    The overall design of the latest version looks good to me.
    But during the review, I found a bug in the current implementation.
    New behavior must apply to crash-recovery only, now it applies to 
    archiveRecovery too.
    That can cause a silent loss of a tablespace during regular standby 
    operation
    since it never calls CheckRecoveryConsistency().
    
    Steps to reproduce:
    1) run master and replica
    2) create dir for tablespace:
    mkdir  /tmp/tblspc1
    
    3) create tablespace and database on the master:
    create tablespace tblspc1 location '/tmp/tblspc1';
    create database db1 tablespace tblspc1 ;
    
    4) wait for replica to receive this changes and pause replication:
    select pg_wal_replay_pause();
    
    5) move replica's tablespace symlink to some empty directory, i.e. 
    /tmp/tblspc2
    mkdir  /tmp/tblspc2
    ln -sfn /tmp/tblspc2 postgresql_data_replica/pg_tblspc/16384
    
    6) create another database in tblspc1 on master:
    create database db2 tablespace tblspc1 ;
    
    7) resume replication on standby:
    select pg_wal_replay_resume();
    
    8) try to connect to db2 on standby
    
    It's expected that dbase_redo() will fail because the directory on 
    standby is not found.
    While with the patch it suppresses the error until we attempt to connect 
    db2 on the standby:
    
    2019-08-22 18:34:39.178 MSK [21066] HINT:  Execute 
    pg_wal_replay_resume() to continue.
    2019-08-22 18:42:41.656 MSK [21066] WARNING:  Skip creating database 
    directory "pg_tblspc/16384/PG_13_201908012". The dest tablespace may 
    have been removed before abnormal shutdown. If the removal is illegal 
    after later checking we will panic.
    2019-08-22 18:42:41.656 MSK [21066] CONTEXT:  WAL redo at 0/3027738 for 
    Database/CREATE: copy dir base/1 to pg_tblspc/16384/PG_13_201908012/16390
    2019-08-22 18:42:46.096 MSK [21688] FATAL: 
    "pg_tblspc/16384/PG_13_201908012/16390" is not a valid data directory
    2019-08-22 18:42:46.096 MSK [21688] DETAIL:  File 
    "pg_tblspc/16384/PG_13_201908012/16390/PG_VERSION" is missing.
    
    Also some nitpicking about code style:
    1) Please, add comment to forget_missing_directory().
    
    2) +               elog(LOG, "Directory \"%s\" was missing during 
    directory copying "
    I think we'd better update this message elevel to WARNING.
    
    3) Shouldn't we also move FlushDatabaseBuffers(xlrec->src_db_id); call under
         if (do_copydir) clause?
    I don't see a reason to flush pages that we won't use later.
    
    -- 
    Anastasia Lubennikova
    Postgres Professional: http://www.postgrespro.com
    The Russian Postgres Company
    
    
  24. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@2ndquadrant.com> — 2019-09-03T15:58:55Z

    On 2019-Aug-22, Anastasia Lubennikova wrote:
    
    > 22.08.2019 16:13, Paul Guo wrote:
    > > Thanks. I updated the patch to v5. It passes install-check testing and
    > > recovery testing.
    > Hi,
    > Thank you for working on this fix.
    > The overall design of the latest version looks good to me.
    > But during the review, I found a bug in the current implementation.
    > New behavior must apply to crash-recovery only, now it applies to
    > archiveRecovery too.
    
    Hello
    
    Paul, Kyotaro, are you working on updating this bugfix?  FWIW the latest
    patch submitted by Paul is still current and CFbot says it passes its
    own test, but from Anastasia's email it still needs a bit of work.
    
    Also: it would be good to have this new bogus scenario described by
    Anastasia covered by a new TAP test.  Anastasia, can we enlist you to
    write that?  Maybe Kyotaro?
    
    Thanks
    
    -- 
    Álvaro Herrera                https://www.2ndQuadrant.com/
    PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
    
    
    
    
  25. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2019-09-05T07:12:23Z

    On Tue, Sep 3, 2019 at 11:58 PM Alvaro Herrera <alvherre@2ndquadrant.com>
    wrote:
    
    > On 2019-Aug-22, Anastasia Lubennikova wrote:
    >
    > > 22.08.2019 16:13, Paul Guo wrote:
    > > > Thanks. I updated the patch to v5. It passes install-check testing and
    > > > recovery testing.
    > > Hi,
    > > Thank you for working on this fix.
    > > The overall design of the latest version looks good to me.
    > > But during the review, I found a bug in the current implementation.
    > > New behavior must apply to crash-recovery only, now it applies to
    > > archiveRecovery too.
    >
    > Hello
    >
    > Paul, Kyotaro, are you working on updating this bugfix?  FWIW the latest
    > patch submitted by Paul is still current and CFbot says it passes its
    > own test, but from Anastasia's email it still needs a bit of work.
    >
    > Also: it would be good to have this new bogus scenario described by
    > Anastasia covered by a new TAP test.  Anastasia, can we enlist you to
    > write that?  Maybe Kyotaro?
    >
    >
    Thanks Anastasia and Alvaro for comment and suggestion. Sorry I've been busy
    working on some non-PG stuffs recently. I've never worked on archive
    recovery,
    so I expect a bit more time after I'm free (hopefully several days later)
    to take a look.
    Of course Kyotaro, Anastasia or anyone feel free to address the concern
    before that.
    
  26. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    P <apraveen@pivotal.io> — 2019-09-10T11:42:55Z

    Hi Anastasia
    
    On Thu, Aug 22, 2019 at 9:43 PM Anastasia Lubennikova <
    a.lubennikova@postgrespro.ru> wrote:
    >
    > But during the review, I found a bug in the current implementation.
    > New behavior must apply to crash-recovery only, now it applies to
    archiveRecovery too.
    > That can cause a silent loss of a tablespace during regular standby
    operation
    > since it never calls CheckRecoveryConsistency().
    >
    > Steps to reproduce:
    > 1) run master and replica
    > 2) create dir for tablespace:
    > mkdir  /tmp/tblspc1
    >
    > 3) create tablespace and database on the master:
    > create tablespace tblspc1 location '/tmp/tblspc1';
    > create database db1 tablespace tblspc1 ;
    >
    > 4) wait for replica to receive this changes and pause replication:
    > select pg_wal_replay_pause();
    >
    > 5) move replica's tablespace symlink to some empty directory, i.e.
    /tmp/tblspc2
    > mkdir  /tmp/tblspc2
    > ln -sfn /tmp/tblspc2 postgresql_data_replica/pg_tblspc/16384
    >
    
    By changing the tablespace symlink target, we are silently nullifying
    effects of a committed transaction from the standby data directory - the
    directory structure created by the standby for create tablespace
    transaction.  This step, therefore, does not look like a valid test case to
    me.  Can you share a sequence of steps that does not involve changing data
    directory manually?
    
    >
    > Also some nitpicking about code style:
    > 1) Please, add comment to forget_missing_directory().
    >
    > 2) +               elog(LOG, "Directory \"%s\" was missing during
    directory copying "
    > I think we'd better update this message elevel to WARNING.
    >
    > 3) Shouldn't we also move FlushDatabaseBuffers(xlrec->src_db_id); call
    under
    >     if (do_copydir) clause?
    > I don't see a reason to flush pages that we won't use later.
    >
    
    Thank you for the review feedback.  I agree with all the points.  Let me
    incorporate them (I plan to pick this work up and drive it to completion as
    Paul got busy with other things).
    
    But before that I'm revisiting another solution upthread, that of creating
    restart points when replaying create/drop database commands before making
    filesystem changes such as removing a directory.  The restart points should
    align with checkpoints on master.  The concern against this solution was
    creation of restart points will slow down recovery.  I don't think crash
    recovery is affected by this solution because of the already existing
    enforcement of checkpoints.  WAL records prior to a create/drop database
    will not be seen by crash recovery due to the checkpoint enforced during
    the command's normal execution.
    
    Asim
    
  27. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Anastasia Lubennikova <a.lubennikova@postgrespro.ru> — 2019-09-11T14:26:44Z

    10.09.2019 14:42, Asim R P wrote:
    > Hi Anastasia
    >
    > On Thu, Aug 22, 2019 at 9:43 PM Anastasia Lubennikova 
    > <a.lubennikova@postgrespro.ru <mailto:a.lubennikova@postgrespro.ru>> 
    > wrote:
    > >
    > > But during the review, I found a bug in the current implementation.
    > > New behavior must apply to crash-recovery only, now it applies to 
    > archiveRecovery too.
    > > That can cause a silent loss of a tablespace during regular standby 
    > operation
    > > since it never calls CheckRecoveryConsistency().
    > >
    > > Steps to reproduce:
    > > 1) run master and replica
    > > 2) create dir for tablespace:
    > > mkdir  /tmp/tblspc1
    > >
    > > 3) create tablespace and database on the master:
    > > create tablespace tblspc1 location '/tmp/tblspc1';
    > > create database db1 tablespace tblspc1 ;
    > >
    > > 4) wait for replica to receive this changes and pause replication:
    > > select pg_wal_replay_pause();
    > >
    > > 5) move replica's tablespace symlink to some empty directory, i.e. 
    > /tmp/tblspc2
    > > mkdir  /tmp/tblspc2
    > > ln -sfn /tmp/tblspc2 postgresql_data_replica/pg_tblspc/16384
    > >
    >
    > By changing the tablespace symlink target, we are silently nullifying 
    > effects of a committed transaction from the standby data directory - 
    > the directory structure created by the standby for create tablespace 
    > transaction.  This step, therefore, does not look like a valid test 
    > case to me.  Can you share a sequence of steps that does not involve 
    > changing data directory manually?
    >
    Hi, the whole idea of the test is to reproduce a data loss. For example, 
    if the disk containing this tablespace failed.
    Probably, simply deleting the directory 
    'postgresql_data_replica/pg_tblspc/16384'
    would work as well, though I was afraid that it can be caught by some 
    earlier checks and my example won't be so illustrative.
    >
    > Thank you for the review feedback.  I agree with all the points.  Let 
    > me incorporate them (I plan to pick this work up and drive it to 
    > completion as Paul got busy with other things).
    >
    > But before that I'm revisiting another solution upthread, that of 
    > creating restart points when replaying create/drop database commands 
    > before making filesystem changes such as removing a directory.  The 
    > restart points should align with checkpoints on master.  The concern 
    > against this solution was creation of restart points will slow down 
    > recovery.  I don't think crash recovery is affected by this solution 
    > because of the already existing enforcement of checkpoints.  WAL 
    > records prior to a create/drop database will not be seen by crash 
    > recovery due to the checkpoint enforced during the command's normal 
    > execution.
    >
    
    I haven't measured the impact of generating extra restart points in 
    previous solution, so I cannot tell whether concerns upthread are 
    justified.  Still, I enjoy latest design more, since it is clear and 
    similar with the code of checking unexpected uninitialized pages. In 
    principle it works. And the issue, I described in previous review can be 
    easily fixed by several additional checks of InHotStandby macro.
    
    -- 
    Anastasia Lubennikova
    Postgres Professional: http://www.postgrespro.com
    The Russian Postgres Company
    
    
  28. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2019-09-12T08:35:36Z

    Hello.
    
    At Wed, 11 Sep 2019 17:26:44 +0300, Anastasia Lubennikova <a.lubennikova@postgrespro.ru> wrote in <a82a896b-93f0-c26c-b941-f5665131381b@postgrespro.ru>
    > 10.09.2019 14:42, Asim R P wrote:
    > > Hi Anastasia
    > >
    > > On Thu, Aug 22, 2019 at 9:43 PM Anastasia Lubennikova
    > > <a.lubennikova@postgrespro.ru <mailto:a.lubennikova@postgrespro.ru>>
    > > wrote:
    > > >
    > > > But during the review, I found a bug in the current implementation.
    > > > New behavior must apply to crash-recovery only, now it applies to
    > > > archiveRecovery too.
    > > > That can cause a silent loss of a tablespace during regular standby
    > > > operation
    > > > since it never calls CheckRecoveryConsistency().
    
    Yeah. We should take the same steps with redo operations on
    missing pages. Just record failure during inconsistent state then
    forget it if underlying tablespace is gone. If we had a record
    when we reached concsistency, we're in a serious situation and
    should stop recovery.  log_invalid_page forget_invalid_pages and
    CheckRecoveryConsistency are the entry points of the feature to
    understand.
    
    > > > Steps to reproduce:
    > > > 1) run master and replica
    > > > 2) create dir for tablespace:
    > > > mkdir  /tmp/tblspc1
    > > >
    > > > 3) create tablespace and database on the master:
    > > > create tablespace tblspc1 location '/tmp/tblspc1';
    > > > create database db1 tablespace tblspc1 ;
    > > >
    > > > 4) wait for replica to receive this changes and pause replication:
    > > > select pg_wal_replay_pause();
    > > >
    > > > 5) move replica's tablespace symlink to some empty directory,
    > > > i.e. /tmp/tblspc2
    > > > mkdir  /tmp/tblspc2
    > > > ln -sfn /tmp/tblspc2 postgresql_data_replica/pg_tblspc/16384
    > > >
    > >
    > > By changing the tablespace symlink target, we are silently nullifying
    > > effects of a committed transaction from the standby data directory -
    > > the directory structure created by the standby for create tablespace
    > > transaction.  This step, therefore, does not look like a valid test
    > > case to me.  Can you share a sequence of steps that does not involve
    > > changing data directory manually?
    
    I see it as the same. WAL is inconsistent with what happend on
    storage with the steps. Database is just broken.
    
    > Hi, the whole idea of the test is to reproduce a data loss. For
    > example, if the disk containing this tablespace failed.
    
    So, apparently we must start recovery from a backup before that
    failure happened in that case, and that should ends in success.
    
    # I remember that the start point of this patch is a crash after
    # table space drop subsequent to several operations within the
    # table space. Then, crash recovery fails at an operation in the
    # finally-removed tablespace. Is it right?
    
    > Probably, simply deleting the directory
    > 'postgresql_data_replica/pg_tblspc/16384'
    > would work as well, though I was afraid that it can be caught by some
    > earlier checks and my example won't be so illustrative.
    > >
    > > Thank you for the review feedback.  I agree with all the points.  Let
    > > me incorporate them (I plan to pick this work up and drive it to
    > > completion as Paul got busy with other things).
    > >
    > > But before that I'm revisiting another solution upthread, that of
    > > creating restart points when replaying create/drop database commands
    > > before making filesystem changes such as removing a directory.  The
    > > restart points should align with checkpoints on master.  The concern
    > > against this solution was creation of restart points will slow down
    > > recovery.  I don't think crash recovery is affected by this solution
    > > because of the already existing enforcement of checkpoints.  WAL
    > > records prior to a create/drop database will not be seen by crash
    > > recovery due to the checkpoint enforced during the command's normal
    > > execution.
    > >
    > 
    > I haven't measured the impact of generating extra restart points in
    > previous solution, so I cannot tell whether concerns upthread are
    > justified.  Still, I enjoy latest design more, since it is clear and
    > similar with the code of checking unexpected uninitialized pages. In
    > principle it works. And the issue, I described in previous review can
    > be easily fixed by several additional checks of InHotStandby macro.
    
    Generally we shouldn't trigger useless restart point for
    uncertain reasons. If standby crashes, it starts the next
    recovery from the latest *restart point*.  Even in that case what
    we should do is the same.
    
    Of course, for testing, we *should* establish a restartpoint
    manually in order to establish the prerequisite state.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  29. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    P <apraveen@pivotal.io> — 2019-09-12T12:02:41Z

    On Thu, Sep 12, 2019 at 2:05 PM Kyotaro Horiguchi <horikyota.ntt@gmail.com>
    wrote:
    >
    > Hello.
    >
    > At Wed, 11 Sep 2019 17:26:44 +0300, Anastasia Lubennikova <
    a.lubennikova@postgrespro.ru> wrote in <
    a82a896b-93f0-c26c-b941-f5665131381b@postgrespro.ru>
    > > 10.09.2019 14:42, Asim R P wrote:
    > > > Hi Anastasia
    > > >
    > > > On Thu, Aug 22, 2019 at 9:43 PM Anastasia Lubennikova
    > > > <a.lubennikova@postgrespro.ru <mailto:a.lubennikova@postgrespro.ru>>
    > > > wrote:
    > > > >
    > > > > But during the review, I found a bug in the current implementation.
    > > > > New behavior must apply to crash-recovery only, now it applies to
    > > > > archiveRecovery too.
    > > > > That can cause a silent loss of a tablespace during regular standby
    > > > > operation
    > > > > since it never calls CheckRecoveryConsistency().
    >
    > Yeah. We should take the same steps with redo operations on
    > missing pages. Just record failure during inconsistent state then
    > forget it if underlying tablespace is gone. If we had a record
    > when we reached concsistency, we're in a serious situation and
    > should stop recovery.  log_invalid_page forget_invalid_pages and
    > CheckRecoveryConsistency are the entry points of the feature to
    > understand.
    >
    
    Yes, I get it now.  I will adjust the patch written by Paul accordingly.
    
    >
    > # I remember that the start point of this patch is a crash after
    > # table space drop subsequent to several operations within the
    > # table space. Then, crash recovery fails at an operation in the
    > # finally-removed tablespace. Is it right?
    >
    
    That's correct.  Once the directories are removed from filesystem, any
    attempt to replay WAL records that depend on their existence fails.
    
    
    > > > But before that I'm revisiting another solution upthread, that of
    > > > creating restart points when replaying create/drop database commands
    > > > before making filesystem changes such as removing a directory.  The
    > > > restart points should align with checkpoints on master.  The concern
    > > > against this solution was creation of restart points will slow down
    > > > recovery.  I don't think crash recovery is affected by this solution
    > > > because of the already existing enforcement of checkpoints.  WAL
    > > > records prior to a create/drop database will not be seen by crash
    > > > recovery due to the checkpoint enforced during the command's normal
    > > > execution.
    > > >
    > >
    > > I haven't measured the impact of generating extra restart points in
    > > previous solution, so I cannot tell whether concerns upthread are
    > > justified.  Still, I enjoy latest design more, since it is clear and
    > > similar with the code of checking unexpected uninitialized pages. In
    > > principle it works. And the issue, I described in previous review can
    > > be easily fixed by several additional checks of InHotStandby macro.
    >
    > Generally we shouldn't trigger useless restart point for
    > uncertain reasons. If standby crashes, it starts the next
    > recovery from the latest *restart point*.  Even in that case what
    > we should do is the same.
    >
    
    The reason is quite clear to me - removing directories from filesystem
    break the ability to replay WAL records second time.  And we already create
    checkpoints during normal operation in such a case, so crash recovery on a
    master node does not suffer from this bug.  I've attached a patch that
    performs restart points during drop database replay, just for reference.
    It passes both the TAP tests written by Kyotaro and Paul.  I had to modify
    drop database WAL record a bit.
    
    Asim
    
  30. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    P <apraveen@pivotal.io> — 2019-09-19T11:59:59Z

    On Thu, Aug 22, 2019 at 6:44 PM Paul Guo <pguo@pivotal.io> wrote:
    >
    > Thanks. I updated the patch to v5. It passes install-check testing and
    recovery testing.
    >
    
    This patch contains one more bug, in addition to what Anastasia has found.
    If the test case in the patch is tweaked slightly, as follows, the standby
    crashes due to PANIC.
    
    --- a/src/test/recovery/t/011_crash_recovery.pl
    +++ b/src/test/recovery/t/011_crash_recovery.pl
    @@ -147,8 +147,6 @@ $node_standby->start;
     $node_master->poll_query_until(
            'postgres', 'SELECT count(*) = 1 FROM pg_stat_replication');
    
    -$node_master->safe_psql('postgres', "CREATE DATABASE db1 TABLESPACE ts1");
    -
     # Make sure to perform restartpoint after tablespace creation
     $node_master->wait_for_catchup($node_standby, 'replay',
    
     $node_master->lsn('replay'));
    @@ -156,7 +154,8 @@ $node_standby->safe_psql('postgres', 'CHECKPOINT');
    
     # Do immediate shutdown ...
     $node_master->safe_psql('postgres',
    -                                               q[ALTER DATABASE db1 SET
    TABLESPACE ts2;
    +                                               q[CREATE DATABASE db1
    TABLESPACE ts1;
    +                                                 ALTER DATABASE db1 SET
    TABLESPACE ts2;
                                                      DROP TABLESPACE ts1;]);
     $node_master->wait_for_catchup($node_standby, 'replay',
    
     $node_master->lsn('replay'));
    
    Notice the create additional create database in the above change.  That
    causes the same tablespace directory (ts1) logged twice in the list of
    missing directories.  At the end of crash recovery, there is one unmatched
    entry in the missing dirs list and the standby PANICs.
    
    Please find attached a couple of tests that are built on top of what was
    already written by Paul, Kyotaro.  The patch includes a test to demonstrate
    the above mentioned failure and a test case that my friend Alexandra wrote
    to implement the archive recovery scenario noted by Anastasia.
    
    In order to fix the test failures, we need to distinguish between a missing
    database directory and a missing tablespace directory.  And also add logic
    to forget missing directories during tablespace drop.  I am working on it.
    
    Asim
    
  31. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    P <apraveen@pivotal.io> — 2019-09-20T12:23:56Z

    On Thu, Sep 19, 2019 at 5:29 PM Asim R P <apraveen@pivotal.io> wrote:
    >
    > In order to fix the test failures, we need to distinguish between a
    missing database directory and a missing tablespace directory.  And also
    add logic to forget missing directories during tablespace drop.  I am
    working on it.
    
    Please find attached a solution that builds on what Paul has propose.  A
    hash table, similar to the invalid page hash table is used to track missing
    directory references.  A missing directory may be a tablespace or a
    database, based on whether the tablespace is found missing or the source
    database is found missing.  The crash recovery succeeds if the hash table
    is empty at the end.
    
    Asim
    
  32. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Anastasia Lubennikova <a.lubennikova@postgrespro.ru> — 2019-12-26T14:45:19Z

    20.09.2019 15:23, Asim R P wrote:
    > On Thu, Sep 19, 2019 at 5:29 PM Asim R P <apraveen@pivotal.io 
    > <mailto:apraveen@pivotal.io>> wrote:
    > >
    > > In order to fix the test failures, we need to distinguish between a 
    > missing database directory and a missing tablespace directory.  And 
    > also add logic to forget missing directories during tablespace drop.  
    > I am working on it.
    >
    > Please find attached a solution that builds on what Paul has propose.  
    > A hash table, similar to the invalid page hash table is used to track 
    > missing directory references.  A missing directory may be a tablespace 
    > or a database, based on whether the tablespace is found missing or the 
    > source database is found missing.  The crash recovery succeeds if the 
    > hash table is empty at the end.
    >
    The v6-0003 patch had merge conflicts due to the recent 
    'xl_dbase_drop_rec' change, so I rebased it.
    See v7-0003 in attachment. Changes are pretty straightforward, though It 
    would be great, if you could check them once more.
    
    Newly introduced test 4 in t/011_crash_recovery.pl fails without the 
    patch and passes with it.
    It seems to me that everything is fine, so I mark it "Ready For Committer"
    
    -- 
    Anastasia Lubennikova
    Postgres Professional: http://www.postgrespro.com
    The Russian Postgres Company
    
    
  33. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@2ndquadrant.com> — 2020-01-10T00:22:45Z

    I looked at this a little while and was bothered by the perl changes; it
    seems out of place to have RecursiveCopy be thinking about tablespaces,
    which is way out of its league.  So I rewrote that to use a callback:
    the PostgresNode code passes a callback that's in charge to handle the
    case of a symlink.  Things look much more in place with that.  I didn't
    verify that all places that should use this are filled.
    
    In 0002 I found adding a new function unnecessary: we can keep backwards
    compat by checking 'ref' of the third argument.  With that we don't have
    to add a new function.  (POD changes pending.)
    
    I haven't reviewed 0003.
    
    v8 of all these patches attached.
    
    "git am" told me your 0001 was in unrecognized format.  It applied fine
    with "patch".  I suggest that if you're going to submit a series with
    commit messages and all, please use "git format-patch" with the same
    "-v" argument (9 in this case) for all patches.
    
    -- 
    Álvaro Herrera                https://www.2ndQuadrant.com/
    PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
    
  34. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@2ndquadrant.com> — 2020-01-10T13:43:35Z

    On 2020-Jan-09, Alvaro Herrera wrote:
    
    > I looked at this a little while and was bothered by the perl changes; it
    > seems out of place to have RecursiveCopy be thinking about tablespaces,
    > which is way out of its league.  So I rewrote that to use a callback:
    > the PostgresNode code passes a callback that's in charge to handle the
    > case of a symlink.  Things look much more in place with that.  I didn't
    > verify that all places that should use this are filled.
    > 
    > In 0002 I found adding a new function unnecessary: we can keep backwards
    > compat by checking 'ref' of the third argument.  With that we don't have
    > to add a new function.  (POD changes pending.)
    
    I forgot to add that something in these changes is broken (probably the
    symlink handling callback) so the tests fail, but I couldn't stay away
    from my daughter's birthday long enough to figure out what or how.  I'm
    on something else today, so if one of you can research and submit fixed
    versions, that'd be great.
    
    Thanks,
    
    -- 
    Álvaro Herrera                https://www.2ndQuadrant.com/
    PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
    
    
    
    
  35. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2020-01-13T10:27:16Z

    On Fri, Jan 10, 2020 at 9:43 PM Alvaro Herrera <alvherre@2ndquadrant.com>
    wrote:
    
    > On 2020-Jan-09, Alvaro Herrera wrote:
    >
    > > I looked at this a little while and was bothered by the perl changes; it
    > > seems out of place to have RecursiveCopy be thinking about tablespaces,
    > > which is way out of its league.  So I rewrote that to use a callback:
    > > the PostgresNode code passes a callback that's in charge to handle the
    > > case of a symlink.  Things look much more in place with that.  I didn't
    > > verify that all places that should use this are filled.
    > >
    > > In 0002 I found adding a new function unnecessary: we can keep backwards
    > > compat by checking 'ref' of the third argument.  With that we don't have
    > > to add a new function.  (POD changes pending.)
    >
    > I forgot to add that something in these changes is broken (probably the
    > symlink handling callback) so the tests fail, but I couldn't stay away
    > from my daughter's birthday long enough to figure out what or how.  I'm
    > on something else today, so if one of you can research and submit fixed
    > versions, that'd be great.
    >
    > Thanks,
    >
    
    I spent some time on this before getting off work today.
    
    With below fix, the 4th test is now ok but the 5th (last one) hangs due to
    panic.
    
    (gdb) bt
    #0  0x0000003397e32625 in raise () from /lib64/libc.so.6
    #1  0x0000003397e33e05 in abort () from /lib64/libc.so.6
    #2  0x0000000000a90506 in errfinish (dummy=0) at elog.c:590
    #3  0x0000000000a92b4b in elog_finish (elevel=22, fmt=0xb2d580 "cannot find
    directory %s tablespace %d database %d") at elog.c:1465
    #4  0x000000000057aa0a in XLogLogMissingDir (spcNode=16384, dbNode=0,
    path=0x1885100 "pg_tblspc/16384/PG_13_202001091/16389") at xlogutils.c:104
    #5  0x000000000065e92e in dbase_redo (record=0x1841568) at dbcommands.c:2225
    #6  0x000000000056ac94 in StartupXLOG () at xlog.c:7200
    
    
    diff --git a/src/include/commands/dbcommands.h
    b/src/include/commands/dbcommands.h
    index b71b400e700..f8f6d5ffd03 100644
    --- a/src/include/commands/dbcommands.h
    +++ b/src/include/commands/dbcommands.h
    @@ -19,8 +19,6 @@
     #include "lib/stringinfo.h"
     #include "nodes/parsenodes.h"
    
    -extern void CheckMissingDirs4DbaseRedo(void);
    -
     extern Oid createdb(ParseState *pstate, const CreatedbStmt *stmt);
     extern void dropdb(const char *dbname, bool missing_ok, bool force);
     extern void DropDatabase(ParseState *pstate, DropdbStmt *stmt);
    diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
    index e6e7ea505d9..4eef8bb1985 100644
    --- a/src/test/perl/PostgresNode.pm
    +++ b/src/test/perl/PostgresNode.pm
    @@ -615,11 +615,11 @@ sub _srcsymlink
        my $srcrealdir = readlink($srcpath);
    
        opendir(my $dh, $srcrealdir);
    -   while (readdir $dh)
    +   while (my $entry = (readdir $dh))
        {
    -       next if (/^\.\.?$/);
    -       my $spath = "$srcrealdir/$_";
    -       my $dpath = "$dstrealdir/$_";
    +       next if ($entry eq '.' or $entry eq '..');
    +       my $spath = "$srcrealdir/$entry";
    +       my $dpath = "$dstrealdir/$entry";
            RecursiveCopy::copypath($spath, $dpath);
        }
        closedir $dh;
    
  36. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <pguo@pivotal.io> — 2020-01-15T10:18:15Z

    I further fixed the last test failure (due to a small bug in the test, not
    in code). Attached are the new patch series. Let's see the CI pipeline
    result.
    
  37. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Fujii Masao <masao.fujii@oss.nttdata.com> — 2020-01-27T15:24:37Z

    
    On 2020/01/15 19:18, Paul Guo wrote:
    > I further fixed the last test failure (due to a small bug in the test, not in code). Attached are the new patch series. Let's see the CI pipeline result.
    
    Thanks for updating the patches!
    
    I started reading the 0003 patch.
    
    The approach that the 0003 patch uses is not the perfect solution.
    If the standby crashes after tblspc_redo() removes the directory and before
    its subsequent COMMIT record is replayed, PANIC error would occur since
    there can be some unresolved missing directory entries when we reach the
    consistent state. The problem would very rarely happen, though...
    Just idea; calling XLogFlush() to update the minimum recovery point just
    before tblspc_redo() performs destroy_tablespace_directories() may be
    safe and helpful for the problem?
    
    -		appendStringInfo(buf, "copy dir %u/%u to %u/%u",
    -						 xlrec->src_tablespace_id, xlrec->src_db_id,
    -						 xlrec->tablespace_id, xlrec->db_id);
    +		dbpath1 = GetDatabasePath(xlrec->src_db_id,  xlrec->src_tablespace_id);
    +		dbpath2 = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
    +		appendStringInfo(buf, "copy dir %s to %s", dbpath1, dbpath2);
    +		pfree(dbpath2);
    +		pfree(dbpath1);
    
    If the patch is for the bug fix and would be back-ported, the above change
    would lead to change pg_waldump's output for CREATE/DROP DATABASE between
    minor versions. IMO it's better to avoid such change and separate the above
    as a separate patch only for master.
    
    -			appendStringInfo(buf, " %u/%u",
    -							 xlrec->tablespace_ids[i], xlrec->db_id);
    +		{
    +			dbpath1 = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]);
    +			appendStringInfo(buf,  "%s", dbpath1);
    +			pfree(dbpath1);
    +		}
    
    Same as above.
    
    BTW, the above "%s" should be " %s", i.e., a space character needs to be
    appended to the head of "%s".
    
    +			get_parent_directory(parent_path);
    +			if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    +			{
    +				XLogLogMissingDir(xlrec->tablespace_id, InvalidOid, dst_path);
    
    The third argument of XLogLogMissingDir() should be parent_path instead of
    dst_path?
    
    +	if (hash_search(missing_dir_tab, &key, HASH_REMOVE, NULL) == NULL)
    +		elog(DEBUG2, "dir %s tablespace %d database %d is not missing",
    +			 path, spcNode, dbNode);
    
    I think that this elog() is useless and rather confusing.
    
    +		XLogForgetMissingDir(xlrec->ts_id, InvalidOid, "");
    
    The third argument should be set to the actual path instead of an empty
    string. Otherwise XLogForgetMissingDir() may emit a confusing DEBUG2
    message. Or the third argument of XLogForgetMissingDir() should be removed
    and the path in the DEBUG2 message should be calculated from the spcNode
    and dbNode in the hash entry in XLogForgetMissingDir().
    
    +#include "common/file_perm.h"
    
    This seems not necessary.
    
    Regards,
    
    -- 
    Fujii Masao
    NTT DATA CORPORATION
    Advanced Platform Technology Group
    Research and Development Headquarters
    
    
    
    
  38. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Fujii Masao <masao.fujii@oss.nttdata.com> — 2020-03-25T05:52:10Z

    
    On 2020/01/28 0:24, Fujii Masao wrote:
    > 
    > 
    > On 2020/01/15 19:18, Paul Guo wrote:
    >> I further fixed the last test failure (due to a small bug in the test, not in code). Attached are the new patch series. Let's see the CI pipeline result.
    > 
    > Thanks for updating the patches!
    > 
    > I started reading the 0003 patch.
    
    I marked this patch as Waiting on Author in CF because there is no update
    since my last review comments. Could you mark it as Needs Review again
    if you post the updated version of the patch.
    
    Regards,
    
    -- 
    Fujii Masao
    NTT DATA CORPORATION
    Advanced Platform Technology Group
    Research and Development Headquarters
    
    
    
    
  39. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Daniel Gustafsson <daniel@yesql.se> — 2020-07-07T21:12:30Z

    > On 25 Mar 2020, at 06:52, Fujii Masao <masao.fujii@oss.nttdata.com> wrote:
    > 
    > On 2020/01/28 0:24, Fujii Masao wrote:
    >> On 2020/01/15 19:18, Paul Guo wrote:
    >>> I further fixed the last test failure (due to a small bug in the test, not in code). Attached are the new patch series. Let's see the CI pipeline result.
    >> Thanks for updating the patches!
    >> I started reading the 0003 patch.
    > 
    > I marked this patch as Waiting on Author in CF because there is no update
    > since my last review comments. Could you mark it as Needs Review again
    > if you post the updated version of the patch.
    
    This thread has been stalled since effectively January, so I'm marking this
    patch Returned with Feedback.  Feel free to open a new entry once the review
    comments have been addressed.
    
    cheers ./daniel
    
    
    
  40. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <guopa@vmware.com> — 2020-07-08T12:56:44Z

    Looks like my previous reply was held for moderation (maybe due to my new email address).
    I configured my pg account today using the new email address. I guess this email would be
    held for moderation.
    
    I’m now replying my previous reply email and attaching the new patch series.
    
    
    On Jul 6, 2020, at 10:18 AM, Paul Guo <guopa@vmware.com<mailto:guopa@vmware.com>> wrote:
    
    Thanks for the review. I’m now re-picking up the work. I modified the code following the comments.
    Besides, I tweaked the test code a bit. There are several things I’m not 100% sure. Please see
    my replies below.
    
    On Jan 27, 2020, at 11:24 PM, Fujii Masao <masao.fujii@oss.nttdata.com<mailto:masao.fujii@oss.nttdata.com>> wrote:
    
    On 2020/01/15 19:18, Paul Guo wrote:
    I further fixed the last test failure (due to a small bug in the test, not in code). Attached are the new patch series. Let's see the CI pipeline result.
    
    Thanks for updating the patches!
    
    I started reading the 0003 patch.
    
    The approach that the 0003 patch uses is not the perfect solution.
    If the standby crashes after tblspc_redo() removes the directory and before
    its subsequent COMMIT record is replayed, PANIC error would occur since
    there can be some unresolved missing directory entries when we reach the
    consistent state. The problem would very rarely happen, though...
    Just idea; calling XLogFlush() to update the minimum recovery point just
    before tblspc_redo() performs destroy_tablespace_directories() may be
    safe and helpful for the problem?
    
    Yes looks like an issue. My understanding is the below scenario.
    
    XLogLogMissingDir()
    
    XLogFlush() in redo (e.g. in a commit redo).  <- create a minimum recovery point (we call it LSN_A).
    
    tblspc_redo()->XLogForgetMissingDir()
           <- If we panic immediately after we remove the directory in tblspc_redo()
           <- when we do replay during crash-recovery, we will check consistency at LSN_A and thus PANIC inXLogCheckMissingDirs()
    
    commit
    
    We should add a XLogFlush() in tblspc_redo(). This brings several other questions to my minds also.
    
    
    1. Should we call XLogFlush() in dbase_redo()  for XLOG_DBASE_DROP also?
       It calls both XLogDropDatabase() and XLogForgetMissingDir, which seem to have this issue also?
    
    2. xact_redo_abort() calls DropRelationFiles() also. Why do not we call XLogFlush() there?
    
    
    
    - appendStringInfo(buf, "copy dir %u/%u to %u/%u",
    -  xlrec->src_tablespace_id, xlrec->src_db_id,
    -  xlrec->tablespace_id, xlrec->db_id);
    + dbpath1 = GetDatabasePath(xlrec->src_db_id,  xlrec->src_tablespace_id);
    + dbpath2 = GetDatabasePath(xlrec->db_id, xlrec->tablespace_id);
    + appendStringInfo(buf, "copy dir %s to %s", dbpath1, dbpath2);
    + pfree(dbpath2);
    + pfree(dbpath1);
    
    If the patch is for the bug fix and would be back-ported, the above change
    would lead to change pg_waldump's output for CREATE/DROP DATABASE between
    minor versions. IMO it's better to avoid such change and separate the above
    as a separate patch only for master.
    
    I know we do not want wal format between minor releases, but does wal description string change
    between minor releases affect users? Anyone I’ll extract this part into a separate patch in the series
    since this change is actually independent of the other changes..
    
    
    - appendStringInfo(buf, " %u/%u",
    -  xlrec->tablespace_ids[i], xlrec->db_id);
    + {
    + dbpath1 = GetDatabasePath(xlrec->db_id, xlrec->tablespace_ids[i]);
    + appendStringInfo(buf,  "%s", dbpath1);
    + pfree(dbpath1);
    + }
    
    Same as above.
    
    BTW, the above "%s" should be " %s", i.e., a space character needs to be
    appended to the head of "%s”.
    
    OK
    
    
    + get_parent_directory(parent_path);
    + if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    + {
    + XLogLogMissingDir(xlrec->tablespace_id, InvalidOid, dst_path);
    
    The third argument of XLogLogMissingDir() should be parent_path instead of
    dst_path?
    
    The argument is for debug message printing so both should be fine, but admittedly we are
    logging for the tablespace directory so parent_path might be better.
    
    
    + if (hash_search(missing_dir_tab, &key, HASH_REMOVE, NULL) == NULL)
    + elog(DEBUG2, "dir %s tablespace %d database %d is not missing",
    +  path, spcNode, dbNode);
    
    I think that this elog() is useless and rather confusing.
    
    OK. Modified.
    
    
    + XLogForgetMissingDir(xlrec->ts_id, InvalidOid, "");
    
    The third argument should be set to the actual path instead of an empty
    string. Otherwise XLogForgetMissingDir() may emit a confusing DEBUG2
    message. Or the third argument of XLogForgetMissingDir() should be removed
    and the path in the DEBUG2 message should be calculated from the spcNode
    and dbNode in the hash entry in XLogForgetMissingDir().
    
    I’m now removing the third argument. Use GetDatabasePath() to get the path if database did I snot InvalidOid.
    
    
    +#include "common/file_perm.h"
    
    This seems not necessary.
    
    Right.
    
    
  41. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2021-01-05T01:07:24Z

    At Wed, 8 Jul 2020 12:56:44 +0000, Paul Guo <guopa@vmware.com> wrote in 
    > On 2020/01/15 19:18, Paul Guo wrote:
    > I further fixed the last test failure (due to a small bug in the test, not in code). Attached are the new patch series. Let's see the CI pipeline result.
    > 
    > Thanks for updating the patches!
    > 
    > I started reading the 0003 patch.
    > 
    > The approach that the 0003 patch uses is not the perfect solution.
    > If the standby crashes after tblspc_redo() removes the directory and before
    > its subsequent COMMIT record is replayed, PANIC error would occur since
    > there can be some unresolved missing directory entries when we reach the
    > consistent state. The problem would very rarely happen, though...
    > Just idea; calling XLogFlush() to update the minimum recovery point just
    > before tblspc_redo() performs destroy_tablespace_directories() may be
    > safe and helpful for the problem?
    
    It seems to me that what the current patch does is too complex.  What
    we need to do here is to remember every invalid operation then forget
    it when the prerequisite object is dropped.
    
    When a table space is dropped before consistency is established, we
    don't need to care what has been performed inside the tablespace.  In
    this perspective, it is enough to remember tablespace ids when failed
    to do something inside it due to the absence of the tablespace and
    then forget it when we remove it.  We could remember individual
    database id to show them in error messages, but I'm not sure it's
    useful.  The reason log_invalid_page records block numbers is to allow
    the machinery handle partial table truncations, but this is not the
    case since dropping tablespace cannot leave some of containing
    databases.
    
    As the result, we won't see an unresolved invalid operations in a
    dropped tablespace.
    
    Am I missing something?
    
    
    dbase_redo:
    +      if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    +      {
    +        XLogRecordMissingDir(xlrec->tablespace_id, InvalidOid, parent_path);
    
    This means "record the belonging table space directory if it is not
    found OR it is not a directory". The former can be valid but the
    latter is unconditionally can not (I don't think we bother considering
    symlinks there).
    
    +    /*
    +     * Source directory may be missing.  E.g. the template database used
    +     * for creating this database may have been dropped, due to reasons
    +     * noted above.  Moving a database from one tablespace may also be a
    +     * partner in the crime.
    +     */
    +    if (!(stat(src_path, &st) == 0 && S_ISDIR(st.st_mode)))
    +    {
    +      XLogLogMissingDir(xlrec->src_tablespace_id, xlrec->src_db_id, src_path);
    
    This is a part of *creation* of the target directory. Lack of the
    source directory cannot be valid even if the source directory is
    dropped afterwards in the WAL stream and we can allow that if the
    *target* tablespace is dropped afterwards. As the result, as I
    mentioned above, we don't need to record about the database directory.
    
    By the way the name XLogLogMiss.. is somewhat confusing. How about
    XLogReportMissingDir (named after report_invalid_page).
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  42. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <guopa@vmware.com> — 2021-01-27T08:36:22Z

    Thanks for the review, please see the replies below.
    
    > On Jan 5, 2021, at 9:07 AM, Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote:
    > 
    > At Wed, 8 Jul 2020 12:56:44 +0000, Paul Guo <guopa@vmware.com> wrote in 
    >> On 2020/01/15 19:18, Paul Guo wrote:
    >> I further fixed the last test failure (due to a small bug in the test, not in code). Attached are the new patch series. Let's see the CI pipeline result.
    >> 
    >> Thanks for updating the patches!
    >> 
    >> I started reading the 0003 patch.
    >> 
    >> The approach that the 0003 patch uses is not the perfect solution.
    >> If the standby crashes after tblspc_redo() removes the directory and before
    >> its subsequent COMMIT record is replayed, PANIC error would occur since
    >> there can be some unresolved missing directory entries when we reach the
    >> consistent state. The problem would very rarely happen, though...
    >> Just idea; calling XLogFlush() to update the minimum recovery point just
    >> before tblspc_redo() performs destroy_tablespace_directories() may be
    >> safe and helpful for the problem?
    > 
    > It seems to me that what the current patch does is too complex.  What
    > we need to do here is to remember every invalid operation then forget
    > it when the prerequisite object is dropped.
    > 
    > When a table space is dropped before consistency is established, we
    > don't need to care what has been performed inside the tablespace.  In
    > this perspective, it is enough to remember tablespace ids when failed
    > to do something inside it due to the absence of the tablespace and
    > then forget it when we remove it.  We could remember individual
    > database id to show them in error messages, but I'm not sure it's
    > useful.  The reason log_invalid_page records block numbers is to allow
    > the machinery handle partial table truncations, but this is not the
    > case since dropping tablespace cannot leave some of containing
    > databases.
    > 
    > As the result, we won't see an unresolved invalid operations in a
    > dropped tablespace.
    > 
    > Am I missing something?
    
    Yes, removing the database id from the hash key in the log/forget code should
    be usually fine, but the previous code does stricter/safer checking.
    
    Consider the scenario:
    
    CREATE DATABASE newdb1 TEMPLATE template_db1;
    CREATE DATABASE newdb2 TEMPLATE template_db2; <- in case the template_db2 database directory is missing abnormally somehow.
    DROP DATABASE template_db1;
    
    The previous code could detect this but if we remove the database id in the code,
    this bad scenario is skipped.
    
    > 
    > 
    > dbase_redo:
    > +      if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
    > +      {
    > +        XLogRecordMissingDir(xlrec->tablespace_id, InvalidOid, parent_path);
    > 
    > This means "record the belonging table space directory if it is not
    > found OR it is not a directory". The former can be valid but the
    > latter is unconditionally can not (I don't think we bother considering
    > symlinks there).
    
    Again this is a safer check, in the case the parent_path is a file for example somehow,
    we should panic finally for the case and let the user checks and then does recovery again.
    
    > 
    > +    /*
    > +     * Source directory may be missing.  E.g. the template database used
    > +     * for creating this database may have been dropped, due to reasons
    > +     * noted above.  Moving a database from one tablespace may also be a
    > +     * partner in the crime.
    > +     */
    > +    if (!(stat(src_path, &st) == 0 && S_ISDIR(st.st_mode)))
    > +    {
    > +      XLogLogMissingDir(xlrec->src_tablespace_id, xlrec->src_db_id, src_path);
    > 
    > This is a part of *creation* of the target directory. Lack of the
    > source directory cannot be valid even if the source directory is
    > dropped afterwards in the WAL stream and we can allow that if the
    > *target* tablespace is dropped afterwards. As the result, as I
    > mentioned above, we don't need to record about the database directory.
    > 
    > By the way the name XLogLogMiss.. is somewhat confusing. How about
    > XLogReportMissingDir (named after report_invalid_page).
    
    Agree with you.
    
    Also your words remind me that we should skip the checking if the consistency point
    is reached.
    
    Here is a git diff against the previous patch. I’ll send out the new rebased patches after
    the consensus is reached.
    
    diff --git a/src/backend/access/transam/xlogutils.c b/src/backend/access/transam/xlogutils.c
    index 7ade385965..c8fe3fe228 100644
    --- a/src/backend/access/transam/xlogutils.c
    +++ b/src/backend/access/transam/xlogutils.c
    @@ -90,7 +90,7 @@ typedef struct xl_missing_dir
     static HTAB *missing_dir_tab = NULL;
    
     void
    -XLogLogMissingDir(Oid spcNode, Oid dbNode, char *path)
    +XLogReportMissingDir(Oid spcNode, Oid dbNode, char *path)
     {
     	xl_missing_dir_key key;
     	bool found;
    @@ -103,16 +103,6 @@ XLogLogMissingDir(Oid spcNode, Oid dbNode, char *path)
     	 */
     	Assert(OidIsValid(spcNode));
    
    -	if (reachedConsistency)
    -	{
    -		if (dbNode == InvalidOid)
    -			elog(PANIC, "cannot find directory %s (tablespace %d)",
    -				 path, spcNode);
    -		else
    -			elog(PANIC, "cannot find directory %s (tablespace %d database %d)",
    -				 path, spcNode, dbNode);
    -	}
    -
     	if (missing_dir_tab == NULL)
     	{
     		/* create hash table when first needed */
    diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
    index fbff422c3b..7bd6d4efd9 100644
    --- a/src/backend/commands/dbcommands.c
    +++ b/src/backend/commands/dbcommands.c
    @@ -2205,7 +2205,7 @@ dbase_redo(XLogReaderState *record)
     						(errmsg("some useless files may be left behind in old database directory \"%s\"",
     								dst_path)));
     		}
    -		else
    +		else if (!reachedConsistency)
     		{
     			/*
     			 * It is possible that drop tablespace record appearing later in
    @@ -2221,7 +2221,7 @@ dbase_redo(XLogReaderState *record)
     			get_parent_directory(parent_path);
     			if (!(stat(parent_path, &st) == 0 && S_ISDIR(st.st_mode)))
     			{
    -				XLogLogMissingDir(xlrec->tablespace_id, InvalidOid, parent_path);
    +				XLogReportMissingDir(xlrec->tablespace_id, InvalidOid, parent_path);
     				skip = true;
     				ereport(WARNING,
     						(errmsg("skipping create database WAL record"),
    @@ -2239,9 +2239,10 @@ dbase_redo(XLogReaderState *record)
     		 * noted above.  Moving a database from one tablespace may also be a
     		 * partner in the crime.
     		 */
    -		if (!(stat(src_path, &st) == 0 && S_ISDIR(st.st_mode)))
    +		if (!(stat(src_path, &st) == 0 && S_ISDIR(st.st_mode)) &&
    +			!reachedConsistency)
     		{
    -			XLogLogMissingDir(xlrec->src_tablespace_id, xlrec->src_db_id, src_path);
    +			XLogReportMissingDir(xlrec->src_tablespace_id, xlrec->src_db_id, src_path);
     			skip = true;
     			ereport(WARNING,
     					(errmsg("skipping create database WAL record"),
    @@ -2311,7 +2312,8 @@ dbase_redo(XLogReaderState *record)
     						(errmsg("some useless files may be left behind in old database directory \"%s\"",
     								dst_path)));
    
    -			XLogForgetMissingDir(xlrec->tablespace_ids[i], xlrec->db_id);
    +			if (!reachedConsistency)
    +				XLogForgetMissingDir(xlrec->tablespace_ids[i], xlrec->db_id);
    
     			pfree(dst_path);
     		}
    diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
    index 294c9676b4..15eaa757cc 100644
    --- a/src/backend/commands/tablespace.c
    +++ b/src/backend/commands/tablespace.c
    @@ -1534,7 +1534,8 @@ tblspc_redo(XLogReaderState *record)
     	{
     		xl_tblspc_drop_rec *xlrec = (xl_tblspc_drop_rec *) XLogRecGetData(record);
    
    -		XLogForgetMissingDir(xlrec->ts_id, InvalidOid);
    +		if (!reachedConsistency)
    +			XLogForgetMissingDir(xlrec->ts_id, InvalidOid);
    
     		XLogFlush(record->EndRecPtr);
    
    diff --git a/src/include/access/xlogutils.h b/src/include/access/xlogutils.h
    index da561af5ab..6561d9cebe 100644
    --- a/src/include/access/xlogutils.h
    +++ b/src/include/access/xlogutils.h
    @@ -23,7 +23,7 @@ extern void XLogDropDatabase(Oid dbid);
     extern void XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum,
     								 BlockNumber nblocks);
    
    -extern void XLogLogMissingDir(Oid spcNode, Oid dbNode, char *path);
    +extern void XLogReportMissingDir(Oid spcNode, Oid dbNode, char *path);
     extern void XLogForgetMissingDir(Oid spcNode, Oid dbNode);
     extern void XLogCheckMissingDirs(void);
    
    diff --git a/src/test/recovery/t/011_crash_recovery.pl b/src/test/recovery/t/011_crash_recovery.pl
    index 748200ebb5..95eb6d26cc 100644
    --- a/src/test/recovery/t/011_crash_recovery.pl
    +++ b/src/test/recovery/t/011_crash_recovery.pl
    @@ -141,7 +141,7 @@ $node_master->wait_for_catchup($node_standby, 'replay',
     $node_standby->safe_psql('postgres', 'CHECKPOINT');
    
     # Do immediate shutdown just after a sequence of CREAT DATABASE / DROP
    -# DATABASE / DROP TABLESPACE. This causes CREATE DATBASE WAL records
    +# DATABASE / DROP TABLESPACE. This causes CREATE DATABASE WAL records
    
    
  43. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@2ndquadrant.com> — 2021-03-27T14:23:16Z

    On 2021-Jan-27, Paul Guo wrote:
    
    > Here is a git diff against the previous patch. I’ll send out the new
    > rebased patches after the consensus is reached.
    
    Hmm, can you post a rebased set, where the points under discussion
    are marked in XXX comments explaining what the issue is?  This thread is
    long and old ago that it's pretty hard to navigate the whole thing in
    order to find out exactly what is being questioned.
    
    I think 0004 can be pushed without further ado, since it's a clear and
    simple fix.  0001 needs a comment about the new parameter in
    RecursiveCopy's POD documentation.
    
    As I understand, this is a backpatchable bug-fix.
    
    -- 
    Álvaro Herrera                            39°49'30"S 73°17'W
    
    
    
    
  44. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <guopa@vmware.com> — 2021-03-30T07:12:19Z

    On 2021/3/27, 10:23 PM, "Alvaro Herrera" <alvherre@2ndquadrant.com> wrote:
    
    >    Hmm, can you post a rebased set, where the points under discussion
    >   are marked in XXX comments explaining what the issue is?  This thread is
    >    long and old ago that it's pretty hard to navigate the whole thing in
    >    order to find out exactly what is being questioned.
    
    OK. Attached are the rebased version that includes the change I discussed
    in my previous reply. Also added POD documentation change for RecursiveCopy,
    and modified the patch to use the backup_options introduced in
    081876d75ea15c3bd2ee5ba64a794fd8ea46d794 for tablespace mapping.
    
    >    I think 0004 can be pushed without further ado, since it's a clear and
    >    simple fix.  0001 needs a comment about the new parameter in
    >    RecursiveCopy's POD documentation.
    
    Yeah, 0004 is no any risky. One concern seemed to be the compatibility of some
    WAL dump/analysis tools(?). I have no idea about this. But if we do not backport
    0004 we do not seem to need to worry about this.
    
    >    As I understand, this is a backpatchable bug-fix.
    
    Yes.
    
    Thanks.
    
    
  45. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Ibrar Ahmed <ibrar.ahmad@gmail.com> — 2021-07-09T19:37:57Z

    On Tue, Mar 30, 2021 at 12:12 PM Paul Guo <guopa@vmware.com> wrote:
    
    > On 2021/3/27, 10:23 PM, "Alvaro Herrera" <alvherre@2ndquadrant.com> wrote:
    >
    > >    Hmm, can you post a rebased set, where the points under discussion
    > >   are marked in XXX comments explaining what the issue is?  This thread
    > is
    > >    long and old ago that it's pretty hard to navigate the whole thing in
    > >    order to find out exactly what is being questioned.
    >
    > OK. Attached are the rebased version that includes the change I discussed
    > in my previous reply. Also added POD documentation change for
    > RecursiveCopy,
    > and modified the patch to use the backup_options introduced in
    > 081876d75ea15c3bd2ee5ba64a794fd8ea46d794 for tablespace mapping.
    >
    > >    I think 0004 can be pushed without further ado, since it's a clear and
    > >    simple fix.  0001 needs a comment about the new parameter in
    > >    RecursiveCopy's POD documentation.
    >
    > Yeah, 0004 is no any risky. One concern seemed to be the compatibility of
    > some
    > WAL dump/analysis tools(?). I have no idea about this. But if we do not
    > backport
    > 0004 we do not seem to need to worry about this.
    >
    > >    As I understand, this is a backpatchable bug-fix.
    >
    > Yes.
    >
    > Thanks.
    >
    > Patch does not apply successfully,
    http://cfbot.cputube.org/patch_33_2161.log
    
    Can you please rebase the patch.
    
    
    -- 
    Ibrar Ahmed
    
  46. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <guopa@vmware.com> — 2021-08-05T10:20:44Z

    Rebased.
    
  47. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Robert Haas <robertmhaas@gmail.com> — 2021-08-10T20:56:37Z

    On Thu, Aug 5, 2021 at 6:20 AM Paul Guo <guopa@vmware.com> wrote:
    > Rebased.
    
    The commit message for 0001 is not clear enough for me to understand
    what problem it's supposed to be fixing. The code comments aren't
    really either. They make it sound like there's some problem with
    copying symlinks but mostly they just talk about callbacks, which
    doesn't really help me understand what problem we'd have if we just
    didn't commit this (or reverted it later).
    
    I am not really convinced by Álvaro's claim that 0004 is a "fix"; I
    think I'd call it an improvement. But either way I agree that could
    just be committed.
    
    I haven't analyzed 0002 and 0003 yet.
    
    -- 
    Robert Haas
    EDB: http://www.enterprisedb.com
    
    
    
    
  48. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Paul Guo <paulguo@gmail.com> — 2021-08-11T07:58:51Z

    On Wed, Aug 11, 2021 at 4:56 AM Robert Haas <robertmhaas@gmail.com> wrote:
    >
    > On Thu, Aug 5, 2021 at 6:20 AM Paul Guo <guopa@vmware.com> wrote:
    > > Rebased.
    >
    > The commit message for 0001 is not clear enough for me to understand
    > what problem it's supposed to be fixing. The code comments aren't
    > really either. They make it sound like there's some problem with
    > copying symlinks but mostly they just talk about callbacks, which
    > doesn't really help me understand what problem we'd have if we just
    > didn't commit this (or reverted it later).
    
    Thanks for reviewing. Let me explain a bit. The patch series includes
    four patches.
    
    0001 and 0002 are test changes for the fix (0003).
       - 0001 is the test framework change that's needed by 0002.
       - 0002 is the test for the code fix (0003).
    0003 is the code change and the commit message explains the issue in detail.
    0004 as said is a small enhancement which is a bit independent of the
    previous patches.
    
    Basically the issue is that without the fix crash recovery might fail
    relevant to tablespace.
    Here is the log after I run the tests in 0001/0002 without the 0003 fix.
    
    2021-08-04 10:00:42.231 CST [875] FATAL:  could not create directory
    "pg_tblspc/16385/PG_15_202107261/16390": No such file or directory
    2021-08-04 10:00:42.231 CST [875] CONTEXT:  WAL redo at 0/3001320 for
    Database/CREATE: copy dir base/1 to
    pg_tblspc/16385/PG_15_202107261/16390
    
    
    >
    > I am not really convinced by Álvaro's claim that 0004 is a "fix"; I
    > think I'd call it an improvement. But either way I agree that could
    > just be committed.
    >
    > I haven't analyzed 0002 and 0003 yet.
    >
    > --
    > Robert Haas
    > EDB: http://www.enterprisedb.com
    >
    >
    
    
    -- 
    Paul Guo (Vmware)
    
    
    
    
  49. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Robert Haas <robertmhaas@gmail.com> — 2021-08-11T12:59:20Z

    On Wed, Aug 11, 2021 at 3:59 AM Paul Guo <paulguo@gmail.com> wrote:
    > Thanks for reviewing. Let me explain a bit. The patch series includes
    > four patches.
    >
    > 0001 and 0002 are test changes for the fix (0003).
    >    - 0001 is the test framework change that's needed by 0002.
    >    - 0002 is the test for the code fix (0003).
    > 0003 is the code change and the commit message explains the issue in detail.
    > 0004 as said is a small enhancement which is a bit independent of the
    > previous patches.
    >
    > Basically the issue is that without the fix crash recovery might fail
    > relevant to tablespace.
    > Here is the log after I run the tests in 0001/0002 without the 0003 fix.
    
    I do understand all of this, but I (or whoever might commit this)
    needs to also be able to understand specifically what each patch is
    doing.
    
    -- 
    Robert Haas
    EDB: http://www.enterprisedb.com
    
    
    
    
  50. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Tom Lane <tgl@sss.pgh.pa.us> — 2021-09-24T18:14:52Z

    Robert Haas <robertmhaas@gmail.com> writes:
    > The commit message for 0001 is not clear enough for me to understand
    > what problem it's supposed to be fixing. The code comments aren't
    > really either. They make it sound like there's some problem with
    > copying symlinks but mostly they just talk about callbacks, which
    > doesn't really help me understand what problem we'd have if we just
    > didn't commit this (or reverted it later).
    
    > I am not really convinced by Álvaro's claim that 0004 is a "fix"; I
    > think I'd call it an improvement. But either way I agree that could
    > just be committed.
    
    > I haven't analyzed 0002 and 0003 yet.
    
    I took a quick look through this:
    
    * I don't like 0001 either, though it seems like the issue is mostly
    documentation.  sub _srcsymlink should have a comment explaining
    what it's doing and why.  The documentation of copypath's new parameter
    seems like gobbledegook too --- I suppose it should read more like
    "By default, copypath fails if a source item is a symlink.  But if
    B<srcsymlinkfn> is provided, that subroutine is called to process any
    symlink."
    
    * I'm allergic to 0002's completely undocumented changes to
    poll_query_until, especially since I don't see anything in the
    patch that actually uses them.  Can't we just drop these diffs
    in PostgresNode.pm?  BTW, the last error message in the patch,
    talking about a 5-second timeout, seems wrong.  With or without
    these changes, poll_query_until's default timeout is 180 sec.
    The actual test case might be okay other than that nit and a
    comment typo or two.
    
    * 0003 might actually be okay.  I've not read it line-by-line,
    but it seems like it's implementing a sane solution and it's
    adequately commented.
    
    * I'm inclined to reject 0004 out of hand, because I don't
    agree with what it's doing.  The purpose of the rmgrdesc
    functions is to show you what is in the WAL records, and
    everywhere else we interpret that as "show the verbatim,
    numeric field contents".  heapdesc.c, for example, doesn't
    attempt to look up the name of the table being operated on.
    0004 isn't adhering to that style, and aside from being
    inconsistent I'm afraid that it's adding failure modes
    we don't want.
    
    			regards, tom lane
    
    
    
    
  51. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Daniel Gustafsson <daniel@yesql.se> — 2021-11-04T12:34:33Z

    > On 24 Sep 2021, at 20:14, Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > 
    > Robert Haas <robertmhaas@gmail.com> writes:
    >> The commit message for 0001 is not clear enough for me to understand
    >> what problem it's supposed to be fixing. The code comments aren't
    >> really either. They make it sound like there's some problem with
    >> copying symlinks but mostly they just talk about callbacks, which
    >> doesn't really help me understand what problem we'd have if we just
    >> didn't commit this (or reverted it later).
    > 
    >> I am not really convinced by Álvaro's claim that 0004 is a "fix"; I
    >> think I'd call it an improvement. But either way I agree that could
    >> just be committed.
    > 
    >> I haven't analyzed 0002 and 0003 yet.
    > 
    > I took a quick look through this:
    > 
    > * I don't like 0001 either, though it seems like the issue is mostly
    > documentation.  sub _srcsymlink should have a comment explaining
    > what it's doing and why.  The documentation of copypath's new parameter
    > seems like gobbledegook too --- I suppose it should read more like
    > "By default, copypath fails if a source item is a symlink.  But if
    > B<srcsymlinkfn> is provided, that subroutine is called to process any
    > symlink."
    > 
    > * I'm allergic to 0002's completely undocumented changes to
    > poll_query_until, especially since I don't see anything in the
    > patch that actually uses them.  Can't we just drop these diffs
    > in PostgresNode.pm?  BTW, the last error message in the patch,
    > talking about a 5-second timeout, seems wrong.  With or without
    > these changes, poll_query_until's default timeout is 180 sec.
    > The actual test case might be okay other than that nit and a
    > comment typo or two.
    > 
    > * 0003 might actually be okay.  I've not read it line-by-line,
    > but it seems like it's implementing a sane solution and it's
    > adequately commented.
    > 
    > * I'm inclined to reject 0004 out of hand, because I don't
    > agree with what it's doing.  The purpose of the rmgrdesc
    > functions is to show you what is in the WAL records, and
    > everywhere else we interpret that as "show the verbatim,
    > numeric field contents".  heapdesc.c, for example, doesn't
    > attempt to look up the name of the table being operated on.
    > 0004 isn't adhering to that style, and aside from being
    > inconsistent I'm afraid that it's adding failure modes
    > we don't want.
    
    This patch again fails to apply (seemingly from the Perl namespace work on the
    testcode), and needs a few updates as per the above review.
    
    --
    Daniel Gustafsson		https://vmware.com/
    
    
    
    
    
  52. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2021-11-08T08:55:16Z

    At Thu, 4 Nov 2021 13:34:33 +0100, Daniel Gustafsson <daniel@yesql.se> wrote in 
    > This patch again fails to apply (seemingly from the Perl namespace work on the
    > testcode), and needs a few updates as per the above review.
    
    Rebased the latest patch removing some of the chages.
    
    0001: (I don't remember about this, though) I don't see how to make it
    work on Windows.  Anyway the next step would be to write comments.
    
    0002: I didin't see it in details and didn't check if it finds the
    issue but it actually scceeds with the fix.  The change to
    poll_query_until is removed since it doesn't seem actually used.
    
    0003: The fix. I didn't touch this.
    
    0004: Removed at all. I agree to Tom. (And I faintly remember that I
    said something like that.)
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  53. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Michael Paquier <michael@paquier.xyz> — 2021-11-09T03:51:15Z

    On Mon, Nov 08, 2021 at 05:55:16PM +0900, Kyotaro Horiguchi wrote:
    
    I have quickly looked at the patch set.
    
    > 0001: (I don't remember about this, though) I don't see how to make it
    > work on Windows.  Anyway the next step would be to write comments.
    
    Look at Utils.pm where we have dir_symlink, then.  symlink() does not
    work on WIN32, so we have a wrapper that uses junction points.  FWIW,
    I don't like much the behavior you are enforcing in init_from_backup
    when coldly copying a source path, but I have not looked enough at the
    patch set to have a strong opinion about this part, either.
    
    > 0002: I didn't see it in details and didn't check if it finds the
    > issue but it actually scceeds with the fix.  The change to
    > poll_query_until is removed since it doesn't seem actually used.
    
    +# Create tablespace
    +my $dropme_ts_master1 = PostgreSQL::Test::Utils::tempdir();
    +$dropme_ts_master1 =
    PostgreSQL::Test::Utils::perl2host($dropme_ts_master1);
    +my $dropme_ts_master2 = PostgreSQL::Test::Utils::tempdir();
    +$dropme_ts_master2 =
    PostgreSQL::Test::Utils::perl2host($dropme_ts_master2);
    +my $source_ts_master = PostgreSQL::Test::Utils::tempdir();
    +$source_ts_master =
    PostgreSQL::Test::Utils::perl2host($source_ts_master);
    +my $target_ts_master = PostgreSQL::Test::Utils::tempdir();
    +$target_ts_master =
    PostgreSQL::Test::Utils::perl2host($target_ts_master);
    
    Rather than creating N temporary directories, it would be simpler to
    create only one, and have subdirs in it for the rest?  It seems to me
    that it would make debugging much easier.  The uses of perl2host()
    seem sufficient.
    --
    Michael
    
  54. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2021-11-09T08:05:49Z

    At Tue, 9 Nov 2021 12:51:15 +0900, Michael Paquier <michael@paquier.xyz> wrote in 
    > On Mon, Nov 08, 2021 at 05:55:16PM +0900, Kyotaro Horiguchi wrote:
    > 
    > I have quickly looked at the patch set.
    > 
    > > 0001: (I don't remember about this, though) I don't see how to make it
    > > work on Windows.  Anyway the next step would be to write comments.
    > 
    > Look at Utils.pm where we have dir_symlink, then.  symlink() does not
    > work on WIN32, so we have a wrapper that uses junction points.  FWIW,
    > I don't like much the behavior you are enforcing in init_from_backup
    > when coldly copying a source path, but I have not looked enough at the
    > patch set to have a strong opinion about this part, either.
    
    Thanks for the info. If we can handle symlink on Windows, we don't
    need to have a cold copy.
    
    > > 0002: I didn't see it in details and didn't check if it finds the
    > > issue but it actually scceeds with the fix.  The change to
    > > poll_query_until is removed since it doesn't seem actually used.
    > 
    > +# Create tablespace
    > +my $dropme_ts_master1 = PostgreSQL::Test::Utils::tempdir();
    > +$dropme_ts_master1 =
    > PostgreSQL::Test::Utils::perl2host($dropme_ts_master1);
    > +my $dropme_ts_master2 = PostgreSQL::Test::Utils::tempdir();
    > +$dropme_ts_master2 =
    > PostgreSQL::Test::Utils::perl2host($dropme_ts_master2);
    > +my $source_ts_master = PostgreSQL::Test::Utils::tempdir();
    > +$source_ts_master =
    > PostgreSQL::Test::Utils::perl2host($source_ts_master);
    > +my $target_ts_master = PostgreSQL::Test::Utils::tempdir();
    > +$target_ts_master =
    > PostgreSQL::Test::Utils::perl2host($target_ts_master);
    > 
    > Rather than creating N temporary directories, it would be simpler to
    > create only one, and have subdirs in it for the rest?  It seems to me
    > that it would make debugging much easier.  The uses of perl2host()
    > seem sufficient.
    
    Thanks for the suggestion.  My eyeballs got hopping around looking
    that part so I gave up looking there in more detail:p I agree to that.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  55. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2021-11-10T08:14:11Z

    At Tue, 09 Nov 2021 17:05:49 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > At Tue, 9 Nov 2021 12:51:15 +0900, Michael Paquier <michael@paquier.xyz> wrote in 
    > > Look at Utils.pm where we have dir_symlink, then.  symlink() does not
    > > work on WIN32, so we have a wrapper that uses junction points.  FWIW,
    > > I don't like much the behavior you are enforcing in init_from_backup
    > > when coldly copying a source path, but I have not looked enough at the
    > > patch set to have a strong opinion about this part, either.
    > 
    > Thanks for the info. If we can handle symlink on Windows, we don't
    > need to have a cold copy.
    
    I bumped into the good-old 100-byte limit of the (v7?) tar format on
    which pg_basebackup is depending. It is unlikely in the real world but
    I think it is quite common in developping environment.  The tablespace
    directory path in my dev environment was 110 chacters-long.  As small
    as 10 bytes but it's quite annoying to chip off that number of bytes
    from the path..
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  56. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@2ndquadrant.com> — 2021-11-10T12:14:30Z

    On 2021-Nov-10, Kyotaro Horiguchi wrote:
    
    > I bumped into the good-old 100-byte limit of the (v7?) tar format on
    > which pg_basebackup is depending. It is unlikely in the real world but
    > I think it is quite common in developping environment.  The tablespace
    > directory path in my dev environment was 110 chacters-long.  As small
    > as 10 bytes but it's quite annoying to chip off that number of bytes
    > from the path..
    
    Can you use PostgreSQL::Test::Utils::tempdir_short() for those
    tablespaces?
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    
    
    
    
  57. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2021-11-11T02:13:52Z

    At Wed, 10 Nov 2021 09:14:30 -0300, Alvaro Herrera <alvherre@2ndquadrant.com> wrote in 
    > Can you use PostgreSQL::Test::Utils::tempdir_short() for those
    > tablespaces?
    
    Thanks for the suggestion!
    
    It works for a live cluster. But doesn't work for backups, since I
    find no way to relate a tablespace directory with a backup directory
    not using a symlink.  One way would be taking a backup with tentative
    tablespace directory in the short-named temporary directory then move
    it into the backup direcotry. I'm going that way for now.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  58. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2021-11-12T07:43:27Z

    At Thu, 11 Nov 2021 11:13:52 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > At Wed, 10 Nov 2021 09:14:30 -0300, Alvaro Herrera <alvherre@2ndquadrant.com> wrote in 
    > > Can you use PostgreSQL::Test::Utils::tempdir_short() for those
    > > tablespaces?
    > 
    > Thanks for the suggestion!
    > 
    > It works for a live cluster. But doesn't work for backups, since I
    > find no way to relate a tablespace directory with a backup directory
    > not using a symlink.  One way would be taking a backup with tentative
    > tablespace directory in the short-named temporary directory then move
    > it into the backup direcotry. I'm going that way for now.
    
    This is that.
    
    0001 adds several routines to handle tablespace directories, and adds
    tablespace support to backup/_backup_fs.
    
    We don't know an oid corresponding to a tablespace directory before
    actually assigning the oid to the tablespace.  So we cannot name a
    tablespace directory after the oid.  On the other hand, after defining
    the tablespace, cold data files don't tell the real directory name of
    the tablespace directory for an oid or a tablespace name, unless we
    have readlink.
    
    The function dir_readlink added to Utils.pm is that. Honestly I don't
    like the way function works. It uses "cmd /c "dir /A:L $dir"" to
    collect information of junctions. I'm not sure that the type label
    "<JUNCTION>" is immutable among locales but at least it is shown as
    "<JUNCTION>" on Japanese (CP-932) environment. I didn't actually
    tested it on Windows and msys environment ...yet.
    
    Premising the availability of the function, we can name tablespace
    directories from meaningful words.
    
    The directory to store tablespace directories can be a temporary
    directory, but with that way it is needed to create a symlink to find
    those directories from a backup.  I chose to place tablespace
    directories directly under backup directory.
    
    The attached first file is a revised (or remade) version of tablespace
    support for TAP test.
    
    The second is the version adapted to the revised framework. (I
    confirmed that the test actually detects the error.)
    
    The third is not changed at all.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  59. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2021-12-24T10:21:59Z

    Just a complaint..
    
    At Fri, 12 Nov 2021 16:43:27 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > "<JUNCTION>" on Japanese (CP-932) environment. I didn't actually
    > tested it on Windows and msys environment ...yet.
    
    Active perl cannot be installed because of (perhaps) a powershell
    version issue... Annoying..
    
    https://community.activestate.com/t/please-update-your-powershell-install-scripts/7897
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  60. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Julien Rouhaud <rjuju123@gmail.com> — 2022-01-16T04:43:03Z

    Hi,
    
    On Fri, Dec 24, 2021 at 07:21:59PM +0900, Kyotaro Horiguchi wrote:
    > Just a complaint..
    > 
    > At Fri, 12 Nov 2021 16:43:27 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > > "<JUNCTION>" on Japanese (CP-932) environment. I didn't actually
    > > tested it on Windows and msys environment ...yet.
    > 
    > Active perl cannot be installed because of (perhaps) a powershell
    > version issue... Annoying..
    > 
    > https://community.activestate.com/t/please-update-your-powershell-install-scripts/7897
    
    I'm not very familiar with windows, but maybe using strawberry perl instead
    ([1]) would fix your problem?  I think it's also quite popular and is commonly
    used to run pgBadger on Windows.
    
    Other than that, I see that the TAP tests are failing on all the environment,
    due to Perl errors.  For instance:
    
    [04:06:00.848] [04:05:54] t/003_promote.pl .....
    [04:06:00.848] Dubious, test returned 2 (wstat 512, 0x200)
    https://api.cirrus-ci.com/v1/artifact/task/4751213722861568/tap/src/bin/pg_basebackup/tmp_check/log/regress_log_020_pg_receivewal
    # Initializing node "standby" from backup "my_backup" of node "primary"
    Odd number of elements in hash assignment at /tmp/cirrus-ci-build/src/bin/pg_ctl/../../../src/test/perl/PostgreSQL/Test/Cluster.pm line 996.
    Use of uninitialized value in list assignment at /tmp/cirrus-ci-build/src/bin/pg_ctl/../../../src/test/perl/PostgreSQL/Test/Cluster.pm line 996.
    Use of uninitialized value $tsp in concatenation (.) or string at /tmp/cirrus-ci-build/src/bin/pg_ctl/../../../src/test/perl/PostgreSQL/Test/Cluster.pm line 1008.
    Use of uninitialized value $tsp in concatenation (.) or string at /tmp/cirrus-ci-build/src/bin/pg_ctl/../../../src/test/perl/PostgreSQL/Test/Cluster.pm line 1009.
    
    That's apparently the same problem on every failure reported.
    
    Can you send a fixed patchset?  In the meantime I will switch the cf entry to
    Waiting on Author.
    
    
    [1] https://strawberryperl.com/
    
    
    
    
  61. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-01-17T08:24:43Z

    At Sun, 16 Jan 2022 12:43:03 +0800, Julien Rouhaud <rjuju123@gmail.com> wrote in 
    > Hi,
    > 
    > On Fri, Dec 24, 2021 at 07:21:59PM +0900, Kyotaro Horiguchi wrote:
    > > Just a complaint..
    > > 
    > > At Fri, 12 Nov 2021 16:43:27 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > > > "<JUNCTION>" on Japanese (CP-932) environment. I didn't actually
    > > > tested it on Windows and msys environment ...yet.
    > > 
    > > Active perl cannot be installed because of (perhaps) a powershell
    > > version issue... Annoying..
    > > 
    > > https://community.activestate.com/t/please-update-your-powershell-install-scripts/7897
    > 
    > I'm not very familiar with windows, but maybe using strawberry perl instead
    > ([1]) would fix your problem?  I think it's also quite popular and is commonly
    > used to run pgBadger on Windows.
    
    Thanks! I'll try it later.
    
    > Other than that, I see that the TAP tests are failing on all the environment,
    > due to Perl errors.  For instance:
    > 
    > [04:06:00.848] [04:05:54] t/003_promote.pl .....
    > [04:06:00.848] Dubious, test returned 2 (wstat 512, 0x200)
    > https://api.cirrus-ci.com/v1/artifact/task/4751213722861568/tap/src/bin/pg_basebackup/tmp_check/log/regress_log_020_pg_receivewal
    > # Initializing node "standby" from backup "my_backup" of node "primary"
    > Odd number of elements in hash assignment at /tmp/cirrus-ci-build/src/bin/pg_ctl/../../../src/test/perl/PostgreSQL/Test/Cluster.pm line 996.
    > Use of uninitialized value in list assignment at /tmp/cirrus-ci-build/src/bin/pg_ctl/../../../src/test/perl/PostgreSQL/Test/Cluster.pm line 996.
    > Use of uninitialized value $tsp in concatenation (.) or string at /tmp/cirrus-ci-build/src/bin/pg_ctl/../../../src/test/perl/PostgreSQL/Test/Cluster.pm line 1008.
    > Use of uninitialized value $tsp in concatenation (.) or string at /tmp/cirrus-ci-build/src/bin/pg_ctl/../../../src/test/perl/PostgreSQL/Test/Cluster.pm line 1009.
    > 
    > That's apparently the same problem on every failure reported.
    > 
    > Can you send a fixed patchset?  In the meantime I will switch the cf entry to
    > Waiting on Author.
    
    I guess that failure came from a recent change that allows in-place
    tablespace directory.  I'll check it out.  Thanks!
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  62. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-01-20T04:25:41Z

    At Mon, 17 Jan 2022 17:24:43 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > At Sun, 16 Jan 2022 12:43:03 +0800, Julien Rouhaud <rjuju123@gmail.com> wrote in 
    > > I'm not very familiar with windows, but maybe using strawberry perl instead
    > > ([1]) would fix your problem?  I think it's also quite popular and is commonly
    > > used to run pgBadger on Windows.
    > 
    > Thanks! I'll try it later.
    
    Build is stopped by some unresolvable symbols.
    
    Strawberry perl is 5.28, which doesn't expose new_ctype, new_collate
    and new_numeric according the past discussion.  (Active perl is 5.32).
    
    https://www.postgresql.org/message-id/20200501134711.08750c5f%40antares.wagner.home
    
    However, the patch provided revealed other around 70 unresolved symbol
    errors...
    
    # Hmm. perl on CentOS 8 is 5.26..
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  63. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-01-20T06:07:22Z

    At Sun, 16 Jan 2022 12:43:03 +0800, Julien Rouhaud <rjuju123@gmail.com> wrote in 
    > Other than that, I see that the TAP tests are failing on all the environment,
    > due to Perl errors.  For instance:
    
    Perl seems to have changed its behavior for undef hash.
    
    It is said that "if (%undef_hash)" is false but actually it is true
    and "keys %undef_hash" is 1..  Finally I had to make
    backup_tablespaces() to return a hash reference.  The test of
    pg_basebackup takes a backup with tar mode, which broke the test
    infrastructure. Cluster::backup now skips symlink adjustment when the
    backup contains "/base.tar".
    
    I gave up testing on Windows on my own environment and used Cirrus CI.
    
    # However, it works for confirmation of a established code.  TAT of CI
    # is still long to do trial and error of unestablished code..
    
    This version works for Unixen but still doesn't for Windows. I'm
    searching for a fix for Windows.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  64. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-01-20T08:19:04Z

    At Thu, 20 Jan 2022 15:07:22 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > This version works for Unixen but still doesn't for Windows. I'm
    > searching for a fix for Windows.
    
    And this version works for Windows.  Maybe I've took a wrong version
    to post. dir_readlink manipulated target file (junction) name in the
    wrong way.
    
    CI now likes this version for all platforms.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  65. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-02T07:59:09Z

    At Thu, 20 Jan 2022 17:19:04 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > At Thu, 20 Jan 2022 15:07:22 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > CI now likes this version for all platforms.
    
    An xlog.c refactoring happend recently hit this.
    Just rebased on the change.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  66. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-02T10:31:24Z

    At Wed, 02 Mar 2022 16:59:09 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > At Thu, 20 Jan 2022 17:19:04 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > > At Thu, 20 Jan 2022 15:07:22 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > > CI now likes this version for all platforms.
    > 
    > An xlog.c refactoring happend recently hit this.
    > Just rebased on the change.
    
    A function added to Util.pm used perl2host, which has been removed
    recently.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  67. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-04T00:10:48Z

    At Wed, 02 Mar 2022 19:31:24 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > A function added to Util.pm used perl2host, which has been removed
    > recently.
    
    And same function contained a maybe-should-have-been-removed line
    which makes Windows build unhappy.
    
    This should make all platforms in the CI happy.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  68. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Michael Paquier <michael@paquier.xyz> — 2022-03-04T04:51:12Z

    On Fri, Mar 04, 2022 at 09:10:48AM +0900, Kyotaro Horiguchi wrote:
    > And same function contained a maybe-should-have-been-removed line
    > which makes Windows build unhappy.
    > 
    > This should make all platforms in the CI happy.
    
    d6d317d as solved the issue of tablespace paths across multiple nodes
    with the new GUC called allow_in_place_tablespaces, and is getting
    successfully used in the recovery tests as of 027_stream_regress.pl.
    
    Shouldn't we rely on that rather than extending more our test perl
    modules?  One tricky part is the emulation of readlink for junction
    points on Windows (dir_readlink in your patch), and the root of the
    problem is that 0003 cares about the path structure of the
    tablespaces so we have no need, as far as I can see, for any
    dependency with link follow-up in the scope of this patch.
    
    This means that you should be able to simplify the patch set, as we
    could entirely drop 0001 in favor of enforcing the new dev GUC in the
    nodes created in the TAP test of 0002.
    
    Speaking of 0002, perhaps this had better be in its own file rather
    than extending more 011_crash_recovery.pl.  0003 looks like a good
    idea to check after the consistency of the path structures created
    during replay, and it touches paths I'd expect it to touch, as of
    database and tbspace redos.
    
    +       if (!reachedConsistency)
    +           XLogForgetMissingDir(xlrec->ts_id, InvalidOid);
    +
    +       XLogFlush(record->EndRecPtr);
    Not sure to understand why this is required.  A comment may be in
    order to explain the hows and the whys.
    --
    Michael
    
  69. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-04T06:30:57Z

    Thanks to look this!
    
    At Fri, 4 Mar 2022 13:51:12 +0900, Michael Paquier <michael@paquier.xyz> wrote i
    n 
    > On Fri, Mar 04, 2022 at 09:10:48AM +0900, Kyotaro Horiguchi wrote:
    > > And same function contained a maybe-should-have-been-removed line
    > > which makes Windows build unhappy.
    > > 
    > > This should make all platforms in the CI happy.
    > 
    > d6d317d as solved the issue of tablespace paths across multiple nodes
    > with the new GUC called allow_in_place_tablespaces, and is getting
    > successfully used in the recovery tests as of 027_stream_regress.pl.
    
    The feature allows only one tablespace directory. but that uses (I'm
    not sure it needs, though) multiple tablespace directories so I think
    the feature doesn't work for the test.
    
    Maybe I'm missing something, but it doesn't use tablespaces.  I see
    that in 002_tablespace.pl but but the test uses only one tablespace
    location.
    
    > Shouldn't we rely on that rather than extending more our test perl
    > modules?  One tricky part is the emulation of readlink for junction
    > points on Windows (dir_readlink in your patch), and the root of the
    
    Yeah, I don't like that as I said before...
    
    > problem is that 0003 cares about the path structure of the
    > tablespaces so we have no need, as far as I can see, for any
    > dependency with link follow-up in the scope of this patch.
    
    I'm not sure how this related to 0001 but maybe I don't follow this.
    
    > This means that you should be able to simplify the patch set, as we
    > could entirely drop 0001 in favor of enforcing the new dev GUC in the
    > nodes created in the TAP test of 0002.
    
    Maybe it's possible by breaking the test into ones that need only one
    tablespace.  I'll give it a try.
    
    > Speaking of 0002, perhaps this had better be in its own file rather
    > than extending more 011_crash_recovery.pl.  0003 looks like a good
    
    Ok, no problem.
    
    > idea to check after the consistency of the path structures created
    > during replay, and it touches paths I'd expect it to touch, as of
    > database and tbspace redos.
    > 
    > +       if (!reachedConsistency)
    > +           XLogForgetMissingDir(xlrec->ts_id, InvalidOid);
    > +
    > +       XLogFlush(record->EndRecPtr);
    > Not sure to understand why this is required.  A comment may be in
    > order to explain the hows and the whys.
    
    Is it about XLogFlush?  As my understanding it is to update
    minRecoveryPoint to that LSN.  I'll add a comment like that.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  70. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-07T08:39:27Z

    So the new framework has been dropped in this version.
    The second test is removed as it is irrelevant to this bug.
    
    In this version the patch is a single file that contains the test.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  71. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Robert Haas <robertmhaas@gmail.com> — 2022-03-14T21:37:40Z

    On Mon, Mar 7, 2022 at 3:39 AM Kyotaro Horiguchi
    <horikyota.ntt@gmail.com> wrote:
    > So the new framework has been dropped in this version.
    > The second test is removed as it is irrelevant to this bug.
    >
    > In this version the patch is a single file that contains the test.
    
    The status of this patch in the CommitFest was set to "Waiting for
    Author." Since a new patch has been submitted since that status was
    set, I have changed it to "Needs Review." Since this is now in its
    15th CommitFest, we really should get it fixed; that's kind of
    ridiculous. (I am as much to blame as anyone.) It does seem to be a
    legitimate bug.
    
    A few questions about the patch:
    
    1. Why is it OK to just skip the operation without making it up later?
    
    2. Why not instead change the code so that the operation can succeed,
    by creating the prerequisite parent directories? Do we not have enough
    information for that? I'm not saying that we definitely should do it
    that way rather than this way, but I think we do take that approach in
    some cases.
    
    Thanks,
    
    -- 
    Robert Haas
    EDB: http://www.enterprisedb.com
    
    
    
    
  72. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-15T06:09:26Z

    At Mon, 14 Mar 2022 17:37:40 -0400, Robert Haas <robertmhaas@gmail.com> wrote in 
    > On Mon, Mar 7, 2022 at 3:39 AM Kyotaro Horiguchi
    > <horikyota.ntt@gmail.com> wrote:
    > > So the new framework has been dropped in this version.
    > > The second test is removed as it is irrelevant to this bug.
    > >
    > > In this version the patch is a single file that contains the test.
    > 
    > The status of this patch in the CommitFest was set to "Waiting for
    > Author." Since a new patch has been submitted since that status was
    > set, I have changed it to "Needs Review." Since this is now in its
    > 15th CommitFest, we really should get it fixed; that's kind of
    > ridiculous. (I am as much to blame as anyone.) It does seem to be a
    > legitimate bug.
    > 
    > A few questions about the patch:
    
    Thanks for looking this!
    
    > 1. Why is it OK to just skip the operation without making it up later?
    
    Does "it" mean removal of directories?  It is not okay, but in the
    first place it is out-of-scope of this patch to fix that.  The patch
    leaves the existing code alone.  This patch just has recovery ignore
    invalid accesses into eventually removed objects.
    
    Maybe, I don't understand you question..
    
    > 2. Why not instead change the code so that the operation can succeed,
    > by creating the prerequisite parent directories? Do we not have enough
    > information for that? I'm not saying that we definitely should do it
    > that way rather than this way, but I think we do take that approach in
    > some cases.
    
    It is proposed first by Paul Guo [1] then changed so that it ignores
    failed directory creations in the very early stage in this thread.
    After that, it gets conscious of recovery consistency by managing
    invalid-access list.
    
    [1] https://www.postgresql.org/message-id/flat/20210327142316.GA32517%40alvherre.pgsql#a557bd47207a446ce206879676e0140a
    
    I think there was no strong reason for the current shape but I
    personally rather like the remembering-invalid-access way because it
    doesn't dirty the data directory and it is consistent with how we
    treat missing heap pages.
    
    I tried a slightly tweaked version (attached) of the first version and
    confirmed that it works for the current test script.  It doesn't check
    recovery consistency but otherwise that way also seems fine.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  73. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-03-21T11:24:38Z

    On 2022-Mar-04, Michael Paquier wrote:
    
    > d6d317d as solved the issue of tablespace paths across multiple nodes
    > with the new GUC called allow_in_place_tablespaces, and is getting
    > successfully used in the recovery tests as of 027_stream_regress.pl.
    
    OK, but that means that the test suite is now not backpatchable.  The
    implication here is that either we're going to commit the fix without
    any tests at all on older branches, or that we're going to fix it only
    in branch master.  Are you thinking that it's okay to leave this bug
    unfixed in older branches?  That seems embarrasing.
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "No me acuerdo, pero no es cierto.  No es cierto, y si fuera cierto,
     no me acuerdo."                 (Augusto Pinochet a una corte de justicia)
    
    
    
    
  74. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-03-21T18:43:52Z

    I had a look at this latest version of the patch, and found some things
    to tweak.  Attached is v21 with three main changes from Kyotaro's v20:
    
    1. the XLogFlush is only done if consistent state has not been reached.
    As I understand, it's not needed in normal mode.  (In any case, if we do
    call XLogFlush in normal mode, what it does is not advance the recovery
    point, so the comment would be incorrect.)
    
    2. use %u to print OIDs rather than %d
    
    3. I changed the warning message wording to this:
    
    +           ereport(WARNING,
    +                   (errmsg("skipping replay of database creation WAL record"),
    +                    errdetail("The source database directory \"%s\" was not found.",
    +                              src_path),
    +                    errhint("A future WAL record that removes the directory before reaching consistent mode is expected.")));
    
    I also renamed the function XLogReportMissingDir to
    XLogRememberMissingDir (which matches the "forget" part) and changed the
    DEBUG2 messages in that function to DEBUG1 (all the calls in other
    functions remain DEBUG2, because ISTM they are not as interesting).
    Finally, I made the TAP test search the WARNING line in the log.
    
    -- 
    Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
    "No tengo por qué estar de acuerdo con lo que pienso"
                                 (Carlos Caszeli)
    
  75. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-03-21T19:03:07Z

    On 2022-Mar-14, Robert Haas wrote:
    
    > 2. Why not instead change the code so that the operation can succeed,
    > by creating the prerequisite parent directories? Do we not have enough
    > information for that? I'm not saying that we definitely should do it
    > that way rather than this way, but I think we do take that approach in
    > some cases.
    
    It seems we can choose freely between these two implementations -- I
    mean I don't see any upsides or downsides to either one.
    
    The current one has the advantage that it never makes the datadir
    "dirty", to use Kyotaro's term.  It verifies that the creation/drop form
    a pair.  A possible downside is that if there's a bug, we could end up
    with a spurious PANIC at the end of recovery, and no way to recover.
    
    -- 
    Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
    
    
    
    
  76. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-03-25T12:26:05Z

    On 2022-Mar-21, Alvaro Herrera wrote:
    
    > I had a look at this latest version of the patch, and found some things
    > to tweak.  Attached is v21 with three main changes from Kyotaro's v20:
    
    Pushed this, backpatching to 14 and 13.  It would have been good to
    backpatch further, but there's an (textually trivial) merge conflict
    related to commit e6d8069522c8.  Because that commit conceptually
    touches the same area that this bugfix is about, I'm not sure that
    backpatching further without a lot more thought is wise -- particularly
    so when there's no way to automate the test in branches older than
    master.
    
    This is quite annoying, considering that the bug was reported shortly
    before 12 went into beta.
    
    -- 
    Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
    "If you have nothing to say, maybe you need just the right tool to help you
    not say it."                   (New York Times, about Microsoft PowerPoint)
    
    
    
    
  77. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-28T01:01:05Z

    At Fri, 25 Mar 2022 13:26:05 +0100, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in 
    > On 2022-Mar-21, Alvaro Herrera wrote:
    > 
    > > I had a look at this latest version of the patch, and found some things
    > > to tweak.  Attached is v21 with three main changes from Kyotaro's v20:
    > 
    > Pushed this, backpatching to 14 and 13.  It would have been good to
    > backpatch further, but there's an (textually trivial) merge conflict
    > related to commit e6d8069522c8.  Because that commit conceptually
    > touches the same area that this bugfix is about, I'm not sure that
    > backpatching further without a lot more thought is wise -- particularly
    > so when there's no way to automate the test in branches older than
    > master.
    
    Thaks for committing.
    
    > This is quite annoying, considering that the bug was reported shortly
    > before 12 went into beta.
    
    Sure.  I'm going to look into that.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  78. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-03-28T01:34:44Z

    On Mon, Mar 28, 2022 at 2:01 PM Kyotaro Horiguchi
    <horikyota.ntt@gmail.com> wrote:
    > At Fri, 25 Mar 2022 13:26:05 +0100, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in
    > > Pushed this, backpatching to 14 and 13.  It would have been good to
    > > backpatch further, but there's an (textually trivial) merge conflict
    > > related to commit e6d8069522c8.  Because that commit conceptually
    > > touches the same area that this bugfix is about, I'm not sure that
    > > backpatching further without a lot more thought is wise -- particularly
    > > so when there's no way to automate the test in branches older than
    > > master.
    
    Just a thought:  we could consider back-patching
    allow_in_place_tablespaces, after a little while, if we're happy with
    how that is working out, if it'd be useful for verifying bug fixes in
    back branches.  It's non-end-user-facing testing infrastructure.
    
    
    
    
  79. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-28T06:25:51Z

    At Mon, 28 Mar 2022 14:34:44 +1300, Thomas Munro <thomas.munro@gmail.com> wrote in 
    > On Mon, Mar 28, 2022 at 2:01 PM Kyotaro Horiguchi
    > <horikyota.ntt@gmail.com> wrote:
    > > At Fri, 25 Mar 2022 13:26:05 +0100, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in
    > > > Pushed this, backpatching to 14 and 13.  It would have been good to
    > > > backpatch further, but there's an (textually trivial) merge conflict
    > > > related to commit e6d8069522c8.  Because that commit conceptually
    > > > touches the same area that this bugfix is about, I'm not sure that
    > > > backpatching further without a lot more thought is wise -- particularly
    > > > so when there's no way to automate the test in branches older than
    > > > master.
    > 
    > Just a thought:  we could consider back-patching
    > allow_in_place_tablespaces, after a little while, if we're happy with
    > how that is working out, if it'd be useful for verifying bug fixes in
    > back branches.  It's non-end-user-facing testing infrastructure.
    
    I appreciate if we accept that.  The patch is simple.  And it now has
    the clear use-case for back-patching.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  80. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-28T08:20:42Z

    At Mon, 28 Mar 2022 10:01:05 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > At Fri, 25 Mar 2022 13:26:05 +0100, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in 
    > > Pushed this, backpatching to 14 and 13.  It would have been good to
    > > backpatch further, but there's an (textually trivial) merge conflict
    > > related to commit e6d8069522c8.  Because that commit conceptually
    > > touches the same area that this bugfix is about, I'm not sure that
    > > backpatching further without a lot more thought is wise -- particularly
    > > so when there's no way to automate the test in branches older than
    > > master.
    > 
    > Thaks for committing.
    > 
    > > This is quite annoying, considering that the bug was reported shortly
    > > before 12 went into beta.
    > 
    > Sure.  I'm going to look into that.
    
    This is a preparatory patch and tentative (yes, it's just tentative)
    test. This is made for 12 but applies with some warnings to 10-11.
    
    (Hope the attachments are attached as "attachment", not  "inline".)
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  81. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Robert Haas <robertmhaas@gmail.com> — 2022-03-28T14:37:04Z

    On Fri, Mar 25, 2022 at 8:26 AM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > On 2022-Mar-21, Alvaro Herrera wrote:
    > > I had a look at this latest version of the patch, and found some things
    > > to tweak.  Attached is v21 with three main changes from Kyotaro's v20:
    >
    > Pushed this, backpatching to 14 and 13.  It would have been good to
    > backpatch further, but there's an (textually trivial) merge conflict
    > related to commit e6d8069522c8.  Because that commit conceptually
    > touches the same area that this bugfix is about, I'm not sure that
    > backpatching further without a lot more thought is wise -- particularly
    > so when there's no way to automate the test in branches older than
    > master.
    >
    > This is quite annoying, considering that the bug was reported shortly
    > before 12 went into beta.
    
    I think that the warnings this patch issues may cause some unnecessary
    end-user alarm. It seems to me that they are basically warning about a
    situation that is unusual but not scary. Isn't the appropriate level
    for that DEBUG1, maybe without the errhint?
    
    -- 
    Robert Haas
    EDB: http://www.enterprisedb.com
    
    
    
    
  82. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Robert Haas <robertmhaas@gmail.com> — 2022-03-28T16:17:50Z

    On Mon, Mar 21, 2022 at 3:02 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > > 2. Why not instead change the code so that the operation can succeed,
    > > by creating the prerequisite parent directories? Do we not have enough
    > > information for that? I'm not saying that we definitely should do it
    > > that way rather than this way, but I think we do take that approach in
    > > some cases.
    >
    > It seems we can choose freely between these two implementations -- I
    > mean I don't see any upsides or downsides to either one.
    
    What got committed here feels inconsistent to me. Suppose we have a
    checkpoint, and then a series of operations that touch a tablespace,
    and then a drop database and drop tablespace. If the first operation
    happens to be CREATE DATABASE, then this patch is going to fix it by
    skipping the operation. However, if the first operation happens to be
    almost anything else, the way it's going to reference the dropped
    tablespace is via a block reference in a WAL record of a wide variety
    of types. That's going to result in a call to
    XLogReadBufferForRedoExtended() which will call
    XLogReadBufferExtended() which will do smgrcreate(smgr, forknum, true)
    which will in turn call TablespaceCreateDbspace() to fill in all the
    missing directories.
    
    I don't think that's very good. It would be reasonable to decide that
    we're never going to create the missing directories and instead just
    remember that they were not found so we can do a cross check. It's
    also reasonable to just create the directories on the fly. But doing a
    mix of those systems doesn't really seem like the right idea -
    particularly because it means that the cross-check system is probably
    not very effective at finding actual problems in the code.
    
    Am I missing something here?
    
    -- 
    Robert Haas
    EDB: http://www.enterprisedb.com
    
    
    
    
  83. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-29T01:34:42Z

    At Mon, 28 Mar 2022 10:37:04 -0400, Robert Haas <robertmhaas@gmail.com> wrote in 
    > On Fri, Mar 25, 2022 at 8:26 AM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > > On 2022-Mar-21, Alvaro Herrera wrote:
    > > > I had a look at this latest version of the patch, and found some things
    > > > to tweak.  Attached is v21 with three main changes from Kyotaro's v20:
    > >
    > > Pushed this, backpatching to 14 and 13.  It would have been good to
    > > backpatch further, but there's an (textually trivial) merge conflict
    > > related to commit e6d8069522c8.  Because that commit conceptually
    > > touches the same area that this bugfix is about, I'm not sure that
    > > backpatching further without a lot more thought is wise -- particularly
    > > so when there's no way to automate the test in branches older than
    > > master.
    > >
    > > This is quite annoying, considering that the bug was reported shortly
    > > before 12 went into beta.
    > 
    > I think that the warnings this patch issues may cause some unnecessary
    > end-user alarm. It seems to me that they are basically warning about a
    > situation that is unusual but not scary. Isn't the appropriate level
    > for that DEBUG1, maybe without the errhint?
    
    log_invalid_page reports missing pages with DEBUG1 before reaching
    consistency.  And since missing directory is not an issue if all of
    those reports are forgotten until reaching consistency, DEBUG1 sounds
    reasonable.  Maybe we lower the DEBUG1 messages to DEBUG2 in
    XLogRememberMissingDir?
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  84. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Michael Paquier <michael@paquier.xyz> — 2022-03-29T01:57:42Z

    On Mon, Mar 28, 2022 at 02:34:44PM +1300, Thomas Munro wrote:
    > Just a thought:  we could consider back-patching
    > allow_in_place_tablespaces, after a little while, if we're happy with
    > how that is working out, if it'd be useful for verifying bug fixes in
    > back branches.  It's non-end-user-facing testing infrastructure.
    
    +1 for a backpatch on that.  That would be useful.
    --
    Michael
    
  85. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-03-29T04:55:35Z

    At Mon, 28 Mar 2022 12:17:50 -0400, Robert Haas <robertmhaas@gmail.com> wrote in 
    > On Mon, Mar 21, 2022 at 3:02 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > > > 2. Why not instead change the code so that the operation can succeed,
    > > > by creating the prerequisite parent directories? Do we not have enough
    > > > information for that? I'm not saying that we definitely should do it
    > > > that way rather than this way, but I think we do take that approach in
    > > > some cases.
    > >
    > > It seems we can choose freely between these two implementations -- I
    > > mean I don't see any upsides or downsides to either one.
    > 
    > What got committed here feels inconsistent to me. Suppose we have a
    > checkpoint, and then a series of operations that touch a tablespace,
    > and then a drop database and drop tablespace. If the first operation
    > happens to be CREATE DATABASE, then this patch is going to fix it by
    > skipping the operation. However, if the first operation happens to be
    > almost anything else, the way it's going to reference the dropped
    > tablespace is via a block reference in a WAL record of a wide variety
    > of types. That's going to result in a call to
    > XLogReadBufferForRedoExtended() which will call
    > XLogReadBufferExtended() which will do smgrcreate(smgr, forknum, true)
    > which will in turn call TablespaceCreateDbspace() to fill in all the
    > missing directories.
    
    Right. I thought that recovery avoids that but that's wrong.  This
    behavior creates a bare (non-linked) directly within pg_tblspc.  The
    directory would dissapear soon if recovery proceeds to the consistency
    point, though.
    
    > I don't think that's very good. It would be reasonable to decide that
    > we're never going to create the missing directories and instead just
    > remember that they were not found so we can do a cross check. It's
    > also reasonable to just create the directories on the fly. But doing a
    > mix of those systems doesn't really seem like the right idea -
    > particularly because it means that the cross-check system is probably
    > not very effective at finding actual problems in the code.
    > 
    > Am I missing something here?
    
    No. I agree that mixing them is not good.  On the other hand we
    already doing that by heapam.  AFAICS sometimes it avoid creating a
    new page but sometimes creates it.  But I don't mean to use the fact
    for justifying this patch to do that, or denying to do that.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  86. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-03-29T11:37:34Z

    On 2022-Mar-29, Kyotaro Horiguchi wrote:
    
    > > That's going to result in a call to
    > > XLogReadBufferForRedoExtended() which will call
    > > XLogReadBufferExtended() which will do smgrcreate(smgr, forknum, true)
    > > which will in turn call TablespaceCreateDbspace() to fill in all the
    > > missing directories.
    > 
    > Right. I thought that recovery avoids that but that's wrong.  This
    > behavior creates a bare (non-linked) directly within pg_tblspc.  The
    > directory would dissapear soon if recovery proceeds to the consistency
    > point, though.
    
    Hmm, this is not good.
    
    > No. I agree that mixing them is not good.  On the other hand we
    > already doing that by heapam.  AFAICS sometimes it avoid creating a
    > new page but sometimes creates it.  But I don't mean to use the fact
    > for justifying this patch to do that, or denying to do that.
    
    I think we should revert this patch and do it again using the other
    approach: create a stub directory during recovery that can be deleted
    later.
    
    -- 
    Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
    "Porque francamente, si para saber manejarse a uno mismo hubiera que
    rendir examen... ¿Quién es el machito que tendría carnet?"  (Mafalda)
    
    
    
    
  87. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Robert Haas <robertmhaas@gmail.com> — 2022-03-29T12:45:13Z

    On Tue, Mar 29, 2022 at 7:37 AM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > I think we should revert this patch and do it again using the other
    > approach: create a stub directory during recovery that can be deleted
    > later.
    
    I'm fine with that approach, but I'd like to ask that we proceed
    expeditiously, because I have another patch that I want to commit that
    touches this area. I can commit to helping with whatever we decide to
    do here, but I don't want to keep that patch on ice while we figure it
    out and then have it miss the release.
    
    -- 
    Robert Haas
    EDB: http://www.enterprisedb.com
    
    
    
    
  88. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-03-29T13:28:26Z

    On 2022-Mar-29, Robert Haas wrote:
    
    > I'm fine with that approach, but I'd like to ask that we proceed
    > expeditiously, because I have another patch that I want to commit that
    > touches this area. I can commit to helping with whatever we decide to
    > do here, but I don't want to keep that patch on ice while we figure it
    > out and then have it miss the release.
    
    OK, this is a bug that's been open for years.   A fix can be committed
    after the feature freeze anyway.
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    
    
    
    
  89. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Robert Haas <robertmhaas@gmail.com> — 2022-03-29T13:31:42Z

    On Tue, Mar 29, 2022 at 9:28 AM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > OK, this is a bug that's been open for years.   A fix can be committed
    > after the feature freeze anyway.
    
    +1
    
    -- 
    Robert Haas
    EDB: http://www.enterprisedb.com
    
    
    
    
  90. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-04-01T04:21:57Z

    At Tue, 29 Mar 2022 09:31:42 -0400, Robert Haas <robertmhaas@gmail.com> wrote in 
    > On Tue, Mar 29, 2022 at 9:28 AM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > > OK, this is a bug that's been open for years.   A fix can be committed
    > > after the feature freeze anyway.
    > 
    > +1
    
    By the way, may I ask how do we fix this?  The existing recovery code
    already generates just-to-be-delete files in a real directory in
    pg_tblspc sometimes, and elsewise skip applying WAL records on
    nonexistent heap pages.  It is the "mixed" way.
    
    1. stop XLogReadBufferForRedo creating a file in nonexistent
      directories then remember the failure (I'm not sure how big the
      impact is.)
    
    
    2. unconditionally create all objects required for recovery to proceed..
      2.1 and igore the failures.
      2.2 and remember the failures.
    
    3. Any other?
    
    2 needs to create a real directory in pg_tblspc. So 1?
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  91. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Robert Haas <robertmhaas@gmail.com> — 2022-04-01T18:51:58Z

    On Fri, Apr 1, 2022 at 12:22 AM Kyotaro Horiguchi
    <horikyota.ntt@gmail.com> wrote:
    > By the way, may I ask how do we fix this?  The existing recovery code
    > already generates just-to-be-delete files in a real directory in
    > pg_tblspc sometimes, and elsewise skip applying WAL records on
    > nonexistent heap pages.  It is the "mixed" way.
    
    Can you be more specific about where we have each behavior now?
    
    > 1. stop XLogReadBufferForRedo creating a file in nonexistent
    >   directories then remember the failure (I'm not sure how big the
    >   impact is.)
    >
    > 2. unconditionally create all objects required for recovery to proceed..
    >   2.1 and igore the failures.
    >   2.2 and remember the failures.
    >
    > 3. Any other?
    >
    > 2 needs to create a real directory in pg_tblspc. So 1?
    
    I think we could either do 1 or 2. My intuition is that getting 2
    working would be less scary and more likely to be something we would
    feel comfortable back-patching, but 1 is probably a better design in
    the long term. However, I might be wrong -- that's just a guess.
    
    -- 
    Robert Haas
    EDB: http://www.enterprisedb.com
    
    
    
    
  92. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-04-04T08:29:48Z

    At Fri, 1 Apr 2022 14:51:58 -0400, Robert Haas <robertmhaas@gmail.com> wrote in 
    > On Fri, Apr 1, 2022 at 12:22 AM Kyotaro Horiguchi
    > <horikyota.ntt@gmail.com> wrote:
    > > By the way, may I ask how do we fix this?  The existing recovery code
    > > already generates just-to-be-delete files in a real directory in
    > > pg_tblspc sometimes, and elsewise skip applying WAL records on
    > > nonexistent heap pages.  It is the "mixed" way.
    > 
    > Can you be more specific about where we have each behavior now?
    
    They're done in  XLogReadBufferExtended.
    
    The second behavior happens here,
    xlogutils.c:
    >		/* hm, page doesn't exist in file */
    >		if (mode == RBM_NORMAL)
    >		{
    >			log_invalid_page(rnode, forknum, blkno, false);
    +			Assert(0);
    >			return InvalidBuffer;
    
    With the assertion, 015_promotion_pages.pl crashes. This prevents page
    creation and the following redo action on the page.
    
    The first behavior is described as the following comment:
    
    >	 * Create the target file if it doesn't already exist.  This lets us cope
    >	 * if the replay sequence contains writes to a relation that is later
    >	 * deleted.  (The original coding of this routine would instead suppress
    >	 * the writes, but that seems like it risks losing valuable data if the
    >	 * filesystem loses an inode during a crash.  Better to write the data
    >	 * until we are actually told to delete the file.)
    >	 */
    >	smgrcreate(smgr, forknum, true);
    
    Without the smgrcreate call, make check-world fails due to missing
    files for FSM and visibility map, and init forks, which it's a bit
    doubtful that the cases fall into the category so-called "creates
    inexistent objects by redo access". In a few places, XLOG_FPI records
    are used to create the first page of a file including main and init
    forks.  But I don't see a case of main fork during make check-world.
    
    # Most of the failure cases happen as standby freeze. I was a bit
    # annoyed that make check-world doesn't tell what is the module
    # currently being tested.  In that case I had to deduce it from the
    # sequence of preceding script names, but if the first TAP script of a
    # module freezes, I had to use ps to find the module..
    
    
    > > 1. stop XLogReadBufferForRedo creating a file in nonexistent
    > >   directories then remember the failure (I'm not sure how big the
    > >   impact is.)
    > >
    > > 2. unconditionally create all objects required for recovery to proceed..
    > >   2.1 and igore the failures.
    > >   2.2 and remember the failures.
    > >
    > > 3. Any other?
    > >
    > > 2 needs to create a real directory in pg_tblspc. So 1?
    > 
    > I think we could either do 1 or 2. My intuition is that getting 2
    > working would be less scary and more likely to be something we would
    > feel comfortable back-patching, but 1 is probably a better design in
    > the long term. However, I might be wrong -- that's just a guess.
    
    Thanks.  I forgot to mention in the previous mail (but mentioned
    somewhere upthread) but if we take 2, there's no way other than
    creating a real directory in pg_tblspc while recovery.  I don't think
    it is neat.
    
    I haven't found how the patch caused creation of a relation file that
    is to be removed soon.  However, I find that v19 patch fails by maybe
    due to some change in Cluster.pm.  It takes a bit more time to check
    that..
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  93. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-04-04T08:54:49Z

    At Mon, 04 Apr 2022 17:29:48 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > I haven't found how the patch caused creation of a relation file that
    > is to be removed soon.  However, I find that v19 patch fails by maybe
    > due to some change in Cluster.pm.  It takes a bit more time to check
    > that..
    
    I was a bit away, of course the wal-logged create database interfares
    with the patch here. But I haven't found that why it stops creating
    database directory under pg_tblspc.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  94. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Dilip Kumar <dilipbalaut@gmail.com> — 2022-04-04T15:44:27Z

    On Mon, Apr 4, 2022 at 2:25 PM Kyotaro Horiguchi
    <horikyota.ntt@gmail.com> wrote:
    >
    > At Mon, 04 Apr 2022 17:29:48 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in
    > > I haven't found how the patch caused creation of a relation file that
    > > is to be removed soon.  However, I find that v19 patch fails by maybe
    > > due to some change in Cluster.pm.  It takes a bit more time to check
    > > that..
    >
    > I was a bit away, of course the wal-logged create database interfares
    > with the patch here. But I haven't found that why it stops creating
    > database directory under pg_tblspc.
    
    I did not understand what is the exact problem here, but the database
    directory and the version file are created under the default
    tablespace of the target database.  However, other than the default
    tablespace of the database, the database directory will be created
    along with the smgrcreate() so that we do not create an unnecessary
    directory under the tablespace where we do not have any data to be
    copied.
    
    -- 
    Regards,
    Dilip Kumar
    EnterpriseDB: http://www.enterprisedb.com
    
    
    
    
  95. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-04-05T02:16:44Z

    At Mon, 4 Apr 2022 21:14:27 +0530, Dilip Kumar <dilipbalaut@gmail.com> wrote in 
    > On Mon, Apr 4, 2022 at 2:25 PM Kyotaro Horiguchi
    > <horikyota.ntt@gmail.com> wrote:
    > >
    > > At Mon, 04 Apr 2022 17:29:48 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in
    > > > I haven't found how the patch caused creation of a relation file that
    > > > is to be removed soon.  However, I find that v19 patch fails by maybe
    > > > due to some change in Cluster.pm.  It takes a bit more time to check
    > > > that..
    > >
    > > I was a bit away, of course the wal-logged create database interfares
    > > with the patch here. But I haven't found that why it stops creating
    > > database directory under pg_tblspc.
    > 
    > I did not understand what is the exact problem here, but the database
    > directory and the version file are created under the default
    > tablespace of the target database.  However, other than the default
    > tablespace of the database, the database directory will be created
    > along with the smgrcreate() so that we do not create an unnecessary
    > directory under the tablespace where we do not have any data to be
    > copied.
    
    Thanks. Yeah, I suspected something like that but I didn't find a
    difference in the code I suspected to be related with, but it's was
    wrong.  I took wrong steps trying to reveal that state and faced the
    wrong error message. With the correct steps, I could see that
    Storage/CREATE creates pg_tblspc/<directory>.
    
    So, if we create missing tablespace directory, we have no way
    otherthan creating it directly in pg_tblspc, which is violating the
    rule that there shouldn't be real directory in pg_tblspc (when
    allow_in_place_tablespaces is false).
    
    So, I have the following points in my mind for now.
    
    - We create the directory "since we know it is just tentative state".
    
    - Then, check that no directory in pg_tblspc when reaching consistency
      when allow_in_place_tablespaces is false.
    
    - Leave the log_invalid_page() mechanism alone as it is always result
      in a corrpt page if a differential WAL record is applied on a newly
      created page that should have been exist.
    
    However, while working on it, I found that I found that recovery faces
    missing tablespace directories *after* reaching consistency.  I'm
    examining that further.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  96. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-04-05T07:38:06Z

    At Tue, 05 Apr 2022 11:16:44 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > So, I have the following points in my mind for now.
    > 
    > - We create the directory "since we know it is just tentative state".
    > 
    > - Then, check that no directory in pg_tblspc when reaching consistency
    >   when allow_in_place_tablespaces is false.
    > 
    > - Leave the log_invalid_page() mechanism alone as it is always result
    >   in a corrpt page if a differential WAL record is applied on a newly
    >   created page that should have been exist.
    > 
    > However, while working on it, I found that I found that recovery faces
    > missing tablespace directories *after* reaching consistency.  I'm
    > examining that further.
    
    Okay, it was my thinko.  But I faced another obstacle.
    
    This is the first cut of the above.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  97. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-04-05T07:54:47Z

    At Tue, 05 Apr 2022 16:38:06 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > > However, while working on it, I found that I found that recovery faces
    > > missing tablespace directories *after* reaching consistency.  I'm
    > > examining that further.
    > 
    > Okay, it was my thinko.  But I faced another obstacle.
    
    I forgot to delete the second sentence. Please ingore it.
    
    regareds.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  98. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-04-05T08:18:57Z

    At Tue, 05 Apr 2022 16:38:06 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > At Tue, 05 Apr 2022 11:16:44 +0900 (JST), Kyotaro Horiguchi <horikyota.ntt@gmail.com> wrote in 
    > > So, I have the following points in my mind for now.
    > > 
    > > - We create the directory "since we know it is just tentative state".
    > > 
    > > - Then, check that no directory in pg_tblspc when reaching consistency
    > >   when allow_in_place_tablespaces is false.
    > > 
    > > - Leave the log_invalid_page() mechanism alone as it is always result
    > >   in a corrpt page if a differential WAL record is applied on a newly
    > >   created page that should have been exist.
    > > 
    > > However, while working on it, I found that I found that recovery faces
    > > missing tablespace directories *after* reaching consistency.  I'm
    > > examining that further.
    > 
    > Okay, it was my thinko.
    > 
    > This is the first cut of the above.
    
    It had an unused variable for Windows.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
  99. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-13T16:43:45Z

    Not a review, just a preparatory rebase across some trivially
    conflicting changes.  I also noticed that
    src/test/recovery/t/031_recovery_conflict.pl, which was added two days
    after v23 was sent, and which uses allow_in_place_tablespaces, bails out
    because of the checks introduced by this patch, so I made the check
    routine do nothing in that case.
    
    Anyway, here's v24.
    
    -- 
    Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
    "La conclusión que podemos sacar de esos estudios es que
    no podemos sacar ninguna conclusión de ellos" (Tanenbaum)
    
  100. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-14T21:47:40Z

    Here's a couple of fixups.  0001 is the same as before.  In 0002 I think
    CheckTablespaceDirectory ends up easier to read if we split out the test
    for validity of the link.  Looking at that again, I think we don't need
    to piggyback on ignore_invalid_pages, which is already a stretch, so
    let's not -- instead we can use allow_in_place_tablespaces if users need
    a workaround.  So that's 0003 (this bit needs more than zero docs,
    however.)
    
    0004 is straightforward: let's check for bad directories before logging
    about consistent state.
    
    After all this, I'm not sure what to think of dbase_redo.  At line 3102,
    is the directory supposed to exist or not?  I'm confused as to what is
    the expected state at that point.  I rewrote this, but now I think my
    rewrite continues to be confusing, so I'll have to think more about it.
    
    Another aspect are the tests.  Robert described a scenario where the
    previously committed version of this patch created trouble.  Do we have
    a test case to cover that problematic case?  I think we should strive to
    cover it, if possible.
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "The eagle never lost so much time, as
    when he submitted to learn of the crow." (William Blake)
    
  101. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-07-15T07:30:59Z

    At Thu, 14 Jul 2022 23:47:40 +0200, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in 
    > Here's a couple of fixups.  0001 is the same as before.  In 0002 I think
    
    Thanks!
    
    + 		if (!S_ISLNK(st.st_mode))
    + #else
    + 		if (!pgwin32_is_junction(path))
    + #endif
    + 			elog(ignore_invalid_pages ? WARNING : PANIC,
    + 				 "real directory found in pg_tblspc directory: %s", de->d_name);
    
    A regular file with an oid-name also causes this error. Doesn't
    something like "unexpected non-(sym)link entry..." work?
    
    > CheckTablespaceDirectory ends up easier to read if we split out the test
    > for validity of the link.  Looking at that again, I think we don't need
    > to piggyback on ignore_invalid_pages, which is already a stretch, so
    > let's not -- instead we can use allow_in_place_tablespaces if users need
    > a workaround.  So that's 0003 (this bit needs more than zero docs,
    > however.)
    
    The result of 0003 looks good.
    
    0002:
    +is_path_tslink(const char *path)
    
    What the "ts" of tslink stands for? If it stands for tablespace, the
    function is not specific for table spaces. We already have 
    
    +					errmsg("could not stat file \"%s\": %m", path));
    
    I'm not sure we need such correctness, but what is failing there is
    lstat.  I found similar codes in two places in backend and one place
    in frontend. So couldn't it be moved to /common and have a more
    generic name?
    
    -	dir = AllocateDir(tblspc_path);
    -	while ((de = ReadDir(dir, tblspc_path)) != NULL)
    +	dir = AllocateDir("pg_tblspc");
    +	while ((de = ReadDir(dir, "pg_tblspc")) != NULL)
    
    xlog.c uses the macro XLOGDIR. Why don't we define TBLSPCDIR?
    
    -		for (p = de->d_name; *p && isdigit(*p); p++);
    -		if (*p)
    +		if (strspn(de->d_name, "0123456789") != strlen(de->d_name))
     			continue;
    
    The pattern "strspn != strlen" looks kind of remote, or somewhat
    pedantic..
    
    +		char	path[MAXPGPATH + 10];
    ..
    -		snprintf(path, MAXPGPATH, "%s/%s", tblspc_path, de->d_name);
    +		snprintf(path, sizeof(path), "pg_tblspc/%s", de->d_name);
    
    I don't think we need the extra 10 bytes. A bit paranoic, but we can
    check the return value to confirm the d_name is fully stored in the
    buffer.
    
    > 0004 is straightforward: let's check for bad directories before logging
    > about consistent state.
    
    I was about to write a comment to do this when looking 0001.
    
    > After all this, I'm not sure what to think of dbase_redo.  At line 3102,
    > is the directory supposed to exist or not?  I'm confused as to what is
    > the expected state at that point.  I rewrote this, but now I think my
    > rewrite continues to be confusing, so I'll have to think more about it.
    
    I'm not sure l3102 exactly points, but haven't we chosen to create
    everything required to keep recovery going, whether it is supposed to
    exist or not?
    
    > Another aspect are the tests.  Robert described a scenario where the
    > previously committed version of this patch created trouble.  Do we have
    > a test case to cover that problematic case?  I think we should strive to
    > cover it, if possible.
    
    I counldn't recall that clearly and failed to dig out from the thread,
    but doesn't the "creating everything needed" strategy naturally save
    that case?  We could add that test, but it seems to me a little
    cumbersome to confirm the test correctly detect that case..
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  102. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-15T07:56:10Z

    On 2022-Jul-15, Kyotaro Horiguchi wrote:
    
    > At Thu, 14 Jul 2022 23:47:40 +0200, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in 
    > > Here's a couple of fixups.  0001 is the same as before.  In 0002 I think
    > 
    > Thanks!
    > 
    > + 		if (!S_ISLNK(st.st_mode))
    > + #else
    > + 		if (!pgwin32_is_junction(path))
    > + #endif
    > + 			elog(ignore_invalid_pages ? WARNING : PANIC,
    > + 				 "real directory found in pg_tblspc directory: %s", de->d_name);
    > 
    > A regular file with an oid-name also causes this error. Doesn't
    > something like "unexpected non-(sym)link entry..." work?
    
    Hmm, good point.  I also wonder if we need to cater for using the term
    "junction point" rather than "symlink" when under Windows.
    
    > > CheckTablespaceDirectory ends up easier to read if we split out the test
    > > for validity of the link.  Looking at that again, I think we don't need
    > > to piggyback on ignore_invalid_pages, which is already a stretch, so
    > > let's not -- instead we can use allow_in_place_tablespaces if users need
    > > a workaround.  So that's 0003 (this bit needs more than zero docs,
    > > however.)
    > 
    > The result of 0003 looks good.
    
    Great, will merge.
    
    > 0002:
    > +is_path_tslink(const char *path)
    > 
    > What the "ts" of tslink stands for? If it stands for tablespace, the
    > function is not specific for table spaces.
    
    Oh, of course. 
    
    > We already have 
    > 
    > +					errmsg("could not stat file \"%s\": %m", path));
    > 
    > I'm not sure we need such correctness, but what is failing there is
    > lstat.
    
    I'll have a look at what we use for lstat failures in other places.
    
    > I found similar codes in two places in backend and one place
    > in frontend. So couldn't it be moved to /common and have a more
    > generic name?
    
    I'll have a look at those.  I had the same instinct initially ...
    
    > -	dir = AllocateDir(tblspc_path);
    > -	while ((de = ReadDir(dir, tblspc_path)) != NULL)
    > +	dir = AllocateDir("pg_tblspc");
    > +	while ((de = ReadDir(dir, "pg_tblspc")) != NULL)
    > 
    > xlog.c uses the macro XLOGDIR. Why don't we define TBLSPCDIR?
    
    Oh yes, let's do that.  I'd even backpatch that, to avoid a future
    backpatching gotcha.
    
    > -		for (p = de->d_name; *p && isdigit(*p); p++);
    > -		if (*p)
    > +		if (strspn(de->d_name, "0123456789") != strlen(de->d_name))
    >  			continue;
    > 
    > The pattern "strspn != strlen" looks kind of remote, or somewhat
    > pedantic..
    > 
    > +		char	path[MAXPGPATH + 10];
    > ..
    > -		snprintf(path, MAXPGPATH, "%s/%s", tblspc_path, de->d_name);
    > +		snprintf(path, sizeof(path), "pg_tblspc/%s", de->d_name);
    > 
    > I don't think we need the extra 10 bytes.
    
    I forgot to mention this, but I just copied these bits from some other
    place that processes pg_tblspc entries.  It seemed to me that the
    bodiless for loop was a bit too suspicious-looking.
    
    > A bit paranoic, but we can check the return value to confirm the
    > d_name is fully stored in the buffer.
    
    Hmm ... I don't think we need to care about that in this patch.  This
    coding pattern is already being used in other places.  If we want to
    change that, let's do it everywhere, and not in an unrelated
    backpatchable bug fix.
    
    > > After all this, I'm not sure what to think of dbase_redo.  At line 3102,
    > > is the directory supposed to exist or not?  I'm confused as to what is
    > > the expected state at that point.  I rewrote this, but now I think my
    > > rewrite continues to be confusing, so I'll have to think more about it.
    > 
    > I'm not sure l3102 exactly points, but haven't we chosen to create
    > everything required to keep recovery going, whether it is supposed to
    > exist or not?
    
    I mean just after the two stat() calls for the target directory.
    
    > > Another aspect are the tests.  Robert described a scenario where the
    > > previously committed version of this patch created trouble.  Do we have
    > > a test case to cover that problematic case?  I think we should strive to
    > > cover it, if possible.
    > 
    > I counldn't recall that clearly and failed to dig out from the thread,
    > but doesn't the "creating everything needed" strategy naturally save
    > that case?  We could add that test, but it seems to me a little
    > cumbersome to confirm the test correctly detect that case..
    
    Well, I *hope* it does ... but hope is no strategy, and I've frequently
    been on the wrong side when trusting that untested code does what I
    think it does.
    
    
    Thanks for reviewing,
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "It takes less than 2 seconds to get to 78% complete; that's a good sign.
    A few seconds later it's at 90%, but it seems to have stuck there.  Did
    somebody make percentages logarithmic while I wasn't looking?"
                    http://smylers.hates-software.com/2005/09/08/1995c749.html
    
    
    
    
  103. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-15T10:58:44Z

    On 2022-Jul-15, Kyotaro Horiguchi wrote:
    
    > 0002:
    > +is_path_tslink(const char *path)
    > 
    > What the "ts" of tslink stands for? If it stands for tablespace, the
    > function is not specific for table spaces. We already have 
    > 
    > +					errmsg("could not stat file \"%s\": %m", path));
    > 
    > I'm not sure we need such correctness, but what is failing there is
    > lstat.  I found similar codes in two places in backend and one place
    > in frontend. So couldn't it be moved to /common and have a more
    > generic name?
    
    I wondered whether it'd be better to check whether get_dirent_type
    returns PGFILETYPE_LNK.  However, that doesn't deal with junction points
    at all, which seems pretty odd ... I mean, isn't it rather useful as an
    abstraction if it doesn't abstract away the one platform-dependent point
    we have in the area?
    
    However, looking closer I noticed that on Windows we use our own
    readdir() implementation, which AFAICT includes everything to handle
    reparse points as symlinks correctly in get_dirent_type.  Which means
    that do_pg_start_backup is wasting its time with the "#ifdef WIN32" bits
    to handle junction points separately.  We could just do this
    
    diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
    index b809a2152c..4966213fde 100644
    --- a/src/backend/access/transam/xlog.c
    +++ b/src/backend/access/transam/xlog.c
    @@ -8302,13 +8302,8 @@ do_pg_backup_start(const char *backupidstr, bool fast, TimeLineID *starttli_p,
     			 * we sometimes use allow_in_place_tablespaces to create
     			 * directories directly under pg_tblspc, which would fail below.
     			 */
    -#ifdef WIN32
    -			if (!pgwin32_is_junction(fullpath))
    -				continue;
    -#else
     			if (get_dirent_type(fullpath, de, false, ERROR) != PGFILETYPE_LNK)
     				continue;
    -#endif
     
     #if defined(HAVE_READLINK) || defined(WIN32)
     			rllen = readlink(fullpath, linkpath, sizeof(linkpath));
    
    
    And everything should continue to work.
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    
    
    
    
  104. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-15T12:03:11Z

    On 2022-Jul-15, Alvaro Herrera wrote:
    
    > However, looking closer I noticed that on Windows we use our own
    > readdir() implementation, which AFAICT includes everything to handle
    > reparse points as symlinks correctly in get_dirent_type.  Which means
    > that do_pg_start_backup is wasting its time with the "#ifdef WIN32" bits
    > to handle junction points separately.  We could just do this
    > 
    > diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
    > index b809a2152c..4966213fde 100644
    > --- a/src/backend/access/transam/xlog.c
    > +++ b/src/backend/access/transam/xlog.c
    > @@ -8302,13 +8302,8 @@ do_pg_backup_start(const char *backupidstr, bool fast, TimeLineID *starttli_p,
    >  			 * we sometimes use allow_in_place_tablespaces to create
    >  			 * directories directly under pg_tblspc, which would fail below.
    >  			 */
    > -#ifdef WIN32
    > -			if (!pgwin32_is_junction(fullpath))
    > -				continue;
    > -#else
    >  			if (get_dirent_type(fullpath, de, false, ERROR) != PGFILETYPE_LNK)
    >  				continue;
    > -#endif
    >  
    >  #if defined(HAVE_READLINK) || defined(WIN32)
    >  			rllen = readlink(fullpath, linkpath, sizeof(linkpath));
    > 
    > And everything should continue to work.
    
    Hmm, but it does not:
    https://cirrus-ci.com/build/4824963784900608
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    
    
    
    
  105. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-20T10:50:49Z

    v26 here.  I spent some time fighting the readdir() stuff for
    Windows (so that get_dirent_type returns LNK for junction points)
    but couldn't make it to work and was unable to figure out why.
    So I ended up doing what do_pg_backup_start is already doing:
    an #ifdef to call pgwin32_is_junction instead.  I remove the
    newly added path_is_symlink function, because I realized that
    it would mean an extra syscall everywhere other than Windows.
    
    So if somebody wants to fix get_dirent_type() so that it works properly
    on Windows, we can change all these places together.
    
    I also change the use of allow_invalid_pages to
    allow_in_place_tablespaces.  We could add a
    separate GUC for this, but it seems overengineering.
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "Most hackers will be perfectly comfortable conceptualizing users as entropy
     sources, so let's move on."                               (Nathaniel Smith)
    
  106. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-20T15:25:33Z

    On 2022-Jul-20, Alvaro Herrera wrote:
    
    > I also change the use of allow_invalid_pages to
    > allow_in_place_tablespaces.  We could add a
    > separate GUC for this, but it seems overengineering.
    
    Oh, but allow_in_place_tablespaces doesn't exist in versions 14 and
    older, so this strategy doesn't really work.
    
    I see the following alternatives:
    
    1. not backpatch this fix to 14 and older
    2. use a different GUC; either allow_invalid_pages as previously
       suggested, or create a new one just for this purpose
    3. not provide any overriding mechanism in versions 14 and older
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "Always assume the user will do much worse than the stupidest thing
    you can imagine."                                (Julien PUYDT)
    
    
    
    
  107. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-20T16:34:23Z

    On 2022-Jul-20, Alvaro Herrera wrote:
    
    > On 2022-Jul-20, Alvaro Herrera wrote:
    > 
    > > I also change the use of allow_invalid_pages to
    > > allow_in_place_tablespaces.  We could add a
    > > separate GUC for this, but it seems overengineering.
    > 
    > Oh, but allow_in_place_tablespaces doesn't exist in versions 14 and
    > older, so this strategy doesn't really work.
    
    ... and get_dirent_type is new in 14, so that'll be one more hurdle.
    
    -- 
    Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
    "Cuando no hay humildad las personas se degradan" (A. Christie)
    
    
    
    
  108. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-21T11:01:06Z

    On 2022-Jul-20, Alvaro Herrera wrote:
    
    > I see the following alternatives:
    > 
    > 1. not backpatch this fix to 14 and older
    > 2. use a different GUC; either allow_invalid_pages as previously
    >    suggested, or create a new one just for this purpose
    > 3. not provide any overriding mechanism in versions 14 and older
    
    I've got no opinions on this.  I don't like either 1 or 3, so I'm going
    to add and backpatch a new GUC allow_recovery_tablespaces as the
    override mechanism.
    
    If others disagree with this choice, please speak up.
    
    -- 
    Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
    
    
    
    
  109. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-07-21T11:11:26Z

    On Wed, Jul 20, 2022 at 10:51 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > v26 here.  I spent some time fighting the readdir() stuff for
    > Windows (so that get_dirent_type returns LNK for junction points)
    > but couldn't make it to work and was unable to figure out why.
    
    Was it because of this?
    
    https://www.postgresql.org/message-id/CA%2BhUKGKv%2B736Pc8kSj3%3DDijDGd1eC79-uT3Vi16n7jYkcc_raw%40mail.gmail.com
    
    
    
    
  110. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-07-21T11:14:57Z

    On Thu, Jul 21, 2022 at 11:01 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > On 2022-Jul-20, Alvaro Herrera wrote:
    > > I see the following alternatives:
    > >
    > > 1. not backpatch this fix to 14 and older
    > > 2. use a different GUC; either allow_invalid_pages as previously
    > >    suggested, or create a new one just for this purpose
    > > 3. not provide any overriding mechanism in versions 14 and older
    >
    > I've got no opinions on this.  I don't like either 1 or 3, so I'm going
    > to add and backpatch a new GUC allow_recovery_tablespaces as the
    > override mechanism.
    >
    > If others disagree with this choice, please speak up.
    
    Would it help if we back-patched the allow_in_place_tablespaces stuff?
     I'm not sure how hard/destabilising that would be, but I could take a
    look tomorrow.
    
    
    
    
  111. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-21T11:17:51Z

    On 2022-Jul-21, Thomas Munro wrote:
    
    > On Wed, Jul 20, 2022 at 10:51 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > > v26 here.  I spent some time fighting the readdir() stuff for
    > > Windows (so that get_dirent_type returns LNK for junction points)
    > > but couldn't make it to work and was unable to figure out why.
    > 
    > Was it because of this?
    > 
    > https://www.postgresql.org/message-id/CA%2BhUKGKv%2B736Pc8kSj3%3DDijDGd1eC79-uT3Vi16n7jYkcc_raw%40mail.gmail.com
    
    Oh, that sounds very likely, yeah.  I didn't think of testing the
    FILE_ATTRIBUTE_DIRECTORY bit for junction points.
    
    I +1 pushing both of these patches to 14.  Then this patch becomes a
    couple of lines shorter.
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "Before you were born your parents weren't as boring as they are now. They
    got that way paying your bills, cleaning up your room and listening to you
    tell them how idealistic you are."  -- Charles J. Sykes' advice to teenagers
    
    
    
    
  112. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-21T11:20:26Z

    On 2022-Jul-21, Thomas Munro wrote:
    
    > On Thu, Jul 21, 2022 at 11:01 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    
    > > I've got no opinions on this.  I don't like either 1 or 3, so I'm going
    > > to add and backpatch a new GUC allow_recovery_tablespaces as the
    > > override mechanism.
    > >
    > > If others disagree with this choice, please speak up.
    > 
    > Would it help if we back-patched the allow_in_place_tablespaces stuff?
    >  I'm not sure how hard/destabilising that would be, but I could take a
    > look tomorrow.
    
    Yeah, I think that would reduce cruft.  I'm not sure this is more
    against backpatching policy or less, compared to adding a separate
    GUC just for this bugfix.
    
    -- 
    Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
    "The problem with the facetime model is not just that it's demoralizing, but
    that the people pretending to work interrupt the ones actually working."
                                                               (Paul Graham)
    
    
    
    
  113. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-21T11:25:05Z

    On 2022-Jul-21, Alvaro Herrera wrote:
    
    > Yeah, I think that would reduce cruft.  I'm not sure this is more
    > against backpatching policy or less, compared to adding a separate
    > GUC just for this bugfix.
    
    cruft:
    
        {
            {"allow_recovery_tablespaces", PG_POSTMASTER, WAL_RECOVERY,
                gettext_noop("Continues recovery after finding invalid database directories."),
                gettext_noop("It is possible for tablespace drop to interfere with database creation "
                             "so that WAL replay is forced to create fake database directories. "
                             "These should have been dropped by the time recovery ends; "
                             "but in case they aren't, this option lets recovery continue if they "
                             "are present.  Note that these directories must be removed manually afterwards."),
                GUC_NOT_IN_SAMPLE
            },
            &allow_recovery_tablespaces,
            false,
            NULL, NULL, NULL
        },
    
    This is not a very good explanation, but I don't know how to make it
    better.
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "I think my standards have lowered enough that now I think 'good design'
    is when the page doesn't irritate the living f*ck out of me." (JWZ)
    
    
    
    
  114. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-07-22T00:20:37Z

    At Thu, 21 Jul 2022 23:14:57 +1200, Thomas Munro <thomas.munro@gmail.com> wrote in 
    > On Thu, Jul 21, 2022 at 11:01 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > > On 2022-Jul-20, Alvaro Herrera wrote:
    > > > I see the following alternatives:
    > > >
    > > > 1. not backpatch this fix to 14 and older
    > > > 2. use a different GUC; either allow_invalid_pages as previously
    > > >    suggested, or create a new one just for this purpose
    > > > 3. not provide any overriding mechanism in versions 14 and older
    > >
    > > I've got no opinions on this.  I don't like either 1 or 3, so I'm going
    > > to add and backpatch a new GUC allow_recovery_tablespaces as the
    > > override mechanism.
    > >
    > > If others disagree with this choice, please speak up.
    > 
    > Would it help if we back-patched the allow_in_place_tablespaces stuff?
    >  I'm not sure how hard/destabilising that would be, but I could take a
    > look tomorrow.
    
    +1. Addiotional reason for me is it is a developer option.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  115. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-07-22T01:02:57Z

    At Thu, 21 Jul 2022 13:25:05 +0200, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in 
    > On 2022-Jul-21, Alvaro Herrera wrote:
    > 
    > > Yeah, I think that would reduce cruft.  I'm not sure this is more
    > > against backpatching policy or less, compared to adding a separate
    > > GUC just for this bugfix.
    > 
    > cruft:
    > 
    >     {
    >         {"allow_recovery_tablespaces", PG_POSTMASTER, WAL_RECOVERY,
    >             gettext_noop("Continues recovery after finding invalid database directories."),
    >             gettext_noop("It is possible for tablespace drop to interfere with database creation "
    >                          "so that WAL replay is forced to create fake database directories. "
    >                          "These should have been dropped by the time recovery ends; "
    >                          "but in case they aren't, this option lets recovery continue if they "
    >                          "are present.  Note that these directories must be removed manually afterwards."),
    >             GUC_NOT_IN_SAMPLE
    >         },
    >         &allow_recovery_tablespaces,
    >         false,
    >         NULL, NULL, NULL
    >     },
    > 
    > This is not a very good explanation, but I don't know how to make it
    > better.
    
    It looks a bit too detailed. I crafted the following..
    
    Recovery can create tentative in-place tablespace directories under
    pg_tblspc/. They are assumed to be removed until reaching recovery
    consistency, but otherwise PostgreSQL raises a PANIC-level error,
    aborting the recovery. Setting allow_recovery_tablespaces to true
    causes the system to allow such directories during normal
    operation. In case those directories are left after reaching
    consistency, that implies data loss and metadata inconsistency and may
    cause failure of future tablespace creation.
    
    Though, after writing this, I became to think that piggy-backing on
    allow_in_place_tablespaces might be a bit different..
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  116. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-22T08:18:58Z

    On 2022-Jul-22, Kyotaro Horiguchi wrote:
    
    > At Thu, 21 Jul 2022 23:14:57 +1200, Thomas Munro <thomas.munro@gmail.com> wrote in 
    
    > > Would it help if we back-patched the allow_in_place_tablespaces stuff?
    > >  I'm not sure how hard/destabilising that would be, but I could take a
    > > look tomorrow.
    > 
    > +1. Addiotional reason for me is it is a developer option.
    
    OK, I'll wait for allow_in_place_tablespaces to be backpatched then.
    
    I would like to get this fix pushed before the next set of minors, so if
    you won't have time for the backpatches early enough, maybe I can work
    on getting it done.
    
    Which commits would we consider?
    
    7170f2159fb2	Allow "in place" tablespaces.
    f6f0db4d6240    Fix pg_tablespace_location() with in-place tablespaces
    
    -- 
    Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
    "Most hackers will be perfectly comfortable conceptualizing users as entropy
     sources, so let's move on."                               (Nathaniel Smith)
    
    
    
    
  117. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-07-22T08:49:37Z

    At Fri, 22 Jul 2022 10:18:58 +0200, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in 
    > OK, I'll wait for allow_in_place_tablespaces to be backpatched then.
    > 
    > I would like to get this fix pushed before the next set of minors, so if
    > you won't have time for the backpatches early enough, maybe I can work
    > on getting it done.
    > 
    > Which commits would we consider?
    > 
    > 7170f2159fb2	Allow "in place" tablespaces.
    > f6f0db4d6240    Fix pg_tablespace_location() with in-place tablespaces
    
    The second one is just to make the function work with in-place
    tablespaces. Without it the function yeilds the following error.
    
    > ERROR:  could not read symbolic link "pg_tblspc/16407": Invalid argument
    
    This looks actually odd but I think no need of back-patching because
    there's no actual user of the feature is not seen in our test suite.
    If we have a test that needs the feature in future, it would be enough
    to back-patch it then.
    
    So I think only the first one is needed for now.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    
    
  118. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-07-22T08:53:12Z

    On Fri, Jul 22, 2022 at 8:19 PM Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    > On 2022-Jul-22, Kyotaro Horiguchi wrote:
    > > At Thu, 21 Jul 2022 23:14:57 +1200, Thomas Munro <thomas.munro@gmail.com> wrote in
    > > > Would it help if we back-patched the allow_in_place_tablespaces stuff?
    > > >  I'm not sure how hard/destabilising that would be, but I could take a
    > > > look tomorrow.
    > >
    > > +1. Addiotional reason for me is it is a developer option.
    >
    > OK, I'll wait for allow_in_place_tablespaces to be backpatched then.
    >
    > I would like to get this fix pushed before the next set of minors, so if
    > you won't have time for the backpatches early enough, maybe I can work
    > on getting it done.
    >
    > Which commits would we consider?
    
    I wonder how crazy it would be to back-patch
    src/test/recovery/t/027_stream_regress.pl too.
    
    
    
    
  119. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-27T06:07:16Z

    On 2022-Jul-22, Kyotaro Horiguchi wrote:
    
    > At Fri, 22 Jul 2022 10:18:58 +0200, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote in 
    
    > > Which commits would we consider?
    > > 
    > > 7170f2159fb2	Allow "in place" tablespaces.
    > > f6f0db4d6240    Fix pg_tablespace_location() with in-place tablespaces
    > 
    > The second one is just to make the function work with in-place
    > tablespaces. Without it the function yeilds the following error.
    > 
    > > ERROR:  could not read symbolic link "pg_tblspc/16407": Invalid argument
    > 
    > This looks actually odd but I think no need of back-patching because
    > there's no actual user of the feature is not seen in our test suite.
    > If we have a test that needs the feature in future, it would be enough
    > to back-patch it then.
    
    Actually, I found that the new test added by the fix in this thread does
    depend on this being fixed, so I included an even larger set, which I
    think makes this more complete:
    
    7170f2159fb2 Allow "in place" tablespaces.
    c6f2f01611d4 Fix pg_basebackup with in-place tablespaces.
    f6f0db4d6240 Fix pg_tablespace_location() with in-place tablespaces
    7a7cd84893e0 doc: Remove mention to in-place tablespaces for pg_tablespace_location()
    5344723755bd Remove unnecessary Windows-specific basebackup code.
    
    I didn't include any of the test changes for now.  I don't intend to do
    so, unless we see another reason for that; I think the new tests that
    are going to be added by the recovery bugfix should be sufficient
    coverage.
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "La fuerza no está en los medios físicos
    sino que reside en una voluntad indomable" (Gandhi)
    
    
    
    
  120. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-27T18:54:49Z

    Okay, I think I'm done with this.  Here's v27 for the master branch,
    where I fixed some comments as well as thinkos in the test script.
    The ones on older branches aren't materially different, they just have
    tonnes of conflicts resolved.  I'll get this pushed tomorrow morning.
    
    I have run it through CI and it seems ... not completely broken, at
    least, but I have no working recipes for Windows on branches 14 and
    older, so it doesn't really work fully.  If anybody does, please share.
    You can see mine here
    https://github.com/alvherre/postgres/commits/REL_11_STABLE [etc]
    https://cirrus-ci.com/build/5320904228995072
    https://cirrus-ci.com/github/alvherre/postgres
    
    -- 
    Álvaro Herrera         PostgreSQL Developer  —  https://www.EnterpriseDB.com/
    "Every machine is a smoke machine if you operate it wrong enough."
    https://twitter.com/libseybieda/status/1541673325781196801
    
  121. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Matthias van de Meent <boekewurm+postgres@gmail.com> — 2022-07-28T18:04:38Z

    On Wed, 27 Jul 2022 at 20:55, Alvaro Herrera <alvherre@alvh.no-ip.org> wrote:
    >
    > Okay, I think I'm done with this.  Here's v27 for the master branch,
    > where I fixed some comments as well as thinkos in the test script.
    > The ones on older branches aren't materially different, they just have
    > tonnes of conflicts resolved.  I'll get this pushed tomorrow morning.
    >
    > I have run it through CI and it seems ... not completely broken, at
    > least, but I have no working recipes for Windows on branches 14 and
    > older, so it doesn't really work fully.  If anybody does, please share.
    > You can see mine here
    > https://github.com/alvherre/postgres/commits/REL_11_STABLE [etc]
    > https://cirrus-ci.com/build/5320904228995072
    > https://cirrus-ci.com/github/alvherre/postgres
    
    I'd like to bring to your attention that the test that was introduced
    with 9e4f914b seem to be flaky in FreeBSD 13 in the CFBot builds: it
    sometimes times out while waiting for the secondary to catch up. Or,
    at least I think it does, and I'm not too familiar with TAP failure
    outputs: it returns with error code 29 and logs that I'd expect when
    the timeout is reached.
    
    See bottom for examples (all 3 builds for different patches).
    
    Kind regards,
    
    Matthias van de Meent.
    
    [1] https://cirrus-ci.com/task/4960990331666432?logs=test_world#L2631-L2662
    [2] https://cirrus-ci.com/task/5012678384025600?logs=test_world#L2631-L2662
    [3] https://cirrus-ci.com/task/5147001137397760?logs=test_world#L2631-L2662
    
    
    
    
  122. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Tom Lane <tgl@sss.pgh.pa.us> — 2022-07-28T21:57:12Z

    Matthias van de Meent <boekewurm+postgres@gmail.com> writes:
    > I'd like to bring to your attention that the test that was introduced
    > with 9e4f914b seem to be flaky in FreeBSD 13 in the CFBot builds: it
    > sometimes times out while waiting for the secondary to catch up. Or,
    > at least I think it does, and I'm not too familiar with TAP failure
    > outputs: it returns with error code 29 and logs that I'd expect when
    > the timeout is reached.
    
    It's also failing in the buildfarm, eg
    
    https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=conchuela&dt=2022-07-28%2020%3A57%3A50
    
    Looks like only conchuela so far, reinforcing the idea that we're
    only seeing it on FreeBSD.  I'd tentatively bet on a timing problem
    that requires some FreeBSD scheduling quirk to manifest; we've seen
    such quirks before.
    
    			regards, tom lane
    
    
    
    
  123. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-07-28T23:27:01Z

    On Fri, Jul 29, 2022 at 9:57 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > Matthias van de Meent <boekewurm+postgres@gmail.com> writes:
    > > I'd like to bring to your attention that the test that was introduced
    > > with 9e4f914b seem to be flaky in FreeBSD 13 in the CFBot builds: it
    > > sometimes times out while waiting for the secondary to catch up. Or,
    > > at least I think it does, and I'm not too familiar with TAP failure
    > > outputs: it returns with error code 29 and logs that I'd expect when
    > > the timeout is reached.
    >
    > It's also failing in the buildfarm, eg
    >
    > https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=conchuela&dt=2022-07-28%2020%3A57%3A50
    >
    > Looks like only conchuela so far, reinforcing the idea that we're
    > only seeing it on FreeBSD.  I'd tentatively bet on a timing problem
    > that requires some FreeBSD scheduling quirk to manifest; we've seen
    > such quirks before.
    
    Maybe it just needs a replication slot?  I see:
    
    ERROR:  requested WAL segment 000000010000000000000003 has already been removed
    
    
    
    
  124. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Kyotaro Horiguchi <horikyota.ntt@gmail.com> — 2022-07-29T05:20:08Z

    At Fri, 29 Jul 2022 11:27:01 +1200, Thomas Munro <thomas.munro@gmail.com> wrote in 
    > Maybe it just needs a replication slot?  I see:
    > 
    > ERROR:  requested WAL segment 000000010000000000000003 has already been removed
    
    Agreed, I see the same.  The same failure can be surely reproducible
    by inserting wal-switch+checkpoint after taking backup [1].  And it is
    fixed by the attached.
    
    regards.
    
    -- 
    Kyotaro Horiguchi
    NTT Open Source Software Center
    
    
    [1]:
    --- a/src/test/recovery/t/033_replay_tsp_drops.pl
    +++ b/src/test/recovery/t/033_replay_tsp_drops.pl
    @@ -30,6 +30,13 @@ sub test_tablespace
     	my $backup_name = 'my_backup';
     	$node_primary->backup($backup_name);
     
    +	$node_primary->psql(
    +		'postgres',
    +		qq[
    +		CREATE TABLE t(); DROP TABLE t; SELECT pg_switch_wal();
    +		CHECKPOINT;
    +		]);
    +
     	my $node_standby = PostgreSQL::Test::Cluster->new("standby2_$strategy");
     	$node_standby->init_from_backup($node_primary, $backup_name,
     		has_streaming => 1);
    
  125. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-07-29T10:59:03Z

    On 2022-Jul-29, Kyotaro Horiguchi wrote:
    
    > At Fri, 29 Jul 2022 11:27:01 +1200, Thomas Munro <thomas.munro@gmail.com> wrote in 
    > > Maybe it just needs a replication slot?  I see:
    > > 
    > > ERROR:  requested WAL segment 000000010000000000000003 has already been removed
    > 
    > Agreed, I see the same.  The same failure can be surely reproducible
    > by inserting wal-switch+checkpoint after taking backup [1].  And it is
    > fixed by the attached.
    
    WFM, pushed that way.  I added a slot drop after the pg_stat_replication
    count check to be a little less intrusive. Thanks Matthias for
    reporting.  (Note that the Cirrus page has a download link for the
    complete logs as artifacts).
    
    -- 
    Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
    "I'm always right, but sometimes I'm more right than other times."
                                                      (Linus Torvalds)
    
    
    
    
  126. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Tom Lane <tgl@sss.pgh.pa.us> — 2022-07-30T14:37:55Z

    Alvaro Herrera <alvherre@alvh.no-ip.org> writes:
    > WFM, pushed that way.
    
    Looks like conchuela is still intermittently unhappy.
    
    https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=conchuela&dt=2022-07-30%2004%3A57%3A51
    
    			regards, tom lane
    
    
    
    
  127. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Tom Lane <tgl@sss.pgh.pa.us> — 2022-07-30T16:51:35Z

    I wrote:
    > Looks like conchuela is still intermittently unhappy.
    
    BTW, quite aside from stability, is it really necessary for this test to
    be so freakin' slow?  florican for instance reports
    
    [12:43:38] t/025_stuck_on_old_timeline.pl ....... ok    49010 ms ( 0.00 usr  0.00 sys +  3.64 cusr  2.49 csys =  6.13 CPU)
    [12:44:12] t/026_overwrite_contrecord.pl ........ ok    34751 ms ( 0.01 usr  0.00 sys +  3.14 cusr  1.76 csys =  4.91 CPU)
    [12:49:00] t/027_stream_regress.pl .............. ok   287278 ms ( 0.00 usr  0.00 sys +  9.66 cusr  6.95 csys = 16.60 CPU)
    [12:50:04] t/028_pitr_timelines.pl .............. ok    64543 ms ( 0.00 usr  0.00 sys +  3.59 cusr  3.20 csys =  6.78 CPU)
    [12:50:17] t/029_stats_restart.pl ............... ok    12505 ms ( 0.02 usr  0.00 sys +  3.16 cusr  1.40 csys =  4.57 CPU)
    [12:50:51] t/030_stats_cleanup_replica.pl ....... ok    33933 ms ( 0.01 usr  0.01 sys +  3.55 cusr  2.46 csys =  6.03 CPU)
    [12:51:25] t/031_recovery_conflict.pl ........... ok    34249 ms ( 0.00 usr  0.00 sys +  3.37 cusr  2.20 csys =  5.57 CPU)
    [12:52:09] t/032_relfilenode_reuse.pl ........... ok    44274 ms ( 0.01 usr  0.00 sys +  3.21 cusr  2.05 csys =  5.27 CPU)
    [12:54:07] t/033_replay_tsp_drops.pl ............ ok   117840 ms ( 0.01 usr  0.00 sys +  8.72 cusr  5.41 csys = 14.14 CPU)
    
    027 is so bloated because it runs the core regression tests YA time,
    which I'm not very happy about either; but that's no excuse for
    every new test to contribute an additional couple of minutes.
    
    			regards, tom lane
    
    
    
    
  128. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-07-30T23:08:01Z

    On Sun, Jul 31, 2022 at 4:51 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > BTW, quite aside from stability, is it really necessary for this test to
    > be so freakin' slow?  florican for instance reports
    >
    > [12:43:38] t/025_stuck_on_old_timeline.pl ....... ok    49010 ms ( 0.00 usr  0.00 sys +  3.64 cusr  2.49 csys =  6.13 CPU)
    > [12:44:12] t/026_overwrite_contrecord.pl ........ ok    34751 ms ( 0.01 usr  0.00 sys +  3.14 cusr  1.76 csys =  4.91 CPU)
    > [12:49:00] t/027_stream_regress.pl .............. ok   287278 ms ( 0.00 usr  0.00 sys +  9.66 cusr  6.95 csys = 16.60 CPU)
    > [12:50:04] t/028_pitr_timelines.pl .............. ok    64543 ms ( 0.00 usr  0.00 sys +  3.59 cusr  3.20 csys =  6.78 CPU)
    > [12:50:17] t/029_stats_restart.pl ............... ok    12505 ms ( 0.02 usr  0.00 sys +  3.16 cusr  1.40 csys =  4.57 CPU)
    > [12:50:51] t/030_stats_cleanup_replica.pl ....... ok    33933 ms ( 0.01 usr  0.01 sys +  3.55 cusr  2.46 csys =  6.03 CPU)
    > [12:51:25] t/031_recovery_conflict.pl ........... ok    34249 ms ( 0.00 usr  0.00 sys +  3.37 cusr  2.20 csys =  5.57 CPU)
    > [12:52:09] t/032_relfilenode_reuse.pl ........... ok    44274 ms ( 0.01 usr  0.00 sys +  3.21 cusr  2.05 csys =  5.27 CPU)
    > [12:54:07] t/033_replay_tsp_drops.pl ............ ok   117840 ms ( 0.01 usr  0.00 sys +  8.72 cusr  5.41 csys = 14.14 CPU)
    >
    > 027 is so bloated because it runs the core regression tests YA time,
    > which I'm not very happy about either; but that's no excuse for
    > every new test to contribute an additional couple of minutes.
    
    Complaints about 027 noted, I'm thinking about what we could do about that.
    
    As for 033, I worried that it might be the new ProcSignalBarrier stuff
    around tablespaces, but thankfully the DEBUG logging I added there
    recently shows those all completing in single digit milliseconds.  I
    also confirmed there are no unexpected fsync'd being produced here.
    
    That is quite a lot of CPU, but it's a huge amount of total runtime.
    It runs in 5-8 seconds on various modern systems, 19 seconds on my
    Linux RPi4, and 50 seconds on my Celeron-powered NAS box with spinning
    disks.
    
    I noticed this is a 32 bit FBSD system.  Is it running on UFS, perhaps
    on slow storage?  Are soft updates enabled (visible as options in
    output of "mount")?  Without soft updates, a lot more file system ops
    perform synchronous I/O, which really slows down our tests.  In
    general, UFS isn't as good as modern file systems at avoiding I/O for
    short-lived files, and we set up and tear down a lot of them in our
    testing.  Another thing that makes a difference is to use a filesystem
    with 8KB block size.  This has been a subject of investigation for
    speeding up CI (see src/tools/ci/gcp_freebsd_repartition.sh), but
    several mysteries remain unsolved...
    
    
    
    
  129. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Tom Lane <tgl@sss.pgh.pa.us> — 2022-07-30T23:17:12Z

    Thomas Munro <thomas.munro@gmail.com> writes:
    > I noticed this is a 32 bit FBSD system.  Is it running on UFS, perhaps
    > on slow storage?  Are soft updates enabled (visible as options in
    > output of "mount")?
    
    It's an ancient (2006) mac mini with 5400RPM spinning rust.
    "mount" says
    
    /dev/ada0s2a on / (ufs, local, soft-updates, journaled soft-updates)
    devfs on /dev (devfs)
    
    			regards, tom lane
    
    
    
    
  130. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-07-31T03:46:33Z

    On Sun, Jul 31, 2022 at 2:37 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > Alvaro Herrera <alvherre@alvh.no-ip.org> writes:
    > > WFM, pushed that way.
    >
    > Looks like conchuela is still intermittently unhappy.
    >
    > https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=conchuela&dt=2022-07-30%2004%3A57%3A51
    
    And here's one from CI that failed on Linux (this was a cfbot run with
    an unrelated patch, parent commit b998196 so a few commits after "Fix
    test instability"):
    
    https://cirrus-ci.com/task/5282155000496128
    
    https://api.cirrus-ci.com/v1/artifact/task/5282155000496128/log/src/test/recovery/tmp_check/log/033_replay_tsp_drops_primary1_WAL_LOG.log
    
    It looks like this sequence is racy and we need to wait for more than
    just "connection is made" before dropping the slot?
    
        $node_standby->start;
    
        # Make sure connection is made
        $node_primary->poll_query_until('postgres',
            'SELECT count(*) = 1 FROM pg_stat_replication');
        $node_primary->safe_psql('postgres', "SELECT
    pg_drop_replication_slot('slot')");
    
    Why not set the replication slot name so that the standby uses it
    "properly", like in other tests?
    
    
    
    
  131. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Andres Freund <andres@anarazel.de> — 2022-08-01T02:01:04Z

    Hi,
    
    On 2022-07-30 10:37:55 -0400, Tom Lane wrote:
    > Alvaro Herrera <alvherre@alvh.no-ip.org> writes:
    > > WFM, pushed that way.
    > 
    > Looks like conchuela is still intermittently unhappy.
    > 
    > https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=conchuela&dt=2022-07-30%2004%3A57%3A51
    
    CI as well:
    https://cirrus-ci.com/task/5295464063959040?logs=test_world#L2671
    https://cirrus-ci.com/task/5042590885085184?logs=test_world#L2664
    
    Greetings,
    
    Andres Freund
    
    
    
    
  132. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-08-02T19:58:08Z

    On Sun, Jul 31, 2022 at 3:46 PM Thomas Munro <thomas.munro@gmail.com> wrote:
    > On Sun, Jul 31, 2022 at 2:37 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > > Alvaro Herrera <alvherre@alvh.no-ip.org> writes:
    > > > WFM, pushed that way.
    > >
    > > Looks like conchuela is still intermittently unhappy.
    > >
    > > https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=conchuela&dt=2022-07-30%2004%3A57%3A51
    >
    > And here's one from CI that failed on Linux (this was a cfbot run with
    > an unrelated patch, parent commit b998196 so a few commits after "Fix
    > test instability"):
    >
    > https://cirrus-ci.com/task/5282155000496128
    >
    > https://api.cirrus-ci.com/v1/artifact/task/5282155000496128/log/src/test/recovery/tmp_check/log/033_replay_tsp_drops_primary1_WAL_LOG.log
    >
    > It looks like this sequence is racy and we need to wait for more than
    > just "connection is made" before dropping the slot?
    >
    >     $node_standby->start;
    >
    >     # Make sure connection is made
    >     $node_primary->poll_query_until('postgres',
    >         'SELECT count(*) = 1 FROM pg_stat_replication');
    >     $node_primary->safe_psql('postgres', "SELECT
    > pg_drop_replication_slot('slot')");
    >
    > Why not set the replication slot name so that the standby uses it
    > "properly", like in other tests?
    
    Or to keep doing it this way, does that pg_stat_replication query need
    a WHERE clause looking at the state?
    
    
    
    
  133. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Thomas Munro <thomas.munro@gmail.com> — 2022-08-04T02:54:41Z

    On Sun, Jul 31, 2022 at 11:17 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > Thomas Munro <thomas.munro@gmail.com> writes:
    > > I noticed this is a 32 bit FBSD system.  Is it running on UFS, perhaps
    > > on slow storage?  Are soft updates enabled (visible as options in
    > > output of "mount")?
    >
    > It's an ancient (2006) mac mini with 5400RPM spinning rust.
    > "mount" says
    >
    > /dev/ada0s2a on / (ufs, local, soft-updates, journaled soft-updates)
    > devfs on /dev (devfs)
    
    I don't have all the details and I may be way off here but I have the
    impression that when you create and then unlink trees of files
    quickly, sometimes soft-updates are flushed synchronously, which turns
    into many 5400 RPM seeks; dtrace could be used to check, but some
    clues in your numbers would be some kind of correlation between time
    and number of clusters that are set up and torn down by each test.
    Without soft-updates, it'd be much worse, because then many more
    things become synchronous I/O.  Even with write caching enabled,
    soft-updates flush the drive cache when there's a barrier needed for
    crash safety.  It may also be that there is something strange about
    Apple hardware that makes it extra slow at full-cache-flush operations
    (cf unexplainable excess slowness of F_FULLFSYNC under macOS including
    old spinning rust systems and current flash systems, and complaints
    about this general area on current Apple hardware from the Asahi
    Linux/M1 port people, though how relevant that is to 2006 spinning
    rust I dunno).  It would be nice to look into how to tune, fix or work
    around all of that, as it also affects CI which has a IO limits
    (though admittedly a couple of orders of mag higher IOPS than 5400
    RPM).
    
    
    
    
  134. Re: standby recovery fails (tablespace related) (tentative patch and discussion)

    Alvaro Herrera <alvherre@alvh.no-ip.org> — 2022-08-05T20:29:40Z

    On 2022-Jul-30, Tom Lane wrote:
    
    > BTW, quite aside from stability, is it really necessary for this test to
    > be so freakin' slow?  florican for instance reports
    > 
    > [12:54:07] t/033_replay_tsp_drops.pl ............ ok   117840 ms ( 0.01 usr  0.00 sys +  8.72 cusr  5.41 csys = 14.14 CPU)
    > 
    > 027 is so bloated because it runs the core regression tests YA time,
    > which I'm not very happy about either; but that's no excuse for
    > every new test to contribute an additional couple of minutes.
    
    Definitely not intended.  It looks like the reason is just that the DROP
    DATABASE/TABLESPACE commands are super slow, and this test does a lot of
    that.  I added some instrumentation and the largest fraction of time
    goes to execute this
    
    		CREATE DATABASE dropme_db1 WITH TABLESPACE dropme_ts1;
    		CREATE TABLE t (a int) TABLESPACE dropme_ts2;
    		CREATE DATABASE dropme_db2 WITH TABLESPACE dropme_ts2;
    		CREATE DATABASE moveme_db TABLESPACE source_ts;
    		ALTER DATABASE moveme_db SET TABLESPACE target_ts;
    		CREATE DATABASE newdb TEMPLATE template_db;
    		ALTER DATABASE template_db IS_TEMPLATE = false;
    		DROP DATABASE dropme_db1;
    		DROP TABLE t;
    		DROP DATABASE dropme_db2;
    		DROP TABLESPACE dropme_ts2;
    		DROP TABLESPACE source_ts;
    		DROP DATABASE template_db;
    
    Maybe this is overkill and we can reduce the test without damaging the
    coverage.  I'll have a look during the weekend.
    
    I'll repair the reliability problem too, separately.
    
    -- 
    Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
    "This is a foot just waiting to be shot"                (Andrew Dunstan)