Streaming replication and WAL archive interactions

Поиск
Список
Период
Сортировка
Искать

Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnakangas@vmware.com>
Дата:
There have been a few threads on the behavior of WAL archiving, after a 
standby server is promoted [1] [2]. In short, it doesn't work as you 
might expect. The standby will start archiving after it's promoted, but 
it will not archive files that were replicated from the old master via 
streaming replication. If those files were not already archived in the 
master before the promotion, they are not archived at all. That's not 
good if you wanted to restore from a base backup + the WAL archive later.

The basic setup is a master server, a standby, a WAL archive that's 
shared by both, and streaming replication between the master and 
standby. This should be a very common setup in the field, so how are 
people doing it in practice? Just live with the wisk that you might miss 
some files in the archive if you promote? Don't even realize there's a 
problem? Something else?

And how would we like it to work?

There was some discussion in August on enabling WAL archiving in the 
standby, always [3]. That's a related idea, but it assumes that you have 
a separate archive in the master and the standby. The problem at 
promotion happens when you have a shared archive between the master and 
standby.

[1] 
http://www.postgresql.org/message-id/CAHGQGwHVYqbX=A+zo+AvFbVHLGoypO9G_QDKbabeXgXBVGd05g@mail.gmail.com

[2] http://www.postgresql.org/message-id/20140904175036.310c6466@erg

[3] 
http://www.postgresql.org/message-id/CAHGQGwHNMs-syU=MEVSESTHna+Exd9pfO_OHHFPJCwOVaYRZKw@mail.gmail.com.

- Heikki


Re: Streaming replication and WAL archive interactions

От:
Borodin Vladimir <root@simply.name>
Дата:

12 дек. 2014 г., в 16:46, Heikki Linnakangas <hlinnakangas@vmware.com> написал(а):

There have been a few threads on the behavior of WAL archiving, after a standby server is promoted [1] [2]. In short, it doesn't work as you might expect. The standby will start archiving after it's promoted, but it will not archive files that were replicated from the old master via streaming replication. If those files were not already archived in the master before the promotion, they are not archived at all. That's not good if you wanted to restore from a base backup + the WAL archive later.

The basic setup is a master server, a standby, a WAL archive that's shared by both, and streaming replication between the master and standby. This should be a very common setup in the field, so how are people doing it in practice? Just live with the wisk that you might miss some files in the archive if you promote? Don't even realize there's a problem? Something else?

Yes, I do live like that (with streaming replication and shared archive between master and replicas) and don’t even realize there’s a problem :( And I think I’m not the only one. Maybe at least a note should be added to the documentation?


And how would we like it to work?

There was some discussion in August on enabling WAL archiving in the standby, always [3]. That's a related idea, but it assumes that you have a separate archive in the master and the standby. The problem at promotion happens when you have a shared archive between the master and standby.

AFAIK most people use the scheme with shared archive.


[1] http://www.postgresql.org/message-id/CAHGQGwHVYqbX=A+zo+AvFbVHLGoypO9G_QDKbabeXgXBVGd05g@mail.gmail.com

[2] http://www.postgresql.org/message-id/20140904175036.310c6466@erg

[3] http://www.postgresql.org/message-id/CAHGQGwHNMs-syU=MEVSESTHna+Exd9pfO_OHHFPJCwOVaYRZKw@mail.gmail.com.

- Heikki


--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


--
Vladimir




Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnakangas@vmware.com>
Дата:
On 12/16/2014 10:24 AM, Borodin Vladimir wrote:
> 12 дек. 2014 г., в 16:46, Heikki Linnakangas
>  написал(а):
>
>> There have been a few threads on the behavior of WAL archiving,
>> after a standby server is promoted [1] [2]. In short, it doesn't
>> work as you might expect. The standby will start archiving after
>> it's promoted, but it will not archive files that were replicated
>> from the old master via streaming replication. If those files were
>> not already archived in the master before the promotion, they are
>> not archived at all. That's not good if you wanted to restore from
>> a base backup + the WAL archive later.
>>
>> The basic setup is a master server, a standby, a WAL archive that's
>> shared by both, and streaming replication between the master and
>> standby. This should be a very common setup in the field, so how
>> are people doing it in practice? Just live with the wisk that you
>> might miss some files in the archive if you promote? Don't even
>> realize there's a problem? Something else?
>
> Yes, I do live like that (with streaming replication and shared
> archive between master and replicas) and don’t even realize there’s a
> problem :( And I think I’m not the only one. Maybe at least a note
> should be added to the documentation?

Let's try to figure out a way to fix this in master, but yeah, a note in 
the documentation is in order.

>> And how would we like it to work?

Here's a plan:

Have a mechanism in the standby, to track how far the master has 
archived its WAL, and don't throw away WAL in the standby that hasn't 
been archived in the master yet. This is similar to the physical 
replication slots, which prevent the master from recycling WAL that a 
standby hasn't received yet, but in reverse. I think we can use the 
.done and .ready files for this. Whenever a file is streamed 
(completely) from the master, create a .ready file for it. When we get 
an acknowledgement from the master that it has archived it, create a 
.done file for it. To get the information from the master, add the "last 
archived WAL segment" e.g. in the streaming replication keep-alive 
message, or invent a new message type for it.

At promotion, archive all the WAL from the old timeline that the master 
hadn't already archived. While doing this, the archive_command can be 
called for files that have in fact already been archived in the master, 
so the command needs to return success if it's asked to archive a file 
and an identical file already exists in the archive. That's a bit 
difficult to write into a one-liner, but hopefully we can still provide 
an example of this. Or have another command, e.g. 
"promotion_archive_command", which can just assume that everything is OK 
if the file already exists.

To enable this new mode, let's add a third option to archive_mode, 
besides on/off. Or just make this the default; I'm not sure if anyone 
would want the old behavior.

>> There was some discussion in August on enabling WAL archiving in
>> the standby, always [3]. That's a related idea, but it assumes that
>> you have a separate archive in the master and the standby. The
>> problem at promotion happens when you have a shared archive between
>> the master and standby.
>
> AFAIK most people use the scheme with shared archive.

Yeah. Anyway, we can support both scenarios.

- Heikki



Re: Streaming replication and WAL archive interactions

От:
Fujii Masao <masao.fujii@gmail.com>
Дата:
On Wed, Dec 17, 2014 at 4:11 AM, Heikki Linnakangas
 wrote:
> On 12/16/2014 10:24 AM, Borodin Vladimir wrote:
>>
>> 12 дек. 2014 г., в 16:46, Heikki Linnakangas
>>  написал(а):
>>
>>> There have been a few threads on the behavior of WAL archiving,
>>> after a standby server is promoted [1] [2]. In short, it doesn't
>>> work as you might expect. The standby will start archiving after
>>> it's promoted, but it will not archive files that were replicated
>>> from the old master via streaming replication. If those files were
>>> not already archived in the master before the promotion, they are
>>> not archived at all. That's not good if you wanted to restore from
>>> a base backup + the WAL archive later.
>>>
>>> The basic setup is a master server, a standby, a WAL archive that's
>>> shared by both, and streaming replication between the master and
>>> standby. This should be a very common setup in the field, so how
>>> are people doing it in practice? Just live with the wisk that you
>>> might miss some files in the archive if you promote? Don't even
>>> realize there's a problem? Something else?
>>
>>
>> Yes, I do live like that (with streaming replication and shared
>> archive between master and replicas) and don’t even realize there’s a
>> problem :( And I think I’m not the only one. Maybe at least a note
>> should be added to the documentation?
>
>
> Let's try to figure out a way to fix this in master, but yeah, a note in the
> documentation is in order.

+1

>>> And how would we like it to work?
>
>
> Here's a plan:
>
> Have a mechanism in the standby, to track how far the master has archived
> its WAL, and don't throw away WAL in the standby that hasn't been archived
> in the master yet. This is similar to the physical replication slots, which
> prevent the master from recycling WAL that a standby hasn't received yet,
> but in reverse. I think we can use the .done and .ready files for this.
> Whenever a file is streamed (completely) from the master, create a .ready
> file for it. When we get an acknowledgement from the master that it has
> archived it, create a .done file for it. To get the information from the
> master, add the "last archived WAL segment" e.g. in the streaming
> replication keep-alive message, or invent a new message type for it.

Sounds OK to me.

How does this work in cascade replication case? The cascading walsender
just relays the archive location to the downstream standby?

What happens when WAL streaming is terminated and the startup process starts to
read the WAL file from the archive? After reading the WAL file from the archive,
probably we would need to change .ready files of every older WAL files to .done.

Regards,

-- 
Fujii Masao


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnakangas@vmware.com>
Дата:
Вложения:
On 12/18/2014 12:32 PM, Fujii Masao wrote:
> On Wed, Dec 17, 2014 at 4:11 AM, Heikki Linnakangas
>  wrote:
>> On 12/16/2014 10:24 AM, Borodin Vladimir wrote:
>>>
>>> 12 дек. 2014 г., в 16:46, Heikki Linnakangas
>>>  написал(а):
>>>
>>>> There have been a few threads on the behavior of WAL archiving,
>>>> after a standby server is promoted [1] [2]. In short, it doesn't
>>>> work as you might expect. The standby will start archiving after
>>>> it's promoted, but it will not archive files that were replicated
>>>> from the old master via streaming replication. If those files were
>>>> not already archived in the master before the promotion, they are
>>>> not archived at all. That's not good if you wanted to restore from
>>>> a base backup + the WAL archive later.
>>>>
>>>> The basic setup is a master server, a standby, a WAL archive that's
>>>> shared by both, and streaming replication between the master and
>>>> standby. This should be a very common setup in the field, so how
>>>> are people doing it in practice? Just live with the wisk that you
>>>> might miss some files in the archive if you promote? Don't even
>>>> realize there's a problem? Something else?
>>>
>>>
>>> Yes, I do live like that (with streaming replication and shared
>>> archive between master and replicas) and don’t even realize there’s a
>>> problem :( And I think I’m not the only one. Maybe at least a note
>>> should be added to the documentation?
>>
>>
>> Let's try to figure out a way to fix this in master, but yeah, a note in the
>> documentation is in order.
>
> +1
>
>>>> And how would we like it to work?
>>
>>
>> Here's a plan:
>>
>> Have a mechanism in the standby, to track how far the master has archived
>> its WAL, and don't throw away WAL in the standby that hasn't been archived
>> in the master yet. This is similar to the physical replication slots, which
>> prevent the master from recycling WAL that a standby hasn't received yet,
>> but in reverse. I think we can use the .done and .ready files for this.
>> Whenever a file is streamed (completely) from the master, create a .ready
>> file for it. When we get an acknowledgement from the master that it has
>> archived it, create a .done file for it. To get the information from the
>> master, add the "last archived WAL segment" e.g. in the streaming
>> replication keep-alive message, or invent a new message type for it.
>
> Sounds OK to me.
>
> How does this work in cascade replication case? The cascading walsender
> just relays the archive location to the downstream standby?

Hmm. Yeah, I guess so.

> What happens when WAL streaming is terminated and the startup process starts to
> read the WAL file from the archive? After reading the WAL file from the archive,
> probably we would need to change .ready files of every older WAL files to .done.

I suppose. Although there's no big harm in leaving them in .ready state. 
As soon as you reconnect, the primary will tell if they were archived. 
If the server is promoted before reconnecting, it will try to archive 
the files and archive_command will see that they are already in the 
archive. It has to be prepared for that situation anyway, so that's OK too.

Here's a first cut at this. It includes the changes from your 
standby_wal_archiving_v1.patch, so you get that behaviour if you set 
archive_mode='always', and the new behaviour I wanted with 
archive_mode='shared'. I wrote it on top of the other patch I posted 
recently to not archive bogus recycled WAL segments after promotion 
(http://www.postgresql.org/message-id/549489FA.4010304@vmware.com), but 
it seems to apply without it too.

I suggest reading the documentation changes first, it hopefully explains 
pretty well how to use this. The code should work too, and comments on 
that are welcome too, but I haven't tested it much. I'll do more testing 
next week.

- Heikki

Re: Streaming replication and WAL archive interactions

От:
Andres Freund <andres@2ndquadrant.com>
Дата:
Hi,

On 2014-12-19 22:56:40 +0200, Heikki Linnakangas wrote:
> This add two new archive_modes, 'shared' and 'always', to indicate whether
> the WAL archive is shared between the primary and standby, or not. In
> shared mode, the standby tracks which files have been archived by the
> primary. The standby refrains from recycling files that the primary has
> not yet archived, and at failover, the standby archives all those files too
> from the old timeline. In 'always' mode, the standby's WAL archive is
> taken to be separate from the primary's, and the standby independently
> archives all files it receives from the primary.

I don't really like this approach. Sharing a archive is rather dangerous
in my experience - if your old master comes up again (and writes in the
last wal file) or similar, you can get into really bad situations.

What I was thinking about was instead trying to detect the point up to
which files were safely archived by running restore command to check for
the presence of archived files. Then archive anything that has valid
content and isn't yet archived. That doesn't sound particularly
complicated to me.

Greetings,

Andres Freund


Re: [HACKERS] Streaming replication and WAL archive interactions

От:
Миша Тюрин <tmihail@bk.ru>
Дата:

> This should be a very common setup in the field, so how are people doing it in practice?

One of possible workaround with archive and streaming was to use pg_receivexlog from standby to copy/save WALs to archive. but with pg_receivexlog was also issue with fsync.


[ master ] -- streaming --> [ standby ] -- pg_receivexlog --> [ /archive ]


In that case archive is always in pre standby state and it could be better than had archive broken on promote.
--
Misha

Re: Streaming replication and WAL archive interactions

От:
Venkata Balaji N <nag1010@gmail.com>
Дата:

Here's a first cut at this. It includes the changes from your standby_wal_archiving_v1.patch, so you get that behaviour if you set archive_mode='always', and the new behaviour I wanted with archive_mode='shared'. I wrote it on top of the other patch I posted recently to not archive bogus recycled WAL segments after promotion (http://www.postgresql.org/message-id/549489FA.4010304@vmware.com), but it seems to apply without it too.

I suggest reading the documentation changes first, it hopefully explains pretty well how to use this. The code should work too, and comments on that are welcome too, but I haven't tested it much. I'll do more testing next week.

Patch did get applied successfully to the latest master. Can you please rebase.

Regards,
Venkata Balaji N


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
Вложения:
On 03/01/2015 12:36 AM, Venkata Balaji N wrote:
> Patch did get applied successfully to the latest master. Can you please
> rebase.

Here you go.

On 01/31/2015 03:07 PM, Andres Freund wrote:
> On 2014-12-19 22:56:40 +0200, Heikki Linnakangas wrote:
>> This add two new archive_modes, 'shared' and 'always', to indicate whether
>> the WAL archive is shared between the primary and standby, or not. In
>> shared mode, the standby tracks which files have been archived by the
>> primary. The standby refrains from recycling files that the primary has
>> not yet archived, and at failover, the standby archives all those files too
>> from the old timeline. In 'always' mode, the standby's WAL archive is
>> taken to be separate from the primary's, and the standby independently
>> archives all files it receives from the primary.
>
> I don't really like this approach. Sharing a archive is rather dangerous
> in my experience - if your old master comes up again (and writes in the
> last wal file) or similar, you can get into really bad situations.

It doesn't have to actually be shared. The master and standby could 
archive to different locations, but the responsibility of archiving is 
shared, so that on promotion, the standby ensures that every WAL file 
gets archived. If the master didn't do it, then the standby will.

Yes, if the master comes up again, it might try to archive a file that 
the standby already archived. But that's not so bad. Both copies of the 
file will be identical. You could put logic in archive_command to check, 
if the file already exists in the archive, whether the contents are 
identical, and return success without doing anything if they are.

Oh, hang on, that's not necessarily true. On promotion, the standby 
archives the last, partial WAL segment from the old timeline. That's 
just wrong 
(http://www.postgresql.org/message-id/52FCD37C.3070806@vmware.com), and 
in fact I somehow thought I changed that already, but apparently not. So 
let's stop doing that.

> What I was thinking about was instead trying to detect the point up to
> which files were safely archived by running restore command to check for
> the presence of archived files. Then archive anything that has valid
> content and isn't yet archived. That doesn't sound particularly
> complicated to me.

Hmm. That assumes that the standby has a valid restore_command, and can 
access the WAL archive. Not a too unreasonable requirement I guess, but 
with the scheme I proposed, it's not necessary. Seems a bit silly to 
copy a whole segment from the archive just to check if it exists, though.

- Heikki

Re: Streaming replication and WAL archive interactions

От:
Michael Paquier <michael.paquier@gmail.com>
Дата:


On Thu, Apr 16, 2015 at 8:57 PM, Heikki Linnakangas wrote:
> Oh, hang on, that's not necessarily true. On promotion, the standby archives
> the last, partial WAL segment from the old timeline. That's just wrong
> (http://www.postgresql.org/message-id/52FCD37C.3070806@vmware.com), and in
> fact I somehow thought I changed that already, but apparently not. So let's
> stop doing that.

Er. Are you planning to prevent the standby from archiving the last partial segment from the old timeline at promotion? I thought from previous discussions that we should do it as master (be it crashed, burned, burried or dead) may not have the occasion to do it. By preventing its archiving you close the door to the case where master did not have the occasion to archive it.

+/* */
+static char primary_last_archived[MAX_XFN_CHARS + 1];
This is visibly missing a comment.

As primary_last_archived is used only by ProcessArchivalReport(), wouldn't it be better to pass it as argument to this function?

+       /* Check that the filename the primary reported looks valid */
+       if (strlen(primary_last_archived) < 24 ||
+               strspn(primary_last_archived, "0123456789ABCDEF") != 24)
+               return;
Not related to this patch, but we had better have a macro doing this job I think... It keeps spreading around.

People may be surprised that a base backup taken from a node that has archive_mode = on set (that's the case in a very large number of cases) will not be able to work as-is as node startup will fail as follows:
FATAL:  archive_mode='on' cannot be used in archive recovery
HINT:  Use 'shared' or 'always' mode instead.
One idea would be to simply ignore the fact that archive_mode = on on nodes in recovery instead of dropping an error. Note that I like the fact that it drops an error as that's clear, I just point the fact that people may be surprised that base backups are not working anymore now in this case.

Are both WalSndArchivalReport() and WalSndArchivalReportIfNecessary() really necessary? I think that for simplicity you could merge them and use last_archival_report as a local variable.

Creating a dependency between the pgstat machinery and the WAL sender looks weak to me. For example with this patch a master cannot stop, as it waits indefinitely:
LOG:  using stale statistics instead of current ones because stats collector is not responding
LOG:  sending archival report:
You could scan archive_status/ but that would be costly if there are many entries to scan and I think that walsender should be highly responsive. Or you could directly store the name of the lastly archived WAL segment marked as .done in let's say archive_status/last_archived. An entry for that in the control file does not seem the right place as a node may not have archive_mode enabled that's why I am not mentioning it.

Regards,
--
Michael

Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 04/21/2015 09:53 AM, Michael Paquier wrote:
> On Thu, Apr 16, 2015 at 8:57 PM, Heikki Linnakangas wrote:
>> Oh, hang on, that's not necessarily true. On promotion, the standby
> archives
>> the last, partial WAL segment from the old timeline. That's just wrong
>> (http://www.postgresql.org/message-id/52FCD37C.3070806@vmware.com), and in
>> fact I somehow thought I changed that already, but apparently not. So
> let's
>> stop doing that.
>
> Er. Are you planning to prevent the standby from archiving the last partial
> segment from the old timeline at promotion?

Yes.

> I thought from previous discussions that we should do it as master
> (be it crashed, burned, burried or dead) may not have the occasion to
> do it. By preventing its archiving you close the door to the case
> where master did not have the occasion to archive it.

The current situation is a mess:

1. Even though we archive the last segment in the standby, there is no 
guarantee that the master had archived all the previous segments already.

2. If the master is not totally dead, it might try to archive the same 
file with more WAL in it, at the same time or just afterwards, or even 
just before the standby has completed promotion. Which copy do you keep 
in the archive? Having to deal with that makes the archive_command more 
complicated.

Note that even though we don't archive the partial last segment on the 
previous timeline, the same WAL is copied to the first segment on the 
new timeline. So the WAL isn't lost.

> People may be surprised that a base backup taken from a node that has
> archive_mode = on set (that's the case in a very large number of cases)
> will not be able to work as-is as node startup will fail as follows:
> FATAL:  archive_mode='on' cannot be used in archive recovery
> HINT:  Use 'shared' or 'always' mode instead.

Hmm, good point.

> One idea would be to simply ignore the fact that archive_mode = on on nodes
> in recovery instead of dropping an error. Note that I like the fact that it
> drops an error as that's clear, I just point the fact that people may be
> surprised that base backups are not working anymore now in this case.

By "ignore", what behaviour do you mean? Would "on" be equivalent to 
"shared", "always", or something else?

Or we could keep the current behaviour with archive_mode=on (except for 
the last segment thing, which is just wrong), where the standby only 
archives the new timeline, and nothing from the previous timelines. Are 
the use cases where you'd want that, rather than the new "shared" mode? 
I wanted to keep the 'on' mode for backwards-compatibility, but if that 
causes more problems, it might be better to just remove it and force the 
admin to choose what kind of a setup he has, with "shared" or "always".

> Creating a dependency between the pgstat machinery and the WAL sender looks
> weak to me. For example with this patch a master cannot stop, as it waits
> indefinitely:
> LOG:  using stale statistics instead of current ones because stats
> collector is not responding
> LOG:  sending archival report:

Hmm, yeah, having walsender to wait for the stats file to appear is not 
good.

> You could scan archive_status/ but that would be costly if there are many
> entries to scan and I think that walsender should be highly responsive. Or
> you could directly store the name of the lastly archived WAL segment marked
> as .done in let's say archive_status/last_archived. An entry for that in
> the control file does not seem the right place as a node may not have
> archive_mode enabled that's why I am not mentioning it.

The ways that the archiver process can communicate with the rest of the 
system are limited, for the sake of robustness. Writing to the control 
file is definitely not OK. I think using the stats collector is OK for 
this, but we'll have to arrange it so that the walsender doesn't block 
on it, and should probably not force new stat file so often. A 5-10 
seconds old stats file would be perfectly fine for this purpose.

- Heikki



Re: Streaming replication and WAL archive interactions

От:
Michael Paquier <michael.paquier@gmail.com>
Дата:


On Tue, Apr 21, 2015 at 4:38 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
On 04/21/2015 09:53 AM, Michael Paquier wrote:
On Thu, Apr 16, 2015 at 8:57 PM, Heikki Linnakangas wrote:
Oh, hang on, that's not necessarily true. On promotion, the standby
archives
the last, partial WAL segment from the old timeline. That's just wrong
(http://www.postgresql.org/message-id/52FCD37C.3070806@vmware.com), and in
fact I somehow thought I changed that already, but apparently not. So
let's
stop doing that.

Er. Are you planning to prevent the standby from archiving the last partial
segment from the old timeline at promotion?

Yes.

I thought from previous discussions that we should do it as master
(be it crashed, burned, burried or dead) may not have the occasion to
do it. By preventing its archiving you close the door to the case
where master did not have the occasion to archive it.

The current situation is a mess:

1. Even though we archive the last segment in the standby, there is no guarantee that the master had archived all the previous segments already.
2. If the master is not totally dead, it might try to archive the same file with more WAL in it, at the same time or just afterwards, or even just before the standby has completed promotion. Which copy do you keep in the archive? Having to deal with that makes the archive_command more complicated.

Note that even though we don't archive the partial last segment on the previous timeline, the same WAL is copied to the first segment on the new timeline. So the WAL isn't lost.

But if the failed master has archived those segments safely, we may need them, no? I am not sure we can ignore a user who would want to do a PITR with recovery_target_timeline pointing to the one of the failed master.
 

People may be surprised that a base backup taken from a node that has
archive_mode = on set (that's the case in a very large number of cases)
will not be able to work as-is as node startup will fail as follows:
FATAL:  archive_mode='on' cannot be used in archive recovery
HINT:  Use 'shared' or 'always' mode instead.

Hmm, good point.

One idea would be to simply ignore the fact that archive_mode = on on nodes
in recovery instead of dropping an error. Note that I like the fact that it
drops an error as that's clear, I just point the fact that people may be
surprised that base backups are not working anymore now in this case.

By "ignore", what behaviour do you mean? Would "on" be equivalent to "shared", "always", or something else?

I meant something backward-compatible, with files marked as .done when they are finished replaying... But now my words *are* weird as on != off ;)

Or we could keep the current behaviour with archive_mode=on (except for the last segment thing, which is just wrong), where the standby only archives the new timeline, and nothing from the previous timelines.

I guess this would solve the issue here then, which is not a bad thing in itself:
http://www.postgresql.org/message-id/20140918180734.361021e1@erg
We would need to check if the situation improves with the 'always' mode btw.
 
Are the use cases where you'd want that, rather than the new "shared" mode? I wanted to keep the 'on' mode for backwards-compatibility, but if that causes more problems, it might be better to just remove it and force the admin to choose what kind of a setup he has, with "shared" or "always".

The 'on' mode is still useful IMO to get a behavior a maximum close to what previous releases did.
Regards,
--
Michael

Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 04/21/2015 12:04 PM, Michael Paquier wrote:
> On Tue, Apr 21, 2015 at 4:38 PM, Heikki Linnakangas  wrote:
>
>> Note that even though we don't archive the partial last segment on the
>> previous timeline, the same WAL is copied to the first segment on the new
>> timeline. So the WAL isn't lost.
>
> But if the failed master has archived those segments safely, we may need
> them, no? I am not sure we can ignore a user who would want to do a PITR
> with recovery_target_timeline pointing to the one of the failed master.

I think it would be acceptable. If you want to maintain an 
up-to-the-second archive, you can use pg_receivexlog. Mind you, if the 
standby wasn't promoted, the partial segment would not be present in the 
archive anyway. And you can copy the WAL segment manually from 
0000000200000000000000XX to pg_xlog/0000000100000000000000XX before 
starting PITR.

Another thought is that we could archive the partial file, but with a 
different name to avoid confusing it with the full segment. For example, 
we could archive a partial 000000010000000000000012 segment as 
"000000020000000000000012.00000128.partial", where 00000128 indicates 
how far that file is valid (this naming is similar to how the backup 
history files are named). Recovery wouldn't automatically pick up those 
files, but the DBA could easily copy the partial file into pg_xlog with 
the full segment's name, if he wants to do PITR to that piece of WAL.

>> Are the use cases where you'd want that, rather than the new "shared"
>> mode? I wanted to keep the 'on' mode for backwards-compatibility, but if
>> that causes more problems, it might be better to just remove it and force
>> the admin to choose what kind of a setup he has, with "shared" or "always".
>
> The 'on' mode is still useful IMO to get a behavior a maximum close to what
> previous releases did.

But would you ever want the old behaviour, rather than the new shared or 
always behaviour?

- Heikki



Re: Streaming replication and WAL archive interactions

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Tue, Apr 21, 2015 at 6:55 AM, Heikki Linnakangas  wrote:
> On 04/21/2015 12:04 PM, Michael Paquier wrote:
>> On Tue, Apr 21, 2015 at 4:38 PM, Heikki Linnakangas 
>> wrote:
>>> Note that even though we don't archive the partial last segment on the
>>> previous timeline, the same WAL is copied to the first segment on the new
>>> timeline. So the WAL isn't lost.
>>
>> But if the failed master has archived those segments safely, we may need
>> them, no? I am not sure we can ignore a user who would want to do a PITR
>> with recovery_target_timeline pointing to the one of the failed master.
>
> I think it would be acceptable. If you want to maintain an up-to-the-second
> archive, you can use pg_receivexlog. Mind you, if the standby wasn't
> promoted, the partial segment would not be present in the archive anyway.
> And you can copy the WAL segment manually from 0000000200000000000000XX to
> pg_xlog/0000000100000000000000XX before starting PITR.
>
> Another thought is that we could archive the partial file, but with a
> different name to avoid confusing it with the full segment. For example, we
> could archive a partial 000000010000000000000012 segment as
> "000000020000000000000012.00000128.partial", where 00000128 indicates how
> far that file is valid (this naming is similar to how the backup history
> files are named). Recovery wouldn't automatically pick up those files, but
> the DBA could easily copy the partial file into pg_xlog with the full
> segment's name, if he wants to do PITR to that piece of WAL.

So, suppose you A replicating to B (via an archive) replicating to C
(via a separate archive); A dies, B is promoted.  It sounds to me like
today this will work and with your proposed change it will require
manual intervention.  I don't think that's OK.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


Re: Streaming replication and WAL archive interactions

От:
Michael Paquier <michael.paquier@gmail.com>
Дата:
On Wed, Apr 22, 2015 at 6:42 AM, Robert Haas  wrote:
>
> On Tue, Apr 21, 2015 at 6:55 AM, Heikki Linnakangas  wrote:
> > On 04/21/2015 12:04 PM, Michael Paquier wrote:
> >> On Tue, Apr 21, 2015 at 4:38 PM, Heikki Linnakangas 
> >> wrote:
> >>> Note that even though we don't archive the partial last segment on the
> >>> previous timeline, the same WAL is copied to the first segment on the new
> >>> timeline. So the WAL isn't lost.
> >>
> >> But if the failed master has archived those segments safely, we may need
> >> them, no? I am not sure we can ignore a user who would want to do a PITR
> >> with recovery_target_timeline pointing to the one of the failed master.
> >
> > I think it would be acceptable. If you want to maintain an up-to-the-second
> > archive, you can use pg_receivexlog. Mind you, if the standby wasn't
> > promoted, the partial segment would not be present in the archive anyway.
> > And you can copy the WAL segment manually from 0000000200000000000000XX to
> > pg_xlog/0000000100000000000000XX before starting PITR.
> >
> > Another thought is that we could archive the partial file, but with a
> > different name to avoid confusing it with the full segment. For example, we
> > could archive a partial 000000010000000000000012 segment as
> > "000000020000000000000012.00000128.partial", where 00000128 indicates how
> > far that file is valid (this naming is similar to how the backup history
> > files are named). Recovery wouldn't automatically pick up those files, but
> > the DBA could easily copy the partial file into pg_xlog with the full
> > segment's name, if he wants to do PITR to that piece of WAL.
>
> So, suppose you A replicating to B (via an archive) replicating to C
> (via a separate archive); A dies, B is promoted.  It sounds to me like
> today this will work and with your proposed change it will require
> manual intervention.  I don't think that's OK.


This is going to change a behavior that people are used to for a
couple of releases. I would not mind having this patch do
"archive_mode = on during recovery" => archive only segments generated
by this node + the last partial segment on the old timeline at
promotion, without renaming to preserve backward compatible behavior.
If master and standby point to separate archive locations, this way
the operator can sort out later the one he would want to use. If they
point to the same location, archive_command scripts already do
internally such renaming, at least that's what I suspect an
experienced user would do, now it is true that not many people are
experienced in this area I see mistakes regarding such things on a
weekly basis... This .partial segment renaming is something that we
should let the archive_command manage with its internal logic.
Regards,
-- 
Michael


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 04/22/2015 12:42 AM, Robert Haas wrote:
> On Tue, Apr 21, 2015 at 6:55 AM, Heikki Linnakangas  wrote:
>> On 04/21/2015 12:04 PM, Michael Paquier wrote:
>>> On Tue, Apr 21, 2015 at 4:38 PM, Heikki Linnakangas 
>>> wrote:
>>>> Note that even though we don't archive the partial last segment on the
>>>> previous timeline, the same WAL is copied to the first segment on the new
>>>> timeline. So the WAL isn't lost.
>>>
>>> But if the failed master has archived those segments safely, we may need
>>> them, no? I am not sure we can ignore a user who would want to do a PITR
>>> with recovery_target_timeline pointing to the one of the failed master.
>>
>> I think it would be acceptable. If you want to maintain an up-to-the-second
>> archive, you can use pg_receivexlog. Mind you, if the standby wasn't
>> promoted, the partial segment would not be present in the archive anyway.
>> And you can copy the WAL segment manually from 0000000200000000000000XX to
>> pg_xlog/0000000100000000000000XX before starting PITR.
>>
>> Another thought is that we could archive the partial file, but with a
>> different name to avoid confusing it with the full segment. For example, we
>> could archive a partial 000000010000000000000012 segment as
>> "000000020000000000000012.00000128.partial", where 00000128 indicates how
>> far that file is valid (this naming is similar to how the backup history
>> files are named). Recovery wouldn't automatically pick up those files, but
>> the DBA could easily copy the partial file into pg_xlog with the full
>> segment's name, if he wants to do PITR to that piece of WAL.
>
> So, suppose you A replicating to B (via an archive) replicating to C
> (via a separate archive); A dies, B is promoted.  It sounds to me like
> today this will work and with your proposed change it will require
> manual intervention.

No. If there is no streaming replication involved, no partial files will 
be archived, with or without this patch. There is no change to that 
scenario.

Note that it's a bit complicated to set up that scenario today. 
Archiving is never enabled in recovery mode, so you'll need to use a 
custom cron job or something to maintain the archive that C uses. The 
files will not automatically flow from B to the second archive. With the 
patch we're discussing, however, it would be easy: just set 
archive_mode='always' in B.

- Heikki


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 04/22/2015 03:30 AM, Michael Paquier wrote:
> This is going to change a behavior that people are used to for a
> couple of releases. I would not mind having this patch do
> "archive_mode = on during recovery" => archive only segments generated
> by this node + the last partial segment on the old timeline at
> promotion, without renaming to preserve backward compatible behavior.
> If master and standby point to separate archive locations, this way
> the operator can sort out later the one he would want to use. If they
> point to the same location, archive_command scripts already do
> internally such renaming, at least that's what I suspect an
> experienced user would do, now it is true that not many people are
> experienced in this area I see mistakes regarding such things on a
> weekly basis... This .partial segment renaming is something that we
> should let the archive_command manage with its internal logic.

Currently, the archive command doesn't know if the segment it's 
archiving is partial or not, so you can't put any logic there to manage 
it. But if we archive it with the .partial suffix, then you can put 
logic in the restore_command to check for .partial files, if you really 
want to.

I feel that the best approach is to archive the last, partial segment, 
but with the .partial suffix. I don't see any plausible real-world setup 
where the current behaviour would be better. I don't really see much 
need to archive the partial segment at all, but there's also no harm in 
doing it, as long as it's clearly marked with the .partial suffix.

BTW, pg_receivexlog also uses a ".partial" file, while it's streaming 
WAL from the server. The .partial suffix is removed when the segment is 
complete. So there's some precedence to this. pg_receivexlog adds just 
".partial" to the filename, it doesn't add any information of what 
portion of the file is valid like I suggested here, though. Perhaps we 
should follow pg_receivexlog's example at promotion too, for consistency.

- Heikki



Re: Streaming replication and WAL archive interactions

От:
Michael Paquier <michael.paquier@gmail.com>
Дата:
On Wed, Apr 22, 2015 at 3:38 PM, Heikki Linnakangas  wrote:
> On 04/22/2015 03:30 AM, Michael Paquier wrote:
>>
>> This is going to change a behavior that people are used to for a
>> couple of releases. I would not mind having this patch do
>> "archive_mode = on during recovery" => archive only segments generated
>> by this node + the last partial segment on the old timeline at
>> promotion, without renaming to preserve backward compatible behavior.
>> If master and standby point to separate archive locations, this way
>> the operator can sort out later the one he would want to use. If they
>> point to the same location, archive_command scripts already do
>> internally such renaming, at least that's what I suspect an
>> experienced user would do, now it is true that not many people are
>> experienced in this area I see mistakes regarding such things on a
>> weekly basis... This .partial segment renaming is something that we
>> should let the archive_command manage with its internal logic.
>
>
> Currently, the archive command doesn't know if the segment it's archiving is
> partial or not, so you can't put any logic there to manage it. But if we
> archive it with the .partial suffix, then you can put logic in the
> restore_command to check for .partial files, if you really want to.

Well, now you can check as well if there is a file with the same name
already archived and append a suffix to the new file copied, keep the
two files, and then let restore_command sort things up as it wants
with the two segment files it finds.

> I feel that the best approach is to archive the last, partial segment, but
> with the .partial suffix. I don't see any plausible real-world setup where
> the current behavior would be better. I don't really see much need to
> archive the partial segment at all, but there's also no harm in doing it, as
> long as it's clearly marked with the .partial suffix.

Well, as long as it is clearly archived at promotion, even with a
suffix, I guess that I am fine... This will need some tweaking on
restore_command for existing applications, but as long as it is
clearly documented I am fine. Shouldn't this be a different patch
though?

> BTW, pg_receivexlog also uses a ".partial" file, while it's streaming WAL
> from the server. The .partial suffix is removed when the segment is
> complete. So there's some precedence to this. pg_receivexlog adds just
> ".partial" to the filename, it doesn't add any information of what portion
> of the file is valid like I suggested here, though. Perhaps we should follow
> pg_receivexlog's example at promotion too, for consistency.

Consistency here sounds good to me.
-- 
Michael


Re: Streaming replication and WAL archive interactions

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Wed, Apr 22, 2015 at 2:17 AM, Heikki Linnakangas  wrote:
> Note that it's a bit complicated to set up that scenario today. Archiving is
> never enabled in recovery mode, so you'll need to use a custom cron job or
> something to maintain the archive that C uses. The files will not
> automatically flow from B to the second archive. With the patch we're
> discussing, however, it would be easy: just set archive_mode='always' in B.

Hmm, I see.  But if C never replays the last, partial segment from the
old timeline, how does it follow the timeline switch?

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


Re: Streaming replication and WAL archive interactions

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Tue, Apr 21, 2015 at 8:30 PM, Michael Paquier
 wrote:
> This .partial segment renaming is something that we
> should let the archive_command manage with its internal logic.

This strikes me as equivalent to saying "we don't know how to make
this work right, but maybe our users will know".  That never works
out.  As things stand, we have a situation where the archive_command
examples in our documentation are known to be flawed.  They don't
fsync the file, and they'll write a partial file and then, when rerun,
fail to copy the full file because there's already something there.
Efforts have been made to fix these problems (see the pg_copy thread),
but they haven't been completed yet, nor have we even documented the
issues with the commands recommended by the documentation.  Let's
please not throw anything else on the pile of things we're expecting
users to somehow "get right".

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 04/22/2015 09:30 PM, Robert Haas wrote:
> On Wed, Apr 22, 2015 at 2:17 AM, Heikki Linnakangas  wrote:
>> Note that it's a bit complicated to set up that scenario today. Archiving is
>> never enabled in recovery mode, so you'll need to use a custom cron job or
>> something to maintain the archive that C uses. The files will not
>> automatically flow from B to the second archive. With the patch we're
>> discussing, however, it would be easy: just set archive_mode='always' in B.
>
> Hmm, I see.  But if C never replays the last, partial segment from the
> old timeline, how does it follow the timeline switch?

At timeline switch, we copy the old segment to the new timeline, and 
start writing where we left off. So the WAL from the old timeline is 
found in the segment nominally belonging to the new timeline.

For example, imagine that perform point-in-time recovery to WAL position 
0/1237E568, on timeline 1. That falls within segment 
000000010000000000000012. Then we end recovery, and switch to timeline 
2. After the switch, and some more WAL-logged actions, we'll have these 
files in pg_xlog:

000000010000000000000011
000000010000000000000012
000000020000000000000012
000000020000000000000013
000000020000000000000014

Note that there are two segments ending in "12". They both have the same 
point up to offset 0x37E568, corresponding to the switch point 
0/1237E568. After that, the contents diverge: the segment on the new 
timeline contains a checkpoint/end-of-recovery record at that point, 
followed by new WAL belonging to the new timeline.

Recovery knows about that, so that if you set recovery target to 
timeline 2, and it needs the WAL at the beginning of segment 12 (still 
belonging to timeline 1), it will try to restoring both 
"000000010000000000000012" and "000000020000000000000012".

- Heikki



Re: Streaming replication and WAL archive interactions

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Wed, Apr 22, 2015 at 3:01 PM, Heikki Linnakangas  wrote:
> On 04/22/2015 09:30 PM, Robert Haas wrote:
>> On Wed, Apr 22, 2015 at 2:17 AM, Heikki Linnakangas 
>> wrote:
>>>
>>> Note that it's a bit complicated to set up that scenario today. Archiving
>>> is
>>> never enabled in recovery mode, so you'll need to use a custom cron job
>>> or
>>> something to maintain the archive that C uses. The files will not
>>> automatically flow from B to the second archive. With the patch we're
>>> discussing, however, it would be easy: just set archive_mode='always' in
>>> B.
>>
>>
>> Hmm, I see.  But if C never replays the last, partial segment from the
>> old timeline, how does it follow the timeline switch?
>
> At timeline switch, we copy the old segment to the new timeline, and start
> writing where we left off. So the WAL from the old timeline is found in the
> segment nominally belonging to the new timeline.

Check.

> For example, imagine that perform point-in-time recovery to WAL position
> 0/1237E568, on timeline 1. That falls within segment
> 000000010000000000000012. Then we end recovery, and switch to timeline 2.
> After the switch, and some more WAL-logged actions, we'll have these files
> in pg_xlog:
>
> 000000010000000000000011
> 000000010000000000000012
> 000000020000000000000012
> 000000020000000000000013
> 000000020000000000000014

Is the 000000010000000000000012 file a "partial" segment of the sort
you're proposing to no longer achive?

> Note that there are two segments ending in "12". They both have the same
> point up to offset 0x37E568, corresponding to the switch point 0/1237E568.
> After that, the contents diverge: the segment on the new timeline contains a
> checkpoint/end-of-recovery record at that point, followed by new WAL
> belonging to the new timeline.

Check.

> Recovery knows about that, so that if you set recovery target to timeline 2,
> and it needs the WAL at the beginning of segment 12 (still belonging to
> timeline 1), it will try to restoring both "000000010000000000000012" and
> "000000020000000000000012".

What if you set the recovery target to timeline 3?

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 04/22/2015 10:21 PM, Robert Haas wrote:
> On Wed, Apr 22, 2015 at 3:01 PM, Heikki Linnakangas  wrote:
>> For example, imagine that perform point-in-time recovery to WAL position
>> 0/1237E568, on timeline 1. That falls within segment
>> 000000010000000000000012. Then we end recovery, and switch to timeline 2.
>> After the switch, and some more WAL-logged actions, we'll have these files
>> in pg_xlog:
>>
>> 000000010000000000000011
>> 000000010000000000000012
>> 000000020000000000000012
>> 000000020000000000000013
>> 000000020000000000000014
>
> Is the 000000010000000000000012 file a "partial" segment of the sort
> you're proposing to no longer achive?

If you did pure archive recovery, with no streaming replication 
involved, then no. If it was created by streaming replication, and the 
replication had not filled the whole segment yet, then yes, it would be 
a partial segment.

>> Note that there are two segments ending in "12". They both have the same
>> point up to offset 0x37E568, corresponding to the switch point 0/1237E568.
>> After that, the contents diverge: the segment on the new timeline contains a
>> checkpoint/end-of-recovery record at that point, followed by new WAL
>> belonging to the new timeline.
>
> Check.
>
>> Recovery knows about that, so that if you set recovery target to timeline 2,
>> and it needs the WAL at the beginning of segment 12 (still belonging to
>> timeline 1), it will try to restoring both "000000010000000000000012" and
>> "000000020000000000000012".
>
> What if you set the recovery target to timeline 3?

It depends how timeline 3 was created. If timeline 3 was forked off from 
timeline 2, then recovery would find it. If it was forked off directly 
from timeline 1, then no.

- Heikki



Re: Streaming replication and WAL archive interactions

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Wed, Apr 22, 2015 at 3:34 PM, Heikki Linnakangas  wrote:
> On 04/22/2015 10:21 PM, Robert Haas wrote:
>> On Wed, Apr 22, 2015 at 3:01 PM, Heikki Linnakangas 
>> wrote:
>>> For example, imagine that perform point-in-time recovery to WAL position
>>> 0/1237E568, on timeline 1. That falls within segment
>>> 000000010000000000000012. Then we end recovery, and switch to timeline 2.
>>> After the switch, and some more WAL-logged actions, we'll have these
>>> files
>>> in pg_xlog:
>>>
>>> 000000010000000000000011
>>> 000000010000000000000012
>>> 000000020000000000000012
>>> 000000020000000000000013
>>> 000000020000000000000014
>>
>>
>> Is the 000000010000000000000012 file a "partial" segment of the sort
>> you're proposing to no longer achive?
>
> If you did pure archive recovery, with no streaming replication involved,
> then no. If it was created by streaming replication, and the replication had
> not filled the whole segment yet, then yes, it would be a partial segment.

Why the difference?

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 04/22/2015 11:58 PM, Robert Haas wrote:
> On Wed, Apr 22, 2015 at 3:34 PM, Heikki Linnakangas  wrote:
>> On 04/22/2015 10:21 PM, Robert Haas wrote:
>>> On Wed, Apr 22, 2015 at 3:01 PM, Heikki Linnakangas 
>>> wrote:
>>>> For example, imagine that perform point-in-time recovery to WAL position
>>>> 0/1237E568, on timeline 1. That falls within segment
>>>> 000000010000000000000012. Then we end recovery, and switch to timeline 2.
>>>> After the switch, and some more WAL-logged actions, we'll have these
>>>> files
>>>> in pg_xlog:
>>>>
>>>> 000000010000000000000011
>>>> 000000010000000000000012
>>>> 000000020000000000000012
>>>> 000000020000000000000013
>>>> 000000020000000000000014
>>>
>>>
>>> Is the 000000010000000000000012 file a "partial" segment of the sort
>>> you're proposing to no longer achive?
>>
>> If you did pure archive recovery, with no streaming replication involved,
>> then no. If it was created by streaming replication, and the replication had
>> not filled the whole segment yet, then yes, it would be a partial segment.
>
> Why the difference?

Because we don't archive partial segments, except for the last one at a 
timeline switch, and there was no timeline switch to timeline 1 within 
that segment.

It doesn't really matter, though. The behaviour at the switch from 
timeline 1 to 2 works the same, whether the 000000010000000000000012 
segment is complete or not.

- Heikki



Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
Вложения:
On 04/22/2015 10:07 AM, Michael Paquier wrote:
> On Wed, Apr 22, 2015 at 3:38 PM, Heikki Linnakangas  wrote:
>> I feel that the best approach is to archive the last, partial segment, but
>> with the .partial suffix. I don't see any plausible real-wold setup where
>> the current behavior would be better. I don't really see much need to
>> archive the partial segment at all, but there's also no harm in doing it, as
>> long as it's clearly marked with the .partial suffix.
>
> Well, as long as it is clearly archived at promotion, even with a
> suffix, I guess that I am fine... This will need some tweaking on
> restore_command for existing applications, but as long as it is
> clearly documented I am fine. Shouldn't this be a different patch
> though?

Ok, I came up with the attached, which adds the .partial suffix to the 
partial WAL segment that's archived after promotion. I couldn't find any 
natural place to talk about it in the docs, though. I think after the 
docs changes from the main patch are applied, it would be natural to 
mention this in the "Continuous archiving in standby", so I think I'll 
add that later.

Barring objections, I'll push this later tonight.

Now that we got this last-partial-segment problem out of the way, I'm 
going to try fixing the problem you (Michael) pointed out about relying 
on pgstat file. Meanwhile, I'd love to get more feedback on the rest of 
the patch, and the documentation.

- Heikki

Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
Вложения:
On 05/08/2015 04:21 PM, Heikki Linnakangas wrote:
> On 04/22/2015 10:07 AM, Michael Paquier wrote:
>> On Wed, Apr 22, 2015 at 3:38 PM, Heikki Linnakangas  wrote:
>>> I feel that the best approach is to archive the last, partial segment, but
>>> with the .partial suffix. I don't see any plausible real-wold setup where
>>> the current behavior would be better. I don't really see much need to
>>> archive the partial segment at all, but there's also no harm in doing it, as
>>> long as it's clearly marked with the .partial suffix.
>>
>> Well, as long as it is clearly archived at promotion, even with a
>> suffix, I guess that I am fine... This will need some tweaking on
>> restore_command for existing applications, but as long as it is
>> clearly documented I am fine. Shouldn't this be a different patch
>> though?
>
> Ok, I came up with the attached, which adds the .partial suffix to the
> partial WAL segment that's archived after promotion. I couldn't find any
> natural place to talk about it in the docs, though. I think after the
> docs changes from the main patch are applied, it would be natural to
> mention this in the "Continuous archiving in standby", so I think I'll
> add that later.
>
> Barring objections, I'll push this later tonight.

Applied that part.

> Now that we got this last-partial-segment problem out of the way, I'm
> going to try fixing the problem you (Michael) pointed out about relying
> on pgstat file. Meanwhile, I'd love to get more feedback on the rest of
> the patch, and the documentation.

And here is a new version of the patch. I kept the approach of using 
pgstat, but it now only polls pgstat every 10 seconds, and doesn't block 
to wait for updated stats.

- Heikki

Re: Streaming replication and WAL archive interactions

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Mon, May 11, 2015 at 12:00 PM, Heikki Linnakangas  wrote:
> And here is a new version of the patch. I kept the approach of using pgstat,
> but it now only polls pgstat every 10 seconds, and doesn't block to wait for
> updated stats.

It's not entirely a new problem, but this error message has gotten pretty crazy:

+                               (errmsg("WAL archival
(archive_mode=on/always/shared) requires wal_level \"archive\",
\"hot_standby\", or \"logical\"")));

Maybe: WAL archival cannot be enabled when wal_level is "minimal"

I think the documentation should be explicit about what happens if the
primary archives a file and dies before the standby gets notified that
the archiving happened.  The standby, running in shared mode, is then
promoted.  My first guess would be that the standby will end up with
files that thinks it needs to archive but, being unable to do so
because they're already there, they'll live forever in pg_xlog.  I
hope that's not the case.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
On 05/13/2015 03:36 PM, Robert Haas wrote:
> On Mon, May 11, 2015 at 12:00 PM, Heikki Linnakangas  wrote:
>> And here is a new version of the patch. I kept the approach of using pgstat,
>> but it now only polls pgstat every 10 seconds, and doesn't block to wait for
>> updated stats.
>
> It's not entirely a new problem, but this error message has gotten pretty crazy:
>
> +                               (errmsg("WAL archival
> (archive_mode=on/always/shared) requires wal_level \"archive\",
> \"hot_standby\", or \"logical\"")));
>
> Maybe: WAL archival cannot be enabled when wal_level is "minimal"
>
> I think the documentation should be explicit about what happens if the
> primary archives a file and dies before the standby gets notified that
> the archiving happened.

Yes, good point.

>  The standby, running in shared mode, is then
> promoted.  My first guess would be that the standby will end up with
> files that thinks it needs to archive but, being unable to do so
> because they're already there, they'll live forever in pg_xlog.  I
> hope that's not the case.

Hmm. That is exactly what happens. The standby will attempt to archive 
them, which will fail, so the archiver will get stuck retrying.

That's not actually a new problem though. Even with a single server 
doing archiving, it's possible that you crash just after archive_command 
has archived a file, but before it has created the .done file. After 
restart, the server will try to archive the file again, which will fail. 
But yeah, with this patch, that's much more likely to happen after a 
promotion.

Our manual says that archive_command should refuse to overwrite an 
existing file. But to work-around the double-archival problem, where the 
same file is archived twice, it would be even better if it would simply 
return success if the file exists, *and has identical contents*. I don't 
know how to code that logic in a simple one-liner though.

- Heikki


Re: Streaming replication and WAL archive interactions

От:
Robert Haas <robertmhaas@gmail.com>
Дата:
On Wed, May 13, 2015 at 8:53 AM, Heikki Linnakangas  wrote:
> Our manual says that archive_command should refuse to overwrite an existing
> file. But to work-around the double-archival problem, where the same file is
> archived twice, it would be even better if it would simply return success if
> the file exists, *and has identical contents*. I don't know how to code that
> logic in a simple one-liner though.

This is why we really, really need that pg_copy command that was
proposed a while back.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


Re: Streaming replication and WAL archive interactions

От:
Heikki Linnakangas <hlinnaka@iki.fi>
Дата:
Вложения:
On 05/13/2015 04:29 PM, Robert Haas wrote:
> On Wed, May 13, 2015 at 8:53 AM, Heikki Linnakangas  wrote:
>> Our manual says that archive_command should refuse to overwrite an existing
>> file. But to work-around the double-archival problem, where the same file is
>> archived twice, it would be even better if it would simply return success if
>> the file exists, *and has identical contents*. I don't know how to code that
>> logic in a simple one-liner though.
>
> This is why we really, really need that pg_copy command that was
> proposed a while back.

Yeah..

I took a step back and looked at the big picture again:

If we just implement the "always" mode, and you have a pg_copy command 
or similar that handles duplicates correctly, you don't necessarily need 
the "shared" mode at all. You can just set archive_command='always', and 
have the master and standby archive to the same location. As long as the 
archive_command works correctly and is race-free, that should work.

I cut back the patch to implement just the "always" mode. The "shared" 
mode might still make sense as a future patch, as I think it's easier to 
understand and has less strict requirements for the archive_command, but 
let's take one step at a time.

So attached is a patch that just adds the "always" mode. This is pretty 
close to what Fujii submitted long ago.

- Heikki

Re: Streaming replication and WAL archive interactions

От:
Andrey Borodin <x4mmm@yandex-team.ru>
Дата:
Вложения:


> On 11 May 2015, at 21:00, Heikki Linnakangas  wrote:
> 
> Applied that part.
> 
>> Now that we got this last-partial-segment problem out of the way, I'm
>> going to try fixing the problem you (Michael) pointed out about relying
>> on pgstat file. Meanwhile, I'd love to get more feedback on the rest of
>> the patch, and the documentation.
> 
> And here is a new version of the patch. I kept the approach of using pgstat, but it now only polls pgstat every 10 seconds, and doesn't block to wait for updated stats.

Hi Heikki,

There’s a nearby thread [0] (about 10 years later) where I’m working on a problem your patch from this thread helps solve.

In datacenter large outages, 1–2% of clusters end up with gaps in their PITR timeline.
In HA setups, when the primary is lost, some WAL can be missing from the archive even though it was streamed to the standby. Many HA tools (PGConsul, Patroni, etc.) try to re-archive from the standby, but those WAL files may already have been removed.

Your “shared” archive mode addresses this: the standby keeps WAL until it’s archived. archive_mode=always plus an archive tool can work, but it’s expensive. In WAL-G, for example, the archive command does a GET on the standby’s WAL, then decrypts and compares. Switching to HEAD would reduce cost in some clouds but still adds cost.

Another option is coordinating archiving outside Postgres, but that would mean building distributed coordination into the archive tool.

Shared archive mode tackles this in Postgres itself.

I’ve retrofitted your patch, incorporated ideas from the Greenplum work [1], and made some improvements.

The patchset has three parts:
 * Rebase + tests – Your original patch, rebased, with tests added.
 * Timeline switching – Correct handling of timeline switches in archive status updates.
 * Avoid directory scans – Skip scanning archive_status when possible, which was costly in WAL-G setups.

What do you think?

Best regards, Andrey Borodin.

Re: Streaming replication and WAL archive interactions

От:
Harinath Kanchu <hkanchu@apple.com>
Дата:
Hi All,

I posted about this last year [1] -- sharing our experience
hitting WAL archival gaps in production and asking for last_archived_wal
to be surfaced on the standby via keep-alive messages.

Glad to see Andrey's  patch reviving this.

If the full shared mode lands, great. But even if that's too much for
now, just exposing last_archived_wal on the standby -- in
pg_stat_wal_receiver or anywhere queryable -- would unblock everyone
solving this problem externally.

[1] https://www.postgresql.org/message-id/flat/CAO7WNRTBrPn0WU9GTimoK-0FHRynaUHa34%3DAp5puCzEipNymFg%40mail.gmail.com

Thanks,
Harinath

Re: Streaming replication and WAL archive interactions

От:
Jaroslav Novikov <njrslv@yandex-team.ru>
Дата:

On 12 Feb 2026, at 09:56, Andrey Borodin <x4mmm@yandex-team.ru> wrote:

Hi Heikki,

There’s a nearby thread [0] (about 10 years later) where I’m working on a problem your patch from this thread helps solve.

In datacenter large outages, 1–2% of clusters end up with gaps in their PITR timeline.
In HA setups, when the primary is lost, some WAL can be missing from the archive even though it was streamed to the standby. Many HA tools (PGConsul, Patroni, etc.) try to re-archive from the standby, but those WAL files may already have been removed.

Your “shared” archive mode addresses this: the standby keeps WAL until it’s archived. archive_mode=always plus an archive tool can work, but it’s expensive. In WAL-G, for example, the archive command does a GET on the standby’s WAL, then decrypts and compares. Switching to HEAD would reduce cost in some clouds but still adds cost.

Another option is coordinating archiving outside Postgres, but that would mean building distributed coordination into the archive tool.

Shared archive mode tackles this in Postgres itself.

I’ve retrofitted your patch, incorporated ideas from the Greenplum work [1], and made some improvements.

The patchset has three parts:
* Rebase + tests – Your original patch, rebased, with tests added.
* Timeline switching – Correct handling of timeline switches in archive status updates.
* Avoid directory scans – Skip scanning archive_status when possible, which was costly in WAL-G setups.

What do you think?

Best regards, Andrey Borodin.

<v4-0001-Add-archive_mode-shared-for-coordinated-WAL-archi.patch><v4-0003-Optimize-ProcessArchivalReport-to-avoid-directory.patch><v4-0002-Mark-ancestor-timeline-WAL-segments-as-archived.patch>

Hi Andrey,

Adding the missing references [0] and [1].

[1] https://github.com/open-gpdb/gpdb/commit/4f2db1929df1b5eed28f33505955636096bb4e8b

Best, Jaroslav Novikov.

Re: Streaming replication and WAL archive interactions

От:
Jaroslav Novikov <njrslv@yandex-team.ru>
Дата:


> On 12 Feb 2026, at 09:56, Andrey Borodin  wrote:
> 
> Hi Heikki,
> 
> There’s a nearby thread [0] (about 10 years later) where I’m working on a problem your patch from this thread helps solve.
> 
> In datacenter large outages, 1–2% of clusters end up with gaps in their PITR timeline.
> In HA setups, when the primary is lost, some WAL can be missing from the archive even though it was streamed to the standby. Many HA tools (PGConsul, Patroni, etc.) try to re-archive from the standby, but those WAL files may already have been removed.
> 
> Your “shared” archive mode addresses this: the standby keeps WAL until it’s archived. archive_mode=always plus an archive tool can work, but it’s expensive. In WAL-G, for example, the archive command does a GET on the standby’s WAL, then decrypts and compares. Switching to HEAD would reduce cost in some clouds but still adds cost.
> 
> Another option is coordinating archiving outside Postgres, but that would mean building distributed coordination into the archive tool.
> 
> Shared archive mode tackles this in Postgres itself.
> 
> I’ve retrofitted your patch, incorporated ideas from the Greenplum work [1], and made some improvements.
> 
> The patchset has three parts:
> * Rebase + tests – Your original patch, rebased, with tests added.
> * Timeline switching – Correct handling of timeline switches in archive status updates.
> * Avoid directory scans – Skip scanning archive_status when possible, which was costly in WAL-G setups.
> 
> What do you think?
> 
> Best regards, Andrey Borodin.
> 
> 

Hi Andrey,

Adding the missing references [0] and [1].

[0] https://www.postgresql.org/message-id/5550D20D.6090703%40iki.fi
[1] https://github.com/open-gpdb/gpdb/commit/4f2db1929df1b5eed28f33505955636096bb4e8b

Best, Jaroslav Novikov.



Re: Streaming replication and WAL archive interactions

От:
Jaroslav Novikov <njrslv@yandex-team.ru>
Дата:

> On 12 Feb 2026, at 09:56, Andrey Borodin  wrote:
> 
> Your “shared” archive mode addresses this: the standby keeps WAL until it’s archived. archive_mode=always plus an archive tool can work, but it’s expensive. In WAL-G, for example, the archive command does a GET on the standby’s WAL, then decrypts and compares. Switching to HEAD would reduce cost in some clouds but still adds cost.

Hi Andrey,

I think XLogArchiveCheckDone() needs adjustment for the shared archive mode.

Currently:
    if (!XLogArchivingAlways() &&
        GetRecoveryState() == RECOVERY_STATE_ARCHIVE)
        return true;

On a standby being in RECOVERY_STATE_ARCHIVE and with archive_mode=shared WAL is considered deletable. My question is whether we expect this or not.

This means that XLogArchiveCheckDone() returns true before reaching the archival status checks from an active primary.

For archive_mode=on this early return is correct - nobody archives on the standby, so checking .done/.ready would block WAL cleanup forever? But shared mode relies on those files to defer recycling until the primary confirms archival.

Additionally, being in RECOVERY_STATE_ARCHIVE could falsify my reasoning. But I don't think that being in archive recovery means we don't need to persist WAL segment files because they are already in the archive. If the standby has not received a successful archival report from the primary, those WAL segments have not been archived yet - I am not sure if it's possible to have such a situation during RECOVERY_STATE_ARCHIVE where some WAL segments have not yet been archived by the primary.

I think shared mode needs to be excluded from this early return, but I am not sure about other details and my questions above:

    if (!XLogArchivingAlways() &&
        XLogArchiveMode != ARCHIVE_MODE_SHARED &&
        GetRecoveryState() == RECOVERY_STATE_ARCHIVE)
        return true;

Thoughts?

Best,
Jaroslav

Re: Streaming replication and WAL archive interactions

От:
Grigory Smolkin <smallkeen@gmail.com>
Дата:
Вложения:

Hello, hackers!
I would like to thank the community and all participants of this thread for their interest in this problem.
In our production system with tens of thousands PostgreSQL clusters we encounter exactly the same issue and are forced to synchronize upstreams and downstreams via external means, which is quite suboptimal.
I`ve done some work on top of the proposed v4 version patch and would like to present v5 version for a discussion.
There are a number of changes, such as sending just TLI and Segno instead of full WAL filename, shifting some work into archiver and adding shared memory for walreceiver/archiver synchronization.
There are a number of issues currently unresolved, which are worth a discussion.

1. Should we update pg_stat_archiver on standby to support cascading replication or should we just resend the report, received from upstream? Personally I'm more inclined towards the pg_stat_archiver path, because this way there will be less `if-else` programming and archive_mode=shared behaviour will be more monitoring-friendly.

2. What should we do with *.backup.ready and *.partial.ready on standby? Can we just XLogArchiveForceDone() them?

3. Should we keep the awkward part with switchpont calculation in timeline switch case? I think all segments that are not in our server history should just be stamped with XLogArchiveForceDone().

4. Currently XLogArchiveForceDone is forced either by walreceiver (on receiving report from upstream) and archiver. Should we move this into the archiver entirely?

Any feedback will be much appreciated.


Re: Streaming replication and WAL archive interactions

От:
Kirill Reshke <reshkekirill@gmail.com>
Дата:
Вложения:
Hi all!

I have revised the patches in a thread. In v6-0001 I have included
v4-0001 - v4-0003, fixes for them and new monitoring features in
v6-0002.
So, first of all, in v4 there were several bugs. One bug is spotted by
Jaroslav in [1]. The if-condition pointed by Jaroslav is indeed wrong,
it leads to WAL segments removal on standby's restartpoint, which is
unintended (they have .ready status). The new 056 TAP test now
executes this code path. I chose to create a new TAP instead of
extending 055 for sake of simplicity and readability.

Another bug is about cascading replication. In v4, only primary sends
archive status reports, so for cascading replication, there is nobody
to inform replica that WAL can be deleted (in v4 WAL file archive
status would be .ready, and files will be never cleared up). In v5,
stadby's walsender simply relays primary archive status downstream. It
was actually spotted & proposed 10 years ago [2], but never
implemented in thread.
So, for implementing this, we need shared memory for walsender &
walreceier coordination. I used Archiver Data (PgArchData) for this,
where primary_last_archived WAL is now stored. I used Archiver Data
because that was v3 choice, but I'm not opposed to using the new
shared memory region.

Note that v4 patches have cascading setup tests, which does not catch
this due to its ill-formed structure. I have reimplemented them, and
polished them a little.

v6-0001 also features changes in protocol.sgml  (grabbed from Heikki's
v3 [0]). Please check the wording.

I also added archive_status_report_interval GUC to regulate archival
status report frequency. Setting this to 10ms instead of default (10s)
speeds up tests from 30-40s to 8-10s runtime.

This patch set does not address switching to archive streaming issues
from [2]. I will soon update the patch with that.

For Grigory's questions from [3]:

> 1. Should we update pg_stat_archiver on standby to support cascading
> replication or should we just resend the report, received from upstream?

Last one. We shouldn't update  pg_stat_archiver standby, because there
is no actual work happening there.
While in recovery, standby's only duty is to keep archive status up to
date, archiving itself always happening on primary.
So, we need to monitor something related to standby's activity, we can
track primary_last_archived WAL for that purpose. v6-0002 implements
that.

> 2. What should we do with *.backup.ready and *.partial.ready on standby?
> Can we just XLogArchiveForceDone() them?

In v4 we only send reports for WAL segments, not backup history files
or other archived files. In v6 we check this before
ProcessArchivalReport call.

> 3. Should we keep the awkward part with switchpont calculation in
> timeline switch case? I think all segments that are not in our server
> history should just be stamped with XLogArchiveForceDone().

I didn't get this one.

> 4. Currently XLogArchiveForceDone is forced either by walreceiver (on
> receiving report from upstream) and archiver. Should we move this into
> the archiver entirely?

For what exact benefit?

I also reviewed Grigory's patch. Its description says:

> There are a number of changes, such as sending just TLI and Segno
> instead of full WAL filename, shifting some work into archiver and
> adding shared memory for walreceiver/archiver synchronization.

But I didn't find anything particularly useful. I maybe like this bit:

> + ShmemRequestStruct(.name = "Archival Report Data",
> +   .size = sizeof(ArchivalReportData),
> +   .ptr = (void **) &ArchReport,

But I didn't include this in v6.

Here is few random comments on v5:

> +// DIR *status_dir;
> +// struct dirent *status_de;

We don't leave commented code here.

> + elog(WARNING, "found ready file for %s", walfile);

That's DEBUG4/DEBUG5 at beast. Ideally, remove

> + // TODO: we must handle somehow partial, .history and .backup files

We use /**/ comment style.

>+ elog(WARNING, "segment %s is not yet archived by upstream", walfile);

DEBUG2

> + // some garbage in archive_status, should error here
> + if (!tliInHistory(file_tli, tli_history))
> + {
> + ereport(ERROR,
> + (errmsg("walfile %s is not in this server's history", walfile)));
> + continue;
> + }

ereport called with severity level higher that ERROR is efficiently
long jump to PG_TRY block, so there will be no 'continue'

>
> --- a/src/backend/postmaster/postmaster.c
> +++ b/src/backend/postmaster/postmaster.c
> @@ -3401,7 +3401,9 @@ LaunchMissingBackgroundProcesses(void)
>   */
>  if (PgArchPMChild == NULL &&
>   ((XLogArchivingActive() && pmState == PM_RUN) ||
> - (XLogArchivingAlways() && (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY))) &&
> + (XLogArchivingAlways() && (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY)) ||
> + (XLogArchivingShared())
> + ) &&

Do we need this?


> +bool
> +IsSegnoArchivedByUpstream(TimeLineID tli, XLogSegNo segno)
> +{
> + elog(WARNING, "received tli: %d, segno: %ld", tli, segno);
> + elog(WARNING, "ArchReport->tli: %d, ArchReport->segno: %ld", ArchReport->tli, ArchReport->segno);
> + return tli == ArchReport->tli && segno <= ArchReport->segno;
> +}

It's DEBUG2. Also, use errdetail() for low-level variable printing.

v5 also misses use_direct_check optimisation from v4-0003



[0] https://www.postgresql.org/message-id/5550D20D.6090703%40iki.fi
[1] https://www.postgresql.org/message-id/50EF1E0E-9212-4BF2-9B2A-E38AC45D8ACD%40yandex-team.ru
[2] https://www.postgresql.org/message-id/CAHGQGwGJzp-QS7BODiv1uc291gAKtjzzCPb_nzUTxYKJhLsUCA%40mail.gmail.com
[3] https://www.postgresql.org/message-id/4516e7e8-46f0-4ded-907a-db5a7c0c75b3%40gmail.com

--
Best regards,
Kirill Reshke
FAQ